1# Test cancelling a task waiting on stream IO
2
3try:
4    import uasyncio as asyncio
5except ImportError:
6    try:
7        import asyncio
8    except ImportError:
9        print("SKIP")
10        raise SystemExit
11
12
13async def get(reader):
14    print("start")
15    try:
16        await reader.read(10)
17        print("fail")
18    except asyncio.CancelledError:
19        print("cancelled")
20
21
22async def main(url):
23    reader, writer = await asyncio.open_connection(url, 80)
24    task = asyncio.create_task(get(reader))
25    await asyncio.sleep(0)
26    print("cancelling")
27    task.cancel()
28    print("waiting")
29    await task
30    print("done")
31    writer.close()
32    await writer.wait_closed()
33
34
35asyncio.run(main("micropython.org"))
36