diff options
| author | Damien George <damien.p.george@gmail.com> | 2019-11-13 21:08:22 +1100 |
|---|---|---|
| committer | Damien George <damien.p.george@gmail.com> | 2020-03-26 01:25:45 +1100 |
| commit | c4935f30490d0446e16a51dbf7a6397b771cf804 (patch) | |
| tree | b095dd91914950939d4d0cdc10e7be3625fff00d /tests/extmod/uasyncio_event.py | |
| parent | 63b99443820f53afbdab5201044629d2bfecd73b (diff) | |
tests/extmod: Add uasyncio tests.
All .exp files are included because they require CPython 3.8 which may not
always be available.
Diffstat (limited to 'tests/extmod/uasyncio_event.py')
| -rw-r--r-- | tests/extmod/uasyncio_event.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/extmod/uasyncio_event.py b/tests/extmod/uasyncio_event.py new file mode 100644 index 000000000..fb8eb9ffa --- /dev/null +++ b/tests/extmod/uasyncio_event.py @@ -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()) |
