tests/extmod: Add uasyncio tests.

All .exp files are included because they require CPython 3.8 which may not
always be available.
This commit is contained in:
Damien George
2019-11-13 21:08:22 +11:00
parent 63b9944382
commit c4935f3049
35 changed files with 1161 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
# Test that tasks return their value correctly to the caller
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def foo():
return 42
async def main():
# Call function directly via an await
print(await foo())
# Create a task and await on it
task = asyncio.create_task(foo())
print(await task)
asyncio.run(main())
@@ -0,0 +1,2 @@
42
42
+43
View File
@@ -0,0 +1,43 @@
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
try:
import utime
ticks = utime.ticks_ms
ticks_diff = utime.ticks_diff
except:
import time
ticks = lambda: int(time.time() * 1000)
ticks_diff = lambda t1, t0: t1 - t0
async def delay_print(t, s):
await asyncio.sleep(t)
print(s)
async def main():
print("start")
await asyncio.sleep(0.001)
print("after sleep")
t0 = ticks()
await delay_print(0.02, "short")
t1 = ticks()
await delay_print(0.04, "long")
t2 = ticks()
print("took {} {}".format(round(ticks_diff(t1, t0), -1), round(ticks_diff(t2, t1), -1)))
asyncio.run(main())
+5
View File
@@ -0,0 +1,5 @@
start
after sleep
short
long
took 20 40
+24
View File
@@ -0,0 +1,24 @@
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def forever():
print("forever start")
await asyncio.sleep(10)
async def main():
print("main start")
asyncio.create_task(forever())
await asyncio.sleep(0.001)
print("main done")
return 42
print(asyncio.run(main()))
+4
View File
@@ -0,0 +1,4 @@
main start
forever start
main done
42
+37
View File
@@ -0,0 +1,37 @@
# Test fairness of cancelling a task
# That tasks which continuously cancel each other don't take over the scheduler
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def task(id, other):
for i in range(3):
try:
print("start", id)
await asyncio.sleep(0)
print("done", id)
except asyncio.CancelledError as er:
print("cancelled", id)
if other is not None:
print(id, "cancels", other)
tasks[other].cancel()
async def main():
global tasks
tasks = [
asyncio.create_task(task(0, 1)),
asyncio.create_task(task(1, 0)),
asyncio.create_task(task(2, None)),
]
await tasks[2]
asyncio.run(main())
+24
View File
@@ -0,0 +1,24 @@
start 0
start 1
start 2
done 0
0 cancels 1
start 0
cancelled 1
1 cancels 0
start 1
done 2
start 2
cancelled 0
0 cancels 1
start 0
cancelled 1
1 cancels 0
start 1
done 2
start 2
cancelled 0
0 cancels 1
cancelled 1
1 cancels 0
done 2
+37
View File
@@ -0,0 +1,37 @@
# Test fairness of cancelling a task
# That tasks which keeps being cancelled by multiple other tasks gets a chance to run
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def task_a():
try:
while True:
print("sleep a")
await asyncio.sleep(0)
except asyncio.CancelledError:
print("cancelled a")
async def task_b(id, other):
while other.cancel():
print("sleep b", id)
await asyncio.sleep(0)
print("done b", id)
async def main():
t = asyncio.create_task(task_a())
for i in range(3):
asyncio.create_task(task_b(i, t))
await t
asyncio.run(main())
@@ -0,0 +1,8 @@
sleep a
sleep b 0
sleep b 1
sleep b 2
cancelled a
done b 0
done b 1
done b 2
+31
View File
@@ -0,0 +1,31 @@
# Test a task cancelling itself (currently unsupported)
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def task():
print("task start")
global_task.cancel()
async def main():
global global_task
global_task = asyncio.create_task(task())
try:
await global_task
except asyncio.CancelledError:
print("main cancel")
print("main done")
try:
asyncio.run(main())
except RuntimeError as er:
print(er)
+2
View File
@@ -0,0 +1,2 @@
task start
cannot cancel self
+85
View File
@@ -0,0 +1,85 @@
# Test cancelling a task
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def task(s, allow_cancel):
try:
print("task start")
await asyncio.sleep(s)
print("task done")
except asyncio.CancelledError as er:
print("task cancel")
if allow_cancel:
raise er
async def task2(allow_cancel):
print("task 2")
try:
await asyncio.create_task(task(0.05, allow_cancel))
except asyncio.CancelledError as er:
print("task 2 cancel")
raise er
print("task 2 done")
async def main():
# Cancel task immediately
t = asyncio.create_task(task(2, True))
print(t.cancel())
# Cancel task after it has started
t = asyncio.create_task(task(2, True))
await asyncio.sleep(0.01)
print(t.cancel())
print("main sleep")
await asyncio.sleep(0.01)
# Cancel task multiple times after it has started
t = asyncio.create_task(task(2, True))
await asyncio.sleep(0.01)
for _ in range(4):
print(t.cancel())
print("main sleep")
await asyncio.sleep(0.01)
# Await on a cancelled task
print("main wait")
try:
await t
except asyncio.CancelledError:
print("main got CancelledError")
# Cancel task after it has finished
t = asyncio.create_task(task(0.01, False))
await asyncio.sleep(0.05)
print(t.cancel())
# Nested: task2 waits on task, task2 is cancelled (should cancel task then task2)
print("----")
t = asyncio.create_task(task2(True))
await asyncio.sleep(0.01)
print("main cancel")
t.cancel()
print("main sleep")
await asyncio.sleep(0.1)
# Nested: task2 waits on task, task2 is cancelled but task doesn't allow it (task2 should continue)
print("----")
t = asyncio.create_task(task2(False))
await asyncio.sleep(0.01)
print("main cancel")
t.cancel()
print("main sleep")
await asyncio.sleep(0.1)
asyncio.run(main())
+31
View File
@@ -0,0 +1,31 @@
True
task start
True
main sleep
task cancel
task start
True
True
True
True
main sleep
task cancel
main wait
main got CancelledError
task start
task done
False
----
task 2
task start
main cancel
main sleep
task cancel
task 2 cancel
----
task 2
task start
main cancel
main sleep
task cancel
task 2 done
+98
View File
@@ -0,0 +1,98 @@
# Test Event class
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def task(id, ev):
print("start", id)
print(await ev.wait())
print("end", id)
async def task_delay_set(t, ev):
await asyncio.sleep(t)
print("set event")
ev.set()
async def main():
ev = asyncio.Event()
# Set and clear without anything waiting, and test is_set()
print(ev.is_set())
ev.set()
print(ev.is_set())
ev.clear()
print(ev.is_set())
# Create 2 tasks waiting on the event
print("----")
asyncio.create_task(task(1, ev))
asyncio.create_task(task(2, ev))
print("yield")
await asyncio.sleep(0)
print("set event")
ev.set()
print("yield")
await asyncio.sleep(0)
# Create a task waiting on the already-set event
print("----")
asyncio.create_task(task(3, ev))
print("yield")
await asyncio.sleep(0)
# Clear event, start a task, then set event again
print("----")
print("clear event")
ev.clear()
asyncio.create_task(task(4, ev))
await asyncio.sleep(0)
print("set event")
ev.set()
await asyncio.sleep(0)
# Cancel a task waiting on an event (set event then cancel task)
print("----")
ev = asyncio.Event()
t = asyncio.create_task(task(5, ev))
await asyncio.sleep(0)
ev.set()
t.cancel()
await asyncio.sleep(0.1)
# Cancel a task waiting on an event (cancel task then set event)
print("----")
ev = asyncio.Event()
t = asyncio.create_task(task(6, ev))
await asyncio.sleep(0)
t.cancel()
ev.set()
await asyncio.sleep(0.1)
# Wait for an event that does get set in time
print("----")
ev.clear()
asyncio.create_task(task_delay_set(0.01, ev))
await asyncio.wait_for(ev.wait(), 0.1)
await asyncio.sleep(0)
# Wait for an event that doesn't get set in time
print("----")
ev.clear()
asyncio.create_task(task_delay_set(0.1, ev))
try:
await asyncio.wait_for(ev.wait(), 0.01)
except asyncio.TimeoutError:
print("TimeoutError")
await ev.wait()
asyncio.run(main())
+33
View File
@@ -0,0 +1,33 @@
False
True
False
----
yield
start 1
start 2
set event
yield
True
end 1
True
end 2
----
yield
start 3
True
end 3
----
clear event
start 4
set event
True
end 4
----
start 5
----
start 6
----
set event
----
TimeoutError
set event
+40
View File
@@ -0,0 +1,40 @@
# Test fairness of Event.set()
# That tasks which continuously wait on events don't take over the scheduler
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
async def task1(id):
for i in range(4):
print("sleep", id)
await asyncio.sleep(0)
async def task2(id, ev):
for i in range(4):
ev.set()
ev.clear()
print("wait", id)
await ev.wait()
async def main():
ev = asyncio.Event()
tasks = [
asyncio.create_task(task1(0)),
asyncio.create_task(task2(2, ev)),
asyncio.create_task(task1(1)),
asyncio.create_task(task2(3, ev)),
]
await tasks[1]
ev.set()
asyncio.run(main())
+16
View File
@@ -0,0 +1,16 @@
sleep 0
wait 2
sleep 1
wait 3
sleep 0
sleep 1
wait 2
sleep 0
sleep 1
wait 3
sleep 0
sleep 1
wait 2
wait 3
wait 2
wait 3
+60
View File
@@ -0,0 +1,60 @@
# Test general exception handling
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
# main task raising an exception
async def main():
print("main start")
raise ValueError(1)
print("main done")
try:
asyncio.run(main())
except ValueError as er:
print("ValueError", er.args[0])
# sub-task raising an exception
async def task():
print("task start")
raise ValueError(2)
print("task done")
async def main():
print("main start")
t = asyncio.create_task(task())
await t
print("main done")
try:
asyncio.run(main())
except ValueError as er:
print("ValueError", er.args[0])
# main task raising an exception with sub-task not yet scheduled
# TODO not currently working, task is never scheduled
async def task():
# print('task run') uncomment this line when it works
pass
async def main():
print("main start")
asyncio.create_task(task())
raise ValueError(3)
print("main done")
try:
asyncio.run(main())
except ValueError as er:
print("ValueError", er.args[0])
+7
View File
@@ -0,0 +1,7 @@
main start
ValueError 1
main start
task start
ValueError 2
main start
ValueError 3

Some files were not shown because too many files have changed in this diff Show More