summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/asyncio_lock_cancel.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_lock_cancel.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_lock_cancel.py')
-rw-r--r--tests/extmod/asyncio_lock_cancel.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/extmod/asyncio_lock_cancel.py b/tests/extmod/asyncio_lock_cancel.py
new file mode 100644
index 0000000000..c81bb3c7ba
--- /dev/null
+++ b/tests/extmod/asyncio_lock_cancel.py
@@ -0,0 +1,52 @@
+# Test that locks work when cancelling multiple waiters on the lock
+
+try:
+ import asyncio
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def task(i, lock, lock_flag):
+ print("task", i, "start")
+ try:
+ await lock.acquire()
+ except asyncio.CancelledError:
+ print("task", i, "cancel")
+ return
+ print("task", i, "lock_flag", lock_flag[0])
+ lock_flag[0] = True
+ await asyncio.sleep(0)
+ lock.release()
+ lock_flag[0] = False
+ print("task", i, "done")
+
+
+async def main():
+ # Create a lock and acquire it so the tasks below must wait
+ lock = asyncio.Lock()
+ await lock.acquire()
+ lock_flag = [True]
+
+ # Create 4 tasks and let them all run
+ t0 = asyncio.create_task(task(0, lock, lock_flag))
+ t1 = asyncio.create_task(task(1, lock, lock_flag))
+ t2 = asyncio.create_task(task(2, lock, lock_flag))
+ t3 = asyncio.create_task(task(3, lock, lock_flag))
+ await asyncio.sleep(0)
+
+ # Cancel 2 of the tasks (which are waiting on the lock) and release the lock
+ t1.cancel()
+ t2.cancel()
+ lock.release()
+ lock_flag[0] = False
+
+ # Let the tasks run to completion
+ for _ in range(4):
+ await asyncio.sleep(0)
+
+ # The locke should be unlocked
+ print(lock.locked())
+
+
+asyncio.run(main())