Skip to content

Commit f4006a9

Browse files
committed
deprecate run_asyncio.
1 parent 023713d commit f4006a9

6 files changed

Lines changed: 359 additions & 32 deletions

File tree

tests/aiotest/test_coroutine.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from tests import aiotest
2+
import trio_asyncio
23
import pytest
34
from .. import utils as test_utils
45

@@ -46,5 +47,22 @@ async def waiter(asyncio, hello_world, result):
4647
result.append(value)
4748

4849
result = []
49-
await loop.run_asyncio(waiter, config.asyncio, hello_world, result)
50+
await trio_asyncio.aio_as_trio(waiter)(config.asyncio, hello_world, result)
51+
assert result == ['Future', 'Hello', 'World', '.']
52+
53+
@pytest.mark.trio
54+
async def test_waiter_depr(self, loop, config):
55+
async def waiter(asyncio, hello_world, result):
56+
fut = asyncio.Future(loop=loop)
57+
loop.call_soon(fut.set_result, "Future")
58+
59+
value = await fut
60+
result.append(value)
61+
62+
value = await hello_world(asyncio, result, 0.001, loop)
63+
result.append(value)
64+
65+
result = []
66+
with test_utils.deprecate(self):
67+
await loop.run_asyncio(waiter, config.asyncio, hello_world, result)
5068
assert result == ['Future', 'Hello', 'World', '.']

tests/interop/test_adapter.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from tests import aiotest
88
import sys
99
from async_generator import asynccontextmanager
10-
10+
from .. import utils as test_utils
1111

1212
class SomeThing:
1313
flag = 0
@@ -70,7 +70,17 @@ async def test_asyncio_trio(self, loop):
7070
"""Call asyncio from trio"""
7171

7272
sth = SomeThing(loop)
73-
res = await loop.run_asyncio(sth.dly_trio)
73+
res = await aio_as_trio(sth.dly_trio, loop=loop)()
74+
assert res == 8
75+
assert sth.flag == 2
76+
77+
@pytest.mark.trio
78+
async def test_asyncio_trio_depr(self, loop):
79+
"""Call asyncio from trio"""
80+
81+
sth = SomeThing(loop)
82+
with test_utils.deprecate(self):
83+
res = await loop.run_asyncio(sth.dly_trio)
7484
assert res == 8
7585
assert sth.flag == 2
7686

tests/interop/test_calls.py

Lines changed: 188 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ async def __aexit__(self, *tb):
6060
class TestCalls(aiotest.TestCase):
6161
async def call_t_a(self, proc, *args, loop=None):
6262
"""called from Trio"""
63-
return await loop.run_asyncio(proc, *args)
63+
return await aio_as_trio(proc, loop=loop)(*args)
64+
65+
async def call_t_a_depr(self, proc, *args, loop=None):
66+
"""called from Trio"""
67+
with test_utils.deprecate(self):
68+
return await loop.run_asyncio(proc, *args)
6469

6570
async def call_a_t(self, proc, *args, loop=None):
6671
"""call from asyncio to an async trio function"""
@@ -78,7 +83,18 @@ async def delay(t):
7883
await done.wait()
7984

8085
t = loop.time() + 0.1
81-
await loop.run_asyncio(delay, t)
86+
await aio_as_trio(delay, loop=loop)(t)
87+
88+
@pytest.mark.trio
89+
async def test_call_at_depr(self, loop):
90+
async def delay(t):
91+
done = asyncio.Event(loop=loop)
92+
loop.call_at(t, done.set)
93+
await done.wait()
94+
95+
t = loop.time() + 0.1
96+
with test_utils.deprecate(self):
97+
await loop.run_asyncio(delay, t)
8298

8399
@pytest.mark.trio
84100
async def test_asyncio_trio(self, loop):
@@ -90,14 +106,14 @@ async def dly_trio(seen):
90106
return 8
91107

