File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
packages/service-library/tests/redis Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -424,6 +424,38 @@ async def limited_function() -> None:
424424 assert "longer than expected" in caplog .messages [- 1 ]
425425
426426
427+ async def test_semaphore_fair_queuing (
428+ redis_client_sdk : RedisClientSDK ,
429+ semaphore_name : str ,
430+ ):
431+ entered_order : list [int ] = []
432+
433+ @with_limited_concurrency (
434+ redis_client_sdk ,
435+ key = semaphore_name ,
436+ capacity = 1 ,
437+ )
438+ async def limited_function (call_id : int ):
439+ entered_order .append (call_id )
440+ await asyncio .sleep (0.1 )
441+ return call_id
442+
443+ # Launch tasks in a specific order
444+ num_tasks = 10
445+ tasks = []
446+ for i in range (num_tasks ):
447+ tasks .append (asyncio .create_task (limited_function (i )))
448+ await asyncio .sleep (0.01 ) # Small delay to help preserve order
449+ results = await asyncio .gather (* tasks )
450+
451+ # All should complete successfully and in order
452+ assert results == list (range (num_tasks ))
453+ # The order in which they entered the critical section should match the order of submission
454+ assert entered_order == list (
455+ range (num_tasks )
456+ ), f"Expected fair queuing, got { entered_order } "
457+
458+
427459async def test_context_manager_basic_functionality (
428460 redis_client_sdk : RedisClientSDK ,
429461 semaphore_name : str ,
You can’t perform that action at this time.
0 commit comments