Skip to content

Commit

Permalink
Correct the typedef of lock.extend() to accept floats, and test that …
Browse files Browse the repository at this point in the history
…float TTLs are honoured precisely
  • Loading branch information
nbertram committed Nov 1, 2024
1 parent 00f5be4 commit c1909f8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fix lock.extend() typedef to accept float TTL extension
* Move doctests (doc code examples) to main branch
* Update `ResponseT` type hint
* Allow to control the minimum SSL version
Expand Down
7 changes: 4 additions & 3 deletions redis/asyncio/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING, Awaitable, Optional, Union

from redis.exceptions import LockError, LockNotOwnedError
from redis.typing import Number

if TYPE_CHECKING:
from redis.asyncio import Redis, RedisCluster
Expand Down Expand Up @@ -82,7 +83,7 @@ def __init__(
timeout: Optional[float] = None,
sleep: float = 0.1,
blocking: bool = True,
blocking_timeout: Optional[float] = None,
blocking_timeout: Optional[Number] = None,
thread_local: bool = True,
):
"""
Expand Down Expand Up @@ -167,7 +168,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
async def acquire(
self,
blocking: Optional[bool] = None,
blocking_timeout: Optional[float] = None,
blocking_timeout: Optional[Number] = None,
token: Optional[Union[str, bytes]] = None,
):
"""
Expand Down Expand Up @@ -262,7 +263,7 @@ async def do_release(self, expected_token: bytes) -> None:
raise LockNotOwnedError("Cannot release a lock that's no longer owned")

def extend(
self, additional_time: float, replace_ttl: bool = False
self, additional_time: Number, replace_ttl: bool = False
) -> Awaitable[bool]:
"""
Adds more time to an already acquired lock.
Expand Down
2 changes: 1 addition & 1 deletion redis/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def do_release(self, expected_token: str) -> None:
lock_name=self.name,
)

def extend(self, additional_time: int, replace_ttl: bool = False) -> bool:
def extend(self, additional_time: Number, replace_ttl: bool = False) -> bool:
"""
Adds more time to an already acquired lock.
Expand Down
9 changes: 5 additions & 4 deletions tests/test_asyncio/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ async def test_extend_lock_replace_ttl(self, r):
await lock.release()

async def test_extend_lock_float(self, r):
lock = self.get_lock(r, "foo", timeout=10.0)
lock = self.get_lock(r, "foo", timeout=10.5)
assert await lock.acquire(blocking=False)
assert 8000 < (await r.pttl("foo")) <= 10000
assert await lock.extend(10.0)
assert 16000 < (await r.pttl("foo")) <= 20000
assert 10400 < (await r.pttl("foo")) <= 10500
old_ttl = await r.pttl("foo")
assert await lock.extend(10.5)
assert old_ttl + 10400 < (await r.pttl("foo")) <= old_ttl + 10500
await lock.release()

async def test_extending_unlocked_lock_raises_error(self, r):
Expand Down
9 changes: 5 additions & 4 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ def test_extend_lock_replace_ttl(self, r):
lock.release()

def test_extend_lock_float(self, r):
lock = self.get_lock(r, "foo", timeout=10.0)
lock = self.get_lock(r, "foo", timeout=10.5)
assert lock.acquire(blocking=False)
assert 8000 < r.pttl("foo") <= 10000
assert lock.extend(10.0)
assert 16000 < r.pttl("foo") <= 20000
assert 10400 < r.pttl("foo") <= 10500
old_ttl = r.pttl("foo")
assert lock.extend(10.5)
assert old_ttl + 10400 < r.pttl("foo") <= old_ttl + 10500
lock.release()

def test_extending_unlocked_lock_raises_error(self, r):
Expand Down

0 comments on commit c1909f8

Please sign in to comment.