92108
seen = Seen()
93-
res = await loop.run_asyncio(partial(self.call_a_t, loop=loop), dly_trio, seen)
109+
res = await aio_as_trio(partial(self.call_a_t, loop=loop), loop=loop)(dly_trio, seen)
94110
assert res == 8
95111
assert seen.flag == 2
96112

97113
@pytest.mark.trio
98114
async def test_call_asyncio_ctx(self, loop):
99115
self.did_it = 0
100-
async with loop.run_asyncio(AioContext(self, loop)) as ctx:
116+
async with aio_as_trio(AioContext(self, loop), loop=loop) as ctx:
101117
assert self.did_it == 2
102118
self.did_it = 3
103119
assert self.did_it == 4
@@ -111,7 +127,19 @@ async def _call_trio_ctx():
111127
self.did_it = 3
112128
assert self.did_it == 4
113129

114-
await loop.run_asyncio(_call_trio_ctx)
130+
await aio_as_trio(_call_trio_ctx, loop=loop)()
131+
132+
@pytest.mark.trio
133+
async def test_call_trio_ctx_depr(self, loop):
134+
async def _call_trio_ctx():
135+
self.did_it = 0
136+
async with loop.wrap_trio_context(TrioContext(self)) as ctx:
137+
assert self.did_it == 2
138+
self.did_it = 3
139+
assert self.did_it == 4
140+
141+
with test_utils.deprecate(self):
142+
await loop.run_asyncio(_call_trio_ctx)
115143

116144
@pytest.mark.trio
117145
async def test_asyncio_trio_sync(self, loop):
@@ -122,7 +150,21 @@ def dly_trio(seen):
122150
return 8
123151

124152
seen = Seen()
125-
res = await loop.run_asyncio(partial(self.call_a_ts, loop=loop), dly_trio, seen)
153+
res = await aio_as_trio(partial(self.call_a_ts, loop=loop), loop=loop)(dly_trio, seen)
154+
assert res == 8
155+
assert seen.flag == 2
156+
157+
@pytest.mark.trio
158+
async def test_asyncio_trio_sync_depr(self, loop):
159+
"""Call asyncio from trio"""
160+
161+
def dly_trio(seen):
162+
seen.flag |= 2
163+
return 8
164+
165+
seen = Seen()
166+
with test_utils.deprecate(self):
167+
res = await loop.run_asyncio(partial(self.call_a_ts, loop=loop), dly_trio, seen)
126168
assert res == 8
127169
assert seen.flag == 2
128170

@@ -138,14 +180,37 @@ async def dly_asyncio(seen):
138180
assert res == 4
139181
assert seen.flag == 1
140182

183+
@pytest.mark.trio
184+
async def test_trio_asyncio_depr(self, loop):
185+
async def dly_asyncio(seen):
186+
await asyncio.sleep(0.01, loop=loop)
187+
seen.flag |= 1
188+
return 4
189+
190+
seen = Seen()
191+
res = await self.call_t_a_depr(dly_asyncio, seen, loop=loop)
192+
assert res == 4
193+
assert seen.flag == 1
194+
141195
@pytest.mark.trio
142196
async def test_asyncio_trio_error(self, loop):
143197
async def err_trio():
144198
await trio.sleep(0.01)
145199
raise RuntimeError("I has another owie")
146200

147201
with pytest.raises(RuntimeError) as err:
148-
await loop.run_asyncio(partial(self.call_a_t, loop=loop), err_trio)
202+
await aio_as_trio(partial(self.call_a_t, loop=loop), loop=loop)(err_trio)
203+
assert err.value.args[0] == "I has another owie"
204+
205+
@pytest.mark.trio
206+
async def test_asyncio_trio_error_depr(self, loop):
207+
async def err_trio():
208+
await trio.sleep(0.01)
209+
raise RuntimeError("I has another owie")
210+
211+
with pytest.raises(RuntimeError) as err:
212+
with test_utils.deprecate(self):
213+
await loop.run_asyncio(partial(self.call_a_t, loop=loop), err_trio)
149214
assert err.value.args[0] == "I has another owie"
150215

