Skip to content

Commit b283118

Browse files
committed
Update test_cancel.py
1 parent 8c2b878 commit b283118

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

tests/test_cancel.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@
66
@pytest.mark.parametrize("num_to_cancel", [0, 1, 2, 3])
77
async def test_cancel(num_to_cancel: int) -> None:
88
cache_item_task_finished = False
9-
9+
1010
@alru_cache
1111
async def coro(val: int) -> int:
12+
# I am a long running coro function
1213
nonlocal cache_item_task_finished
1314
await asyncio.sleep(2)
1415
cache_item_task_finished = True
1516
return val
1617

17-
tasks = [asyncio.create_task(coro()) for _ in range(3)]
18+
# create 3 tasks for the cached function using the same key
19+
tasks = [asyncio.create_task(coro(1)) for _ in range(3)]
1820

1921
# force the event loop to run once so the tasks can begin
2022
await asyncio.sleep(0)
2123

24+
# maybe cancel some tasks
2225
for i in range(num_to_cancel):
2326
tasks[i].cancel()
2427

28+
# allow enough time for the non-cancelled tasks to complete
2529
await asyncio.sleep(3)
2630

31+
# check state
2732
assert cache_item_task_finished is num_to_cancel < 3
33+
assert all(task.cancelled() for task in tasks[:num_to_cancel])
34+
assert all(task.finished() for task in tasks[num_to_cancel:])

0 commit comments

Comments
 (0)