Skip to content

Commit 3628819

Browse files
authored
Merge pull request #36 from touilleMan/pr-35-async_generator
Pr 35 async generator
2 parents a8d22d3 + 66c528f commit 3628819

File tree

4 files changed

+96
-49
lines changed

4 files changed

+96
-49
lines changed

.travis.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ matrix:
1313
- os: linux
1414
language: generic
1515
env: USE_PYPY_RELEASE_VERSION=5.9-beta
16-
- os: osx
17-
language: generic
18-
env: MACPYTHON=3.5.4
16+
# Python3.5 on MacOS doesn't currently support TLSv1.2 needed to use pip :(
17+
# http://pyfound.blogspot.fr/2017/01/time-to-upgrade-your-python-tls-v12.html
18+
# - os: osx
19+
# language: generic
20+
# env: MACPYTHON=3.5.4
1921
- os: osx
2022
language: generic
2123
env: MACPYTHON=3.6.3

pytest_trio/_tests/test_async_yield_fixture.py

+80-31
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
import sys
22
import pytest
3+
import re
34

45

5-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
6-
def test_single_async_yield_fixture(testdir):
6+
@pytest.fixture(params=['Python>=36', 'async_generator'])
7+
def async_yield_implementation(request):
8+
if request.param == 'Python>=36':
9+
if sys.version_info < (3, 6):
10+
pytest.skip("requires python3.6")
11+
else:
712

13+
def patch_code(code):
14+
# Convert code to use Python>=3.6 builtin async generator
15+
code = re.sub(r'(?m)^\s*@async_generator\n', r'', code)
16+
code = re.sub(r'await yield_', r'yield', code)
17+
return code
18+
19+
return patch_code
20+
else:
21+
return lambda x: x
22+
23+
24+
def test_single_async_yield_fixture(testdir, async_yield_implementation):
825
testdir.makepyfile(
9-
"""
26+
async_yield_implementation(
27+
"""
1028
import pytest
1129
import trio
30+
from async_generator import async_generator, yield_
1231
1332
events = []
1433
1534
@pytest.fixture
35+
@async_generator
1636
async def fix1():
1737
events.append('fix1 setup')
1838
await trio.sleep(0)
1939
20-
yield 'fix1'
40+
await yield_('fix1')
2141
2242
await trio.sleep(0)
2343
events.append('fix1 teardown')
@@ -36,39 +56,43 @@ def test_after():
3656
'fix1 teardown',
3757
]
3858
"""
59+
)
3960
)
4061

4162
result = testdir.runpytest()
4263

4364
result.assert_outcomes(passed=3)
4465

4566

46-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
47-
def test_nested_async_yield_fixture(testdir):
67+
def test_nested_async_yield_fixture(testdir, async_yield_implementation):
4868

4969
testdir.makepyfile(
50-
"""
70+
async_yield_implementation(
71+
"""
5172
import pytest
5273
import trio
74+
from async_generator import async_generator, yield_
5375
5476
events = []
5577
5678
@pytest.fixture
79+
@async_generator
5780
async def fix2():
5881
events.append('fix2 setup')
5982
await trio.sleep(0)
6083
61-
yield 'fix2'
84+
await yield_('fix2')
6285
6386
await trio.sleep(0)
6487
events.append('fix2 teardown')
6588
6689
@pytest.fixture
90+
@async_generator
6791
async def fix1(fix2):
6892
events.append('fix1 setup')
6993
await trio.sleep(0)
7094
71-
yield 'fix1'
95+
await yield_('fix1')
7296
7397
await trio.sleep(0)
7498
events.append('fix1 teardown')
@@ -92,29 +116,34 @@ def test_after():
92116
'fix2 teardown',
93117
]
94118
"""
119+
)
95120
)
96121

97122
result = testdir.runpytest()
98123

99124
result.assert_outcomes(passed=3)
100125

101126

102-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
103-
def test_async_yield_fixture_within_sync_fixture(testdir):
127+
def test_async_yield_fixture_within_sync_fixture(
128+
testdir, async_yield_implementation
129+
):
104130