151216
@pytest.mark.trio
@@ -155,7 +220,18 @@ def err_trio_sync():
155220
raise RuntimeError("I has more owie")
156221

157222
with pytest.raises(RuntimeError) as err:
158-
await loop.run_asyncio(partial(self.call_a_ts, loop=loop), err_trio_sync)
223+
await aio_as_trio(partial(self.call_a_ts, loop=loop), loop=loop)(err_trio_sync)
224+
assert err.value.args[0] == "I has more owie"
225+
226+
@pytest.mark.trio
227+
async def test_asyncio_trio_sync_error_depr(self, loop):
228+
def err_trio_sync():
229+
loop.time() # verify that the loop is running
230+
raise RuntimeError("I has more owie")
231+
232+
with pytest.raises(RuntimeError) as err:
233+
with test_utils.deprecate(self):
234+
await loop.run_asyncio(partial(self.call_a_ts, loop=loop), err_trio_sync)
159235
assert err.value.args[0] == "I has more owie"
160236

161237
@pytest.mark.trio
@@ -168,6 +244,16 @@ async def err_asyncio():
168244
await self.call_t_a(err_asyncio, loop=loop)
169245
assert err.value.args[0] == "I has an owie"
170246

247+
@pytest.mark.trio
248+
async def test_trio_asyncio_error_depr(self, loop):
249+
async def err_asyncio():
250+
await asyncio.sleep(0.01, loop=loop)
251+
raise RuntimeError("I has an owie")
252+
253+
with pytest.raises(RuntimeError) as err:
254+
await self.call_t_a_depr(err_asyncio, loop=loop)
255+
assert err.value.args[0] == "I has an owie"
256+
171257
@pytest.mark.trio
172258
async def test_asyncio_trio_cancel_out(self, loop):
173259
async def cancelled_trio(seen):
@@ -181,7 +267,7 @@ async def cancelled_trio(seen):
181267

182268
seen = Seen()
183269
with pytest.raises(asyncio.CancelledError):
184-
await loop.run_asyncio(partial(self.call_a_t, loop=loop), cancelled_trio, seen)
270+
await aio_as_trio(partial(self.call_a_t, loop=loop), loop=loop)(cancelled_trio, seen)
185271
assert seen.flag == 3
186272

187273
@pytest.mark.trio
@@ -214,6 +300,36 @@ async def check_cancel(proc, seen):
214300
await check_cancel(cancelled_asyncio, seen)
215301
assert seen.flag == 1 | 4
216302

303+
@pytest.mark.trio
304+
async def test_trio_asyncio_cancel_out_depr(self, loop):
305+
async def cancelled_asyncio(seen):
306+
seen.flag |= 1
307+
await asyncio.sleep(0.01, loop=loop)
308+
f = asyncio.Future(loop=loop)
309+
f.cancel()
310+
return f.result() # raises error
311+
312+
def cancelled_future(seen):
313+
seen.flag |= 1
314+
f = asyncio.Future(loop=loop)
315+
f.cancel()
316+
return f # contains error
317+
318+
async def check_cancel(proc, seen):
319+
with trio.open_cancel_scope() as scope:
320+
with pytest.raises(asyncio.CancelledError):
321+
await self.call_t_a_depr(proc, seen, loop=loop)
322+
assert not scope.cancel_called
323+
seen.flag |= 4
324+
325+
seen = Seen()
326+
await check_cancel(cancelled_future, seen)
327+
assert seen.flag == 1 | 4
328+
329+
seen = Seen()
330+
await check_cancel(cancelled_asyncio, seen)
331+
assert seen.flag == 1 | 4
332+
217333
@pytest.mark.trio
218334
async def test_asyncio_trio_cancel_in(self, loop):
219335
async def in_trio(started, seen):
@@ -237,7 +353,7 @@ async def cancel_asyncio(seen):
237353
seen.flag |= 8
238354

