|
5 | 5 |
|
6 | 6 | # Tests for concurrent or nested loops |
7 | 7 |
|
| 8 | + |
8 | 9 | @pytest.mark.trio |
9 | 10 | async def test_parallel(): |
10 | | - loops = [None, None] |
11 | | - async with trio.open_nursery() as n: |
12 | | - async def gen_loop(i, task_status=trio.TASK_STATUS_IGNORED): |
13 | | - task_status.started() |
14 | | - async with trio_asyncio.open_loop() as loop: |
15 | | - loops[i] = loop |
| 11 | + loops = [None, None] |
| 12 | + async with trio.open_nursery() as n: |
| 13 | + |
| 14 | + async def gen_loop(i, task_status=trio.TASK_STATUS_IGNORED): |
| 15 | + task_status.started() |
| 16 | + async with trio_asyncio.open_loop() as loop: |
| 17 | + loops[i] = loop |
| 18 | + |
| 19 | + assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
| 20 | + await n.start(gen_loop, 0) |
| 21 | + await n.start(gen_loop, 1) |
16 | 22 |
|
17 | | - assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
18 | | - await n.start(gen_loop, 0) |
19 | | - await n.start(gen_loop, 1) |
| 23 | + assert isinstance(loops[0], trio_asyncio.TrioEventLoop) |
| 24 | + assert isinstance(loops[1], trio_asyncio.TrioEventLoop) |
| 25 | + assert loops[0] is not loops[1] |
20 | 26 |
|
21 | | - assert isinstance(loops[0], trio_asyncio.TrioEventLoop) |
22 | | - assert isinstance(loops[1], trio_asyncio.TrioEventLoop) |
23 | | - assert loops[0] is not loops[1] |
24 | 27 |
|
25 | 28 | @pytest.mark.trio |
26 | 29 | async def test_nested(): |
27 | | - loops = [None, None] |
28 | | - async with trio.open_nursery() as n: |
29 | | - async def gen_loop(i, task_status=trio.TASK_STATUS_IGNORED): |
30 | | - task_status.started() |
31 | | - async with trio_asyncio.open_loop() as loop: |
32 | | - loops[i] = loop |
33 | | - if i > 0: |
34 | | - await n.start(gen_loop, i-1) |
35 | | - |
36 | | - assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
37 | | - await n.start(gen_loop, 1) |
38 | | - assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
39 | | - assert isinstance(loops[0], trio_asyncio.TrioEventLoop) |
40 | | - assert isinstance(loops[1], trio_asyncio.TrioEventLoop) |
41 | | - assert loops[0] is not loops[1] |
| 30 | + loops = [None, None] |
| 31 | + async with trio.open_nursery() as n: |
| 32 | + |
| 33 | + async def gen_loop(i, task_status=trio.TASK_STATUS_IGNORED): |
| 34 | + task_status.started() |
| 35 | + async with trio_asyncio.open_loop() as loop: |
| 36 | + loops[i] = loop |
| 37 | + if i > 0: |
| 38 | + await n.start(gen_loop, i - 1) |
| 39 | + |
| 40 | + assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
| 41 | + await n.start(gen_loop, 1) |
| 42 | + assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
| 43 | + assert isinstance(loops[0], trio_asyncio.TrioEventLoop) |
| 44 | + assert isinstance(loops[1], trio_asyncio.TrioEventLoop) |
| 45 | + assert loops[0] is not loops[1] |
| 46 | + |
42 | 47 |
|
43 | 48 | async def _test_same_task(): |
44 | | - loops = [None, None] |
45 | | - assert isinstance(asyncio.get_event_loop_policy(), trio_asyncio.TrioPolicy) |
46 | | - def get_loop(i): |
47 | | - loops[i] = (asyncio.get_event_loop(), asyncio.get_event_loop_policy()) |
48 | | - async with trio.open_nursery() as n: |
49 | | - async with trio_asyncio.open_loop() as loop1: |
50 | | - async with trio_asyncio.open_loop() as loop2: |
51 | | - loop1.call_later(0.1, get_loop, 0) |
52 | | - loop2.call_later(0.1, get_loop, 1) |
53 | | - await trio.sleep(0.2) |
54 | | - |
55 | | - assert isinstance(asyncio.get_event_loop_policy(), trio_asyncio.TrioPolicy) |
56 | | - assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
57 | | - assert isinstance(loops[0][0], trio_asyncio.TrioEventLoop) |
58 | | - assert isinstance(loops[1][0], trio_asyncio.TrioEventLoop) |
59 | | - assert isinstance(loops[1][1], trio_asyncio.TrioPolicy) |
60 | | - assert loops[0][0] is not loops[1][0] |
61 | | - assert loops[0][1] is loops[1][1] |
62 | | - assert loops[0][1] is asyncio.get_event_loop_policy() |
| 49 | + loops = [None, None] |
| 50 | + assert isinstance(asyncio.get_event_loop_policy(), trio_asyncio.TrioPolicy) |
| 51 | + |
| 52 | + def get_loop(i): |
| 53 | + loops[i] = (asyncio.get_event_loop(), asyncio.get_event_loop_policy()) |
| 54 | + |
| 55 | + async with trio.open_nursery() as n: |
| 56 | + async with trio_asyncio.open_loop() as loop1: |
| 57 | + async with trio_asyncio.open_loop() as loop2: |
| 58 | + loop1.call_later(0.1, get_loop, 0) |
| 59 | + loop2.call_later(0.1, get_loop, 1) |
| 60 | + await trio.sleep(0.2) |
| 61 | + |
| 62 | + assert isinstance(asyncio.get_event_loop_policy(), trio_asyncio.TrioPolicy) |
| 63 | + assert not isinstance(asyncio._get_running_loop(), trio_asyncio.TrioEventLoop) |
| 64 | + assert isinstance(loops[0][0], trio_asyncio.TrioEventLoop) |
| 65 | + assert isinstance(loops[1][0], trio_asyncio.TrioEventLoop) |
| 66 | + assert isinstance(loops[1][1], trio_asyncio.TrioPolicy) |
| 67 | + assert loops[0][0] is not loops[1][0] |
| 68 | + assert loops[0][1] is loops[1][1] |
| 69 | + assert loops[0][1] is asyncio.get_event_loop_policy() |
| 70 | + |
63 | 71 |
|
64 | 72 | def test_same_task(old_policy): |
65 | | - assert not isinstance(asyncio.get_event_loop_policy(), trio_asyncio.TrioPolicy) |
66 | | - trio.run(_test_same_task) |
| 73 | + assert not isinstance(asyncio.get_event_loop_policy(), trio_asyncio.TrioPolicy) |
| 74 | + trio.run(_test_same_task) |
0 commit comments