Skip to content

Commit 8314b7f

Browse files
authored
Correct the typedef of lock.extend() to accept floats, and test that float TTLs are honoured precisely (#3420)
1 parent 4122f7c commit 8314b7f

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Fix lock.extend() typedef to accept float TTL extension
12
* Update URL in the readme linking to Redis University
23
* Move doctests (doc code examples) to main branch
34
* Update `ResponseT` type hint

redis/asyncio/lock.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import TYPE_CHECKING, Awaitable, Optional, Union
66

77
from redis.exceptions import LockError, LockNotOwnedError
8+
from redis.typing import Number
89

910
if TYPE_CHECKING:
1011
from redis.asyncio import Redis, RedisCluster
@@ -82,7 +83,7 @@ def __init__(
8283
timeout: Optional[float] = None,
8384
sleep: float = 0.1,
8485
blocking: bool = True,
85-
blocking_timeout: Optional[float] = None,
86+
blocking_timeout: Optional[Number] = None,
8687
thread_local: bool = True,
8788
):
8889
"""
@@ -167,7 +168,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
167168
async def acquire(
168169
self,
169170
blocking: Optional[bool] = None,
170-
blocking_timeout: Optional[float] = None,
171+
blocking_timeout: Optional[Number] = None,
171172
token: Optional[Union[str, bytes]] = None,
172173
):
173174
"""
@@ -265,7 +266,7 @@ async def do_release(self, expected_token: bytes) -> None:
265266
raise LockNotOwnedError("Cannot release a lock that's no longer owned")
266267

267268
def extend(
268-
self, additional_time: float, replace_ttl: bool = False
269+
self, additional_time: Number, replace_ttl: bool = False
269270
) -> Awaitable[bool]:
270271
"""
271272
Adds more time to an already acquired lock.

tests/test_asyncio/test_lock.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ async def test_extend_lock_replace_ttl(self, r):
174174
await lock.release()
175175

176176
async def test_extend_lock_float(self, r):
177-
lock = self.get_lock(r, "foo", timeout=10.0)
177+
lock = self.get_lock(r, "foo", timeout=10.5)
178178
assert await lock.acquire(blocking=False)
179-
assert 8000 < (await r.pttl("foo")) <= 10000
180-
assert await lock.extend(10.0)
181-
assert 16000 < (await r.pttl("foo")) <= 20000
179+
assert 10400 < (await r.pttl("foo")) <= 10500
180+
old_ttl = await r.pttl("foo")
181+
assert await lock.extend(10.5)
182+
assert old_ttl + 10400 < (await r.pttl("foo")) <= old_ttl + 10500
182183
await lock.release()
183184

184185
async def test_extending_unlocked_lock_raises_error(self, r):

tests/test_lock.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,12 @@ def test_extend_lock_replace_ttl(self, r):
178178
lock.release()
179179

180180
def test_extend_lock_float(self, r):
181-
lock = self.get_lock(r, "foo", timeout=10.0)
181+
lock = self.get_lock(r, "foo", timeout=10.5)
182182
assert lock.acquire(blocking=False)
183-
assert 8000 < r.pttl("foo") <= 10000
184-
assert lock.extend(10.0)
185-
assert 16000 < r.pttl("foo") <= 20000
183+
assert 10400 < r.pttl("foo") <= 10500
184+
old_ttl = r.pttl("foo")
185+
assert lock.extend(10.5)
186+
assert old_ttl + 10400 < r.pttl("foo") <= old_ttl + 10500
186187
lock.release()
187188

188189
def test_extending_unlocked_lock_raises_error(self, r):

0 commit comments

Comments
 (0)