Skip to content

Commit c457e54

Browse files
committed
test: add tests from circuitpython proper
1 parent 5d980f2 commit c457e54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2027
-0
lines changed

Diff for: tests/asyncio/asyncio_await_return.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# MicroPython uasyncio module
6+
# MIT license; Copyright (c) 2019 Damien P. George
7+
#
8+
# pylint: skip-file
9+
#
10+
# Test that tasks return their value correctly to the caller
11+
12+
import asyncio
13+
14+
15+
async def example():
16+
return 42
17+
18+
19+
async def main():
20+
# Call function directly via an await
21+
print(await example())
22+
23+
# Create a task and await on it
24+
task = asyncio.create_task(example())
25+
print(await task)
26+
27+
28+
asyncio.run(main())

Diff for: tests/asyncio/asyncio_await_return.py.exp

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
42
5+
42

Diff for: tests/asyncio/asyncio_basic.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# MicroPython uasyncio module
6+
# MIT license; Copyright (c) 2019 Damien P. George
7+
#
8+
# pylint: skip-file
9+
10+
import asyncio
11+
import time
12+
13+
14+
if hasattr(time, "ticks_ms"):
15+
ticks = time.ticks_ms
16+
ticks_diff = time.ticks_diff
17+
else:
18+
ticks = lambda: int(time.time() * 1000)
19+
ticks_diff = lambda t1, t0: t1 - t0
20+
21+
22+
async def delay_print(t, s):
23+
await asyncio.sleep(t)
24+
print(s)
25+
26+
27+
async def main():
28+
print("start")
29+
30+
await asyncio.sleep(0.001)
31+
print("after sleep")
32+
33+
t0 = ticks()
34+
await delay_print(0.2, "short")
35+
t1 = ticks()
36+
await delay_print(0.4, "long")
37+
t2 = ticks()
38+
await delay_print(-1, "negative")
39+
t3 = ticks()
40+
41+
print(
42+
"took {} {} {}".format(
43+
round(ticks_diff(t1, t0), -2),
44+
round(ticks_diff(t2, t1), -2),
45+
round(ticks_diff(t3, t2), -2),
46+
)
47+
)
48+
49+
50+
asyncio.run(main())

Diff for: tests/asyncio/asyncio_basic.py.exp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
start
5+
after sleep
6+
short
7+
long
8+
negative
9+
took 200 400 0

Diff for: tests/asyncio/asyncio_basic2.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# MicroPython uasyncio module
6+
# MIT license; Copyright (c) 2019 Damien P. George
7+
#
8+
# pylint: skip-file
9+
10+
import asyncio
11+
12+
13+
async def forever():
14+
print("forever start")
15+
await asyncio.sleep(10)
16+
17+
18+
async def main():
19+
print("main start")
20+
asyncio.create_task(forever())
21+
await asyncio.sleep(0.001)
22+
print("main done")
23+
return 42
24+
25+
26+
print(asyncio.run(main()))

Diff for: tests/asyncio/asyncio_basic2.py.exp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
main start
5+
forever start
6+
main done
7+
42

Diff for: tests/asyncio/asyncio_cancel_fair.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# MicroPython uasyncio module
6+
# MIT license; Copyright (c) 2019 Damien P. George
7+
#
8+
# pylint: skip-file
9+
#
10+
# Test fairness of cancelling a task
11+
# That tasks which continuously cancel each other don't take over the scheduler
12+
import asyncio
13+
14+
15+
async def task(id, other):
16+
for i in range(3):
17+
try:
18+
print("start", id)
19+
await asyncio.sleep(0)
20+
print("done", id)
21+
except asyncio.CancelledError as er:
22+
print("cancelled", id)
23+
if other is not None:
24+
print(id, "cancels", other)
25+
tasks[other].cancel()
26+
27+
28+
async def main():
29+
global tasks
30+
tasks = [
31+
asyncio.create_task(task(0, 1)),
32+
asyncio.create_task(task(1, 0)),
33+
asyncio.create_task(task(2, None)),
34+
]
35+
await tasks[2]
36+
37+
38+
asyncio.run(main())

Diff for: tests/asyncio/asyncio_cancel_fair.py.exp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
start 0
5+
start 1
6+
start 2
7+
done 0
8+
0 cancels 1
9+
start 0
10+
cancelled 1
11+
1 cancels 0
12+
start 1
13+
done 2
14+
start 2
15+
cancelled 0
16+
0 cancels 1
17+
start 0
18+
cancelled 1
19+
1 cancels 0
20+
start 1
21+
done 2
22+
start 2
23+
cancelled 0
24+
0 cancels 1
25+
cancelled 1
26+
1 cancels 0
27+
done 2

