2023-05-15 18:25:02 +02:00
|
|
|
import structio
|
|
|
|
|
|
|
|
|
|
|
|
async def test_silent(i, j):
|
|
|
|
print(f"[test] Parent is alive, exiting after {i:.2f} seconds")
|
|
|
|
k = structio.clock()
|
|
|
|
with structio.skip_after(i) as scope:
|
|
|
|
print(f"[test] Sleeping for {j} seconds")
|
|
|
|
await structio.sleep(j)
|
2023-06-19 17:34:44 +02:00
|
|
|
print(
|
2023-07-10 12:14:14 +02:00
|
|
|
f"[test] Finished in {structio.clock() - k:.2f} seconds (timed out: {scope.timed_out})"
|
2023-06-19 17:34:44 +02:00
|
|
|
)
|
2023-05-15 18:25:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_loud(i, j):
|
|
|
|
print(f"[test] Parent is alive, exiting after {i:.2f} seconds")
|
|
|
|
k = structio.clock()
|
|
|
|
try:
|
2023-05-17 00:27:24 +02:00
|
|
|
with structio.with_timeout(i):
|
2023-05-15 18:25:02 +02:00
|
|
|
print(f"[test] Sleeping for {j} seconds")
|
|
|
|
await structio.sleep(j)
|
2023-05-16 11:59:01 +02:00
|
|
|
except structio.TimedOut:
|
2023-05-15 18:25:02 +02:00
|
|
|
print("[test] Timed out!")
|
|
|
|
print(f"[test] Finished in {structio.clock() - k:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
async def deadlock():
|
|
|
|
await structio.Event().wait()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_deadlock(i):
|
|
|
|
print(f"[test] Parent is alive, will exit in {i} seconds")
|
|
|
|
t = structio.clock()
|
|
|
|
with structio.skip_after(i):
|
|
|
|
print("[test] Entering deadlock")
|
|
|
|
await deadlock()
|
|
|
|
print(f"[test] Done in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
async def test_nested(i):
|
|
|
|
print(f"[test] Parent is alive, will exit in {i} seconds")
|
|
|
|
t = structio.clock()
|
|
|
|
with structio.skip_after(i):
|
|
|
|
print("[test] Entered first scope")
|
|
|
|
with structio.skip_after(i * 2):
|
|
|
|
# Even though this scope's timeout is
|
|
|
|
# larger than its parent, structio will
|
|
|
|
# still cancel it when its containing
|
|
|
|
# scope expires
|
|
|
|
print("[test] Entered second scope")
|
|
|
|
await deadlock()
|
|
|
|
print(f"[test] Done in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
2023-08-20 12:38:07 +02:00
|
|
|
async def nested_mess(n):
|
|
|
|
print(f"[test] Exiting in {n} seconds")
|
|
|
|
t = structio.clock()
|
|
|
|
with structio.TaskScope() as r:
|
|
|
|
with structio.skip_after(n) as s:
|
|
|
|
assert r.get_effective_deadline()[1] is s
|
|
|
|
# This never completes
|
|
|
|
await structio.sleep(n * 10)
|
|
|
|
print(f"[test] Exited in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
structio.run(nested_mess, 3)
|
2023-05-15 18:25:02 +02:00
|
|
|
structio.run(test_silent, 3, 5)
|
|
|
|
structio.run(test_loud, 3, 5)
|
|
|
|
structio.run(test_deadlock, 5)
|
|
|
|
structio.run(test_nested, 5)
|