|
1 | | -import asyncio |
2 | | -import gc |
3 | 1 | from io import StringIO |
4 | 2 |
|
5 | 3 | import pytest |
6 | 4 |
|
7 | 5 |
|
8 | | -# COPIED FROM aiohttp # |
9 | | - |
10 | | - |
11 | | -@pytest.yield_fixture |
12 | | -def loop(request): |
13 | | - #loop = asyncio.get_event_loop() |
14 | | - loop = asyncio.new_event_loop() |
15 | | - asyncio.set_event_loop(None) |
16 | | - #asyncio.set_event_loop(loop) |
17 | | - |
18 | | - yield loop |
19 | | - |
20 | | - is_closed = getattr(loop, 'is_closed') |
21 | | - if is_closed is not None: |
22 | | - closed = is_closed() |
23 | | - else: |
24 | | - closed = loop._closed |
25 | | - if not closed: |
26 | | - loop.call_soon(loop.stop) |
27 | | - loop.run_forever() |
28 | | - loop.close() |
29 | | - gc.collect() |
30 | | - asyncio.set_event_loop(None) |
31 | | - |
32 | | - |
33 | | -@pytest.mark.tryfirst |
34 | | -def pytest_pycollect_makeitem(collector, name, obj): |
35 | | - if collector.funcnamefilter(name): |
36 | | - if not callable(obj): |
37 | | - return |
38 | | - item = pytest.Function(name, parent=collector) |
39 | | - if 'run_loop' in item.keywords: |
40 | | - return list(collector._genfunctions(name, obj)) |
41 | | - |
42 | | - |
43 | | -@pytest.mark.tryfirst |
44 | | -def pytest_pyfunc_call(pyfuncitem): |
45 | | - """ |
46 | | - Run asyncio marked test functions in an event loop instead of a normal |
47 | | - function call. |
48 | | - """ |
49 | | - if 'run_loop' in pyfuncitem.keywords: |
50 | | - funcargs = pyfuncitem.funcargs |
51 | | - loop = funcargs['loop'] |
52 | | - testargs = {arg: funcargs[arg] |
53 | | - for arg in pyfuncitem._fixtureinfo.argnames} |
54 | | - loop.run_until_complete(pyfuncitem.obj(**testargs)) |
55 | | - return True |
56 | | - |
57 | | - |
58 | | -def pytest_runtest_setup(item): |
59 | | - if 'run_loop' in item.keywords and 'loop' not in item.fixturenames: |
60 | | - # inject an event loop fixture for all async tests |
61 | | - item.fixturenames.append('loop') |
62 | | - |
63 | | -# end copy from aiohttp # |
64 | | - |
65 | | - |
66 | 6 | @pytest.yield_fixture |
67 | | -def websocket_cli(loop): |
| 7 | +def websocket_cli(event_loop): |
68 | 8 |
|
69 | 9 | from pushpull.websocket.client import challenge |
70 | 10 |
|
71 | 11 | inp, out = StringIO(), StringIO() |
72 | | - yield challenge('http://localhost:8080', 'test', inp, out, loop=loop), inp, out |
| 12 | + yield challenge('http://localhost:8080', 'test', inp, out, loop=event_loop), inp, out |
73 | 13 |
|
74 | 14 |
|
75 | 15 | @pytest.yield_fixture |
76 | | -def websocket_server(loop): |
| 16 | +def websocket_server(event_loop): |
77 | 17 |
|
78 | 18 | # TODO |
79 | 19 | yield None |
0 commit comments