diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2023-06-08 16:01:38 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-06-19 17:33:03 +1000 |
commit | 6027c41c8f5b8f1a9e7b85b2bb93b3e6f2718e54 (patch) | |
tree | 08f41a4d0cd48fa5c0bc49519832ac2faba6923a /tests/extmod/asyncio_gather.py | |
parent | 2fbc08c462e247e7f78460783c9a07c76c5b762e (diff) | |
download | micropython-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_gather.py')
-rw-r--r-- | tests/extmod/asyncio_gather.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/extmod/asyncio_gather.py b/tests/extmod/asyncio_gather.py new file mode 100644 index 0000000000..78a40ba3aa --- /dev/null +++ b/tests/extmod/asyncio_gather.py @@ -0,0 +1,111 @@ +# test asyncio.gather() function + +try: + import asyncio +except ImportError: + print("SKIP") + raise SystemExit + + +async def factorial(name, number): + f = 1 + for i in range(2, number + 1): + print("Task {}: Compute factorial({})...".format(name, i)) + await asyncio.sleep(0.01) + f *= i + print("Task {}: factorial({}) = {}".format(name, number, f)) + return f + + +async def task(id, t=0.1): + print("start", id) + await asyncio.sleep(t) + print("end", id) + return id + + +async def task_loop(id): + print("task_loop start", id) + while True: + await asyncio.sleep(0.1) + print("task_loop loop", id) + + +async def task_raise(id, t=0.1): + print("task_raise start", id) + await asyncio.sleep(t) + print("task_raise raise", id) + raise ValueError(id) + + +async def gather_task(t0, t1): + print("gather_task") + await asyncio.gather(t0, t1) + print("gather_task2") + + +async def main(): + # Simple gather with return values + print(await asyncio.gather(factorial("A", 2), factorial("B", 3), factorial("C", 4))) + + print("====") + + # Gather with no awaitables + print(await asyncio.gather()) + + print("====") + + # Test return_exceptions, where one task is cancelled and the other finishes normally + tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))] + tasks[0].cancel() + print(await asyncio.gather(*tasks, return_exceptions=True)) + + print("====") + + # Test return_exceptions, where one task raises an exception and the other finishes normally. + tasks = [asyncio.create_task(task(1)), asyncio.create_task(task_raise(2))] + print(await asyncio.gather(*tasks, return_exceptions=True)) + + print("====") + + # Test case where one task raises an exception and other task keeps running. + tasks = [asyncio.create_task(task_loop(1)), asyncio.create_task(task_raise(2))] + try: + await asyncio.gather(*tasks) + except ValueError as er: + print(repr(er)) + print(tasks[0].done(), tasks[1].done()) + for t in tasks: + t.cancel() + await asyncio.sleep(0.2) + + print("====") + + # Test case where both tasks raise an exception. + # Use t=0 so they raise one after the other, between the gather starting and finishing. + tasks = [asyncio.create_task(task_raise(1, t=0)), asyncio.create_task(task_raise(2, t=0))] + try: + await asyncio.gather(*tasks) + except ValueError as er: + print(repr(er)) + print(tasks[0].done(), tasks[1].done()) + + print("====") + + # Cancel a multi gather. + t = asyncio.create_task(gather_task(task(1), task(2))) + await asyncio.sleep(0.05) + t.cancel() + await asyncio.sleep(0.2) + + # Test edge cases where the gather is cancelled just as tasks are created and ending. + for i in range(1, 4): + print("====") + t = asyncio.create_task(gather_task(task(1, t=0), task(2, t=0))) + for _ in range(i): + await asyncio.sleep(0) + t.cancel() + await asyncio.sleep(0.2) + + +asyncio.run(main()) |