Skip to content

Commit 58ed2e1

Browse files
committed
test: add tests for new task result and exception behavior
1 parent 4f41506 commit 58ed2e1

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

tests/extmod/asyncio_cancel_task.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ async def main():
3333
t = asyncio.create_task(task(2, True))
3434
print(t.cancel())
3535

36+
print("main checking task is cancelled", t.cancelled())
37+
3638
# Cancel task after it has started
3739
t = asyncio.create_task(task(2, True))
3840
await asyncio.sleep(0.01)
3941
print(t.cancel())
4042
print("main sleep")
4143
await asyncio.sleep(0.01)
4244

45+
print("main checking task is cancelled", t.cancelled())
46+
4347
# Cancel task multiple times after it has started
4448
t = asyncio.create_task(task(2, True))
4549
await asyncio.sleep(0.01)
@@ -48,6 +52,8 @@ async def main():
4852
print("main sleep")
4953
await asyncio.sleep(0.01)
5054

55+
print("main checking task is cancelled", t.cancelled())
56+
5157
# Await on a cancelled task
5258
print("main wait")
5359
try:
@@ -60,6 +66,8 @@ async def main():
6066
await asyncio.sleep(0.05)
6167
print(t.cancel())
6268

69+
print("main checking task is cancelled", t.cancelled())
70+
6371
# Nested: task2 waits on task, task2 is cancelled (should cancel task then task2)
6472
print("----")
6573
t = asyncio.create_task(task2(True))
@@ -69,6 +77,8 @@ async def main():
6977
print("main sleep")
7078
await asyncio.sleep(0.1)
7179

80+
print("main checking task 2 is cancelled", t.cancelled())
81+
7282
# Nested: task2 waits on task, task2 is cancelled but task doesn't allow it (task2 should continue)
7383
print("----")
7484
t = asyncio.create_task(task2(False))
@@ -77,6 +87,7 @@ async def main():
7787
t.cancel()
7888
print("main sleep")
7989
await asyncio.sleep(0.1)
90+
print("main checking task 2 is cancelled", t.cancelled())
8091

8192

8293
asyncio.run(main())
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Test the Task.done() method
2+
3+
try:
4+
import asyncio
5+
except ImportError:
6+
print("SKIP")
7+
raise SystemExit
8+
9+
10+
async def task(t, exc=None):
11+
print("task start")
12+
if t >= 0:
13+
await asyncio.sleep(t)
14+
if exc:
15+
raise exc
16+
print("task done")
17+
18+
19+
async def main():
20+
# Task that is not done yet raises an InvalidStateError
21+
print("=" * 10)
22+
t = asyncio.create_task(task(1))
23+
await asyncio.sleep(0)
24+
try:
25+
t.exception()
26+
assert False, "Should not get here"
27+
except Exception as e:
28+
print(repr(e))
29+
30+
# Task that is cancelled raises CancelledError
31+
print("=" * 10)
32+
t = asyncio.create_task(task(1))
33+
t.cancel()
34+
await asyncio.sleep(0)
35+
try:
36+
t.exception()
37+
assert False, "Should not get here"
38+
except asyncio.CancelledError as e:
39+
print(repr(e))
40+
41+
# Task that starts, runs and finishes without an exception should return None
42+
print("=" * 10)
43+
t = asyncio.create_task(task(0.01))
44+
await t
45+
print(t.exception())
46+
47+
# Task that raises immediately should return that exception
48+
print("=" * 10)
49+
t = asyncio.create_task(task(-1, ValueError))
50+
print(t.done())
51+
try:
52+
await t
53+
assert False, "Should not get here"
54+
except ValueError as e:
55+
print(repr(e))
56+
print(repr(t.exception()))
57+
58+
59+
asyncio.run(main())
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
==========
2+
False
3+
task start
4+
task done
5+
True
6+
True
7+
==========
8+
False
9+
task start
10+
False
11+
task done
12+
True
13+
==========
14+
False
15+
task start
16+
True
17+
ValueError()
18+
True
19+
==========
20+
False
21+
task start
22+
False
23+
ValueError()
24+
True

tests/extmod/asyncio_task_result.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Test the Task.done() method
2+
3+
try:
4+
import asyncio
5+
except ImportError:
6+
print("SKIP")
7+
raise SystemExit
8+
9+
10+
async def task(t, exc=None, ret=None):
11+
print("task start")
12+
if t >= 0:
13+
await asyncio.sleep(t)
14+
if exc:
15+
raise exc
16+
print("task done")
17+
return ret
18+
19+
20+
async def main():
21+
# Task that is not done yet raises an InvalidStateError
22+
print("=" * 10)
23+
t = asyncio.create_task(task(1))
24+
await asyncio.sleep(0)
25+
try:
26+
t.result()
27+
assert False, "Should not get here"
28+
except Exception as e:
29+
print(repr(e))
30+
31+
# Task that is cancelled raises CancelledError
32+
print("=" * 10)
33+
t = asyncio.create_task(task(1))
34+
t.cancel()
35+
await asyncio.sleep(0)
36+
try:
37+
t.result()
38+
assert False, "Should not get here"
39+
except asyncio.CancelledError as e:
40+
print(repr(e))
41+
42+
# Task that starts, runs and finishes without an exception or value should return None
43+
print("=" * 10)
44+
t = asyncio.create_task(task(0.01))
45+
await t
46+
print(t.result())
47+
assert t.result() is None
48+
49+
# Task that raises immediately should raise that exception when calling result
50+
print("=" * 10)
51+
t = asyncio.create_task(task(-1, ValueError))
52+
try:
53+
await t
54+
assert False, "Should not get here"
55+
except ValueError as e:
56+
print(repr(e))
57+
58+
try:
59+
t.result()
60+
assert False, "Should not get here"
61+
except ValueError as e:
62+
print(repr(e))
63+
64+
# Task that starts, runs and finishes without exception should return result
65+
print("=" * 10)
66+
t = asyncio.create_task(task(0.01, None, "hello world"))
67+
await t
68+
print(t.result())
69+
70+
71+
asyncio.run(main())
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
==========
2+
False
3+
task start
4+
task done
5+
True
6+
True
7+
==========
8+
False
9+
task start
10+
False
11+
task done
12+
True
13+
==========
14+
False
15+
task start
16+
True
17+
ValueError()
18+
True
19+
==========
20+
False
21+
task start
22+
False
23+
ValueError()
24+
True

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy