diff options
author | Damien George <damien.p.george@gmail.com> | 2020-06-05 21:26:27 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-06-10 22:29:44 +1000 |
commit | a4c96fb3b0c5e3bf83238a0edd0fbcbfd96208c8 (patch) | |
tree | 32f02633f7a2aae51e3882693cdcfa8122e73f15 /tests/extmod/uasyncio_micropython.py | |
parent | f3062b5cbdeea5900486a38902e3155117ebf820 (diff) | |
download | micropython-a4c96fb3b0c5e3bf83238a0edd0fbcbfd96208c8.tar.gz micropython-a4c96fb3b0c5e3bf83238a0edd0fbcbfd96208c8.zip |
extmod/uasyncio: Add asyncio.wait_for_ms function.
Fixes issue #6107.
Diffstat (limited to 'tests/extmod/uasyncio_micropython.py')
-rw-r--r-- | tests/extmod/uasyncio_micropython.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/extmod/uasyncio_micropython.py b/tests/extmod/uasyncio_micropython.py new file mode 100644 index 0000000000..69e5fa3224 --- /dev/null +++ b/tests/extmod/uasyncio_micropython.py @@ -0,0 +1,37 @@ +# Test MicroPython extensions on CPython asyncio: +# - sleep_ms +# - wait_for_ms + +try: + import utime, uasyncio +except ImportError: + print("SKIP") + raise SystemExit + + +async def task(id, t): + print("task start", id) + await uasyncio.sleep_ms(t) + print("task end", id) + return id * 2 + + +async def main(): + # Simple sleep_ms + t0 = utime.ticks_ms() + await uasyncio.sleep_ms(1) + print(utime.ticks_diff(utime.ticks_ms(), t0) < 100) + + # When task finished before the timeout + print(await uasyncio.wait_for_ms(task(1, 5), 50)) + + # When timeout passes and task is cancelled + try: + print(await uasyncio.wait_for_ms(task(2, 50), 5)) + except uasyncio.TimeoutError: + print("timeout") + + print("finish") + + +uasyncio.run(main()) |