Skip to content

Commit 0283383

Browse files
committed
Improve polling
1 parent 1ea06da commit 0283383

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

tests/integration/rest_sync/db/data/test_list.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
11
import logging
2+
import time
23
import pytest
34
from tests.integration.helpers import embedding_values, random_string, poll_until_lsn_reconciled
45

56
logger = logging.getLogger(__name__)
67

78

9+
def poll_until_list_has_results(
10+
idx, prefix: str, namespace: str, expected_count: int, max_wait_time: int = 60
11+
):
12+
"""Poll until list returns the expected number of results for a given prefix.
13+
14+
Args:
15+
idx: The index client
16+
prefix: The prefix to search for
17+
namespace: The namespace to search in
18+
expected_count: The expected number of results
19+
max_wait_time: Maximum time to wait in seconds
20+
21+
Raises:
22+
TimeoutError: If the expected count is not reached within max_wait_time seconds
23+
"""
24+
time_waited = 0
25+
wait_per_iteration = 2
26+
27+
while time_waited < max_wait_time:
28+
# Try to list vectors with the prefix
29+
results = list(idx.list(prefix=prefix, namespace=namespace))
30+
total_count = sum(len(page) for page in results)
31+
32+
if total_count >= expected_count:
33+
logger.debug(
34+
f"List returned {total_count} results for prefix '{prefix}' in namespace '{namespace}'"
35+
)
36+
return
37+
38+
logger.debug(
39+
f"Polling for list results. Prefix: '{prefix}', namespace: '{namespace}', "
40+
f"current count: {total_count}, expected: {expected_count}, waited: {time_waited}s"
41+
)
42+
43+
time.sleep(wait_per_iteration)
44+
time_waited += wait_per_iteration
45+
46+
raise TimeoutError(
47+
f"Timeout waiting for list to return {expected_count} results for prefix '{prefix}' "
48+
f"in namespace '{namespace}' after {time_waited} seconds"
49+
)
50+
51+
852
@pytest.fixture(scope="session")
953
def list_namespace():
1054
return random_string(10)
@@ -54,6 +98,9 @@ def test_list_when_limit(self, idx, list_namespace):
5498
assert results.pagination.next != ""
5599

56100
def test_list_when_using_pagination(self, idx, list_namespace):
101+
# Poll to ensure vectors are available for listing
102+
poll_until_list_has_results(idx, prefix="99", namespace=list_namespace, expected_count=11)
103+
57104
results = idx.list_paginated(prefix="99", limit=5, namespace=list_namespace)
58105
next_results = idx.list_paginated(
59106
prefix="99", limit=5, namespace=list_namespace, pagination_token=results.pagination.next
@@ -78,6 +125,9 @@ def test_list_when_using_pagination(self, idx, list_namespace):
78125
@pytest.mark.usefixtures("seed_for_list")
79126
class TestList:
80127
def test_list(self, idx, list_namespace):
128+
# Poll to ensure vectors are available for listing
129+
poll_until_list_has_results(idx, prefix="99", namespace=list_namespace, expected_count=11)
130+
81131
results = idx.list(prefix="99", limit=20, namespace=list_namespace)
82132

83133
page_count = 0
@@ -113,6 +163,9 @@ def test_list_when_no_results_for_namespace(self, idx):
113163
assert page_count == 0
114164

115165
def test_list_when_multiple_pages(self, idx, list_namespace):
166+
# Poll to ensure vectors are available for listing
167+
poll_until_list_has_results(idx, prefix="99", namespace=list_namespace, expected_count=11)
168+
116169
pages = []
117170
page_sizes = []
118171
page_count = 0
@@ -130,6 +183,9 @@ def test_list_when_multiple_pages(self, idx, list_namespace):
130183
assert pages[2] == ["999"]
131184

132185
def test_list_then_fetch(self, idx, list_namespace):
186+
# Poll to ensure vectors are available for listing
187+
poll_until_list_has_results(idx, prefix="99", namespace=list_namespace, expected_count=11)
188+
133189
vectors = []
134190

135191
for ids in idx.list(prefix="99", limit=5, namespace=list_namespace):

0 commit comments

Comments
 (0)