239355
seen = Seen()
240-
await loop.run_asyncio(cancel_asyncio, seen)
356+
await aio_as_trio(cancel_asyncio, loop=loop)(seen)
241357
assert seen.flag == 1 | 2 | 8
242358

243359
@pytest.mark.trio
@@ -267,6 +383,33 @@ async def cancel_trio(seen):
267383
await cancel_trio(seen)
268384
assert seen.flag == 1 | 2 | 8
269385

386+
@pytest.mark.trio
387+
async def test_trio_asyncio_cancel_in_depr(self, loop):
388+
async def in_asyncio(started, seen):
389+
started.set()
390+
try:
391+
await asyncio.sleep(9999, loop=loop)
392+
except asyncio.CancelledError:
393+
seen.flag |= 1
394+
except trio.Cancelled:
395+
seen.flag |= 16
396+
else:
397+
seen.flag |= 4
398+
finally:
399+
seen.flag |= 2
400+
401+
async def cancel_trio(seen):
402+
started = trio.Event()
403+
async with trio.open_nursery() as nursery:
404+
nursery.start_soon(partial(self.call_t_a_depr, loop=loop), in_asyncio, started, seen)
405+
await started.wait()
406+
nursery.cancel_scope.cancel()
407+
seen.flag |= 8
408+
409+
seen = Seen()
410+
await cancel_trio(seen)
411+
assert seen.flag == 1 | 2 | 8
412+
270413
@pytest.mark.trio
271414
async def test_trio_asyncio_cancel_direct(self, loop):
272415
def in_asyncio(started, seen):
@@ -291,6 +434,30 @@ async def cancel_trio(seen):
291434
await cancel_trio(seen)
292435
assert seen.flag == 1 | 8
293436

437+
@pytest.mark.trio
438+
async def test_trio_asyncio_cancel_direct_depr(self, loop):
439+
def in_asyncio(started, seen):
440+
# This is intentionally not async
441+
seen.flag |= 1
442+
raise asyncio.CancelledError()
443+
444+
async def cancel_trio(seen):
445+
started = trio.Event()
446+
try:
447+
async with trio.open_nursery() as nursery:
448+
nursery.start_soon(
449+
partial(self.call_t_a_depr, loop=loop), in_asyncio, started, seen
450+
)
451+
await started.wait()
452+
nursery.cancel_scope.cancel()
453+
finally:
454+
seen.flag |= 8
455+
456+
seen = Seen()
457+
with pytest.raises(asyncio.CancelledError):
458+
await cancel_trio(seen)
459+
assert seen.flag == 1 | 8
460+
294461
@pytest.mark.trio
295462
async def test_trio_asyncio_error_direct(self, loop):
296463
def err_asyncio():
@@ -301,6 +468,16 @@ def err_asyncio():
301468
await self.call_t_a(err_asyncio, loop=loop)
302469
assert err.value.args[0] == "I has an owie"
303470

471+
@pytest.mark.trio
472+
async def test_trio_asyncio_error_direct_depr(self, loop):
473+
def err_asyncio():
474+
# This is intentionally not async
475+
raise RuntimeError("I has an owie")
476+
477+
with pytest.raises(RuntimeError) as err:
478+
await self.call_t_a_depr(err_asyncio, loop=loop)
479+
assert err.value.args[0] == "I has an owie"
480+
304481
@pytest.mark.trio
305482
async def test_trio_asyncio_generator(self, loop):
306483
async def dly_asyncio():
@@ -366,6 +543,6 @@ async def slow_nums():
366543

367544
sum = 0
368545
# with test_utils.deprecate(self): ## not yet
369-
async for n in loop.run_asyncio(slow_nums()):
546+
async for n in aio_as_trio(slow_nums(), loop=loop):
370547
sum += n
371548
assert sum == 15

0 commit comments

Comments
 (0)