Skip to content

Commit 4fb899a

Browse files
committed
ongoing
1 parent 1db4153 commit 4fb899a

File tree

2 files changed

+73
-100
lines changed

2 files changed

+73
-100
lines changed

packages/service-library/tests/redis/test_client.py

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -104,106 +104,6 @@ async def test_redis_lock_with_ttl(
104104
assert not await ttl_lock.locked()
105105

106106

107-
# async def test_lock_context_with_already_locked_lock_raises(
108-
# redis_client_sdk: RedisClientSDK, faker: Faker
109-
# ):
110-
# lock_name = faker.pystr()
111-
# assert await _is_locked(redis_client_sdk, lock_name) is False
112-
# async with redis_client_sdk.lock_context(lock_name) as lock:
113-
# assert await _is_locked(redis_client_sdk, lock_name) is True
114-
115-
# assert isinstance(lock.name, str)
116-
117-
# # case where gives up immediately to acquire lock without waiting
118-
# with pytest.raises(CouldNotAcquireLockError):
119-
# async with redis_client_sdk.lock_context(lock.name, blocking=False):
120-
# ...
121-
122-
# # case when lock waits up to blocking_timeout_s before giving up on
123-
# # lock acquisition
124-
# with pytest.raises(CouldNotAcquireLockError):
125-
# async with redis_client_sdk.lock_context(
126-
# lock.name, blocking=True, blocking_timeout_s=0.1
127-
# ):
128-
# ...
129-
130-
# assert await lock.locked() is True
131-
# assert await _is_locked(redis_client_sdk, lock_name) is False
132-
133-
134-
# async def test_lock_context_with_data(redis_client_sdk: RedisClientSDK, faker: Faker):
135-
# lock_data = faker.text()
136-
# lock_name = faker.pystr()
137-
# assert await _is_locked(redis_client_sdk, lock_name) is False
138-
# assert await redis_client_sdk.lock_value(lock_name) is None
139-
# async with redis_client_sdk.lock_context(lock_name, lock_value=lock_data):
140-
# assert await _is_locked(redis_client_sdk, lock_name) is True
141-
# assert await redis_client_sdk.lock_value(lock_name) == lock_data
142-
# assert await _is_locked(redis_client_sdk, lock_name) is False
143-
# assert await redis_client_sdk.lock_value(lock_name) is None
144-
145-
146-
# async def test_lock_context_released_after_error(
147-
# redis_client_sdk: RedisClientSDK, faker: Faker
148-
# ):
149-
# lock_name = faker.pystr()
150-
151-
# assert await redis_client_sdk.lock_value(lock_name) is None
152-
153-
# with pytest.raises(RuntimeError):
154-
# async with redis_client_sdk.lock_context(lock_name):
155-
# assert await redis_client_sdk.redis.get(lock_name) is not None
156-
# msg = "Expected error"
157-
# raise RuntimeError(msg)
158-
159-
# assert await redis_client_sdk.lock_value(lock_name) is None
160-
161-
162-
# async def test_lock_acquired_in_parallel_to_update_same_resource(
163-
# with_short_default_redis_lock_ttl: None,
164-
# get_redis_client_sdk: Callable[
165-
# [RedisDatabase], AbstractAsyncContextManager[RedisClientSDK]
166-
# ],
167-
# faker: Faker,
168-
# ):
169-
# INCREASE_OPERATIONS: Final[int] = 250
170-
# INCREASE_BY: Final[int] = 10
171-
172-
# class RaceConditionCounter:
173-
# def __init__(self):
174-
# self.value: int = 0
175-
176-
# async def race_condition_increase(self, by: int) -> None:
177-
# current_value = self.value
178-
# current_value += by
179-
# # most likely situation which creates issues
180-
# await asyncio.sleep(redis_constants.DEFAULT_LOCK_TTL.total_seconds() / 2)
181-
# self.value = current_value
182-
183-
# counter = RaceConditionCounter()
184-
# lock_name: str = faker.pystr()
185-
# # ensures it does nto time out before acquiring the lock
186-
# time_for_all_inc_counter_calls_to_finish_s: float = (
187-
# redis_constants.DEFAULT_LOCK_TTL.total_seconds() * INCREASE_OPERATIONS * 10
188-
# )
189-
190-
# async def _inc_counter() -> None:
191-
# async with get_redis_client_sdk(
192-
# RedisDatabase.RESOURCES
193-
# ) as redis_client_sdk:
194-
# async with redis_client_sdk.lock_context(
195-
# lock_key=lock_name,
196-
# blocking=True,
197-
# blocking_timeout_s=time_for_all_inc_counter_calls_to_finish_s,
198-
# ):
199-
# await counter.race_condition_increase(INCREASE_BY)
200-
201-
# await limited_gather(
202-
# *(_inc_counter() for _ in range(INCREASE_OPERATIONS)), limit=15
203-
# )
204-
# assert counter.value == INCREASE_BY * INCREASE_OPERATIONS
205-
206-
207107
async def test_redis_client_sdk_setup_shutdown(
208108
mock_redis_socket_timeout: None, redis_service: RedisSettings
209109
):

packages/service-library/tests/redis/test_decorators.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,76 @@ async def _(time_to_sleep: datetime.timedelta) -> datetime.timedelta:
276276

277277
with pytest.raises(LockLostError):
278278
await exclusive_task
279+
280+
281+
# async def test_lock_context_with_data(redis_client_sdk: RedisClientSDK, faker: Faker):
282+
# lock_data = faker.text()
283+
# lock_name = faker.pystr()
284+
# assert await _is_locked(redis_client_sdk, lock_name) is False
285+
# assert await redis_client_sdk.lock_value(lock_name) is None
286+
# async with redis_client_sdk.lock_context(lock_name, lock_value=lock_data):
287+
# assert await _is_locked(redis_client_sdk, lock_name) is True
288+
# assert await redis_client_sdk.lock_value(lock_name) == lock_data
289+
# assert await _is_locked(redis_client_sdk, lock_name) is False
290+
# assert await redis_client_sdk.lock_value(lock_name) is None
291+
292+
293+
# async def test_lock_context_released_after_error(
294+
# redis_client_sdk: RedisClientSDK, faker: Faker
295+
# ):
296+
# lock_name = faker.pystr()
297+
298+
# assert await redis_client_sdk.lock_value(lock_name) is None
299+
300+
# with pytest.raises(RuntimeError):
301+
# async with redis_client_sdk.lock_context(lock_name):
302+
# assert await redis_client_sdk.redis.get(lock_name) is not None
303+
# msg = "Expected error"
304+
# raise RuntimeError(msg)
305+
306+
# assert await redis_client_sdk.lock_value(lock_name) is None
307+
308+
309+
# async def test_lock_acquired_in_parallel_to_update_same_resource(
310+
# with_short_default_redis_lock_ttl: None,
311+
# get_redis_client_sdk: Callable[
312+
# [RedisDatabase], AbstractAsyncContextManager[RedisClientSDK]
313+
# ],
314+
# faker: Faker,
315+
# ):
316+
# INCREASE_OPERATIONS: Final[int] = 250
317+
# INCREASE_BY: Final[int] = 10
318+
319+
# class RaceConditionCounter:
320+
# def __init__(self):
321+
# self.value: int = 0
322+
323+
# async def race_condition_increase(self, by: int) -> None:
324+
# current_value = self.value
325+
# current_value += by
326+
# # most likely situation which creates issues
327+
# await asyncio.sleep(redis_constants.DEFAULT_LOCK_TTL.total_seconds() / 2)
328+
# self.value = current_value
329+
330+
# counter = RaceConditionCounter()
331+
# lock_name: str = faker.pystr()
332+
# # ensures it does nto time out before acquiring the lock
333+
# time_for_all_inc_counter_calls_to_finish_s: float = (
334+
# redis_constants.DEFAULT_LOCK_TTL.total_seconds() * INCREASE_OPERATIONS * 10
335+
# )
336+
337+
# async def _inc_counter() -> None:
338+
# async with get_redis_client_sdk(
339+
# RedisDatabase.RESOURCES
340+
# ) as redis_client_sdk:
341+
# async with redis_client_sdk.lock_context(
342+
# lock_key=lock_name,
343+
# blocking=True,
344+
# blocking_timeout_s=time_for_all_inc_counter_calls_to_finish_s,
345+
# ):
346+
# await counter.race_condition_increase(INCREASE_BY)
347+
348+
# await limited_gather(
349+
# *(_inc_counter() for _ in range(INCREASE_OPERATIONS)), limit=15
350+
# )
351+
# assert counter.value == INCREASE_BY * INCREASE_OPERATIONS

0 commit comments

Comments
 (0)