Skip to content

Commit 8d1f30f

Browse files
committed
added test that shows unfairness
1 parent 758d97f commit 8d1f30f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
427459
async def test_context_manager_basic_functionality(
428460
redis_client_sdk: RedisClientSDK,
429461
semaphore_name: str,

0 commit comments

Comments
 (0)