Skip to content

Commit fcc19d3

Browse files
authored
Fixing flaky tests - part 2 (#3592)
1 parent 77e8db2 commit fcc19d3

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

tests/test_asyncio/test_search.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import bz2
22
import csv
33
import os
4-
import time
4+
import asyncio
55
from io import TextIOWrapper
66

77
import numpy as np
88
import pytest
99
import pytest_asyncio
1010
import redis.asyncio as redis
11-
import redis.commands.search
1211
import redis.commands.search.aggregation as aggregations
1312
import redis.commands.search.reducers as reducers
1413
from redis.commands.search import AsyncSearch
@@ -49,8 +48,8 @@ async def decoded_r(create_redis, stack_url):
4948
async def waitForIndex(env, idx, timeout=None):
5049
delay = 0.1
5150
while True:
52-
res = await env.execute_command("FT.INFO", idx)
5351
try:
52+
res = await env.execute_command("FT.INFO", idx)
5453
if int(res[res.index("indexing") + 1]) == 0:
5554
break
5655
except ValueError:
@@ -62,7 +61,7 @@ async def waitForIndex(env, idx, timeout=None):
6261
except ValueError:
6362
break
6463

65-
time.sleep(delay)
64+
await asyncio.sleep(delay)
6665
if timeout is not None:
6766
timeout -= delay
6867
if timeout <= 0:
@@ -1765,7 +1764,7 @@ async def test_binary_and_text_fields(decoded_r: redis.Redis):
17651764
mixed_data = {"first_name": "🐍python", "vector_emb": fake_vec.tobytes()}
17661765
await decoded_r.hset(f"{index_name}:1", mapping=mixed_data)
17671766

1768-
schema = (
1767+
schema = [
17691768
TagField("first_name"),
17701769
VectorField(
17711770
"embeddings_bio",
@@ -1776,14 +1775,15 @@ async def test_binary_and_text_fields(decoded_r: redis.Redis):
17761775
"DISTANCE_METRIC": "COSINE",
17771776
},
17781777
),
1779-
)
1778+
]
17801779

17811780
await decoded_r.ft(index_name).create_index(
17821781
fields=schema,
17831782
definition=IndexDefinition(
17841783
prefix=[f"{index_name}:"], index_type=IndexType.HASH
17851784
),
17861785
)
1786+
await waitForIndex(decoded_r, index_name)
17871787

17881788
query = (
17891789
Query("*")
@@ -1793,6 +1793,12 @@ async def test_binary_and_text_fields(decoded_r: redis.Redis):
17931793
result = await decoded_r.ft(index_name).search(query=query, query_params={})
17941794
docs = result.docs
17951795

1796+
if len(docs) == 0:
1797+
hash_content = await decoded_r.hget(f"{index_name}:1", "first_name")
1798+
assert len(docs) > 0, (
1799+
f"Returned search results are empty. Result: {result}; Hash: {hash_content}"
1800+
)
1801+
17961802
decoded_vec_from_search_results = np.frombuffer(
17971803
docs[0]["vector_emb"], dtype=np.float32
17981804
)

tests/test_cache.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
import redis
5+
56
from redis.cache import (
67
CacheConfig,
78
CacheEntry,
@@ -636,6 +637,7 @@ def test_get_from_default_cache(self, r, r2):
636637
]
637638
# change key in redis (cause invalidation)
638639
r2.set("foo", "barbar")
640+
time.sleep(0.1)
639641
# Retrieves a new value from server and cache_data it
640642
assert r.get("foo") in [b"barbar", "barbar"]
641643
# Make sure that new value was cached

0 commit comments

Comments
 (0)