summaryrefslogtreecommitdiff
path: root/tests/extmod/uasyncio_lock.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-11-13 21:08:22 +1100
committerDamien George <damien.p.george@gmail.com>2020-03-26 01:25:45 +1100
commitc4935f30490d0446e16a51dbf7a6397b771cf804 (patch)
treeb095dd91914950939d4d0cdc10e7be3625fff00d /tests/extmod/uasyncio_lock.py
parent63b99443820f53afbdab5201044629d2bfecd73b (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_lock.py')
-rw-r--r--tests/extmod/uasyncio_lock.py97
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/extmod/uasyncio_lock.py b/tests/extmod/uasyncio_lock.py
new file mode 100644
index 000000000..096a8c82b
--- /dev/null
+++ b/tests/extmod/uasyncio_lock.py
@@ -0,0 +1,97 @@
+# Test Lock class
+
+try:
+ import uasyncio as asyncio
+except ImportError:
+ try:
+ import asyncio
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def task_loop(id, lock):
+ print("task start", id)
+ for i in range(3):
+ async with lock:
+ print("task have", id, i)
+ print("task end", id)
+
+
+async def task_sleep(lock):
+ async with lock:
+ print("task have", lock.locked())
+ await asyncio.sleep(0.2)
+ print("task release", lock.locked())
+ await lock.acquire()
+ print("task have again")
+ lock.release()
+
+
+async def task_cancel(id, lock, to_cancel=None):
+ try:
+ async with lock:
+ print("task got", id)
+ await asyncio.sleep(0.1)
+ print("task release", id)
+ if to_cancel:
+ to_cancel[0].cancel()
+ except asyncio.CancelledError:
+ print("task cancel", id)
+
+
+async def main():
+ lock = asyncio.Lock()
+
+ # Basic acquire/release
+ print(lock.locked())
+ await lock.acquire()
+ print(lock.locked())
+ await asyncio.sleep(0)
+ lock.release()
+ print(lock.locked())
+ await asyncio.sleep(0)
+
+ # Use with "async with"
+ async with lock:
+ print("have lock")
+
+ # 3 tasks wanting the lock
+ print("----")
+ asyncio.create_task(task_loop(1, lock))
+ asyncio.create_task(task_loop(2, lock))
+ t3 = asyncio.create_task(task_loop(3, lock))
+ await lock.acquire()
+ await asyncio.sleep(0)
+ lock.release()
+ await t3
+
+ # 2 sleeping tasks both wanting the lock
+ print("----")
+ asyncio.create_task(task_sleep(lock))
+ await asyncio.sleep(0.1)
+ await task_sleep(lock)
+
+ # 3 tasks, the first cancelling the second, the third should still run
+ print("----")
+ ts = [None]
+ asyncio.create_task(task_cancel(0, lock, ts))
+ ts[0] = asyncio.create_task(task_cancel(1, lock))
+ asyncio.create_task(task_cancel(2, lock))
+ await asyncio.sleep(0.3)
+ print(lock.locked())
+
+ # 3 tasks, the second and third being cancelled while waiting on the lock
+ print("----")
+ t0 = asyncio.create_task(task_cancel(0, lock))
+ t1 = asyncio.create_task(task_cancel(1, lock))
+ t2 = asyncio.create_task(task_cancel(2, lock))
+ await asyncio.sleep(0.05)
+ t1.cancel()
+ await asyncio.sleep(0.1)
+ t2.cancel()
+ await asyncio.sleep(0.1)
+ print(lock.locked())
+
+
+asyncio.run(main())