Diff for: tests/asyncio/asyncio_cancel_fair2.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# MicroPython uasyncio module
6+
# MIT license; Copyright (c) 2019 Damien P. George
7+
#
8+
# pylint: skip-file
9+
#
10+
# Test fairness of cancelling a task
11+
# That tasks which keeps being cancelled by multiple other tasks gets a chance to run
12+
import asyncio
13+
14+
15+
async def task_a():
16+
try:
17+
while True:
18+
print("sleep a")
19+
await asyncio.sleep(0)
20+
except asyncio.CancelledError:
21+
print("cancelled a")
22+
23+
24+
async def task_b(id, other):
25+
while other.cancel():
26+
print("sleep b", id)
27+
await asyncio.sleep(0)
28+
print("done b", id)
29+
30+
31+
async def main():
32+
t = asyncio.create_task(task_a())
33+
for i in range(3):
34+
asyncio.create_task(task_b(i, t))
35+
await t
36+
37+
38+
asyncio.run(main())

Diff for: tests/asyncio/asyncio_cancel_fair2.py.exp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
sleep a
5+
sleep b 0
6+
sleep b 1
7+
sleep b 2
8+
cancelled a
9+
done b 0
10+
done b 1
11+
done b 2

Diff for: tests/asyncio/asyncio_cancel_self.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# MicroPython uasyncio module
6+
# MIT license; Copyright (c) 2019 Damien P. George
7+
#
8+
# pylint: skip-file
9+
#
10+
# Test a task cancelling itself (currently unsupported)
11+
import asyncio
12+
13+
14+
async def task():
15+
print("task start")
16+
global_task.cancel()
17+
18+
19+
async def main():
20+
global global_task
21+
global_task = asyncio.create_task(task())
22+
try:
23+
await global_task
24+
except asyncio.CancelledError:
25+
print("main cancel")
26+
print("main done")
27+
28+
29+
try:
30+
asyncio.run(main())
31+
except RuntimeError as er:
32+
print(er)

Diff for: tests/asyncio/asyncio_cancel_self.py.exp

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
task start
5+
can't cancel self

Diff for: tests/asyncio/asyncio_cancel_task.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# SPDX-FileCopyrightText: 2019 Damien P. George
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# MicroPython uasyncio module
6+
# MIT license; Copyright (c) 2019 Damien P. George
7+
#
8+
# pylint: skip-file
9+
#
10+
# Test cancelling a task
11+
12+
try:
13+
import asyncio
14+
except ImportError:
15+
print("SKIP")
16+
raise SystemExit
17+
18+
19+
async def task(s, allow_cancel):
20+
try:
21+
print("task start")
22+
await asyncio.sleep(s)
23+
print("task done")
24+
except asyncio.CancelledError as er:
25+
print("task cancel")
26+
if allow_cancel:
27+
raise er
28+
29+
30+
async def task2(allow_cancel):
31+
print("task 2")
32+
try:
33+
await asyncio.create_task(task(0.05, allow_cancel))
34+
except asyncio.CancelledError as er:
35+
print("task 2 cancel")
36+
raise er
37+
print("task 2 done")
38+
39+
40+
async def main():
41+
# Cancel task immediately
42+
t = asyncio.create_task(task(2, True))
43+
print(t.cancel())
44+
45+
# Cancel task after it has started
46+
t = asyncio.create_task(task(2, True))
47+
await asyncio.sleep(0.01)
48+
print(t.cancel())
49+
print("main sleep")
50+
await asyncio.sleep(0.01)
51+
52+
# Cancel task multiple times after it has started
53+
t = asyncio.create_task(task(2, True))
54+
await asyncio.sleep(0.01)
55+
for _ in range(4):
56+
print(t.cancel())
57+
print("main sleep")
58+
await asyncio.sleep(0.01)
59+
60+
# Await on a cancelled task
61+
print("main wait")
62+
try:
63+
await t
64+
except asyncio.CancelledError:
65+
print("main got CancelledError")
66+
67+
# Cancel task after it has finished
68+
t = asyncio.create_task(task(0.01, False))
69+
await asyncio.sleep(0.05)
70+
print(t.cancel())
71+
72+
# Nested: task2 waits on task, task2 is cancelled (should cancel task then task2)
73+
print("----")
74+
t = asyncio.create_task(task2(True))
75+
await asyncio.sleep(0.01)
76+
print("main cancel")
77+
t.cancel()
78+
print("main sleep")
79+
await asyncio.sleep(0.1)
80+
81+
# Nested: task2 waits on task, task2 is cancelled but task doesn't allow it (task2 should continue)
82+
print("----")
83+
t = asyncio.create_task(task2(False))
84+
await asyncio.sleep(0.01)
85+
print("main cancel")
86+
t.cancel()
87+
print("main sleep")
88+
await asyncio.sleep(0.1)
89+
90+
91+
asyncio.run(main())

0 commit comments

Comments
 (0)