Skip to content

Commit c1909f8

Browse files
committed
Correct the typedef of lock.extend() to accept floats, and test that float TTLs are honoured precisely
1 parent 00f5be4 commit c1909f8

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
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
* Move doctests (doc code examples) to main branch
23
* Update `ResponseT` type hint
34
* Allow to control the minimum SSL version

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
"""
@@ -262,7 +263,7 @@ async def do_release(self, expected_token: bytes) -> None:
262263
raise LockNotOwnedError("Cannot release a lock that's no longer owned")
263264

264265
def extend(
265-
self, additional_time: float, replace_ttl: bool = False
266+
self, additional_time: Number, replace_ttl: bool = False
266267
) -> Awaitable[bool]:
267268
"""
268269
Adds more time to an already acquired lock.

redis/lock.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def do_release(self, expected_token: str) -> None:
264264
lock_name=self.name,
265265
)
266266

267-
def extend(self, additional_time: int, replace_ttl: bool = False) -> bool:
267+
def extend(self, additional_time: Number, replace_ttl: bool = False) -> bool:
268268
"""
269269
Adds more time to an already acquired lock.
270270

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)