Skip to content

Commit 0a8a29e

Browse files
committed
doc
1 parent 0a504c2 commit 0a8a29e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

packages/common-library/tests/test_async_tools.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import time
23
from concurrent.futures import ThreadPoolExecutor
34
from typing import Any
45

@@ -98,11 +99,12 @@ def fetchone(self) -> Any: # pylint: disable=no-self-use
9899

99100
async def test_cancel_and_wait():
100101
state = {"started": False, "cancelled": False, "cleaned_up": False}
102+
SLEEP_TIME = 5 # seconds
101103

102104
async def coro():
103105
try:
104106
state["started"] = True
105-
await asyncio.sleep(5)
107+
await asyncio.sleep(SLEEP_TIME)
106108
except asyncio.CancelledError:
107109
state["cancelled"] = True
108110
raise
@@ -112,8 +114,11 @@ async def coro():
112114
task = asyncio.create_task(coro())
113115
await asyncio.sleep(0.1) # Let coro start
114116

117+
start = time.time()
115118
await cancel_and_wait(task)
116119

120+
elapsed = time.time() - start
121+
assert elapsed < SLEEP_TIME, "Task should be cancelled quickly"
117122
assert task.done()
118123
assert task.cancelled()
119124
assert state["started"]
@@ -142,7 +147,7 @@ async def outer_coro():
142147
except asyncio.CancelledError:
143148
assert (
144149
not inner_task.cancelled()
145-
), "Internal Task should not be cancelled yet (shielded)"
150+
), "Internal Task DOES NOT RAISE CancelledError"
146151
raise
147152

148153
# Cancel the wrapper after a short delay

0 commit comments

Comments
 (0)