Skip to content

Commit 310f13b

Browse files
committed
YAPF'd
1 parent 213a07c commit 310f13b

4 files changed

Lines changed: 70 additions & 51 deletions

File tree

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ def pytest_pyfunc_call(pyfuncitem):
5252
_new_policy = trio_asyncio.TrioPolicy()
5353
asyncio.set_event_loop_policy(_new_policy)
5454

55+
5556
@pytest.fixture
5657
def old_policy():
5758
asyncio.set_event_loop_policy(_old_policy)
5859
try:
5960
yield _old_policy
6061
finally:
6162
asyncio.set_event_loop_policy(_new_policy)
62-

tests/test_concurrent.py

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,70 @@
55

66
# Tests for concurrent or nested loops
77

8+
89
@pytest.mark.trio
910
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)
1622

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]
2026

21-
assert isinstance(loops[0], trio_asyncio.TrioEventLoop)
22-
assert isinstance(loops[1], trio_asyncio.TrioEventLoop)
23-
assert loops[0] is not loops[1]
2427

2528
@pytest.mark.trio
2629
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+
4247

4348
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+
6371

6472
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)

trio_asyncio/async_.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,3 @@ def _main_loop_exit(self):
124124
loop.close()
125125
nursery.cancel_scope.cancel()
126126
_current_loop.loop = outer_loop
127-

trio_asyncio/loop.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
_current_loop = trio.TaskLocal(loop=None, policy=None)
3434

35+
3536
class _TrioPolicy(asyncio.events.BaseDefaultEventLoopPolicy):
3637
_loop_factory = TrioEventLoop
3738

@@ -96,13 +97,14 @@ def set_event_loop(self, loop):
9697
return super().set_event_loop(loop)
9798

9899

99-
100100
# We need to monkey-patch asyncio's policy+loop getters to return our
101101
# TrioPolicy+loop whenever we are within Trio.
102102

103103
from asyncio import events as _aio_event
104104

105105
_orig_policy_get = _aio_event.get_event_loop_policy
106+
107+
106108
def _new_policy_get():
107109
try:
108110
policy = _current_loop.policy
@@ -113,23 +115,33 @@ def _new_policy_get():
113115
policy = TrioPolicy()
114116
_current_loop.policy = policy
115117
return policy
118+
119+
116120
_aio_event.get_event_loop_policy = _new_policy_get
117121
asyncio.get_event_loop_policy = _new_policy_get
118122

119123
_orig_run_get = _aio_event._get_running_loop
124+
125+
120126
def _new_loop_get():
121127
try:
122128
return _current_loop.loop
123129
except RuntimeError:
124130
return _orig_run_get()
131+
132+
125133
_aio_event._get_running_loop = _new_run_get
126134

127135
_orig_loop_get = _aio_event.get_event_loop
136+
137+
128138
def _new_loop_get():
129139
try:
130140
return _current_loop.loop
131141
except RuntimeError:
132142
return _orig_loop_get()
143+
144+
133145
_aio_event.get_event_loop = _new_loop_get
134146
asyncio.get_event_loop = _new_loop_get
135147

0 commit comments

Comments
 (0)