forked from python-trio/trio-asyncio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_adapter.py
245 lines (206 loc) · 7.04 KB
/
test_adapter.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import pytest
from trio_asyncio import aio_as_trio, trio_as_aio, allow_asyncio
import asyncio
import trio
import sniffio
from tests import aiotest
import sys
import warnings
try:
from contextlib import asynccontextmanager
except ImportError:
from async_generator import asynccontextmanager
from trio_asyncio import TrioAsyncioDeprecationWarning
def de_deprecate_converter(func):
def wrapper(proc):
with warnings.catch_warnings():
warnings.simplefilter('ignore', TrioAsyncioDeprecationWarning)
return func(proc)
return wrapper
class SomeThing:
flag = 0
def __init__(self, loop):
self.loop = loop
async def dly_trio(self):
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "trio"
await trio.sleep(0.01)
self.flag |= 2
return 8
@trio_as_aio
async def dly_trio_adapted(self):
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "trio"
await trio.sleep(0.01)
self.flag |= 2
return 8
@aio_as_trio
async def dly_asyncio_adapted(self):
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "asyncio"
await asyncio.sleep(0.01, loop=self.loop)
self.flag |= 1
return 4
async def dly_asyncio(self, do_test=True):
if do_test and sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "asyncio"
await asyncio.sleep(0.01, loop=self.loop)
self.flag |= 1
return 4
async def iter_asyncio(self, do_test=True):
if do_test and sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "asyncio"
await asyncio.sleep(0.01, loop=self.loop)
yield 1
await asyncio.sleep(0.01, loop=self.loop)
yield 2
await asyncio.sleep(0.01, loop=self.loop)
self.flag |= 1
async def iter_trio(self):
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "trio"
await trio.sleep(0.01)
yield 1
await trio.sleep(0.01)
yield 2
await trio.sleep(0.01)
self.flag |= 1
@asynccontextmanager
async def ctx_asyncio(self):
await asyncio.sleep(0.01, loop=self.loop)
self.flag |= 1
yield self
await asyncio.sleep(0.01, loop=self.loop)
self.flag |= 2
@asynccontextmanager
async def ctx_trio(self):
await trio.sleep(0.01)
self.flag |= 1
yield self
await trio.sleep(0.01)
self.flag |= 2
class TestAdapt(aiotest.TestCase):
@pytest.mark.trio
async def test_asyncio_trio_adapted(self, loop):
"""Call asyncio from trio"""
sth = SomeThing(loop)
res = await aio_as_trio(sth.dly_trio_adapted, loop=loop)()
assert res == 8
assert sth.flag == 2
@pytest.mark.trio
async def test_trio_asyncio_adapted(self, loop):
sth = SomeThing(loop)
res = await sth.dly_asyncio_adapted()
assert res == 4
assert sth.flag == 1
@pytest.mark.trio
async def test_trio_asyncio(self, loop):
sth = SomeThing(loop)
res = await aio_as_trio(sth.dly_asyncio)()
assert res == 4
assert sth.flag == 1
@pytest.mark.trio
async def test_trio_asyncio_awaitable(self, loop):
sth = SomeThing(loop)
res = await aio_as_trio(sth.dly_asyncio())
assert res == 4
assert sth.flag == 1
@pytest.mark.trio
async def test_trio_asyncio_future(self, loop):
sth = SomeThing(loop)
f = sth.dly_asyncio(do_test=False)
f = asyncio.ensure_future(f)
res = await aio_as_trio(f)
assert res == 4
assert sth.flag == 1
@pytest.mark.trio
async def test_trio_asyncio_iter(self, loop):
sth = SomeThing(loop)
n = 0
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "trio"
async for x in aio_as_trio(sth.iter_asyncio()):
n += 1
assert x == n
assert n == 2
assert sth.flag == 1
async def run_asyncio_trio_iter(self, loop):
sth = SomeThing(loop)
n = 0
if sys.version_info >= (3, 7):
assert sniffio.current_async_library() == "asyncio"
async for x in trio_as_aio(sth.iter_trio()):
n += 1
assert x == n
assert n == 2
assert sth.flag == 1
@pytest.mark.trio
async def test_asyncio_trio_iter(self, loop):
await aio_as_trio(self.run_asyncio_trio_iter)(loop)
@pytest.mark.trio
async def test_trio_asyncio_ctx(self, loop):
sth = SomeThing(loop)
async with aio_as_trio(sth.ctx_asyncio()):
assert sth.flag == 1
assert sth.flag == 3
async def run_asyncio_trio_ctx(self, loop):
sth = SomeThing(loop)
async with trio_as_aio(sth.ctx_trio()):
assert sth.flag == 1
assert sth.flag == 3
@pytest.mark.trio
async def test_asyncio_trio_ctx(self, loop):
await aio_as_trio(self.run_asyncio_trio_ctx)(loop)
class TestAllow(aiotest.TestCase):
async def run_asyncio_trio(self, loop):
"""Call asyncio from trio"""
sth = SomeThing(loop)
res = await trio_as_aio(sth.dly_trio, loop=loop)()
assert res == 8
assert sth.flag == 2
async def run_asyncio_trio_adapted(self, loop):
"""Call asyncio from trio"""
sth = SomeThing(loop)
res = await sth.dly_trio_adapted()
assert res == 8
assert sth.flag == 2
@pytest.mark.trio
async def test_asyncio_trio(self, loop):
await allow_asyncio(self.run_asyncio_trio, loop)
async def run_trio_asyncio(self, loop):
sth = SomeThing(loop)
res = await sth.dly_asyncio(do_test=False)
assert res == 4
assert sth.flag == 1
@pytest.mark.trio
async def test_trio_asyncio(self, loop):
await allow_asyncio(self.run_trio_asyncio, loop)
async def run_trio_asyncio_future(self, loop):
sth = SomeThing(loop)
f = sth.dly_asyncio(do_test=False)
f = asyncio.ensure_future(f)
res = await f
assert res == 4
assert sth.flag == 1
@pytest.mark.trio
async def test_trio_asyncio_future(self, loop):
await allow_asyncio(self.run_trio_asyncio_future, loop)
async def run_trio_asyncio_iter(self, loop):
sth = SomeThing(loop)
n = 0
async for x in sth.iter_asyncio(do_test=False):
n += 1
assert x == n
assert n == 2
assert sth.flag == 1
@pytest.mark.trio
async def test_trio_asyncio_iter(self, loop):
await allow_asyncio(self.run_trio_asyncio_iter, loop)
async def run_trio_asyncio_ctx(self, loop):
sth = SomeThing(loop)
async with sth.ctx_asyncio():
assert sth.flag == 1
assert sth.flag == 3
@pytest.mark.trio
async def test_trio_asyncio_ctx(self, loop):
await allow_asyncio(self.run_trio_asyncio_ctx, loop)