Skip to content

Commit 0e17b08

Browse files
committed
renaming
1 parent 953af61 commit 0e17b08

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

packages/service-library/tests/redis/test_redis.py renamed to packages/service-library/tests/redis/test_client.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
from redis.exceptions import LockError, LockNotOwnedError
1717
from servicelib.redis import (
1818
CouldNotAcquireLockError,
19-
LockLostError,
2019
RedisClientSDK,
2120
RedisClientsManager,
2221
RedisManagerDBConfig,
2322
)
2423
from servicelib.redis import _constants as redis_constants
25-
from servicelib.redis._decorators import exclusive
2624
from servicelib.utils import limited_gather
2725
from settings_library.redis import RedisDatabase, RedisSettings
2826
from tenacity import (
@@ -152,42 +150,6 @@ async def test_redis_lock_with_ttl(
152150
assert not await ttl_lock.locked()
153151

154152

155-
async def test_lock_context(
156-
redis_client_sdk: RedisClientSDK, faker: Faker, lock_timeout: datetime.timedelta
157-
):
158-
lock_name = faker.pystr()
159-
assert await _is_locked(redis_client_sdk, lock_name) is False
160-
async with redis_client_sdk.lock_context(lock_name) as ttl_lock:
161-
assert await _is_locked(redis_client_sdk, lock_name) is True
162-
assert await ttl_lock.owned() is True
163-
await asyncio.sleep(5 * lock_timeout.total_seconds())
164-
assert await _is_locked(redis_client_sdk, lock_name) is True
165-
assert await ttl_lock.owned() is True
166-
assert await _is_locked(redis_client_sdk, lock_name) is False
167-
assert await ttl_lock.owned() is False
168-
169-
170-
async def test_lock_context_raises_if_lock_is_lost(
171-
redis_client_sdk: RedisClientSDK, faker: Faker
172-
):
173-
lock_name = faker.pystr()
174-
started_event = asyncio.Event()
175-
176-
@exclusive(redis_client_sdk, lock_key=lock_name)
177-
async def _(time_to_sleep: datetime.timedelta) -> datetime.timedelta:
178-
started_event.set()
179-
await asyncio.sleep(time_to_sleep.total_seconds())
180-
return time_to_sleep
181-
182-
exclusive_task = asyncio.create_task(_(datetime.timedelta(seconds=10)))
183-
await asyncio.wait_for(started_event.wait(), timeout=2)
184-
# let's simlulate lost lock by forcefully deleting it
185-
await redis_client_sdk._client.delete(lock_name) # noqa: SLF001
186-
187-
with pytest.raises(LockLostError):
188-
await exclusive_task
189-
190-
191153
async def test_lock_context_with_already_locked_lock_raises(
192154
redis_client_sdk: RedisClientSDK, faker: Faker
193155
):

packages/service-library/tests/redis/test_redis_utils.py renamed to packages/service-library/tests/redis/test_decorators.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# pylint:disable=redefined-outer-name
22

33
import asyncio
4-
from collections.abc import Callable
4+
import datetime
5+
from collections.abc import Awaitable, Callable
56
from contextlib import AbstractAsyncContextManager
67
from datetime import timedelta
78
from itertools import chain
8-
from typing import Awaitable
99
from unittest.mock import Mock
1010

1111
import arrow
@@ -18,6 +18,7 @@
1818
exclusive,
1919
start_exclusive_periodic_task,
2020
)
21+
from servicelib.redis._errors import LockLostError
2122
from servicelib.utils import logged_gather
2223
from settings_library.redis import RedisDatabase
2324
from tenacity.asyncio import AsyncRetrying
@@ -267,3 +268,24 @@ async def test_start_exclusive_periodic_task_parallel_all_finish(
267268
# NOTE all entries should be in increasing order;
268269
# this means that the `_sleep_task` ran sequentially
269270
assert _check_elements_lower(flattened_results)
271+
272+
273+
async def test_exclusive_raises_if_lock_is_lost(
274+
redis_client_sdk: RedisClientSDK, faker: Faker
275+
):
276+
lock_name = faker.pystr()
277+
started_event = asyncio.Event()
278+
279+
@exclusive(redis_client_sdk, lock_key=lock_name)
280+
async def _(time_to_sleep: datetime.timedelta) -> datetime.timedelta:
281+
started_event.set()
282+
await asyncio.sleep(time_to_sleep.total_seconds())
283+
return time_to_sleep
284+
285+
exclusive_task = asyncio.create_task(_(datetime.timedelta(seconds=10)))
286+
await asyncio.wait_for(started_event.wait(), timeout=2)
287+
# let's simlulate lost lock by forcefully deleting it
288+
await redis_client_sdk._client.delete(lock_name) # noqa: SLF001
289+
290+
with pytest.raises(LockLostError):
291+
await exclusive_task

0 commit comments

Comments
 (0)