summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/uasyncio_task_done.py
blob: 2700da8c341689f78878cc2856c31e5eea6bf8ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Test the Task.done() method

try:
    import uasyncio as asyncio
except ImportError:
    try:
        import asyncio
    except ImportError:
        print("SKIP")
        raise SystemExit


async def task(t, exc=None):
    print("task start")
    if t >= 0:
        await asyncio.sleep(t)
    if exc:
        raise exc
    print("task done")


async def main():
    # Task that finishes immediately.
    print("=" * 10)
    t = asyncio.create_task(task(-1))
    print(t.done())
    await asyncio.sleep(0)
    print(t.done())
    await t
    print(t.done())

    # Task that starts, runs and finishes.
    print("=" * 10)
    t = asyncio.create_task(task(0.01))
    print(t.done())
    await asyncio.sleep(0)
    print(t.done())
    await t
    print(t.done())

    # Task that raises immediately.
    print("=" * 10)
    t = asyncio.create_task(task(-1, ValueError))
    print(t.done())
    await asyncio.sleep(0)
    print(t.done())
    try:
        await t
    except ValueError as er:
        print(repr(er))
    print(t.done())

    # Task that raises after a delay.
    print("=" * 10)
    t = asyncio.create_task(task(0.01, ValueError))
    print(t.done())
    await asyncio.sleep(0)
    print(t.done())
    try:
        await t
    except ValueError as er:
        print(repr(er))
    print(t.done())


asyncio.run(main())