summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/asyncio_set_exception_handler.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_set_exception_handler.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_set_exception_handler.py')
-rw-r--r--tests/extmod/asyncio_set_exception_handler.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/extmod/asyncio_set_exception_handler.py b/tests/extmod/asyncio_set_exception_handler.py
new file mode 100644
index 0000000000..5935f0f4eb
--- /dev/null
+++ b/tests/extmod/asyncio_set_exception_handler.py
@@ -0,0 +1,53 @@
+# Test that tasks return their value correctly to the caller
+
+try:
+ import asyncio
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+def custom_handler(loop, context):
+ print("custom_handler", repr(context["exception"]))
+
+
+async def task(i):
+ # Raise with 2 args so exception prints the same in uPy and CPython
+ raise ValueError(i, i + 1)
+
+
+async def main():
+ loop = asyncio.get_event_loop()
+
+ # Check default exception handler, should be None
+ print(loop.get_exception_handler())
+
+ # Set exception handler and test it was set
+ loop.set_exception_handler(custom_handler)
+ print(loop.get_exception_handler() == custom_handler)
+
+ # Create a task that raises and uses the custom exception handler
+ asyncio.create_task(task(0))
+ print("sleep")
+ for _ in range(2):
+ await asyncio.sleep(0)
+
+ # Create 2 tasks to test order of printing exception
+ asyncio.create_task(task(1))
+ asyncio.create_task(task(2))
+ print("sleep")
+ for _ in range(2):
+ await asyncio.sleep(0)
+
+ # Create a task, let it run, then await it (no exception should be printed)
+ t = asyncio.create_task(task(3))
+ await asyncio.sleep(0)
+ try:
+ await t
+ except ValueError as er:
+ print(repr(er))
+
+ print("done")
+
+
+asyncio.run(main())