1# Test fairness of cancelling a task
2# That tasks which continuously cancel each other don't take over the scheduler
3
4try:
5    import uasyncio as asyncio
6except ImportError:
7    try:
8        import asyncio
9    except ImportError:
10        print("SKIP")
11        raise SystemExit
12
13
14async def task(id, other):
15    for i in range(3):
16        try:
17            print("start", id)
18            await asyncio.sleep(0)
19            print("done", id)
20        except asyncio.CancelledError as er:
21            print("cancelled", id)
22        if other is not None:
23            print(id, "cancels", other)
24            tasks[other].cancel()
25
26
27async def main():
28    global tasks
29    tasks = [
30        asyncio.create_task(task(0, 1)),
31        asyncio.create_task(task(1, 0)),
32        asyncio.create_task(task(2, None)),
33    ]
34    await tasks[2]
35
36
37asyncio.run(main())
38