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
Lines changed: 28 additions & 0 deletions
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())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
42
2+
42

tests/asyncio/asyncio_basic.py

Lines changed: 50 additions & 0 deletions
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())

tests/asyncio/asyncio_basic.py.exp

Lines changed: 6 additions & 0 deletions
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

tests/asyncio/asyncio_basic2.py

Lines changed: 26 additions & 0 deletions
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()))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
main start
2+
forever start
3+
main done
4+
42
Lines changed: 38 additions & 0 deletions
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())
Lines changed: 24 additions & 0 deletions
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
Lines changed: 38 additions & 0 deletions
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())
Lines changed: 8 additions & 0 deletions
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

0 commit comments

Comments
 (0)