Skip to content

Commit f06f3db

Browse files
authored
Add TIMEOUT to query class (#2519)
* add timeout to query class * Add test_timeout * fix lines * fix format * add test & fixes * merge tests * change timeout to not_a_number * change q1 to q2 * Fix async method
1 parent f28a9f5 commit f06f3db

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

redis/commands/search/query.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self, query_string):
2828
self._filters = list()
2929
self._ids = None
3030
self._slop = -1
31+
self._timeout = None
3132
self._in_order = False
3233
self._sortby = None
3334
self._return_fields = []
@@ -131,6 +132,11 @@ def slop(self, slop):
131132
self._slop = slop
132133
return self
133134

135+
def timeout(self, timeout):
136+
"""overrides the timeout parameter of the module"""
137+
self._timeout = timeout
138+
return self
139+
134140
def in_order(self):
135141
"""
136142
Match only documents where the query terms appear in
@@ -188,6 +194,8 @@ def _get_args_tags(self):
188194
args += self._ids
189195
if self._slop >= 0:
190196
args += ["SLOP", self._slop]
197+
if self._timeout:
198+
args += ["TIMEOUT", self._timeout]
191199
if self._in_order:
192200
args.append("INORDER")
193201
if self._return_fields:

tests/test_asyncio/test_search.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,12 @@ async def test_search_commands_in_pipeline(modclient: redis.Redis):
10011001
assert "doc2" == res[3][4]
10021002
assert res[3][5] is None
10031003
assert res[3][3] == res[3][6] == ["txt", "foo bar"]
1004+
1005+
1006+
@pytest.mark.redismod
1007+
async def test_query_timeout(modclient: redis.Redis):
1008+
q1 = Query("foo").timeout(5000)
1009+
assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10]
1010+
q2 = Query("foo").timeout("not_a_number")
1011+
with pytest.raises(redis.ResponseError):
1012+
await modclient.ft().search(q2)

tests/test_search.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,3 +1615,12 @@ def test_withsuffixtrie(modclient: redis.Redis):
16151615
waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
16161616
info = modclient.ft().info()
16171617
assert "WITHSUFFIXTRIE" in info["attributes"][0]
1618+
1619+
1620+
@pytest.mark.redismod
1621+
def test_query_timeout(modclient: redis.Redis):
1622+
q1 = Query("foo").timeout(5000)
1623+
assert q1.get_args() == ["foo", "TIMEOUT", 5000, "LIMIT", 0, 10]
1624+
q2 = Query("foo").timeout("not_a_number")
1625+
with pytest.raises(redis.ResponseError):
1626+
modclient.ft().search(q2)

0 commit comments

Comments
 (0)