Skip to content
Prev Previous commit
Next Next commit
test: add tests from circuitpython proper
  • Loading branch information
imnotjames committed Nov 12, 2023
commit c457e546764422e8def3c98f223c1a3d60672fc5
28 changes: 28 additions & 0 deletions tests/asyncio/asyncio_await_return.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
#
# MicroPython uasyncio module
# MIT license; Copyright (c) 2019 Damien P. George
#
# pylint: skip-file
#
# Test that tasks return their value correctly to the caller

import asyncio


async def example():
return 42


async def main():
# Call function directly via an await
print(await example())

# Create a task and await on it
task = asyncio.create_task(example())
print(await task)


asyncio.run(main())
5 changes: 5 additions & 0 deletions tests/asyncio/asyncio_await_return.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
42
42
50 changes: 50 additions & 0 deletions tests/asyncio/asyncio_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
#
# MicroPython uasyncio module
# MIT license; Copyright (c) 2019 Damien P. George
#
# pylint: skip-file

import asyncio
import time


if hasattr(time, "ticks_ms"):
ticks = time.ticks_ms
ticks_diff = time.ticks_diff
else:
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.2, "short")
t1 = ticks()
await delay_print(0.4, "long")
t2 = ticks()
await delay_print(-1, "negative")
t3 = ticks()

print(
"took {} {} {}".format(
round(ticks_diff(t1, t0), -2),
round(ticks_diff(t2, t1), -2),
round(ticks_diff(t3, t2), -2),
)
)


asyncio.run(main())
9 changes: 9 additions & 0 deletions tests/asyncio/asyncio_basic.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
start
after sleep
short
long
negative
took 200 400 0
26 changes: 26 additions & 0 deletions tests/asyncio/asyncio_basic2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
#
# MicroPython uasyncio module
# MIT license; Copyright (c) 2019 Damien P. George
#
# pylint: skip-file

import asyncio


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()))
7 changes: 7 additions & 0 deletions tests/asyncio/asyncio_basic2.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
main start
forever start
main done
42
38 changes: 38 additions & 0 deletions tests/asyncio/asyncio_cancel_fair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
#
# MicroPython uasyncio module
# MIT license; Copyright (c) 2019 Damien P. George
#
# pylint: skip-file
#
# Test fairness of cancelling a task
# That tasks which continuously cancel each other don't take over the scheduler
import asyncio


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())
27 changes: 27 additions & 0 deletions tests/asyncio/asyncio_cancel_fair.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
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
38 changes: 38 additions & 0 deletions tests/asyncio/asyncio_cancel_fair2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
#
# MicroPython uasyncio module
# MIT license; Copyright (c) 2019 Damien P. George
#
# pylint: skip-file
#
# Test fairness of cancelling a task
# That tasks which keeps being cancelled by multiple other tasks gets a chance to run
import asyncio


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())
11 changes: 11 additions & 0 deletions tests/asyncio/asyncio_cancel_fair2.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
sleep a
sleep b 0
sleep b 1
sleep b 2
cancelled a
done b 0
done b 1
done b 2
32 changes: 32 additions & 0 deletions tests/asyncio/asyncio_cancel_self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
#
# MicroPython uasyncio module
# MIT license; Copyright (c) 2019 Damien P. George
#
# pylint: skip-file
#
# Test a task cancelling itself (currently unsupported)
import asyncio


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)
5 changes: 5 additions & 0 deletions tests/asyncio/asyncio_cancel_self.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
task start
can't cancel self
91 changes: 91 additions & 0 deletions tests/asyncio/asyncio_cancel_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# SPDX-FileCopyrightText: 2019 Damien P. George
#
# SPDX-License-Identifier: MIT
#
# MicroPython uasyncio module
# MIT license; Copyright (c) 2019 Damien P. George
#
# pylint: skip-file
#
# Test cancelling a task

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())
Loading