forked from pytest-dev/pytest-asyncio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_simple.py
160 lines (119 loc) · 4.8 KB
/
test_simple.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
"""Quick'n'dirty unit tests for provided fixtures and markers."""
import asyncio
import os
import pytest
import pytest_asyncio.plugin
@asyncio.coroutine
def async_coro(loop=None):
"""A very simple coroutine."""
yield from asyncio.sleep(0, loop=loop)
return 'ok'
def test_event_loop_fixture(event_loop):
"""Test the injection of the event_loop fixture."""
assert event_loop
ret = event_loop.run_until_complete(async_coro(event_loop))
assert ret == 'ok'
def test_event_loop_processpool_fixture(event_loop_process_pool):
"""Test the injection of the event_loop with a process pool fixture."""
assert event_loop_process_pool
ret = event_loop_process_pool.run_until_complete(
async_coro(event_loop_process_pool))
assert ret == 'ok'
this_pid = os.getpid()
future = event_loop_process_pool.run_in_executor(None, os.getpid)
pool_pid = event_loop_process_pool.run_until_complete(future)
assert this_pid != pool_pid
@pytest.mark.asyncio
def test_asyncio_marker():
"""Test the asyncio pytest marker."""
yield # sleep(0)
@pytest.mark.xfail(reason='need a failure', strict=True)
@pytest.mark.asyncio
def test_asyncio_marker_fail():
assert False
@pytest.mark.asyncio
def test_asyncio_marker_with_default_param(a_param=None):
"""Test the asyncio pytest marker."""
yield # sleep(0)
@pytest.mark.asyncio_process_pool
def test_asyncio_process_pool_marker(event_loop):
"""Test the asyncio pytest marker."""
ret = yield from async_coro(event_loop)
assert ret == 'ok'
@pytest.mark.asyncio
def test_unused_port_fixture(unused_tcp_port, event_loop):
"""Test the unused TCP port fixture."""
@asyncio.coroutine
def closer(_, writer):
writer.close()
server1 = yield from asyncio.start_server(closer, host='localhost',
port=unused_tcp_port,
loop=event_loop)
with pytest.raises(IOError):
yield from asyncio.start_server(closer, host='localhost',
port=unused_tcp_port,
loop=event_loop)
server1.close()
yield from server1.wait_closed()
@pytest.mark.asyncio
def test_unused_port_factory_fixture(unused_tcp_port_factory, event_loop):
"""Test the unused TCP port factory fixture."""
@asyncio.coroutine
def closer(_, writer):
writer.close()
port1, port2, port3 = (unused_tcp_port_factory(), unused_tcp_port_factory(),
unused_tcp_port_factory())
server1 = yield from asyncio.start_server(closer, host='localhost',
port=port1,
loop=event_loop)
server2 = yield from asyncio.start_server(closer, host='localhost',
port=port2,
loop=event_loop)
server3 = yield from asyncio.start_server(closer, host='localhost',
port=port3,
loop=event_loop)
for port in port1, port2, port3:
with pytest.raises(IOError):
yield from asyncio.start_server(closer, host='localhost',
port=port,
loop=event_loop)
server1.close()
yield from server1.wait_closed()
server2.close()
yield from server2.wait_closed()
server3.close()
yield from server3.wait_closed()
def test_unused_port_factory_duplicate(unused_tcp_port_factory, monkeypatch):
"""Test correct avoidance of duplicate ports."""
counter = 0
def mock_unused_tcp_port():
"""Force some duplicate ports."""
nonlocal counter
counter += 1
if counter < 5:
return 10000
else:
return 10000 + counter
monkeypatch.setattr(pytest_asyncio.plugin, 'unused_tcp_port',
mock_unused_tcp_port)
assert unused_tcp_port_factory() == 10000
assert unused_tcp_port_factory() > 10000
class Test:
"""Test that asyncio marked functions work in test methods."""
@pytest.mark.asyncio
def test_asyncio_marker_method(self, event_loop):
"""Test the asyncio pytest marker in a Test class."""
ret = yield from async_coro(event_loop)
assert ret == 'ok'
class TestUnexistingLoop:
@pytest.fixture
def remove_loop(self):
old_loop = asyncio.get_event_loop()
asyncio.set_event_loop(None)
yield
asyncio.set_event_loop(old_loop)
@pytest.mark.asyncio
def test_asyncio_marker_without_loop(self, remove_loop):
"""Test the asyncio pytest marker in a Test class."""
ret = yield from async_coro()
assert ret == 'ok'