Skip to content

Commit d67cd4d

Browse files
committed
Don't special-case negative max_tries
Signed-off-by: Hauke Daempfling <haukex@zero-g.net>
1 parent baffe45 commit d67cd4d

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

tests/test_asyncio/test_pipeline.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,33 @@ async def my_transaction(pipe):
343343
pipe.multi()
344344
await r.set("a", a_value + 1) # force WatchError
345345

346+
# without max_tries (infinite loop)
346347
with pytest.raises(RuntimeError) as ex:
347348
await r.transaction(my_transaction, "a")
348349
assert str(ex.value).startswith("Run too many times")
349350
assert run_count == 11
351+
350352
run_count = 0
353+
# with max_tries
351354
with pytest.raises(valkey.ValkeyError) as ex:
352355
await r.transaction(my_transaction, "a", max_tries=3)
353356
assert str(ex.value).startswith("Bailing out of transaction after 3 tries")
354357
assert run_count == 3
355358

359+
run_count = 0
360+
# with max_tries=0 (same as without; infinite loop)
361+
with pytest.raises(RuntimeError) as ex:
362+
await r.transaction(my_transaction, "a", max_tries=0)
363+
assert str(ex.value).startswith("Run too many times")
364+
assert run_count == 11
365+
366+
run_count = 0
367+
# with negative max_tries (immediate error)
368+
with pytest.raises(valkey.ValkeyError) as ex:
369+
await r.transaction(my_transaction, "a", max_tries=-3)
370+
assert str(ex.value).startswith("Bailing out of transaction after 0 tries")
371+
assert run_count == 0
372+
356373
@pytest.mark.onlynoncluster
357374
async def test_transaction_callable_returns_value_from_callable(self, r):
358375
async def callback(pipe):

tests/test_pipeline.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,33 @@ def my_transaction(pipe):
344344
pipe.multi()
345345
r.set("a", a_value + 1) # force WatchError
346346

347+
# without max_tries (infinite loop)
347348
with pytest.raises(RuntimeError) as ex:
348349
r.transaction(my_transaction, "a")
349350
assert str(ex.value).startswith("Run too many times")
350351
assert run_count == 11
352+
351353
run_count = 0
354+
# with max_tries
352355
with pytest.raises(valkey.ValkeyError) as ex:
353356
r.transaction(my_transaction, "a", max_tries=3)
354357
assert str(ex.value).startswith("Bailing out of transaction after 3 tries")
355358
assert run_count == 3
356359

360+
run_count = 0
361+
# with max_tries=0 (same as without; infinite loop)
362+
with pytest.raises(RuntimeError) as ex:
363+
r.transaction(my_transaction, "a", max_tries=0)
364+
assert str(ex.value).startswith("Run too many times")
365+
assert run_count == 11
366+
367+
run_count = 0
368+
# with negative max_tries (immediate error)
369+
with pytest.raises(valkey.ValkeyError) as ex:
370+
r.transaction(my_transaction, "a", max_tries=-3)
371+
assert str(ex.value).startswith("Bailing out of transaction after 0 tries")
372+
assert run_count == 0
373+
357374
@pytest.mark.onlynoncluster
358375
def test_transaction_callable_returns_value_from_callable(self, r):
359376
def callback(pipe):

valkey/asyncio/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,14 @@ async def transaction(
448448
`WatchError` before the transaction is retried. Default is no delay.
449449
:param max_tries: Lets you specify the maximum number of times the
450450
transaction is retried. If the limit is reached, a `ValkeyError`
451-
is raised. Default is an **infinite** number of retries!
451+
is raised. Default is 0, meaning an **infinite** number of retries!
452452
"""
453453
pipe: Pipeline
454454
async with self.pipeline(True, shard_hint) as pipe:
455455
tries = 0
456456
while True:
457457
tries += 1
458-
if max_tries and max_tries > 0 and tries > max_tries:
458+
if max_tries and tries > max_tries:
459459
raise ValkeyError(
460460
f"Bailing out of transaction after {tries - 1} tries"
461461
)

valkey/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ def transaction(
410410
is retried. Default is no delay.
411411
:param max_tries: This keyword-only argument lets you specify the
412412
maximum number to times the transaction should be retried. If the
413-
limit is reached, a `ValkeyError` is raised. Default is an
414-
**infinite** number of retries!
413+
limit is reached, a `ValkeyError` is raised. Default is 0, meaning
414+
an **infinite** number of retries!
415415
"""
416416
shard_hint = kwargs.pop("shard_hint", None)
417417
value_from_callable = kwargs.pop("value_from_callable", False)
@@ -421,7 +421,7 @@ def transaction(
421421
tries = 0
422422
while True:
423423
tries += 1
424-
if max_tries and max_tries > 0 and tries > max_tries:
424+
if max_tries and tries > max_tries:
425425
raise ValkeyError(
426426
f"Bailing out of transaction after {tries - 1} tries"
427427
)

0 commit comments

Comments
 (0)