105131
testdir.makepyfile(
106-
"""
132+
async_yield_implementation(
133+
"""
107134
import pytest
108135
import trio
136+
from async_generator import async_generator, yield_
109137
110138
events = []
111139
112140
@pytest.fixture
141+
@async_generator
113142
async def fix2():
114143
events.append('fix2 setup')
115144
await trio.sleep(0)
116145
117-
yield 'fix2'
146+
await yield_('fix2')
118147
119148
await trio.sleep(0)
120149
events.append('fix2 teardown')
@@ -139,29 +168,34 @@ def test_after():
139168
'fix2 teardown',
140169
]
141170
"""
171+
)
142172
)
143173

144174
result = testdir.runpytest()
145175

146176
result.assert_outcomes(passed=3)
147177

148178

149-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
150-
def test_async_yield_fixture_within_sync_yield_fixture(testdir):
179+
def test_async_yield_fixture_within_sync_yield_fixture(
180+
testdir, async_yield_implementation
181+
):
151182

152183
testdir.makepyfile(
153-
"""
184+
async_yield_implementation(
185+
"""
154186
import pytest
155187
import trio
188+
from async_generator import async_generator, yield_
156189
157190
events = []
158191
159192
@pytest.fixture
193+
@async_generator
160194
async def fix2():
161195
events.append('fix2 setup')
162196
await trio.sleep(0)
163197
164-
yield 'fix2'
198+
await yield_('fix2')
165199
166200
await trio.sleep(0)
167201
events.append('fix2 teardown')
@@ -191,32 +225,38 @@ def test_after():
191225
'fix2 teardown',
192226
]
193227
"""
228+
)
194229
)
195230

196231
result = testdir.runpytest()
197232

198233
result.assert_outcomes(passed=3)
199234

200235

201-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
202-
def test_async_yield_fixture_with_multiple_yields(testdir):
236+
def test_async_yield_fixture_with_multiple_yields(
237+
testdir, async_yield_implementation
238+
):
203239

204240
testdir.makepyfile(
205-
"""
241+
async_yield_implementation(
242+
"""
206243
import pytest
207244
import trio
245+
from async_generator import async_generator, yield_
208246
209247
@pytest.fixture
248+
@async_generator
210249
async def fix1():
211250
await trio.sleep(0)
212-
yield 'good'
251+
await yield_('good')
213252
await trio.sleep(0)
214-
yield 'bad'
253+
await yield_('bad')
215254
216255
@pytest.mark.trio
217256
async def test_actual_test(fix1):
218257
pass
219258
"""
259+
)
220260
)
221261

222262
result = testdir.runpytest()
@@ -226,13 +266,14 @@ async def test_actual_test(fix1):
226266
result.assert_outcomes(failed=1)
227267

228268

229-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
230-
def test_async_yield_fixture_with_nursery(testdir):
269+
def test_async_yield_fixture_with_nursery(testdir, async_yield_implementation):
231270

232271
testdir.makepyfile(
233-
"""
272+
async_yield_implementation(
273+
"""
234274
import pytest
235275
import trio
276+
from async_generator import async_generator, yield_
236277
237278
238279
async def handle_client(stream):
@@ -242,10 +283,11 @@ async def handle_client(stream):
242283
243284
244285
@pytest.fixture
286+
@async_generator
245287
async def server():
246288
async with trio.open_nursery() as nursery:
247289
listeners = await nursery.start(trio.serve_tcp, handle_client, 0)
248-
yield listeners[0]
290+
await yield_(listeners[0])
249291
nursery.cancel_scope.cancel()
250292
251293
@@ -256,35 +298,41 @@ async def test_actual_test(server):
256298
rep = await stream.receive_some(4)
257299
assert rep == b'ping'
258300
"""
301+
)
259302
)
260303

261304
result = testdir.runpytest()
262305

263306
result.assert_outcomes(passed=1)
264307

265308

266-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
267-
def test_async_yield_fixture_crashed_teardown_allow_other_teardowns(testdir):
309+
def test_async_yield_fixture_crashed_teardown_allow_other_teardowns(
310+
testdir, async_yield_implementation
311+
):
268312

269313
testdir.makepyfile(
270-
"""
314+
async_yield_implementation(
315+
"""
271316
import pytest
272317
import trio
318+
from async_generator import async_generator, yield_
273319
274320
events = []
275321
276322
@pytest.fixture
323+
@async_generator
277324
async def good_fixture():
278325
async with trio.open_nursery() as nursery:
279326
events.append('good_fixture setup')
280-
yield
327+
await yield_(None)
281328
events.append('good_fixture teardown')
282329
283330
@pytest.fixture
331+
@async_generator
284332
async def bad_fixture():
285333
async with trio.open_nursery() as nursery:
286334
events.append('bad_fixture setup')
287-
yield
335+
await yield_(None)
288336
events.append('bad_fixture teardown')
289337
raise RuntimeError('Crash during fixture teardown')
290338
@@ -303,6 +351,7 @@ def test_after():
303351
'good_fixture teardown',
304352
]
305353
"""
354+
)
306355
)
307356

308357
result = testdir.runpytest()

0 commit comments

Comments
 (0)