diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2021-02-15 17:28:39 +1100 |
---|---|---|
committer | Jim Mussared <jim.mussared@gmail.com> | 2021-02-16 17:08:36 +1100 |
commit | 83d23059ef80d0d28d1e0769a721c83665b0095f (patch) | |
tree | f7ad2651a3af12d4f0fe1b07ef8ad0897ca2dca6 /tests/extmod/uasyncio_threadsafeflag.py | |
parent | cdf9c8648f81dd4f2795a71b075aab75f01a9f1f (diff) | |
download | micropython-83d23059ef80d0d28d1e0769a721c83665b0095f.tar.gz micropython-83d23059ef80d0d28d1e0769a721c83665b0095f.zip |
tests/extmod: Add test for ThreadSafeFlag.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tests/extmod/uasyncio_threadsafeflag.py')
-rw-r--r-- | tests/extmod/uasyncio_threadsafeflag.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/extmod/uasyncio_threadsafeflag.py b/tests/extmod/uasyncio_threadsafeflag.py new file mode 100644 index 0000000000..4e002a3d2a --- /dev/null +++ b/tests/extmod/uasyncio_threadsafeflag.py @@ -0,0 +1,79 @@ +# Test Event class + +try: + import uasyncio as 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 uselect as 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 + + +asyncio.run(main()) |