forked from aio-libs/aiohttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_worker.py
More file actions
180 lines (134 loc) · 4.84 KB
/
Copy pathtest_worker.py
File metadata and controls
180 lines (134 loc) · 4.84 KB
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
"""Tests for aiohttp/worker.py"""
import asyncio
import pytest
import sys
from unittest import mock
from aiohttp import helpers
base_worker = pytest.importorskip('aiohttp.worker')
pytest.importorskip('uvloop')
class BaseTestWorker:
def __init__(self):
self.servers = []
self.exit_code = 0
self.cfg = mock.Mock()
self.cfg.graceful_timeout = 100
class AsyncioWorker(BaseTestWorker, base_worker.GunicornWebWorker):
pass
class UvloopWorker(BaseTestWorker, base_worker.GunicornUVLoopWebWorker):
def __init__(self):
if sys.version_info < (3, 5) \
or sys.platform in ('win32', 'cygwin', 'cli'):
raise pytest.skip("uvloop requires Python 3.5 and *nix.")
super().__init__()
@pytest.fixture(params=[AsyncioWorker, UvloopWorker])
def worker(request):
return request.param()
def test_init_process(worker):
with mock.patch('aiohttp.worker.asyncio') as m_asyncio:
try:
worker.init_process()
except TypeError:
pass
assert m_asyncio.get_event_loop.return_value.close.called
assert m_asyncio.new_event_loop.called
assert m_asyncio.set_event_loop.called
def test_run(worker, loop):
worker.loop = loop
worker._run = mock.Mock(
wraps=asyncio.coroutine(lambda: None))
with pytest.raises(SystemExit):
worker.run()
assert worker._run.called
is_closed = getattr(loop, 'is_closed')
if is_closed is not None:
closed = is_closed()
else:
closed = loop._closed
assert closed
def test_handle_quit(worker):
worker.handle_quit(object(), object())
assert not worker.alive
assert worker.exit_code == 0
def test_handle_abort(worker):
worker.handle_abort(object(), object())
assert not worker.alive
assert worker.exit_code == 1
def test_init_signals(worker):
worker.loop = mock.Mock()
worker.init_signals()
assert worker.loop.add_signal_handler.called
def test_make_handler(worker):
worker.wsgi = mock.Mock()
worker.loop = mock.Mock()
worker.log = mock.Mock()
worker.cfg = mock.Mock()
f = worker.make_handler(worker.wsgi)
assert f is worker.wsgi.make_handler.return_value
def test__run_ok(worker, loop):
worker.ppid = 1
worker.alive = True
worker.servers = {}
sock = mock.Mock()
sock.cfg_addr = ('localhost', 8080)
worker.sockets = [sock]
worker.wsgi = mock.Mock()
worker.close = mock.Mock()
worker.close.return_value = helpers.create_future(loop)
worker.close.return_value.set_result(())
worker.log = mock.Mock()
worker.notify = mock.Mock()
worker.loop = loop
ret = helpers.create_future(loop)
loop.create_server = mock.Mock(
wraps=asyncio.coroutine(lambda *a, **kw: ret))
ret.set_result(sock)
worker.wsgi.make_handler.return_value.num_connections = 1
worker.cfg.max_requests = 100
with mock.patch('aiohttp.worker.asyncio') as m_asyncio:
m_asyncio.sleep = mock.Mock(
wraps=asyncio.coroutine(lambda *a, **kw: None))
loop.run_until_complete(worker._run())
assert worker.notify.called
assert worker.log.info.called
def test__run_exc(worker, loop):
with mock.patch('aiohttp.worker.os') as m_os:
m_os.getpid.return_value = 1
m_os.getppid.return_value = 1
worker.servers = [mock.Mock()]
worker.ppid = 1
worker.alive = True
worker.sockets = []
worker.log = mock.Mock()
worker.loop = mock.Mock()
worker.notify = mock.Mock()
with mock.patch('aiohttp.worker.asyncio.sleep') as m_sleep:
slp = helpers.create_future(loop)
slp.set_exception(KeyboardInterrupt)
m_sleep.return_value = slp
worker.close = mock.Mock()
worker.close.return_value = helpers.create_future(loop)
worker.close.return_value.set_result(1)
loop.run_until_complete(worker._run())
assert m_sleep.called
assert worker.close.called
def test_close(worker, loop):
srv = mock.Mock()
handler = mock.Mock()
worker.servers = {srv: handler}
worker.log = mock.Mock()
worker.loop = loop
app = worker.wsgi = mock.Mock()
app.finish.return_value = helpers.create_future(loop)
app.finish.return_value.set_result(1)
handler.connections = [object()]
handler.finish_connections.return_value = helpers.create_future(loop)
handler.finish_connections.return_value.set_result(1)
app.shutdown.return_value = helpers.create_future(loop)
app.shutdown.return_value.set_result(None)
loop.run_until_complete(worker.close())
app.shutdown.assert_called_with()
app.finish.assert_called_with()
handler.finish_connections.assert_called_with(timeout=95.0)
srv.close.assert_called_with()
assert worker.servers is None
loop.run_until_complete(worker.close())