@@ -181,6 +181,7 @@ async def test_exclusive_parallel_lock_is_released_and_reacquired(
181181async def _sleep_task (sleep_interval : float , on_sleep_events : Mock ) -> None :
182182 on_sleep_events (arrow .utcnow ())
183183 await asyncio .sleep (sleep_interval )
184+ print ("Slept for" , sleep_interval )
184185 on_sleep_events (arrow .utcnow ())
185186
186187
@@ -193,6 +194,7 @@ async def _assert_on_sleep_done(on_sleep_events: Mock, *, stop_after: float):
193194 ):
194195 with attempt :
195196 assert on_sleep_events .call_count == 2
197+ print ("sleep was done with" , on_sleep_events .call_count , " counts" )
196198
197199
198200async def _assert_task_completes_once (
@@ -274,21 +276,27 @@ async def test_start_exclusive_periodic_task_parallel_all_finish(
274276
275277
276278async def test_exclusive_raises_if_lock_is_lost (
277- redis_client_sdk : RedisClientSDK , faker : Faker
279+ get_redis_client_sdk : Callable [
280+ [RedisDatabase ], AbstractAsyncContextManager [RedisClientSDK ]
281+ ],
282+ faker : Faker ,
278283):
279284 lock_name = faker .pystr ()
285+
280286 started_event = asyncio .Event ()
281287
282- @exclusive (redis_client_sdk , lock_key = lock_name )
283- async def _ (time_to_sleep : datetime .timedelta ) -> datetime .timedelta :
284- started_event .set ()
285- await asyncio .sleep (time_to_sleep .total_seconds ())
286- return time_to_sleep
287-
288- exclusive_task = asyncio .create_task (_ (datetime .timedelta (seconds = 10 )))
289- await asyncio .wait_for (started_event .wait (), timeout = 2 )
290- # let's simlulate lost lock by forcefully deleting it
291- await redis_client_sdk ._client .delete (lock_name ) # noqa: SLF001
292-
293- with pytest .raises (LockLostError ):
294- await exclusive_task
288+ async with get_redis_client_sdk (RedisDatabase .RESOURCES ) as redis_client_sdk :
289+
290+ @exclusive (redis_client_sdk , lock_key = lock_name )
291+ async def _ (time_to_sleep : datetime .timedelta ) -> datetime .timedelta :
292+ started_event .set ()
293+ await asyncio .sleep (time_to_sleep .total_seconds ())
294+ return time_to_sleep
295+
296+ exclusive_task = asyncio .create_task (_ (datetime .timedelta (seconds = 10 )))
297+ await asyncio .wait_for (started_event .wait (), timeout = 2 )
298+ # let's simlulate lost lock by forcefully deleting it
299+ await redis_client_sdk ._client .delete (lock_name ) # noqa: SLF001
300+
301+ with pytest .raises (LockLostError ):
302+ await exclusive_task
0 commit comments