Skip to content

Commit b5a0d1b

Browse files
committed
test: add tests from circuitpython proper
1 parent e935a66 commit b5a0d1b

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

+1952
-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

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
42
2+
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

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
start
2+
after sleep
3+
short
4+
long
5+
negative
6+
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

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
main start
2+
forever start
3+
main done
4+
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

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
start 0
2+
start 1
3+
start 2
4+
done 0
5+
0 cancels 1
6+
start 0
7+
cancelled 1
8+
1 cancels 0
9+
start 1
10+
done 2
11+
start 2
12+
cancelled 0
13+
0 cancels 1
14+
start 0
15+
cancelled 1
16+
1 cancels 0
17+
start 1
18+
done 2
19+
start 2
20+
cancelled 0
21+
0 cancels 1
22+
cancelled 1
23+
1 cancels 0
24+
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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sleep a
2+
sleep b 0
3+
sleep b 1
4+
sleep b 2
5+
cancelled a
6+
done b 0
7+
done b 1
8+
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

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
task start
2+
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())

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

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
True
2+
task start
3+
True
4+
main sleep
5+
task cancel
6+
task start
7+
True
8+
True
9+
True
10+
True
11+
main sleep
12+
task cancel
13+
main wait
14+
main got CancelledError
15+
task start
16+
task done
17+
False
18+
----
19+
task 2
20+
task start
21+
main cancel
22+
main sleep
23+
task cancel
24+
task 2 cancel
25+
----
26+
task 2
27+
task start
28+
main cancel
29+
main sleep
30+
task cancel
31+
task 2 done

0 commit comments

Comments
 (0)