From 6027c41c8f5b8f1a9e7b85b2bb93b3e6f2718e54 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 8 Jun 2023 16:01:38 +1000 Subject: tests: Rename uasyncio to asyncio. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- tests/extmod/asyncio_micropython.py | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/extmod/asyncio_micropython.py (limited to 'tests/extmod/asyncio_micropython.py') diff --git a/tests/extmod/asyncio_micropython.py b/tests/extmod/asyncio_micropython.py new file mode 100644 index 0000000000..cc96d34be2 --- /dev/null +++ b/tests/extmod/asyncio_micropython.py @@ -0,0 +1,43 @@ +# Test MicroPython extensions on CPython asyncio: +# - sleep_ms +# - wait_for_ms + +try: + import time, asyncio +except ImportError: + print("SKIP") + raise SystemExit + + +async def task(id, t): + print("task start", id) + await asyncio.sleep_ms(t) + print("task end", id) + return id * 2 + + +async def main(): + # Simple sleep_ms + t0 = time.ticks_ms() + await asyncio.sleep_ms(1) + print(time.ticks_diff(time.ticks_ms(), t0) < 100) + + try: + # Sleep 1ms beyond maximum allowed sleep value + await asyncio.sleep_ms(time.ticks_add(0, -1) // 2 + 1) + except OverflowError: + print("OverflowError") + + # When task finished before the timeout + print(await asyncio.wait_for_ms(task(1, 5), 50)) + + # When timeout passes and task is cancelled + try: + print(await asyncio.wait_for_ms(task(2, 50), 5)) + except asyncio.TimeoutError: + print("timeout") + + print("finish") + + +asyncio.run(main()) -- cgit v1.2.3