summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/asyncio_event.py
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2023-06-08 16:01:38 +1000
committerDamien George <damien@micropython.org>2023-06-19 17:33:03 +1000
commit6027c41c8f5b8f1a9e7b85b2bb93b3e6f2718e54 (patch)
tree08f41a4d0cd48fa5c0bc49519832ac2faba6923a /tests/extmod/asyncio_event.py
parent2fbc08c462e247e7f78460783c9a07c76c5b762e (diff)
downloadmicropython-6027c41c8f5b8f1a9e7b85b2bb93b3e6f2718e54.tar.gz
micropython-6027c41c8f5b8f1a9e7b85b2bb93b3e6f2718e54.zip
tests: Rename uasyncio to asyncio.
This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tests/extmod/asyncio_event.py')
-rw-r--r--tests/extmod/asyncio_event.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/extmod/asyncio_event.py b/tests/extmod/asyncio_event.py
new file mode 100644
index 0000000000..290323a7de
--- /dev/null
+++ b/tests/extmod/asyncio_event.py
@@ -0,0 +1,95 @@
+# Test Event class
+
+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())