summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/asyncio_threadsafeflag.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_threadsafeflag.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_threadsafeflag.py')
-rw-r--r--tests/extmod/asyncio_threadsafeflag.py99
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/extmod/asyncio_threadsafeflag.py b/tests/extmod/asyncio_threadsafeflag.py
new file mode 100644
index 0000000000..46da1b7b48
--- /dev/null
+++ b/tests/extmod/asyncio_threadsafeflag.py
@@ -0,0 +1,99 @@
+# Test Event class
+
+try:
+ import asyncio
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+import micropython
+
+try:
+ micropython.schedule
+except AttributeError:
+ print("SKIP")
+ raise SystemExit
+
+
+try:
+ # Unix port can't select/poll on user-defined types.
+ import select
+
+ poller = select.poll()
+ poller.register(asyncio.ThreadSafeFlag())
+except TypeError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def task(id, flag):
+ print("task", id)
+ await flag.wait()
+ print("task", id, "done")
+
+
+def set_from_schedule(flag):
+ print("schedule")
+ flag.set()
+ print("schedule done")
+
+
+async def main():
+ flag = asyncio.ThreadSafeFlag()
+
+ # Set the flag from within the loop.
+ t = asyncio.create_task(task(1, flag))
+ print("yield")
+ await asyncio.sleep(0)
+ print("set event")
+ flag.set()
+ print("yield")
+ await asyncio.sleep(0)
+ print("wait task")
+ await t
+
+ # Set the flag from scheduler context.
+ print("----")
+ t = asyncio.create_task(task(2, flag))
+ print("yield")
+ await asyncio.sleep(0)
+ print("set event")
+ micropython.schedule(set_from_schedule, flag)
+ print("yield")
+ await asyncio.sleep(0)
+ print("wait task")
+ await t
+
+ # Flag already set.
+ print("----")
+ print("set event")
+ flag.set()
+ t = asyncio.create_task(task(3, flag))
+ print("yield")
+ await asyncio.sleep(0)
+ print("wait task")
+ await t
+
+ # Flag set, cleared, and set again.
+ print("----")
+ print("set event")
+ flag.set()
+ print("yield")
+ await asyncio.sleep(0)
+ print("clear event")
+ flag.clear()
+ print("yield")
+ await asyncio.sleep(0)
+ t = asyncio.create_task(task(4, flag))
+ print("yield")
+ await asyncio.sleep(0)
+ print("set event")
+ flag.set()
+ print("yield")
+ await asyncio.sleep(0)
+ print("wait task")
+ await t
+
+
+asyncio.run(main())