1 | n/a | """Tests for selector_events.py""" |
---|
2 | n/a | |
---|
3 | n/a | import errno |
---|
4 | n/a | import socket |
---|
5 | n/a | import unittest |
---|
6 | n/a | from unittest import mock |
---|
7 | n/a | try: |
---|
8 | n/a | import ssl |
---|
9 | n/a | except ImportError: |
---|
10 | n/a | ssl = None |
---|
11 | n/a | |
---|
12 | n/a | import asyncio |
---|
13 | n/a | from asyncio import selectors |
---|
14 | n/a | from asyncio import test_utils |
---|
15 | n/a | from asyncio.selector_events import BaseSelectorEventLoop |
---|
16 | n/a | from asyncio.selector_events import _SelectorTransport |
---|
17 | n/a | from asyncio.selector_events import _SelectorSslTransport |
---|
18 | n/a | from asyncio.selector_events import _SelectorSocketTransport |
---|
19 | n/a | from asyncio.selector_events import _SelectorDatagramTransport |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | MOCK_ANY = mock.ANY |
---|
23 | n/a | |
---|
24 | n/a | |
---|
25 | n/a | class TestBaseSelectorEventLoop(BaseSelectorEventLoop): |
---|
26 | n/a | |
---|
27 | n/a | def close(self): |
---|
28 | n/a | # Don't call the close() method of the parent class, because the |
---|
29 | n/a | # selector is mocked |
---|
30 | n/a | self._closed = True |
---|
31 | n/a | |
---|
32 | n/a | def _make_self_pipe(self): |
---|
33 | n/a | self._ssock = mock.Mock() |
---|
34 | n/a | self._csock = mock.Mock() |
---|
35 | n/a | self._internal_fds += 1 |
---|
36 | n/a | |
---|
37 | n/a | |
---|
38 | n/a | def list_to_buffer(l=()): |
---|
39 | n/a | return bytearray().join(l) |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | def close_transport(transport): |
---|
43 | n/a | # Don't call transport.close() because the event loop and the selector |
---|
44 | n/a | # are mocked |
---|
45 | n/a | if transport._sock is None: |
---|
46 | n/a | return |
---|
47 | n/a | transport._sock.close() |
---|
48 | n/a | transport._sock = None |
---|
49 | n/a | |
---|
50 | n/a | |
---|
51 | n/a | class BaseSelectorEventLoopTests(test_utils.TestCase): |
---|
52 | n/a | |
---|
53 | n/a | def setUp(self): |
---|
54 | n/a | super().setUp() |
---|
55 | n/a | self.selector = mock.Mock() |
---|
56 | n/a | self.selector.select.return_value = [] |
---|
57 | n/a | self.loop = TestBaseSelectorEventLoop(self.selector) |
---|
58 | n/a | self.set_event_loop(self.loop) |
---|
59 | n/a | |
---|
60 | n/a | def test_make_socket_transport(self): |
---|
61 | n/a | m = mock.Mock() |
---|
62 | n/a | self.loop.add_reader = mock.Mock() |
---|
63 | n/a | self.loop.add_reader._is_coroutine = False |
---|
64 | n/a | transport = self.loop._make_socket_transport(m, asyncio.Protocol()) |
---|
65 | n/a | self.assertIsInstance(transport, _SelectorSocketTransport) |
---|
66 | n/a | |
---|
67 | n/a | # Calling repr() must not fail when the event loop is closed |
---|
68 | n/a | self.loop.close() |
---|
69 | n/a | repr(transport) |
---|
70 | n/a | |
---|
71 | n/a | close_transport(transport) |
---|
72 | n/a | |
---|
73 | n/a | @unittest.skipIf(ssl is None, 'No ssl module') |
---|
74 | n/a | def test_make_ssl_transport(self): |
---|
75 | n/a | m = mock.Mock() |
---|
76 | n/a | self.loop._add_reader = mock.Mock() |
---|
77 | n/a | self.loop._add_reader._is_coroutine = False |
---|
78 | n/a | self.loop._add_writer = mock.Mock() |
---|
79 | n/a | self.loop._remove_reader = mock.Mock() |
---|
80 | n/a | self.loop._remove_writer = mock.Mock() |
---|
81 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
82 | n/a | with test_utils.disable_logger(): |
---|
83 | n/a | transport = self.loop._make_ssl_transport( |
---|
84 | n/a | m, asyncio.Protocol(), m, waiter) |
---|
85 | n/a | # execute the handshake while the logger is disabled |
---|
86 | n/a | # to ignore SSL handshake failure |
---|
87 | n/a | test_utils.run_briefly(self.loop) |
---|
88 | n/a | |
---|
89 | n/a | # Sanity check |
---|
90 | n/a | class_name = transport.__class__.__name__ |
---|
91 | n/a | self.assertIn("ssl", class_name.lower()) |
---|
92 | n/a | self.assertIn("transport", class_name.lower()) |
---|
93 | n/a | |
---|
94 | n/a | transport.close() |
---|
95 | n/a | # execute pending callbacks to close the socket transport |
---|
96 | n/a | test_utils.run_briefly(self.loop) |
---|
97 | n/a | |
---|
98 | n/a | @mock.patch('asyncio.selector_events.ssl', None) |
---|
99 | n/a | @mock.patch('asyncio.sslproto.ssl', None) |
---|
100 | n/a | def test_make_ssl_transport_without_ssl_error(self): |
---|
101 | n/a | m = mock.Mock() |
---|
102 | n/a | self.loop.add_reader = mock.Mock() |
---|
103 | n/a | self.loop.add_writer = mock.Mock() |
---|
104 | n/a | self.loop.remove_reader = mock.Mock() |
---|
105 | n/a | self.loop.remove_writer = mock.Mock() |
---|
106 | n/a | with self.assertRaises(RuntimeError): |
---|
107 | n/a | self.loop._make_ssl_transport(m, m, m, m) |
---|
108 | n/a | |
---|
109 | n/a | def test_close(self): |
---|
110 | n/a | class EventLoop(BaseSelectorEventLoop): |
---|
111 | n/a | def _make_self_pipe(self): |
---|
112 | n/a | self._ssock = mock.Mock() |
---|
113 | n/a | self._csock = mock.Mock() |
---|
114 | n/a | self._internal_fds += 1 |
---|
115 | n/a | |
---|
116 | n/a | self.loop = EventLoop(self.selector) |
---|
117 | n/a | self.set_event_loop(self.loop) |
---|
118 | n/a | |
---|
119 | n/a | ssock = self.loop._ssock |
---|
120 | n/a | ssock.fileno.return_value = 7 |
---|
121 | n/a | csock = self.loop._csock |
---|
122 | n/a | csock.fileno.return_value = 1 |
---|
123 | n/a | remove_reader = self.loop._remove_reader = mock.Mock() |
---|
124 | n/a | |
---|
125 | n/a | self.loop._selector.close() |
---|
126 | n/a | self.loop._selector = selector = mock.Mock() |
---|
127 | n/a | self.assertFalse(self.loop.is_closed()) |
---|
128 | n/a | |
---|
129 | n/a | self.loop.close() |
---|
130 | n/a | self.assertTrue(self.loop.is_closed()) |
---|
131 | n/a | self.assertIsNone(self.loop._selector) |
---|
132 | n/a | self.assertIsNone(self.loop._csock) |
---|
133 | n/a | self.assertIsNone(self.loop._ssock) |
---|
134 | n/a | selector.close.assert_called_with() |
---|
135 | n/a | ssock.close.assert_called_with() |
---|
136 | n/a | csock.close.assert_called_with() |
---|
137 | n/a | remove_reader.assert_called_with(7) |
---|
138 | n/a | |
---|
139 | n/a | # it should be possible to call close() more than once |
---|
140 | n/a | self.loop.close() |
---|
141 | n/a | self.loop.close() |
---|
142 | n/a | |
---|
143 | n/a | # operation blocked when the loop is closed |
---|
144 | n/a | f = asyncio.Future(loop=self.loop) |
---|
145 | n/a | self.assertRaises(RuntimeError, self.loop.run_forever) |
---|
146 | n/a | self.assertRaises(RuntimeError, self.loop.run_until_complete, f) |
---|
147 | n/a | fd = 0 |
---|
148 | n/a | def callback(): |
---|
149 | n/a | pass |
---|
150 | n/a | self.assertRaises(RuntimeError, self.loop.add_reader, fd, callback) |
---|
151 | n/a | self.assertRaises(RuntimeError, self.loop.add_writer, fd, callback) |
---|
152 | n/a | |
---|
153 | n/a | def test_close_no_selector(self): |
---|
154 | n/a | self.loop.remove_reader = mock.Mock() |
---|
155 | n/a | self.loop._selector.close() |
---|
156 | n/a | self.loop._selector = None |
---|
157 | n/a | self.loop.close() |
---|
158 | n/a | self.assertIsNone(self.loop._selector) |
---|
159 | n/a | |
---|
160 | n/a | def test_socketpair(self): |
---|
161 | n/a | self.assertRaises(NotImplementedError, self.loop._socketpair) |
---|
162 | n/a | |
---|
163 | n/a | def test_read_from_self_tryagain(self): |
---|
164 | n/a | self.loop._ssock.recv.side_effect = BlockingIOError |
---|
165 | n/a | self.assertIsNone(self.loop._read_from_self()) |
---|
166 | n/a | |
---|
167 | n/a | def test_read_from_self_exception(self): |
---|
168 | n/a | self.loop._ssock.recv.side_effect = OSError |
---|
169 | n/a | self.assertRaises(OSError, self.loop._read_from_self) |
---|
170 | n/a | |
---|
171 | n/a | def test_write_to_self_tryagain(self): |
---|
172 | n/a | self.loop._csock.send.side_effect = BlockingIOError |
---|
173 | n/a | with test_utils.disable_logger(): |
---|
174 | n/a | self.assertIsNone(self.loop._write_to_self()) |
---|
175 | n/a | |
---|
176 | n/a | def test_write_to_self_exception(self): |
---|
177 | n/a | # _write_to_self() swallows OSError |
---|
178 | n/a | self.loop._csock.send.side_effect = RuntimeError() |
---|
179 | n/a | self.assertRaises(RuntimeError, self.loop._write_to_self) |
---|
180 | n/a | |
---|
181 | n/a | def test_sock_recv(self): |
---|
182 | n/a | sock = test_utils.mock_nonblocking_socket() |
---|
183 | n/a | self.loop._sock_recv = mock.Mock() |
---|
184 | n/a | |
---|
185 | n/a | f = self.loop.sock_recv(sock, 1024) |
---|
186 | n/a | self.assertIsInstance(f, asyncio.Future) |
---|
187 | n/a | self.loop._sock_recv.assert_called_with(f, False, sock, 1024) |
---|
188 | n/a | |
---|
189 | n/a | def test__sock_recv_canceled_fut(self): |
---|
190 | n/a | sock = mock.Mock() |
---|
191 | n/a | |
---|
192 | n/a | f = asyncio.Future(loop=self.loop) |
---|
193 | n/a | f.cancel() |
---|
194 | n/a | |
---|
195 | n/a | self.loop._sock_recv(f, False, sock, 1024) |
---|
196 | n/a | self.assertFalse(sock.recv.called) |
---|
197 | n/a | |
---|
198 | n/a | def test__sock_recv_unregister(self): |
---|
199 | n/a | sock = mock.Mock() |
---|
200 | n/a | sock.fileno.return_value = 10 |
---|
201 | n/a | |
---|
202 | n/a | f = asyncio.Future(loop=self.loop) |
---|
203 | n/a | f.cancel() |
---|
204 | n/a | |
---|
205 | n/a | self.loop.remove_reader = mock.Mock() |
---|
206 | n/a | self.loop._sock_recv(f, True, sock, 1024) |
---|
207 | n/a | self.assertEqual((10,), self.loop.remove_reader.call_args[0]) |
---|
208 | n/a | |
---|
209 | n/a | def test__sock_recv_tryagain(self): |
---|
210 | n/a | f = asyncio.Future(loop=self.loop) |
---|
211 | n/a | sock = mock.Mock() |
---|
212 | n/a | sock.fileno.return_value = 10 |
---|
213 | n/a | sock.recv.side_effect = BlockingIOError |
---|
214 | n/a | |
---|
215 | n/a | self.loop.add_reader = mock.Mock() |
---|
216 | n/a | self.loop._sock_recv(f, False, sock, 1024) |
---|
217 | n/a | self.assertEqual((10, self.loop._sock_recv, f, True, sock, 1024), |
---|
218 | n/a | self.loop.add_reader.call_args[0]) |
---|
219 | n/a | |
---|
220 | n/a | def test__sock_recv_exception(self): |
---|
221 | n/a | f = asyncio.Future(loop=self.loop) |
---|
222 | n/a | sock = mock.Mock() |
---|
223 | n/a | sock.fileno.return_value = 10 |
---|
224 | n/a | err = sock.recv.side_effect = OSError() |
---|
225 | n/a | |
---|
226 | n/a | self.loop._sock_recv(f, False, sock, 1024) |
---|
227 | n/a | self.assertIs(err, f.exception()) |
---|
228 | n/a | |
---|
229 | n/a | def test_sock_sendall(self): |
---|
230 | n/a | sock = test_utils.mock_nonblocking_socket() |
---|
231 | n/a | self.loop._sock_sendall = mock.Mock() |
---|
232 | n/a | |
---|
233 | n/a | f = self.loop.sock_sendall(sock, b'data') |
---|
234 | n/a | self.assertIsInstance(f, asyncio.Future) |
---|
235 | n/a | self.assertEqual( |
---|
236 | n/a | (f, False, sock, b'data'), |
---|
237 | n/a | self.loop._sock_sendall.call_args[0]) |
---|
238 | n/a | |
---|
239 | n/a | def test_sock_sendall_nodata(self): |
---|
240 | n/a | sock = test_utils.mock_nonblocking_socket() |
---|
241 | n/a | self.loop._sock_sendall = mock.Mock() |
---|
242 | n/a | |
---|
243 | n/a | f = self.loop.sock_sendall(sock, b'') |
---|
244 | n/a | self.assertIsInstance(f, asyncio.Future) |
---|
245 | n/a | self.assertTrue(f.done()) |
---|
246 | n/a | self.assertIsNone(f.result()) |
---|
247 | n/a | self.assertFalse(self.loop._sock_sendall.called) |
---|
248 | n/a | |
---|
249 | n/a | def test__sock_sendall_canceled_fut(self): |
---|
250 | n/a | sock = mock.Mock() |
---|
251 | n/a | |
---|
252 | n/a | f = asyncio.Future(loop=self.loop) |
---|
253 | n/a | f.cancel() |
---|
254 | n/a | |
---|
255 | n/a | self.loop._sock_sendall(f, False, sock, b'data') |
---|
256 | n/a | self.assertFalse(sock.send.called) |
---|
257 | n/a | |
---|
258 | n/a | def test__sock_sendall_unregister(self): |
---|
259 | n/a | sock = mock.Mock() |
---|
260 | n/a | sock.fileno.return_value = 10 |
---|
261 | n/a | |
---|
262 | n/a | f = asyncio.Future(loop=self.loop) |
---|
263 | n/a | f.cancel() |
---|
264 | n/a | |
---|
265 | n/a | self.loop.remove_writer = mock.Mock() |
---|
266 | n/a | self.loop._sock_sendall(f, True, sock, b'data') |
---|
267 | n/a | self.assertEqual((10,), self.loop.remove_writer.call_args[0]) |
---|
268 | n/a | |
---|
269 | n/a | def test__sock_sendall_tryagain(self): |
---|
270 | n/a | f = asyncio.Future(loop=self.loop) |
---|
271 | n/a | sock = mock.Mock() |
---|
272 | n/a | sock.fileno.return_value = 10 |
---|
273 | n/a | sock.send.side_effect = BlockingIOError |
---|
274 | n/a | |
---|
275 | n/a | self.loop.add_writer = mock.Mock() |
---|
276 | n/a | self.loop._sock_sendall(f, False, sock, b'data') |
---|
277 | n/a | self.assertEqual( |
---|
278 | n/a | (10, self.loop._sock_sendall, f, True, sock, b'data'), |
---|
279 | n/a | self.loop.add_writer.call_args[0]) |
---|
280 | n/a | |
---|
281 | n/a | def test__sock_sendall_interrupted(self): |
---|
282 | n/a | f = asyncio.Future(loop=self.loop) |
---|
283 | n/a | sock = mock.Mock() |
---|
284 | n/a | sock.fileno.return_value = 10 |
---|
285 | n/a | sock.send.side_effect = InterruptedError |
---|
286 | n/a | |
---|
287 | n/a | self.loop.add_writer = mock.Mock() |
---|
288 | n/a | self.loop._sock_sendall(f, False, sock, b'data') |
---|
289 | n/a | self.assertEqual( |
---|
290 | n/a | (10, self.loop._sock_sendall, f, True, sock, b'data'), |
---|
291 | n/a | self.loop.add_writer.call_args[0]) |
---|
292 | n/a | |
---|
293 | n/a | def test__sock_sendall_exception(self): |
---|
294 | n/a | f = asyncio.Future(loop=self.loop) |
---|
295 | n/a | sock = mock.Mock() |
---|
296 | n/a | sock.fileno.return_value = 10 |
---|
297 | n/a | err = sock.send.side_effect = OSError() |
---|
298 | n/a | |
---|
299 | n/a | self.loop._sock_sendall(f, False, sock, b'data') |
---|
300 | n/a | self.assertIs(f.exception(), err) |
---|
301 | n/a | |
---|
302 | n/a | def test__sock_sendall(self): |
---|
303 | n/a | sock = mock.Mock() |
---|
304 | n/a | |
---|
305 | n/a | f = asyncio.Future(loop=self.loop) |
---|
306 | n/a | sock.fileno.return_value = 10 |
---|
307 | n/a | sock.send.return_value = 4 |
---|
308 | n/a | |
---|
309 | n/a | self.loop._sock_sendall(f, False, sock, b'data') |
---|
310 | n/a | self.assertTrue(f.done()) |
---|
311 | n/a | self.assertIsNone(f.result()) |
---|
312 | n/a | |
---|
313 | n/a | def test__sock_sendall_partial(self): |
---|
314 | n/a | sock = mock.Mock() |
---|
315 | n/a | |
---|
316 | n/a | f = asyncio.Future(loop=self.loop) |
---|
317 | n/a | sock.fileno.return_value = 10 |
---|
318 | n/a | sock.send.return_value = 2 |
---|
319 | n/a | |
---|
320 | n/a | self.loop.add_writer = mock.Mock() |
---|
321 | n/a | self.loop._sock_sendall(f, False, sock, b'data') |
---|
322 | n/a | self.assertFalse(f.done()) |
---|
323 | n/a | self.assertEqual( |
---|
324 | n/a | (10, self.loop._sock_sendall, f, True, sock, b'ta'), |
---|
325 | n/a | self.loop.add_writer.call_args[0]) |
---|
326 | n/a | |
---|
327 | n/a | def test__sock_sendall_none(self): |
---|
328 | n/a | sock = mock.Mock() |
---|
329 | n/a | |
---|
330 | n/a | f = asyncio.Future(loop=self.loop) |
---|
331 | n/a | sock.fileno.return_value = 10 |
---|
332 | n/a | sock.send.return_value = 0 |
---|
333 | n/a | |
---|
334 | n/a | self.loop.add_writer = mock.Mock() |
---|
335 | n/a | self.loop._sock_sendall(f, False, sock, b'data') |
---|
336 | n/a | self.assertFalse(f.done()) |
---|
337 | n/a | self.assertEqual( |
---|
338 | n/a | (10, self.loop._sock_sendall, f, True, sock, b'data'), |
---|
339 | n/a | self.loop.add_writer.call_args[0]) |
---|
340 | n/a | |
---|
341 | n/a | def test_sock_connect_timeout(self): |
---|
342 | n/a | # asyncio issue #205: sock_connect() must unregister the socket on |
---|
343 | n/a | # timeout error |
---|
344 | n/a | |
---|
345 | n/a | # prepare mocks |
---|
346 | n/a | self.loop.add_writer = mock.Mock() |
---|
347 | n/a | self.loop.remove_writer = mock.Mock() |
---|
348 | n/a | sock = test_utils.mock_nonblocking_socket() |
---|
349 | n/a | sock.connect.side_effect = BlockingIOError |
---|
350 | n/a | |
---|
351 | n/a | # first call to sock_connect() registers the socket |
---|
352 | n/a | fut = self.loop.create_task( |
---|
353 | n/a | self.loop.sock_connect(sock, ('127.0.0.1', 80))) |
---|
354 | n/a | self.loop._run_once() |
---|
355 | n/a | self.assertTrue(sock.connect.called) |
---|
356 | n/a | self.assertTrue(self.loop.add_writer.called) |
---|
357 | n/a | |
---|
358 | n/a | # on timeout, the socket must be unregistered |
---|
359 | n/a | sock.connect.reset_mock() |
---|
360 | n/a | fut.cancel() |
---|
361 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
362 | n/a | self.loop.run_until_complete(fut) |
---|
363 | n/a | self.assertTrue(self.loop.remove_writer.called) |
---|
364 | n/a | |
---|
365 | n/a | @mock.patch('socket.getaddrinfo') |
---|
366 | n/a | def test_sock_connect_resolve_using_socket_params(self, m_gai): |
---|
367 | n/a | addr = ('need-resolution.com', 8080) |
---|
368 | n/a | sock = test_utils.mock_nonblocking_socket() |
---|
369 | n/a | m_gai.side_effect = (None, None, None, None, ('127.0.0.1', 0)) |
---|
370 | n/a | m_gai._is_coroutine = False |
---|
371 | n/a | con = self.loop.create_task(self.loop.sock_connect(sock, addr)) |
---|
372 | n/a | while not m_gai.called: |
---|
373 | n/a | self.loop._run_once() |
---|
374 | n/a | m_gai.assert_called_with( |
---|
375 | n/a | addr[0], addr[1], sock.family, sock.type, sock.proto, 0) |
---|
376 | n/a | |
---|
377 | n/a | con.cancel() |
---|
378 | n/a | with self.assertRaises(asyncio.CancelledError): |
---|
379 | n/a | self.loop.run_until_complete(con) |
---|
380 | n/a | |
---|
381 | n/a | def test__sock_connect(self): |
---|
382 | n/a | f = asyncio.Future(loop=self.loop) |
---|
383 | n/a | |
---|
384 | n/a | sock = mock.Mock() |
---|
385 | n/a | sock.fileno.return_value = 10 |
---|
386 | n/a | |
---|
387 | n/a | resolved = self.loop.create_future() |
---|
388 | n/a | resolved.set_result([(socket.AF_INET, socket.SOCK_STREAM, |
---|
389 | n/a | socket.IPPROTO_TCP, '', ('127.0.0.1', 8080))]) |
---|
390 | n/a | self.loop._sock_connect(f, sock, resolved) |
---|
391 | n/a | self.assertTrue(f.done()) |
---|
392 | n/a | self.assertIsNone(f.result()) |
---|
393 | n/a | self.assertTrue(sock.connect.called) |
---|
394 | n/a | |
---|
395 | n/a | def test__sock_connect_cb_cancelled_fut(self): |
---|
396 | n/a | sock = mock.Mock() |
---|
397 | n/a | self.loop.remove_writer = mock.Mock() |
---|
398 | n/a | |
---|
399 | n/a | f = asyncio.Future(loop=self.loop) |
---|
400 | n/a | f.cancel() |
---|
401 | n/a | |
---|
402 | n/a | self.loop._sock_connect_cb(f, sock, ('127.0.0.1', 8080)) |
---|
403 | n/a | self.assertFalse(sock.getsockopt.called) |
---|
404 | n/a | |
---|
405 | n/a | def test__sock_connect_writer(self): |
---|
406 | n/a | # check that the fd is registered and then unregistered |
---|
407 | n/a | self.loop._process_events = mock.Mock() |
---|
408 | n/a | self.loop.add_writer = mock.Mock() |
---|
409 | n/a | self.loop.remove_writer = mock.Mock() |
---|
410 | n/a | |
---|
411 | n/a | sock = mock.Mock() |
---|
412 | n/a | sock.fileno.return_value = 10 |
---|
413 | n/a | sock.connect.side_effect = BlockingIOError |
---|
414 | n/a | sock.getsockopt.return_value = 0 |
---|
415 | n/a | address = ('127.0.0.1', 8080) |
---|
416 | n/a | resolved = self.loop.create_future() |
---|
417 | n/a | resolved.set_result([(socket.AF_INET, socket.SOCK_STREAM, |
---|
418 | n/a | socket.IPPROTO_TCP, '', address)]) |
---|
419 | n/a | |
---|
420 | n/a | f = asyncio.Future(loop=self.loop) |
---|
421 | n/a | self.loop._sock_connect(f, sock, resolved) |
---|
422 | n/a | self.loop._run_once() |
---|
423 | n/a | self.assertTrue(self.loop.add_writer.called) |
---|
424 | n/a | self.assertEqual(10, self.loop.add_writer.call_args[0][0]) |
---|
425 | n/a | |
---|
426 | n/a | self.loop._sock_connect_cb(f, sock, address) |
---|
427 | n/a | # need to run the event loop to execute _sock_connect_done() callback |
---|
428 | n/a | self.loop.run_until_complete(f) |
---|
429 | n/a | self.assertEqual((10,), self.loop.remove_writer.call_args[0]) |
---|
430 | n/a | |
---|
431 | n/a | def test__sock_connect_cb_tryagain(self): |
---|
432 | n/a | f = asyncio.Future(loop=self.loop) |
---|
433 | n/a | sock = mock.Mock() |
---|
434 | n/a | sock.fileno.return_value = 10 |
---|
435 | n/a | sock.getsockopt.return_value = errno.EAGAIN |
---|
436 | n/a | |
---|
437 | n/a | # check that the exception is handled |
---|
438 | n/a | self.loop._sock_connect_cb(f, sock, ('127.0.0.1', 8080)) |
---|
439 | n/a | |
---|
440 | n/a | def test__sock_connect_cb_exception(self): |
---|
441 | n/a | f = asyncio.Future(loop=self.loop) |
---|
442 | n/a | sock = mock.Mock() |
---|
443 | n/a | sock.fileno.return_value = 10 |
---|
444 | n/a | sock.getsockopt.return_value = errno.ENOTCONN |
---|
445 | n/a | |
---|
446 | n/a | self.loop.remove_writer = mock.Mock() |
---|
447 | n/a | self.loop._sock_connect_cb(f, sock, ('127.0.0.1', 8080)) |
---|
448 | n/a | self.assertIsInstance(f.exception(), OSError) |
---|
449 | n/a | |
---|
450 | n/a | def test_sock_accept(self): |
---|
451 | n/a | sock = test_utils.mock_nonblocking_socket() |
---|
452 | n/a | self.loop._sock_accept = mock.Mock() |
---|
453 | n/a | |
---|
454 | n/a | f = self.loop.sock_accept(sock) |
---|
455 | n/a | self.assertIsInstance(f, asyncio.Future) |
---|
456 | n/a | self.assertEqual( |
---|
457 | n/a | (f, False, sock), self.loop._sock_accept.call_args[0]) |
---|
458 | n/a | |
---|
459 | n/a | def test__sock_accept(self): |
---|
460 | n/a | f = asyncio.Future(loop=self.loop) |
---|
461 | n/a | |
---|
462 | n/a | conn = mock.Mock() |
---|
463 | n/a | |
---|
464 | n/a | sock = mock.Mock() |
---|
465 | n/a | sock.fileno.return_value = 10 |
---|
466 | n/a | sock.accept.return_value = conn, ('127.0.0.1', 1000) |
---|
467 | n/a | |
---|
468 | n/a | self.loop._sock_accept(f, False, sock) |
---|
469 | n/a | self.assertTrue(f.done()) |
---|
470 | n/a | self.assertEqual((conn, ('127.0.0.1', 1000)), f.result()) |
---|
471 | n/a | self.assertEqual((False,), conn.setblocking.call_args[0]) |
---|
472 | n/a | |
---|
473 | n/a | def test__sock_accept_canceled_fut(self): |
---|
474 | n/a | sock = mock.Mock() |
---|
475 | n/a | |
---|
476 | n/a | f = asyncio.Future(loop=self.loop) |
---|
477 | n/a | f.cancel() |
---|
478 | n/a | |
---|
479 | n/a | self.loop._sock_accept(f, False, sock) |
---|
480 | n/a | self.assertFalse(sock.accept.called) |
---|
481 | n/a | |
---|
482 | n/a | def test__sock_accept_unregister(self): |
---|
483 | n/a | sock = mock.Mock() |
---|
484 | n/a | sock.fileno.return_value = 10 |
---|
485 | n/a | |
---|
486 | n/a | f = asyncio.Future(loop=self.loop) |
---|
487 | n/a | f.cancel() |
---|
488 | n/a | |
---|
489 | n/a | self.loop.remove_reader = mock.Mock() |
---|
490 | n/a | self.loop._sock_accept(f, True, sock) |
---|
491 | n/a | self.assertEqual((10,), self.loop.remove_reader.call_args[0]) |
---|
492 | n/a | |
---|
493 | n/a | def test__sock_accept_tryagain(self): |
---|
494 | n/a | f = asyncio.Future(loop=self.loop) |
---|
495 | n/a | sock = mock.Mock() |
---|
496 | n/a | sock.fileno.return_value = 10 |
---|
497 | n/a | sock.accept.side_effect = BlockingIOError |
---|
498 | n/a | |
---|
499 | n/a | self.loop.add_reader = mock.Mock() |
---|
500 | n/a | self.loop._sock_accept(f, False, sock) |
---|
501 | n/a | self.assertEqual( |
---|
502 | n/a | (10, self.loop._sock_accept, f, True, sock), |
---|
503 | n/a | self.loop.add_reader.call_args[0]) |
---|
504 | n/a | |
---|
505 | n/a | def test__sock_accept_exception(self): |
---|
506 | n/a | f = asyncio.Future(loop=self.loop) |
---|
507 | n/a | sock = mock.Mock() |
---|
508 | n/a | sock.fileno.return_value = 10 |
---|
509 | n/a | err = sock.accept.side_effect = OSError() |
---|
510 | n/a | |
---|
511 | n/a | self.loop._sock_accept(f, False, sock) |
---|
512 | n/a | self.assertIs(err, f.exception()) |
---|
513 | n/a | |
---|
514 | n/a | def test_add_reader(self): |
---|
515 | n/a | self.loop._selector.get_key.side_effect = KeyError |
---|
516 | n/a | cb = lambda: True |
---|
517 | n/a | self.loop.add_reader(1, cb) |
---|
518 | n/a | |
---|
519 | n/a | self.assertTrue(self.loop._selector.register.called) |
---|
520 | n/a | fd, mask, (r, w) = self.loop._selector.register.call_args[0] |
---|
521 | n/a | self.assertEqual(1, fd) |
---|
522 | n/a | self.assertEqual(selectors.EVENT_READ, mask) |
---|
523 | n/a | self.assertEqual(cb, r._callback) |
---|
524 | n/a | self.assertIsNone(w) |
---|
525 | n/a | |
---|
526 | n/a | def test_add_reader_existing(self): |
---|
527 | n/a | reader = mock.Mock() |
---|
528 | n/a | writer = mock.Mock() |
---|
529 | n/a | self.loop._selector.get_key.return_value = selectors.SelectorKey( |
---|
530 | n/a | 1, 1, selectors.EVENT_WRITE, (reader, writer)) |
---|
531 | n/a | cb = lambda: True |
---|
532 | n/a | self.loop.add_reader(1, cb) |
---|
533 | n/a | |
---|
534 | n/a | self.assertTrue(reader.cancel.called) |
---|
535 | n/a | self.assertFalse(self.loop._selector.register.called) |
---|
536 | n/a | self.assertTrue(self.loop._selector.modify.called) |
---|
537 | n/a | fd, mask, (r, w) = self.loop._selector.modify.call_args[0] |
---|
538 | n/a | self.assertEqual(1, fd) |
---|
539 | n/a | self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask) |
---|
540 | n/a | self.assertEqual(cb, r._callback) |
---|
541 | n/a | self.assertEqual(writer, w) |
---|
542 | n/a | |
---|
543 | n/a | def test_add_reader_existing_writer(self): |
---|
544 | n/a | writer = mock.Mock() |
---|
545 | n/a | self.loop._selector.get_key.return_value = selectors.SelectorKey( |
---|
546 | n/a | 1, 1, selectors.EVENT_WRITE, (None, writer)) |
---|
547 | n/a | cb = lambda: True |
---|
548 | n/a | self.loop.add_reader(1, cb) |
---|
549 | n/a | |
---|
550 | n/a | self.assertFalse(self.loop._selector.register.called) |
---|
551 | n/a | self.assertTrue(self.loop._selector.modify.called) |
---|
552 | n/a | fd, mask, (r, w) = self.loop._selector.modify.call_args[0] |
---|
553 | n/a | self.assertEqual(1, fd) |
---|
554 | n/a | self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask) |
---|
555 | n/a | self.assertEqual(cb, r._callback) |
---|
556 | n/a | self.assertEqual(writer, w) |
---|
557 | n/a | |
---|
558 | n/a | def test_remove_reader(self): |
---|
559 | n/a | self.loop._selector.get_key.return_value = selectors.SelectorKey( |
---|
560 | n/a | 1, 1, selectors.EVENT_READ, (None, None)) |
---|
561 | n/a | self.assertFalse(self.loop.remove_reader(1)) |
---|
562 | n/a | |
---|
563 | n/a | self.assertTrue(self.loop._selector.unregister.called) |
---|
564 | n/a | |
---|
565 | n/a | def test_remove_reader_read_write(self): |
---|
566 | n/a | reader = mock.Mock() |
---|
567 | n/a | writer = mock.Mock() |
---|
568 | n/a | self.loop._selector.get_key.return_value = selectors.SelectorKey( |
---|
569 | n/a | 1, 1, selectors.EVENT_READ | selectors.EVENT_WRITE, |
---|
570 | n/a | (reader, writer)) |
---|
571 | n/a | self.assertTrue( |
---|
572 | n/a | self.loop.remove_reader(1)) |
---|
573 | n/a | |
---|
574 | n/a | self.assertFalse(self.loop._selector.unregister.called) |
---|
575 | n/a | self.assertEqual( |
---|
576 | n/a | (1, selectors.EVENT_WRITE, (None, writer)), |
---|
577 | n/a | self.loop._selector.modify.call_args[0]) |
---|
578 | n/a | |
---|
579 | n/a | def test_remove_reader_unknown(self): |
---|
580 | n/a | self.loop._selector.get_key.side_effect = KeyError |
---|
581 | n/a | self.assertFalse( |
---|
582 | n/a | self.loop.remove_reader(1)) |
---|
583 | n/a | |
---|
584 | n/a | def test_add_writer(self): |
---|
585 | n/a | self.loop._selector.get_key.side_effect = KeyError |
---|
586 | n/a | cb = lambda: True |
---|
587 | n/a | self.loop.add_writer(1, cb) |
---|
588 | n/a | |
---|
589 | n/a | self.assertTrue(self.loop._selector.register.called) |
---|
590 | n/a | fd, mask, (r, w) = self.loop._selector.register.call_args[0] |
---|
591 | n/a | self.assertEqual(1, fd) |
---|
592 | n/a | self.assertEqual(selectors.EVENT_WRITE, mask) |
---|
593 | n/a | self.assertIsNone(r) |
---|
594 | n/a | self.assertEqual(cb, w._callback) |
---|
595 | n/a | |
---|
596 | n/a | def test_add_writer_existing(self): |
---|
597 | n/a | reader = mock.Mock() |
---|
598 | n/a | writer = mock.Mock() |
---|
599 | n/a | self.loop._selector.get_key.return_value = selectors.SelectorKey( |
---|
600 | n/a | 1, 1, selectors.EVENT_READ, (reader, writer)) |
---|
601 | n/a | cb = lambda: True |
---|
602 | n/a | self.loop.add_writer(1, cb) |
---|
603 | n/a | |
---|
604 | n/a | self.assertTrue(writer.cancel.called) |
---|
605 | n/a | self.assertFalse(self.loop._selector.register.called) |
---|
606 | n/a | self.assertTrue(self.loop._selector.modify.called) |
---|
607 | n/a | fd, mask, (r, w) = self.loop._selector.modify.call_args[0] |
---|
608 | n/a | self.assertEqual(1, fd) |
---|
609 | n/a | self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask) |
---|
610 | n/a | self.assertEqual(reader, r) |
---|
611 | n/a | self.assertEqual(cb, w._callback) |
---|
612 | n/a | |
---|
613 | n/a | def test_remove_writer(self): |
---|
614 | n/a | self.loop._selector.get_key.return_value = selectors.SelectorKey( |
---|
615 | n/a | 1, 1, selectors.EVENT_WRITE, (None, None)) |
---|
616 | n/a | self.assertFalse(self.loop.remove_writer(1)) |
---|
617 | n/a | |
---|
618 | n/a | self.assertTrue(self.loop._selector.unregister.called) |
---|
619 | n/a | |
---|
620 | n/a | def test_remove_writer_read_write(self): |
---|
621 | n/a | reader = mock.Mock() |
---|
622 | n/a | writer = mock.Mock() |
---|
623 | n/a | self.loop._selector.get_key.return_value = selectors.SelectorKey( |
---|
624 | n/a | 1, 1, selectors.EVENT_READ | selectors.EVENT_WRITE, |
---|
625 | n/a | (reader, writer)) |
---|
626 | n/a | self.assertTrue( |
---|
627 | n/a | self.loop.remove_writer(1)) |
---|
628 | n/a | |
---|
629 | n/a | self.assertFalse(self.loop._selector.unregister.called) |
---|
630 | n/a | self.assertEqual( |
---|
631 | n/a | (1, selectors.EVENT_READ, (reader, None)), |
---|
632 | n/a | self.loop._selector.modify.call_args[0]) |
---|
633 | n/a | |
---|
634 | n/a | def test_remove_writer_unknown(self): |
---|
635 | n/a | self.loop._selector.get_key.side_effect = KeyError |
---|
636 | n/a | self.assertFalse( |
---|
637 | n/a | self.loop.remove_writer(1)) |
---|
638 | n/a | |
---|
639 | n/a | def test_process_events_read(self): |
---|
640 | n/a | reader = mock.Mock() |
---|
641 | n/a | reader._cancelled = False |
---|
642 | n/a | |
---|
643 | n/a | self.loop._add_callback = mock.Mock() |
---|
644 | n/a | self.loop._process_events( |
---|
645 | n/a | [(selectors.SelectorKey( |
---|
646 | n/a | 1, 1, selectors.EVENT_READ, (reader, None)), |
---|
647 | n/a | selectors.EVENT_READ)]) |
---|
648 | n/a | self.assertTrue(self.loop._add_callback.called) |
---|
649 | n/a | self.loop._add_callback.assert_called_with(reader) |
---|
650 | n/a | |
---|
651 | n/a | def test_process_events_read_cancelled(self): |
---|
652 | n/a | reader = mock.Mock() |
---|
653 | n/a | reader.cancelled = True |
---|
654 | n/a | |
---|
655 | n/a | self.loop._remove_reader = mock.Mock() |
---|
656 | n/a | self.loop._process_events( |
---|
657 | n/a | [(selectors.SelectorKey( |
---|
658 | n/a | 1, 1, selectors.EVENT_READ, (reader, None)), |
---|
659 | n/a | selectors.EVENT_READ)]) |
---|
660 | n/a | self.loop._remove_reader.assert_called_with(1) |
---|
661 | n/a | |
---|
662 | n/a | def test_process_events_write(self): |
---|
663 | n/a | writer = mock.Mock() |
---|
664 | n/a | writer._cancelled = False |
---|
665 | n/a | |
---|
666 | n/a | self.loop._add_callback = mock.Mock() |
---|
667 | n/a | self.loop._process_events( |
---|
668 | n/a | [(selectors.SelectorKey(1, 1, selectors.EVENT_WRITE, |
---|
669 | n/a | (None, writer)), |
---|
670 | n/a | selectors.EVENT_WRITE)]) |
---|
671 | n/a | self.loop._add_callback.assert_called_with(writer) |
---|
672 | n/a | |
---|
673 | n/a | def test_process_events_write_cancelled(self): |
---|
674 | n/a | writer = mock.Mock() |
---|
675 | n/a | writer.cancelled = True |
---|
676 | n/a | self.loop._remove_writer = mock.Mock() |
---|
677 | n/a | |
---|
678 | n/a | self.loop._process_events( |
---|
679 | n/a | [(selectors.SelectorKey(1, 1, selectors.EVENT_WRITE, |
---|
680 | n/a | (None, writer)), |
---|
681 | n/a | selectors.EVENT_WRITE)]) |
---|
682 | n/a | self.loop._remove_writer.assert_called_with(1) |
---|
683 | n/a | |
---|
684 | n/a | def test_accept_connection_multiple(self): |
---|
685 | n/a | sock = mock.Mock() |
---|
686 | n/a | sock.accept.return_value = (mock.Mock(), mock.Mock()) |
---|
687 | n/a | backlog = 100 |
---|
688 | n/a | # Mock the coroutine generation for a connection to prevent |
---|
689 | n/a | # warnings related to un-awaited coroutines. |
---|
690 | n/a | mock_obj = mock.patch.object |
---|
691 | n/a | with mock_obj(self.loop, '_accept_connection2') as accept2_mock: |
---|
692 | n/a | accept2_mock.return_value = None |
---|
693 | n/a | with mock_obj(self.loop, 'create_task') as task_mock: |
---|
694 | n/a | task_mock.return_value = None |
---|
695 | n/a | self.loop._accept_connection(mock.Mock(), sock, backlog=backlog) |
---|
696 | n/a | self.assertEqual(sock.accept.call_count, backlog) |
---|
697 | n/a | |
---|
698 | n/a | |
---|
699 | n/a | class SelectorTransportTests(test_utils.TestCase): |
---|
700 | n/a | |
---|
701 | n/a | def setUp(self): |
---|
702 | n/a | super().setUp() |
---|
703 | n/a | self.loop = self.new_test_loop() |
---|
704 | n/a | self.protocol = test_utils.make_test_protocol(asyncio.Protocol) |
---|
705 | n/a | self.sock = mock.Mock(socket.socket) |
---|
706 | n/a | self.sock.fileno.return_value = 7 |
---|
707 | n/a | |
---|
708 | n/a | def create_transport(self): |
---|
709 | n/a | transport = _SelectorTransport(self.loop, self.sock, self.protocol, |
---|
710 | n/a | None) |
---|
711 | n/a | self.addCleanup(close_transport, transport) |
---|
712 | n/a | return transport |
---|
713 | n/a | |
---|
714 | n/a | def test_ctor(self): |
---|
715 | n/a | tr = self.create_transport() |
---|
716 | n/a | self.assertIs(tr._loop, self.loop) |
---|
717 | n/a | self.assertIs(tr._sock, self.sock) |
---|
718 | n/a | self.assertIs(tr._sock_fd, 7) |
---|
719 | n/a | |
---|
720 | n/a | def test_abort(self): |
---|
721 | n/a | tr = self.create_transport() |
---|
722 | n/a | tr._force_close = mock.Mock() |
---|
723 | n/a | |
---|
724 | n/a | tr.abort() |
---|
725 | n/a | tr._force_close.assert_called_with(None) |
---|
726 | n/a | |
---|
727 | n/a | def test_close(self): |
---|
728 | n/a | tr = self.create_transport() |
---|
729 | n/a | tr.close() |
---|
730 | n/a | |
---|
731 | n/a | self.assertTrue(tr.is_closing()) |
---|
732 | n/a | self.assertEqual(1, self.loop.remove_reader_count[7]) |
---|
733 | n/a | self.protocol.connection_lost(None) |
---|
734 | n/a | self.assertEqual(tr._conn_lost, 1) |
---|
735 | n/a | |
---|
736 | n/a | tr.close() |
---|
737 | n/a | self.assertEqual(tr._conn_lost, 1) |
---|
738 | n/a | self.assertEqual(1, self.loop.remove_reader_count[7]) |
---|
739 | n/a | |
---|
740 | n/a | def test_close_write_buffer(self): |
---|
741 | n/a | tr = self.create_transport() |
---|
742 | n/a | tr._buffer.extend(b'data') |
---|
743 | n/a | tr.close() |
---|
744 | n/a | |
---|
745 | n/a | self.assertFalse(self.loop.readers) |
---|
746 | n/a | test_utils.run_briefly(self.loop) |
---|
747 | n/a | self.assertFalse(self.protocol.connection_lost.called) |
---|
748 | n/a | |
---|
749 | n/a | def test_force_close(self): |
---|
750 | n/a | tr = self.create_transport() |
---|
751 | n/a | tr._buffer.extend(b'1') |
---|
752 | n/a | self.loop._add_reader(7, mock.sentinel) |
---|
753 | n/a | self.loop._add_writer(7, mock.sentinel) |
---|
754 | n/a | tr._force_close(None) |
---|
755 | n/a | |
---|
756 | n/a | self.assertTrue(tr.is_closing()) |
---|
757 | n/a | self.assertEqual(tr._buffer, list_to_buffer()) |
---|
758 | n/a | self.assertFalse(self.loop.readers) |
---|
759 | n/a | self.assertFalse(self.loop.writers) |
---|
760 | n/a | |
---|
761 | n/a | # second close should not remove reader |
---|
762 | n/a | tr._force_close(None) |
---|
763 | n/a | self.assertFalse(self.loop.readers) |
---|
764 | n/a | self.assertEqual(1, self.loop.remove_reader_count[7]) |
---|
765 | n/a | |
---|
766 | n/a | @mock.patch('asyncio.log.logger.error') |
---|
767 | n/a | def test_fatal_error(self, m_exc): |
---|
768 | n/a | exc = OSError() |
---|
769 | n/a | tr = self.create_transport() |
---|
770 | n/a | tr._force_close = mock.Mock() |
---|
771 | n/a | tr._fatal_error(exc) |
---|
772 | n/a | |
---|
773 | n/a | m_exc.assert_called_with( |
---|
774 | n/a | test_utils.MockPattern( |
---|
775 | n/a | 'Fatal error on transport\nprotocol:.*\ntransport:.*'), |
---|
776 | n/a | exc_info=(OSError, MOCK_ANY, MOCK_ANY)) |
---|
777 | n/a | |
---|
778 | n/a | tr._force_close.assert_called_with(exc) |
---|
779 | n/a | |
---|
780 | n/a | def test_connection_lost(self): |
---|
781 | n/a | exc = OSError() |
---|
782 | n/a | tr = self.create_transport() |
---|
783 | n/a | self.assertIsNotNone(tr._protocol) |
---|
784 | n/a | self.assertIsNotNone(tr._loop) |
---|
785 | n/a | tr._call_connection_lost(exc) |
---|
786 | n/a | |
---|
787 | n/a | self.protocol.connection_lost.assert_called_with(exc) |
---|
788 | n/a | self.sock.close.assert_called_with() |
---|
789 | n/a | self.assertIsNone(tr._sock) |
---|
790 | n/a | |
---|
791 | n/a | self.assertIsNone(tr._protocol) |
---|
792 | n/a | self.assertIsNone(tr._loop) |
---|
793 | n/a | |
---|
794 | n/a | |
---|
795 | n/a | class SelectorSocketTransportTests(test_utils.TestCase): |
---|
796 | n/a | |
---|
797 | n/a | def setUp(self): |
---|
798 | n/a | super().setUp() |
---|
799 | n/a | self.loop = self.new_test_loop() |
---|
800 | n/a | self.protocol = test_utils.make_test_protocol(asyncio.Protocol) |
---|
801 | n/a | self.sock = mock.Mock(socket.socket) |
---|
802 | n/a | self.sock_fd = self.sock.fileno.return_value = 7 |
---|
803 | n/a | |
---|
804 | n/a | def socket_transport(self, waiter=None): |
---|
805 | n/a | transport = _SelectorSocketTransport(self.loop, self.sock, |
---|
806 | n/a | self.protocol, waiter=waiter) |
---|
807 | n/a | self.addCleanup(close_transport, transport) |
---|
808 | n/a | return transport |
---|
809 | n/a | |
---|
810 | n/a | def test_ctor(self): |
---|
811 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
812 | n/a | tr = self.socket_transport(waiter=waiter) |
---|
813 | n/a | self.loop.run_until_complete(waiter) |
---|
814 | n/a | |
---|
815 | n/a | self.loop.assert_reader(7, tr._read_ready) |
---|
816 | n/a | test_utils.run_briefly(self.loop) |
---|
817 | n/a | self.protocol.connection_made.assert_called_with(tr) |
---|
818 | n/a | |
---|
819 | n/a | def test_ctor_with_waiter(self): |
---|
820 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
821 | n/a | self.socket_transport(waiter=waiter) |
---|
822 | n/a | self.loop.run_until_complete(waiter) |
---|
823 | n/a | |
---|
824 | n/a | self.assertIsNone(waiter.result()) |
---|
825 | n/a | |
---|
826 | n/a | def test_pause_resume_reading(self): |
---|
827 | n/a | tr = self.socket_transport() |
---|
828 | n/a | test_utils.run_briefly(self.loop) |
---|
829 | n/a | self.assertFalse(tr._paused) |
---|
830 | n/a | self.loop.assert_reader(7, tr._read_ready) |
---|
831 | n/a | tr.pause_reading() |
---|
832 | n/a | self.assertTrue(tr._paused) |
---|
833 | n/a | self.assertFalse(7 in self.loop.readers) |
---|
834 | n/a | tr.resume_reading() |
---|
835 | n/a | self.assertFalse(tr._paused) |
---|
836 | n/a | self.loop.assert_reader(7, tr._read_ready) |
---|
837 | n/a | with self.assertRaises(RuntimeError): |
---|
838 | n/a | tr.resume_reading() |
---|
839 | n/a | |
---|
840 | n/a | def test_read_ready(self): |
---|
841 | n/a | transport = self.socket_transport() |
---|
842 | n/a | |
---|
843 | n/a | self.sock.recv.return_value = b'data' |
---|
844 | n/a | transport._read_ready() |
---|
845 | n/a | |
---|
846 | n/a | self.protocol.data_received.assert_called_with(b'data') |
---|
847 | n/a | |
---|
848 | n/a | def test_read_ready_eof(self): |
---|
849 | n/a | transport = self.socket_transport() |
---|
850 | n/a | transport.close = mock.Mock() |
---|
851 | n/a | |
---|
852 | n/a | self.sock.recv.return_value = b'' |
---|
853 | n/a | transport._read_ready() |
---|
854 | n/a | |
---|
855 | n/a | self.protocol.eof_received.assert_called_with() |
---|
856 | n/a | transport.close.assert_called_with() |
---|
857 | n/a | |
---|
858 | n/a | def test_read_ready_eof_keep_open(self): |
---|
859 | n/a | transport = self.socket_transport() |
---|
860 | n/a | transport.close = mock.Mock() |
---|
861 | n/a | |
---|
862 | n/a | self.sock.recv.return_value = b'' |
---|
863 | n/a | self.protocol.eof_received.return_value = True |
---|
864 | n/a | transport._read_ready() |
---|
865 | n/a | |
---|
866 | n/a | self.protocol.eof_received.assert_called_with() |
---|
867 | n/a | self.assertFalse(transport.close.called) |
---|
868 | n/a | |
---|
869 | n/a | @mock.patch('logging.exception') |
---|
870 | n/a | def test_read_ready_tryagain(self, m_exc): |
---|
871 | n/a | self.sock.recv.side_effect = BlockingIOError |
---|
872 | n/a | |
---|
873 | n/a | transport = self.socket_transport() |
---|
874 | n/a | transport._fatal_error = mock.Mock() |
---|
875 | n/a | transport._read_ready() |
---|
876 | n/a | |
---|
877 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
878 | n/a | |
---|
879 | n/a | @mock.patch('logging.exception') |
---|
880 | n/a | def test_read_ready_tryagain_interrupted(self, m_exc): |
---|
881 | n/a | self.sock.recv.side_effect = InterruptedError |
---|
882 | n/a | |
---|
883 | n/a | transport = self.socket_transport() |
---|
884 | n/a | transport._fatal_error = mock.Mock() |
---|
885 | n/a | transport._read_ready() |
---|
886 | n/a | |
---|
887 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
888 | n/a | |
---|
889 | n/a | @mock.patch('logging.exception') |
---|
890 | n/a | def test_read_ready_conn_reset(self, m_exc): |
---|
891 | n/a | err = self.sock.recv.side_effect = ConnectionResetError() |
---|
892 | n/a | |
---|
893 | n/a | transport = self.socket_transport() |
---|
894 | n/a | transport._force_close = mock.Mock() |
---|
895 | n/a | with test_utils.disable_logger(): |
---|
896 | n/a | transport._read_ready() |
---|
897 | n/a | transport._force_close.assert_called_with(err) |
---|
898 | n/a | |
---|
899 | n/a | @mock.patch('logging.exception') |
---|
900 | n/a | def test_read_ready_err(self, m_exc): |
---|
901 | n/a | err = self.sock.recv.side_effect = OSError() |
---|
902 | n/a | |
---|
903 | n/a | transport = self.socket_transport() |
---|
904 | n/a | transport._fatal_error = mock.Mock() |
---|
905 | n/a | transport._read_ready() |
---|
906 | n/a | |
---|
907 | n/a | transport._fatal_error.assert_called_with( |
---|
908 | n/a | err, |
---|
909 | n/a | 'Fatal read error on socket transport') |
---|
910 | n/a | |
---|
911 | n/a | def test_write(self): |
---|
912 | n/a | data = b'data' |
---|
913 | n/a | self.sock.send.return_value = len(data) |
---|
914 | n/a | |
---|
915 | n/a | transport = self.socket_transport() |
---|
916 | n/a | transport.write(data) |
---|
917 | n/a | self.sock.send.assert_called_with(data) |
---|
918 | n/a | |
---|
919 | n/a | def test_write_bytearray(self): |
---|
920 | n/a | data = bytearray(b'data') |
---|
921 | n/a | self.sock.send.return_value = len(data) |
---|
922 | n/a | |
---|
923 | n/a | transport = self.socket_transport() |
---|
924 | n/a | transport.write(data) |
---|
925 | n/a | self.sock.send.assert_called_with(data) |
---|
926 | n/a | self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated. |
---|
927 | n/a | |
---|
928 | n/a | def test_write_memoryview(self): |
---|
929 | n/a | data = memoryview(b'data') |
---|
930 | n/a | self.sock.send.return_value = len(data) |
---|
931 | n/a | |
---|
932 | n/a | transport = self.socket_transport() |
---|
933 | n/a | transport.write(data) |
---|
934 | n/a | self.sock.send.assert_called_with(data) |
---|
935 | n/a | |
---|
936 | n/a | def test_write_no_data(self): |
---|
937 | n/a | transport = self.socket_transport() |
---|
938 | n/a | transport._buffer.extend(b'data') |
---|
939 | n/a | transport.write(b'') |
---|
940 | n/a | self.assertFalse(self.sock.send.called) |
---|
941 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
942 | n/a | |
---|
943 | n/a | def test_write_buffer(self): |
---|
944 | n/a | transport = self.socket_transport() |
---|
945 | n/a | transport._buffer.extend(b'data1') |
---|
946 | n/a | transport.write(b'data2') |
---|
947 | n/a | self.assertFalse(self.sock.send.called) |
---|
948 | n/a | self.assertEqual(list_to_buffer([b'data1', b'data2']), |
---|
949 | n/a | transport._buffer) |
---|
950 | n/a | |
---|
951 | n/a | def test_write_partial(self): |
---|
952 | n/a | data = b'data' |
---|
953 | n/a | self.sock.send.return_value = 2 |
---|
954 | n/a | |
---|
955 | n/a | transport = self.socket_transport() |
---|
956 | n/a | transport.write(data) |
---|
957 | n/a | |
---|
958 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
959 | n/a | self.assertEqual(list_to_buffer([b'ta']), transport._buffer) |
---|
960 | n/a | |
---|
961 | n/a | def test_write_partial_bytearray(self): |
---|
962 | n/a | data = bytearray(b'data') |
---|
963 | n/a | self.sock.send.return_value = 2 |
---|
964 | n/a | |
---|
965 | n/a | transport = self.socket_transport() |
---|
966 | n/a | transport.write(data) |
---|
967 | n/a | |
---|
968 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
969 | n/a | self.assertEqual(list_to_buffer([b'ta']), transport._buffer) |
---|
970 | n/a | self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated. |
---|
971 | n/a | |
---|
972 | n/a | def test_write_partial_memoryview(self): |
---|
973 | n/a | data = memoryview(b'data') |
---|
974 | n/a | self.sock.send.return_value = 2 |
---|
975 | n/a | |
---|
976 | n/a | transport = self.socket_transport() |
---|
977 | n/a | transport.write(data) |
---|
978 | n/a | |
---|
979 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
980 | n/a | self.assertEqual(list_to_buffer([b'ta']), transport._buffer) |
---|
981 | n/a | |
---|
982 | n/a | def test_write_partial_none(self): |
---|
983 | n/a | data = b'data' |
---|
984 | n/a | self.sock.send.return_value = 0 |
---|
985 | n/a | self.sock.fileno.return_value = 7 |
---|
986 | n/a | |
---|
987 | n/a | transport = self.socket_transport() |
---|
988 | n/a | transport.write(data) |
---|
989 | n/a | |
---|
990 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
991 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
992 | n/a | |
---|
993 | n/a | def test_write_tryagain(self): |
---|
994 | n/a | self.sock.send.side_effect = BlockingIOError |
---|
995 | n/a | |
---|
996 | n/a | data = b'data' |
---|
997 | n/a | transport = self.socket_transport() |
---|
998 | n/a | transport.write(data) |
---|
999 | n/a | |
---|
1000 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
1001 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1002 | n/a | |
---|
1003 | n/a | @mock.patch('asyncio.selector_events.logger') |
---|
1004 | n/a | def test_write_exception(self, m_log): |
---|
1005 | n/a | err = self.sock.send.side_effect = OSError() |
---|
1006 | n/a | |
---|
1007 | n/a | data = b'data' |
---|
1008 | n/a | transport = self.socket_transport() |
---|
1009 | n/a | transport._fatal_error = mock.Mock() |
---|
1010 | n/a | transport.write(data) |
---|
1011 | n/a | transport._fatal_error.assert_called_with( |
---|
1012 | n/a | err, |
---|
1013 | n/a | 'Fatal write error on socket transport') |
---|
1014 | n/a | transport._conn_lost = 1 |
---|
1015 | n/a | |
---|
1016 | n/a | self.sock.reset_mock() |
---|
1017 | n/a | transport.write(data) |
---|
1018 | n/a | self.assertFalse(self.sock.send.called) |
---|
1019 | n/a | self.assertEqual(transport._conn_lost, 2) |
---|
1020 | n/a | transport.write(data) |
---|
1021 | n/a | transport.write(data) |
---|
1022 | n/a | transport.write(data) |
---|
1023 | n/a | transport.write(data) |
---|
1024 | n/a | m_log.warning.assert_called_with('socket.send() raised exception.') |
---|
1025 | n/a | |
---|
1026 | n/a | def test_write_str(self): |
---|
1027 | n/a | transport = self.socket_transport() |
---|
1028 | n/a | self.assertRaises(TypeError, transport.write, 'str') |
---|
1029 | n/a | |
---|
1030 | n/a | def test_write_closing(self): |
---|
1031 | n/a | transport = self.socket_transport() |
---|
1032 | n/a | transport.close() |
---|
1033 | n/a | self.assertEqual(transport._conn_lost, 1) |
---|
1034 | n/a | transport.write(b'data') |
---|
1035 | n/a | self.assertEqual(transport._conn_lost, 2) |
---|
1036 | n/a | |
---|
1037 | n/a | def test_write_ready(self): |
---|
1038 | n/a | data = b'data' |
---|
1039 | n/a | self.sock.send.return_value = len(data) |
---|
1040 | n/a | |
---|
1041 | n/a | transport = self.socket_transport() |
---|
1042 | n/a | transport._buffer.extend(data) |
---|
1043 | n/a | self.loop._add_writer(7, transport._write_ready) |
---|
1044 | n/a | transport._write_ready() |
---|
1045 | n/a | self.assertTrue(self.sock.send.called) |
---|
1046 | n/a | self.assertFalse(self.loop.writers) |
---|
1047 | n/a | |
---|
1048 | n/a | def test_write_ready_closing(self): |
---|
1049 | n/a | data = b'data' |
---|
1050 | n/a | self.sock.send.return_value = len(data) |
---|
1051 | n/a | |
---|
1052 | n/a | transport = self.socket_transport() |
---|
1053 | n/a | transport._closing = True |
---|
1054 | n/a | transport._buffer.extend(data) |
---|
1055 | n/a | self.loop._add_writer(7, transport._write_ready) |
---|
1056 | n/a | transport._write_ready() |
---|
1057 | n/a | self.assertTrue(self.sock.send.called) |
---|
1058 | n/a | self.assertFalse(self.loop.writers) |
---|
1059 | n/a | self.sock.close.assert_called_with() |
---|
1060 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
1061 | n/a | |
---|
1062 | n/a | def test_write_ready_no_data(self): |
---|
1063 | n/a | transport = self.socket_transport() |
---|
1064 | n/a | # This is an internal error. |
---|
1065 | n/a | self.assertRaises(AssertionError, transport._write_ready) |
---|
1066 | n/a | |
---|
1067 | n/a | def test_write_ready_partial(self): |
---|
1068 | n/a | data = b'data' |
---|
1069 | n/a | self.sock.send.return_value = 2 |
---|
1070 | n/a | |
---|
1071 | n/a | transport = self.socket_transport() |
---|
1072 | n/a | transport._buffer.extend(data) |
---|
1073 | n/a | self.loop._add_writer(7, transport._write_ready) |
---|
1074 | n/a | transport._write_ready() |
---|
1075 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
1076 | n/a | self.assertEqual(list_to_buffer([b'ta']), transport._buffer) |
---|
1077 | n/a | |
---|
1078 | n/a | def test_write_ready_partial_none(self): |
---|
1079 | n/a | data = b'data' |
---|
1080 | n/a | self.sock.send.return_value = 0 |
---|
1081 | n/a | |
---|
1082 | n/a | transport = self.socket_transport() |
---|
1083 | n/a | transport._buffer.extend(data) |
---|
1084 | n/a | self.loop._add_writer(7, transport._write_ready) |
---|
1085 | n/a | transport._write_ready() |
---|
1086 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
1087 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1088 | n/a | |
---|
1089 | n/a | def test_write_ready_tryagain(self): |
---|
1090 | n/a | self.sock.send.side_effect = BlockingIOError |
---|
1091 | n/a | |
---|
1092 | n/a | transport = self.socket_transport() |
---|
1093 | n/a | transport._buffer = list_to_buffer([b'data1', b'data2']) |
---|
1094 | n/a | self.loop._add_writer(7, transport._write_ready) |
---|
1095 | n/a | transport._write_ready() |
---|
1096 | n/a | |
---|
1097 | n/a | self.loop.assert_writer(7, transport._write_ready) |
---|
1098 | n/a | self.assertEqual(list_to_buffer([b'data1data2']), transport._buffer) |
---|
1099 | n/a | |
---|
1100 | n/a | def test_write_ready_exception(self): |
---|
1101 | n/a | err = self.sock.send.side_effect = OSError() |
---|
1102 | n/a | |
---|
1103 | n/a | transport = self.socket_transport() |
---|
1104 | n/a | transport._fatal_error = mock.Mock() |
---|
1105 | n/a | transport._buffer.extend(b'data') |
---|
1106 | n/a | transport._write_ready() |
---|
1107 | n/a | transport._fatal_error.assert_called_with( |
---|
1108 | n/a | err, |
---|
1109 | n/a | 'Fatal write error on socket transport') |
---|
1110 | n/a | |
---|
1111 | n/a | def test_write_eof(self): |
---|
1112 | n/a | tr = self.socket_transport() |
---|
1113 | n/a | self.assertTrue(tr.can_write_eof()) |
---|
1114 | n/a | tr.write_eof() |
---|
1115 | n/a | self.sock.shutdown.assert_called_with(socket.SHUT_WR) |
---|
1116 | n/a | tr.write_eof() |
---|
1117 | n/a | self.assertEqual(self.sock.shutdown.call_count, 1) |
---|
1118 | n/a | tr.close() |
---|
1119 | n/a | |
---|
1120 | n/a | def test_write_eof_buffer(self): |
---|
1121 | n/a | tr = self.socket_transport() |
---|
1122 | n/a | self.sock.send.side_effect = BlockingIOError |
---|
1123 | n/a | tr.write(b'data') |
---|
1124 | n/a | tr.write_eof() |
---|
1125 | n/a | self.assertEqual(tr._buffer, list_to_buffer([b'data'])) |
---|
1126 | n/a | self.assertTrue(tr._eof) |
---|
1127 | n/a | self.assertFalse(self.sock.shutdown.called) |
---|
1128 | n/a | self.sock.send.side_effect = lambda _: 4 |
---|
1129 | n/a | tr._write_ready() |
---|
1130 | n/a | self.assertTrue(self.sock.send.called) |
---|
1131 | n/a | self.sock.shutdown.assert_called_with(socket.SHUT_WR) |
---|
1132 | n/a | tr.close() |
---|
1133 | n/a | |
---|
1134 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
1135 | n/a | def test_transport_close_remove_writer(self, m_log): |
---|
1136 | n/a | remove_writer = self.loop._remove_writer = mock.Mock() |
---|
1137 | n/a | |
---|
1138 | n/a | transport = self.socket_transport() |
---|
1139 | n/a | transport.close() |
---|
1140 | n/a | remove_writer.assert_called_with(self.sock_fd) |
---|
1141 | n/a | |
---|
1142 | n/a | |
---|
1143 | n/a | @unittest.skipIf(ssl is None, 'No ssl module') |
---|
1144 | n/a | class SelectorSslTransportTests(test_utils.TestCase): |
---|
1145 | n/a | |
---|
1146 | n/a | def setUp(self): |
---|
1147 | n/a | super().setUp() |
---|
1148 | n/a | self.loop = self.new_test_loop() |
---|
1149 | n/a | self.protocol = test_utils.make_test_protocol(asyncio.Protocol) |
---|
1150 | n/a | self.sock = mock.Mock(socket.socket) |
---|
1151 | n/a | self.sock.fileno.return_value = 7 |
---|
1152 | n/a | self.sslsock = mock.Mock() |
---|
1153 | n/a | self.sslsock.fileno.return_value = 1 |
---|
1154 | n/a | self.sslcontext = mock.Mock() |
---|
1155 | n/a | self.sslcontext.wrap_socket.return_value = self.sslsock |
---|
1156 | n/a | |
---|
1157 | n/a | def ssl_transport(self, waiter=None, server_hostname=None): |
---|
1158 | n/a | transport = _SelectorSslTransport(self.loop, self.sock, self.protocol, |
---|
1159 | n/a | self.sslcontext, waiter=waiter, |
---|
1160 | n/a | server_hostname=server_hostname) |
---|
1161 | n/a | self.addCleanup(close_transport, transport) |
---|
1162 | n/a | return transport |
---|
1163 | n/a | |
---|
1164 | n/a | def _make_one(self, create_waiter=None): |
---|
1165 | n/a | transport = self.ssl_transport() |
---|
1166 | n/a | self.sock.reset_mock() |
---|
1167 | n/a | self.sslsock.reset_mock() |
---|
1168 | n/a | self.sslcontext.reset_mock() |
---|
1169 | n/a | self.loop.reset_counters() |
---|
1170 | n/a | return transport |
---|
1171 | n/a | |
---|
1172 | n/a | def test_on_handshake(self): |
---|
1173 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
1174 | n/a | tr = self.ssl_transport(waiter=waiter) |
---|
1175 | n/a | self.assertTrue(self.sslsock.do_handshake.called) |
---|
1176 | n/a | self.loop.assert_reader(1, tr._read_ready) |
---|
1177 | n/a | test_utils.run_briefly(self.loop) |
---|
1178 | n/a | self.assertIsNone(waiter.result()) |
---|
1179 | n/a | |
---|
1180 | n/a | def test_on_handshake_reader_retry(self): |
---|
1181 | n/a | self.loop.set_debug(False) |
---|
1182 | n/a | self.sslsock.do_handshake.side_effect = ssl.SSLWantReadError |
---|
1183 | n/a | transport = self.ssl_transport() |
---|
1184 | n/a | self.loop.assert_reader(1, transport._on_handshake, None) |
---|
1185 | n/a | |
---|
1186 | n/a | def test_on_handshake_writer_retry(self): |
---|
1187 | n/a | self.loop.set_debug(False) |
---|
1188 | n/a | self.sslsock.do_handshake.side_effect = ssl.SSLWantWriteError |
---|
1189 | n/a | transport = self.ssl_transport() |
---|
1190 | n/a | self.loop.assert_writer(1, transport._on_handshake, None) |
---|
1191 | n/a | |
---|
1192 | n/a | def test_on_handshake_exc(self): |
---|
1193 | n/a | exc = ValueError() |
---|
1194 | n/a | self.sslsock.do_handshake.side_effect = exc |
---|
1195 | n/a | with test_utils.disable_logger(): |
---|
1196 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
1197 | n/a | self.ssl_transport(waiter=waiter) |
---|
1198 | n/a | self.assertTrue(waiter.done()) |
---|
1199 | n/a | self.assertIs(exc, waiter.exception()) |
---|
1200 | n/a | self.assertTrue(self.sslsock.close.called) |
---|
1201 | n/a | |
---|
1202 | n/a | def test_on_handshake_base_exc(self): |
---|
1203 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
1204 | n/a | transport = self.ssl_transport(waiter=waiter) |
---|
1205 | n/a | exc = BaseException() |
---|
1206 | n/a | self.sslsock.do_handshake.side_effect = exc |
---|
1207 | n/a | with test_utils.disable_logger(): |
---|
1208 | n/a | self.assertRaises(BaseException, transport._on_handshake, 0) |
---|
1209 | n/a | self.assertTrue(self.sslsock.close.called) |
---|
1210 | n/a | self.assertTrue(waiter.done()) |
---|
1211 | n/a | self.assertIs(exc, waiter.exception()) |
---|
1212 | n/a | |
---|
1213 | n/a | def test_cancel_handshake(self): |
---|
1214 | n/a | # Python issue #23197: cancelling a handshake must not raise an |
---|
1215 | n/a | # exception or log an error, even if the handshake failed |
---|
1216 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
1217 | n/a | transport = self.ssl_transport(waiter=waiter) |
---|
1218 | n/a | waiter.cancel() |
---|
1219 | n/a | exc = ValueError() |
---|
1220 | n/a | self.sslsock.do_handshake.side_effect = exc |
---|
1221 | n/a | with test_utils.disable_logger(): |
---|
1222 | n/a | transport._on_handshake(0) |
---|
1223 | n/a | transport.close() |
---|
1224 | n/a | test_utils.run_briefly(self.loop) |
---|
1225 | n/a | |
---|
1226 | n/a | def test_pause_resume_reading(self): |
---|
1227 | n/a | tr = self._make_one() |
---|
1228 | n/a | self.assertFalse(tr._paused) |
---|
1229 | n/a | self.loop.assert_reader(1, tr._read_ready) |
---|
1230 | n/a | tr.pause_reading() |
---|
1231 | n/a | self.assertTrue(tr._paused) |
---|
1232 | n/a | self.assertFalse(1 in self.loop.readers) |
---|
1233 | n/a | tr.resume_reading() |
---|
1234 | n/a | self.assertFalse(tr._paused) |
---|
1235 | n/a | self.loop.assert_reader(1, tr._read_ready) |
---|
1236 | n/a | with self.assertRaises(RuntimeError): |
---|
1237 | n/a | tr.resume_reading() |
---|
1238 | n/a | |
---|
1239 | n/a | def test_write(self): |
---|
1240 | n/a | transport = self._make_one() |
---|
1241 | n/a | transport.write(b'data') |
---|
1242 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1243 | n/a | |
---|
1244 | n/a | def test_write_bytearray(self): |
---|
1245 | n/a | transport = self._make_one() |
---|
1246 | n/a | data = bytearray(b'data') |
---|
1247 | n/a | transport.write(data) |
---|
1248 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1249 | n/a | self.assertEqual(data, bytearray(b'data')) # Hasn't been mutated. |
---|
1250 | n/a | self.assertIsNot(data, transport._buffer) # Hasn't been incorporated. |
---|
1251 | n/a | |
---|
1252 | n/a | def test_write_memoryview(self): |
---|
1253 | n/a | transport = self._make_one() |
---|
1254 | n/a | data = memoryview(b'data') |
---|
1255 | n/a | transport.write(data) |
---|
1256 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1257 | n/a | |
---|
1258 | n/a | def test_write_no_data(self): |
---|
1259 | n/a | transport = self._make_one() |
---|
1260 | n/a | transport._buffer.extend(b'data') |
---|
1261 | n/a | transport.write(b'') |
---|
1262 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1263 | n/a | |
---|
1264 | n/a | def test_write_str(self): |
---|
1265 | n/a | transport = self._make_one() |
---|
1266 | n/a | self.assertRaises(TypeError, transport.write, 'str') |
---|
1267 | n/a | |
---|
1268 | n/a | def test_write_closing(self): |
---|
1269 | n/a | transport = self._make_one() |
---|
1270 | n/a | transport.close() |
---|
1271 | n/a | self.assertEqual(transport._conn_lost, 1) |
---|
1272 | n/a | transport.write(b'data') |
---|
1273 | n/a | self.assertEqual(transport._conn_lost, 2) |
---|
1274 | n/a | |
---|
1275 | n/a | @mock.patch('asyncio.selector_events.logger') |
---|
1276 | n/a | def test_write_exception(self, m_log): |
---|
1277 | n/a | transport = self._make_one() |
---|
1278 | n/a | transport._conn_lost = 1 |
---|
1279 | n/a | transport.write(b'data') |
---|
1280 | n/a | self.assertEqual(transport._buffer, list_to_buffer()) |
---|
1281 | n/a | transport.write(b'data') |
---|
1282 | n/a | transport.write(b'data') |
---|
1283 | n/a | transport.write(b'data') |
---|
1284 | n/a | transport.write(b'data') |
---|
1285 | n/a | m_log.warning.assert_called_with('socket.send() raised exception.') |
---|
1286 | n/a | |
---|
1287 | n/a | def test_read_ready_recv(self): |
---|
1288 | n/a | self.sslsock.recv.return_value = b'data' |
---|
1289 | n/a | transport = self._make_one() |
---|
1290 | n/a | transport._read_ready() |
---|
1291 | n/a | self.assertTrue(self.sslsock.recv.called) |
---|
1292 | n/a | self.assertEqual((b'data',), self.protocol.data_received.call_args[0]) |
---|
1293 | n/a | |
---|
1294 | n/a | def test_read_ready_write_wants_read(self): |
---|
1295 | n/a | self.loop._add_writer = mock.Mock() |
---|
1296 | n/a | self.sslsock.recv.side_effect = BlockingIOError |
---|
1297 | n/a | transport = self._make_one() |
---|
1298 | n/a | transport._write_wants_read = True |
---|
1299 | n/a | transport._write_ready = mock.Mock() |
---|
1300 | n/a | transport._buffer.extend(b'data') |
---|
1301 | n/a | transport._read_ready() |
---|
1302 | n/a | |
---|
1303 | n/a | self.assertFalse(transport._write_wants_read) |
---|
1304 | n/a | transport._write_ready.assert_called_with() |
---|
1305 | n/a | self.loop._add_writer.assert_called_with( |
---|
1306 | n/a | transport._sock_fd, transport._write_ready) |
---|
1307 | n/a | |
---|
1308 | n/a | def test_read_ready_recv_eof(self): |
---|
1309 | n/a | self.sslsock.recv.return_value = b'' |
---|
1310 | n/a | transport = self._make_one() |
---|
1311 | n/a | transport.close = mock.Mock() |
---|
1312 | n/a | transport._read_ready() |
---|
1313 | n/a | transport.close.assert_called_with() |
---|
1314 | n/a | self.protocol.eof_received.assert_called_with() |
---|
1315 | n/a | |
---|
1316 | n/a | def test_read_ready_recv_conn_reset(self): |
---|
1317 | n/a | err = self.sslsock.recv.side_effect = ConnectionResetError() |
---|
1318 | n/a | transport = self._make_one() |
---|
1319 | n/a | transport._force_close = mock.Mock() |
---|
1320 | n/a | with test_utils.disable_logger(): |
---|
1321 | n/a | transport._read_ready() |
---|
1322 | n/a | transport._force_close.assert_called_with(err) |
---|
1323 | n/a | |
---|
1324 | n/a | def test_read_ready_recv_retry(self): |
---|
1325 | n/a | self.sslsock.recv.side_effect = ssl.SSLWantReadError |
---|
1326 | n/a | transport = self._make_one() |
---|
1327 | n/a | transport._read_ready() |
---|
1328 | n/a | self.assertTrue(self.sslsock.recv.called) |
---|
1329 | n/a | self.assertFalse(self.protocol.data_received.called) |
---|
1330 | n/a | |
---|
1331 | n/a | self.sslsock.recv.side_effect = BlockingIOError |
---|
1332 | n/a | transport._read_ready() |
---|
1333 | n/a | self.assertFalse(self.protocol.data_received.called) |
---|
1334 | n/a | |
---|
1335 | n/a | self.sslsock.recv.side_effect = InterruptedError |
---|
1336 | n/a | transport._read_ready() |
---|
1337 | n/a | self.assertFalse(self.protocol.data_received.called) |
---|
1338 | n/a | |
---|
1339 | n/a | def test_read_ready_recv_write(self): |
---|
1340 | n/a | self.loop._remove_reader = mock.Mock() |
---|
1341 | n/a | self.loop._add_writer = mock.Mock() |
---|
1342 | n/a | self.sslsock.recv.side_effect = ssl.SSLWantWriteError |
---|
1343 | n/a | transport = self._make_one() |
---|
1344 | n/a | transport._read_ready() |
---|
1345 | n/a | self.assertFalse(self.protocol.data_received.called) |
---|
1346 | n/a | self.assertTrue(transport._read_wants_write) |
---|
1347 | n/a | |
---|
1348 | n/a | self.loop._remove_reader.assert_called_with(transport._sock_fd) |
---|
1349 | n/a | self.loop._add_writer.assert_called_with( |
---|
1350 | n/a | transport._sock_fd, transport._write_ready) |
---|
1351 | n/a | |
---|
1352 | n/a | def test_read_ready_recv_exc(self): |
---|
1353 | n/a | err = self.sslsock.recv.side_effect = OSError() |
---|
1354 | n/a | transport = self._make_one() |
---|
1355 | n/a | transport._fatal_error = mock.Mock() |
---|
1356 | n/a | transport._read_ready() |
---|
1357 | n/a | transport._fatal_error.assert_called_with( |
---|
1358 | n/a | err, |
---|
1359 | n/a | 'Fatal read error on SSL transport') |
---|
1360 | n/a | |
---|
1361 | n/a | def test_write_ready_send(self): |
---|
1362 | n/a | self.sslsock.send.return_value = 4 |
---|
1363 | n/a | transport = self._make_one() |
---|
1364 | n/a | transport._buffer = list_to_buffer([b'data']) |
---|
1365 | n/a | transport._write_ready() |
---|
1366 | n/a | self.assertEqual(list_to_buffer(), transport._buffer) |
---|
1367 | n/a | self.assertTrue(self.sslsock.send.called) |
---|
1368 | n/a | |
---|
1369 | n/a | def test_write_ready_send_none(self): |
---|
1370 | n/a | self.sslsock.send.return_value = 0 |
---|
1371 | n/a | transport = self._make_one() |
---|
1372 | n/a | transport._buffer = list_to_buffer([b'data1', b'data2']) |
---|
1373 | n/a | transport._write_ready() |
---|
1374 | n/a | self.assertTrue(self.sslsock.send.called) |
---|
1375 | n/a | self.assertEqual(list_to_buffer([b'data1data2']), transport._buffer) |
---|
1376 | n/a | |
---|
1377 | n/a | def test_write_ready_send_partial(self): |
---|
1378 | n/a | self.sslsock.send.return_value = 2 |
---|
1379 | n/a | transport = self._make_one() |
---|
1380 | n/a | transport._buffer = list_to_buffer([b'data1', b'data2']) |
---|
1381 | n/a | transport._write_ready() |
---|
1382 | n/a | self.assertTrue(self.sslsock.send.called) |
---|
1383 | n/a | self.assertEqual(list_to_buffer([b'ta1data2']), transport._buffer) |
---|
1384 | n/a | |
---|
1385 | n/a | def test_write_ready_send_closing_partial(self): |
---|
1386 | n/a | self.sslsock.send.return_value = 2 |
---|
1387 | n/a | transport = self._make_one() |
---|
1388 | n/a | transport._buffer = list_to_buffer([b'data1', b'data2']) |
---|
1389 | n/a | transport._write_ready() |
---|
1390 | n/a | self.assertTrue(self.sslsock.send.called) |
---|
1391 | n/a | self.assertFalse(self.sslsock.close.called) |
---|
1392 | n/a | |
---|
1393 | n/a | def test_write_ready_send_closing(self): |
---|
1394 | n/a | self.sslsock.send.return_value = 4 |
---|
1395 | n/a | transport = self._make_one() |
---|
1396 | n/a | transport._buffer = list_to_buffer([b'data']) |
---|
1397 | n/a | transport.close() |
---|
1398 | n/a | transport._write_ready() |
---|
1399 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
1400 | n/a | |
---|
1401 | n/a | def test_write_ready_send_closing_empty_buffer(self): |
---|
1402 | n/a | self.sslsock.send.return_value = 4 |
---|
1403 | n/a | call_soon = self.loop.call_soon = mock.Mock() |
---|
1404 | n/a | transport = self._make_one() |
---|
1405 | n/a | transport._buffer = list_to_buffer() |
---|
1406 | n/a | transport.close() |
---|
1407 | n/a | transport._write_ready() |
---|
1408 | n/a | call_soon.assert_called_with(transport._call_connection_lost, None) |
---|
1409 | n/a | |
---|
1410 | n/a | def test_write_ready_send_retry(self): |
---|
1411 | n/a | transport = self._make_one() |
---|
1412 | n/a | transport._buffer = list_to_buffer([b'data']) |
---|
1413 | n/a | |
---|
1414 | n/a | self.sslsock.send.side_effect = ssl.SSLWantWriteError |
---|
1415 | n/a | transport._write_ready() |
---|
1416 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1417 | n/a | |
---|
1418 | n/a | self.sslsock.send.side_effect = BlockingIOError() |
---|
1419 | n/a | transport._write_ready() |
---|
1420 | n/a | self.assertEqual(list_to_buffer([b'data']), transport._buffer) |
---|
1421 | n/a | |
---|
1422 | n/a | def test_write_ready_send_read(self): |
---|
1423 | n/a | transport = self._make_one() |
---|
1424 | n/a | transport._buffer = list_to_buffer([b'data']) |
---|
1425 | n/a | |
---|
1426 | n/a | self.loop._remove_writer = mock.Mock() |
---|
1427 | n/a | self.sslsock.send.side_effect = ssl.SSLWantReadError |
---|
1428 | n/a | transport._write_ready() |
---|
1429 | n/a | self.assertFalse(self.protocol.data_received.called) |
---|
1430 | n/a | self.assertTrue(transport._write_wants_read) |
---|
1431 | n/a | self.loop._remove_writer.assert_called_with(transport._sock_fd) |
---|
1432 | n/a | |
---|
1433 | n/a | def test_write_ready_send_exc(self): |
---|
1434 | n/a | err = self.sslsock.send.side_effect = OSError() |
---|
1435 | n/a | |
---|
1436 | n/a | transport = self._make_one() |
---|
1437 | n/a | transport._buffer = list_to_buffer([b'data']) |
---|
1438 | n/a | transport._fatal_error = mock.Mock() |
---|
1439 | n/a | transport._write_ready() |
---|
1440 | n/a | transport._fatal_error.assert_called_with( |
---|
1441 | n/a | err, |
---|
1442 | n/a | 'Fatal write error on SSL transport') |
---|
1443 | n/a | self.assertEqual(list_to_buffer(), transport._buffer) |
---|
1444 | n/a | |
---|
1445 | n/a | def test_write_ready_read_wants_write(self): |
---|
1446 | n/a | self.loop._add_reader = mock.Mock() |
---|
1447 | n/a | self.sslsock.send.side_effect = BlockingIOError |
---|
1448 | n/a | transport = self._make_one() |
---|
1449 | n/a | transport._read_wants_write = True |
---|
1450 | n/a | transport._read_ready = mock.Mock() |
---|
1451 | n/a | transport._write_ready() |
---|
1452 | n/a | |
---|
1453 | n/a | self.assertFalse(transport._read_wants_write) |
---|
1454 | n/a | transport._read_ready.assert_called_with() |
---|
1455 | n/a | self.loop._add_reader.assert_called_with( |
---|
1456 | n/a | transport._sock_fd, transport._read_ready) |
---|
1457 | n/a | |
---|
1458 | n/a | def test_write_eof(self): |
---|
1459 | n/a | tr = self._make_one() |
---|
1460 | n/a | self.assertFalse(tr.can_write_eof()) |
---|
1461 | n/a | self.assertRaises(NotImplementedError, tr.write_eof) |
---|
1462 | n/a | |
---|
1463 | n/a | def check_close(self): |
---|
1464 | n/a | tr = self._make_one() |
---|
1465 | n/a | tr.close() |
---|
1466 | n/a | |
---|
1467 | n/a | self.assertTrue(tr.is_closing()) |
---|
1468 | n/a | self.assertEqual(1, self.loop.remove_reader_count[1]) |
---|
1469 | n/a | self.assertEqual(tr._conn_lost, 1) |
---|
1470 | n/a | |
---|
1471 | n/a | tr.close() |
---|
1472 | n/a | self.assertEqual(tr._conn_lost, 1) |
---|
1473 | n/a | self.assertEqual(1, self.loop.remove_reader_count[1]) |
---|
1474 | n/a | |
---|
1475 | n/a | test_utils.run_briefly(self.loop) |
---|
1476 | n/a | |
---|
1477 | n/a | def test_close(self): |
---|
1478 | n/a | self.check_close() |
---|
1479 | n/a | self.assertTrue(self.protocol.connection_made.called) |
---|
1480 | n/a | self.assertTrue(self.protocol.connection_lost.called) |
---|
1481 | n/a | |
---|
1482 | n/a | def test_close_not_connected(self): |
---|
1483 | n/a | self.sslsock.do_handshake.side_effect = ssl.SSLWantReadError |
---|
1484 | n/a | self.check_close() |
---|
1485 | n/a | self.assertFalse(self.protocol.connection_made.called) |
---|
1486 | n/a | self.assertFalse(self.protocol.connection_lost.called) |
---|
1487 | n/a | |
---|
1488 | n/a | @unittest.skipIf(ssl is None, 'No SSL support') |
---|
1489 | n/a | def test_server_hostname(self): |
---|
1490 | n/a | self.ssl_transport(server_hostname='localhost') |
---|
1491 | n/a | self.sslcontext.wrap_socket.assert_called_with( |
---|
1492 | n/a | self.sock, do_handshake_on_connect=False, server_side=False, |
---|
1493 | n/a | server_hostname='localhost') |
---|
1494 | n/a | |
---|
1495 | n/a | |
---|
1496 | n/a | class SelectorSslWithoutSslTransportTests(unittest.TestCase): |
---|
1497 | n/a | |
---|
1498 | n/a | @mock.patch('asyncio.selector_events.ssl', None) |
---|
1499 | n/a | def test_ssl_transport_requires_ssl_module(self): |
---|
1500 | n/a | Mock = mock.Mock |
---|
1501 | n/a | with self.assertRaises(RuntimeError): |
---|
1502 | n/a | _SelectorSslTransport(Mock(), Mock(), Mock(), Mock()) |
---|
1503 | n/a | |
---|
1504 | n/a | |
---|
1505 | n/a | class SelectorDatagramTransportTests(test_utils.TestCase): |
---|
1506 | n/a | |
---|
1507 | n/a | def setUp(self): |
---|
1508 | n/a | super().setUp() |
---|
1509 | n/a | self.loop = self.new_test_loop() |
---|
1510 | n/a | self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol) |
---|
1511 | n/a | self.sock = mock.Mock(spec_set=socket.socket) |
---|
1512 | n/a | self.sock.fileno.return_value = 7 |
---|
1513 | n/a | |
---|
1514 | n/a | def datagram_transport(self, address=None): |
---|
1515 | n/a | transport = _SelectorDatagramTransport(self.loop, self.sock, |
---|
1516 | n/a | self.protocol, |
---|
1517 | n/a | address=address) |
---|
1518 | n/a | self.addCleanup(close_transport, transport) |
---|
1519 | n/a | return transport |
---|
1520 | n/a | |
---|
1521 | n/a | def test_read_ready(self): |
---|
1522 | n/a | transport = self.datagram_transport() |
---|
1523 | n/a | |
---|
1524 | n/a | self.sock.recvfrom.return_value = (b'data', ('0.0.0.0', 1234)) |
---|
1525 | n/a | transport._read_ready() |
---|
1526 | n/a | |
---|
1527 | n/a | self.protocol.datagram_received.assert_called_with( |
---|
1528 | n/a | b'data', ('0.0.0.0', 1234)) |
---|
1529 | n/a | |
---|
1530 | n/a | def test_read_ready_tryagain(self): |
---|
1531 | n/a | transport = self.datagram_transport() |
---|
1532 | n/a | |
---|
1533 | n/a | self.sock.recvfrom.side_effect = BlockingIOError |
---|
1534 | n/a | transport._fatal_error = mock.Mock() |
---|
1535 | n/a | transport._read_ready() |
---|
1536 | n/a | |
---|
1537 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
1538 | n/a | |
---|
1539 | n/a | def test_read_ready_err(self): |
---|
1540 | n/a | transport = self.datagram_transport() |
---|
1541 | n/a | |
---|
1542 | n/a | err = self.sock.recvfrom.side_effect = RuntimeError() |
---|
1543 | n/a | transport._fatal_error = mock.Mock() |
---|
1544 | n/a | transport._read_ready() |
---|
1545 | n/a | |
---|
1546 | n/a | transport._fatal_error.assert_called_with( |
---|
1547 | n/a | err, |
---|
1548 | n/a | 'Fatal read error on datagram transport') |
---|
1549 | n/a | |
---|
1550 | n/a | def test_read_ready_oserr(self): |
---|
1551 | n/a | transport = self.datagram_transport() |
---|
1552 | n/a | |
---|
1553 | n/a | err = self.sock.recvfrom.side_effect = OSError() |
---|
1554 | n/a | transport._fatal_error = mock.Mock() |
---|
1555 | n/a | transport._read_ready() |
---|
1556 | n/a | |
---|
1557 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
1558 | n/a | self.protocol.error_received.assert_called_with(err) |
---|
1559 | n/a | |
---|
1560 | n/a | def test_sendto(self): |
---|
1561 | n/a | data = b'data' |
---|
1562 | n/a | transport = self.datagram_transport() |
---|
1563 | n/a | transport.sendto(data, ('0.0.0.0', 1234)) |
---|
1564 | n/a | self.assertTrue(self.sock.sendto.called) |
---|
1565 | n/a | self.assertEqual( |
---|
1566 | n/a | self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234))) |
---|
1567 | n/a | |
---|
1568 | n/a | def test_sendto_bytearray(self): |
---|
1569 | n/a | data = bytearray(b'data') |
---|
1570 | n/a | transport = self.datagram_transport() |
---|
1571 | n/a | transport.sendto(data, ('0.0.0.0', 1234)) |
---|
1572 | n/a | self.assertTrue(self.sock.sendto.called) |
---|
1573 | n/a | self.assertEqual( |
---|
1574 | n/a | self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234))) |
---|
1575 | n/a | |
---|
1576 | n/a | def test_sendto_memoryview(self): |
---|
1577 | n/a | data = memoryview(b'data') |
---|
1578 | n/a | transport = self.datagram_transport() |
---|
1579 | n/a | transport.sendto(data, ('0.0.0.0', 1234)) |
---|
1580 | n/a | self.assertTrue(self.sock.sendto.called) |
---|
1581 | n/a | self.assertEqual( |
---|
1582 | n/a | self.sock.sendto.call_args[0], (data, ('0.0.0.0', 1234))) |
---|
1583 | n/a | |
---|
1584 | n/a | def test_sendto_no_data(self): |
---|
1585 | n/a | transport = self.datagram_transport() |
---|
1586 | n/a | transport._buffer.append((b'data', ('0.0.0.0', 12345))) |
---|
1587 | n/a | transport.sendto(b'', ()) |
---|
1588 | n/a | self.assertFalse(self.sock.sendto.called) |
---|
1589 | n/a | self.assertEqual( |
---|
1590 | n/a | [(b'data', ('0.0.0.0', 12345))], list(transport._buffer)) |
---|
1591 | n/a | |
---|
1592 | n/a | def test_sendto_buffer(self): |
---|
1593 | n/a | transport = self.datagram_transport() |
---|
1594 | n/a | transport._buffer.append((b'data1', ('0.0.0.0', 12345))) |
---|
1595 | n/a | transport.sendto(b'data2', ('0.0.0.0', 12345)) |
---|
1596 | n/a | self.assertFalse(self.sock.sendto.called) |
---|
1597 | n/a | self.assertEqual( |
---|
1598 | n/a | [(b'data1', ('0.0.0.0', 12345)), |
---|
1599 | n/a | (b'data2', ('0.0.0.0', 12345))], |
---|
1600 | n/a | list(transport._buffer)) |
---|
1601 | n/a | |
---|
1602 | n/a | def test_sendto_buffer_bytearray(self): |
---|
1603 | n/a | data2 = bytearray(b'data2') |
---|
1604 | n/a | transport = self.datagram_transport() |
---|
1605 | n/a | transport._buffer.append((b'data1', ('0.0.0.0', 12345))) |
---|
1606 | n/a | transport.sendto(data2, ('0.0.0.0', 12345)) |
---|
1607 | n/a | self.assertFalse(self.sock.sendto.called) |
---|
1608 | n/a | self.assertEqual( |
---|
1609 | n/a | [(b'data1', ('0.0.0.0', 12345)), |
---|
1610 | n/a | (b'data2', ('0.0.0.0', 12345))], |
---|
1611 | n/a | list(transport._buffer)) |
---|
1612 | n/a | self.assertIsInstance(transport._buffer[1][0], bytes) |
---|
1613 | n/a | |
---|
1614 | n/a | def test_sendto_buffer_memoryview(self): |
---|
1615 | n/a | data2 = memoryview(b'data2') |
---|
1616 | n/a | transport = self.datagram_transport() |
---|
1617 | n/a | transport._buffer.append((b'data1', ('0.0.0.0', 12345))) |
---|
1618 | n/a | transport.sendto(data2, ('0.0.0.0', 12345)) |
---|
1619 | n/a | self.assertFalse(self.sock.sendto.called) |
---|
1620 | n/a | self.assertEqual( |
---|
1621 | n/a | [(b'data1', ('0.0.0.0', 12345)), |
---|
1622 | n/a | (b'data2', ('0.0.0.0', 12345))], |
---|
1623 | n/a | list(transport._buffer)) |
---|
1624 | n/a | self.assertIsInstance(transport._buffer[1][0], bytes) |
---|
1625 | n/a | |
---|
1626 | n/a | def test_sendto_tryagain(self): |
---|
1627 | n/a | data = b'data' |
---|
1628 | n/a | |
---|
1629 | n/a | self.sock.sendto.side_effect = BlockingIOError |
---|
1630 | n/a | |
---|
1631 | n/a | transport = self.datagram_transport() |
---|
1632 | n/a | transport.sendto(data, ('0.0.0.0', 12345)) |
---|
1633 | n/a | |
---|
1634 | n/a | self.loop.assert_writer(7, transport._sendto_ready) |
---|
1635 | n/a | self.assertEqual( |
---|
1636 | n/a | [(b'data', ('0.0.0.0', 12345))], list(transport._buffer)) |
---|
1637 | n/a | |
---|
1638 | n/a | @mock.patch('asyncio.selector_events.logger') |
---|
1639 | n/a | def test_sendto_exception(self, m_log): |
---|
1640 | n/a | data = b'data' |
---|
1641 | n/a | err = self.sock.sendto.side_effect = RuntimeError() |
---|
1642 | n/a | |
---|
1643 | n/a | transport = self.datagram_transport() |
---|
1644 | n/a | transport._fatal_error = mock.Mock() |
---|
1645 | n/a | transport.sendto(data, ()) |
---|
1646 | n/a | |
---|
1647 | n/a | self.assertTrue(transport._fatal_error.called) |
---|
1648 | n/a | transport._fatal_error.assert_called_with( |
---|
1649 | n/a | err, |
---|
1650 | n/a | 'Fatal write error on datagram transport') |
---|
1651 | n/a | transport._conn_lost = 1 |
---|
1652 | n/a | |
---|
1653 | n/a | transport._address = ('123',) |
---|
1654 | n/a | transport.sendto(data) |
---|
1655 | n/a | transport.sendto(data) |
---|
1656 | n/a | transport.sendto(data) |
---|
1657 | n/a | transport.sendto(data) |
---|
1658 | n/a | transport.sendto(data) |
---|
1659 | n/a | m_log.warning.assert_called_with('socket.send() raised exception.') |
---|
1660 | n/a | |
---|
1661 | n/a | def test_sendto_error_received(self): |
---|
1662 | n/a | data = b'data' |
---|
1663 | n/a | |
---|
1664 | n/a | self.sock.sendto.side_effect = ConnectionRefusedError |
---|
1665 | n/a | |
---|
1666 | n/a | transport = self.datagram_transport() |
---|
1667 | n/a | transport._fatal_error = mock.Mock() |
---|
1668 | n/a | transport.sendto(data, ()) |
---|
1669 | n/a | |
---|
1670 | n/a | self.assertEqual(transport._conn_lost, 0) |
---|
1671 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
1672 | n/a | |
---|
1673 | n/a | def test_sendto_error_received_connected(self): |
---|
1674 | n/a | data = b'data' |
---|
1675 | n/a | |
---|
1676 | n/a | self.sock.send.side_effect = ConnectionRefusedError |
---|
1677 | n/a | |
---|
1678 | n/a | transport = self.datagram_transport(address=('0.0.0.0', 1)) |
---|
1679 | n/a | transport._fatal_error = mock.Mock() |
---|
1680 | n/a | transport.sendto(data) |
---|
1681 | n/a | |
---|
1682 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
1683 | n/a | self.assertTrue(self.protocol.error_received.called) |
---|
1684 | n/a | |
---|
1685 | n/a | def test_sendto_str(self): |
---|
1686 | n/a | transport = self.datagram_transport() |
---|
1687 | n/a | self.assertRaises(TypeError, transport.sendto, 'str', ()) |
---|
1688 | n/a | |
---|
1689 | n/a | def test_sendto_connected_addr(self): |
---|
1690 | n/a | transport = self.datagram_transport(address=('0.0.0.0', 1)) |
---|
1691 | n/a | self.assertRaises( |
---|
1692 | n/a | ValueError, transport.sendto, b'str', ('0.0.0.0', 2)) |
---|
1693 | n/a | |
---|
1694 | n/a | def test_sendto_closing(self): |
---|
1695 | n/a | transport = self.datagram_transport(address=(1,)) |
---|
1696 | n/a | transport.close() |
---|
1697 | n/a | self.assertEqual(transport._conn_lost, 1) |
---|
1698 | n/a | transport.sendto(b'data', (1,)) |
---|
1699 | n/a | self.assertEqual(transport._conn_lost, 2) |
---|
1700 | n/a | |
---|
1701 | n/a | def test_sendto_ready(self): |
---|
1702 | n/a | data = b'data' |
---|
1703 | n/a | self.sock.sendto.return_value = len(data) |
---|
1704 | n/a | |
---|
1705 | n/a | transport = self.datagram_transport() |
---|
1706 | n/a | transport._buffer.append((data, ('0.0.0.0', 12345))) |
---|
1707 | n/a | self.loop._add_writer(7, transport._sendto_ready) |
---|
1708 | n/a | transport._sendto_ready() |
---|
1709 | n/a | self.assertTrue(self.sock.sendto.called) |
---|
1710 | n/a | self.assertEqual( |
---|
1711 | n/a | self.sock.sendto.call_args[0], (data, ('0.0.0.0', 12345))) |
---|
1712 | n/a | self.assertFalse(self.loop.writers) |
---|
1713 | n/a | |
---|
1714 | n/a | def test_sendto_ready_closing(self): |
---|
1715 | n/a | data = b'data' |
---|
1716 | n/a | self.sock.send.return_value = len(data) |
---|
1717 | n/a | |
---|
1718 | n/a | transport = self.datagram_transport() |
---|
1719 | n/a | transport._closing = True |
---|
1720 | n/a | transport._buffer.append((data, ())) |
---|
1721 | n/a | self.loop._add_writer(7, transport._sendto_ready) |
---|
1722 | n/a | transport._sendto_ready() |
---|
1723 | n/a | self.sock.sendto.assert_called_with(data, ()) |
---|
1724 | n/a | self.assertFalse(self.loop.writers) |
---|
1725 | n/a | self.sock.close.assert_called_with() |
---|
1726 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
1727 | n/a | |
---|
1728 | n/a | def test_sendto_ready_no_data(self): |
---|
1729 | n/a | transport = self.datagram_transport() |
---|
1730 | n/a | self.loop._add_writer(7, transport._sendto_ready) |
---|
1731 | n/a | transport._sendto_ready() |
---|
1732 | n/a | self.assertFalse(self.sock.sendto.called) |
---|
1733 | n/a | self.assertFalse(self.loop.writers) |
---|
1734 | n/a | |
---|
1735 | n/a | def test_sendto_ready_tryagain(self): |
---|
1736 | n/a | self.sock.sendto.side_effect = BlockingIOError |
---|
1737 | n/a | |
---|
1738 | n/a | transport = self.datagram_transport() |
---|
1739 | n/a | transport._buffer.extend([(b'data1', ()), (b'data2', ())]) |
---|
1740 | n/a | self.loop._add_writer(7, transport._sendto_ready) |
---|
1741 | n/a | transport._sendto_ready() |
---|
1742 | n/a | |
---|
1743 | n/a | self.loop.assert_writer(7, transport._sendto_ready) |
---|
1744 | n/a | self.assertEqual( |
---|
1745 | n/a | [(b'data1', ()), (b'data2', ())], |
---|
1746 | n/a | list(transport._buffer)) |
---|
1747 | n/a | |
---|
1748 | n/a | def test_sendto_ready_exception(self): |
---|
1749 | n/a | err = self.sock.sendto.side_effect = RuntimeError() |
---|
1750 | n/a | |
---|
1751 | n/a | transport = self.datagram_transport() |
---|
1752 | n/a | transport._fatal_error = mock.Mock() |
---|
1753 | n/a | transport._buffer.append((b'data', ())) |
---|
1754 | n/a | transport._sendto_ready() |
---|
1755 | n/a | |
---|
1756 | n/a | transport._fatal_error.assert_called_with( |
---|
1757 | n/a | err, |
---|
1758 | n/a | 'Fatal write error on datagram transport') |
---|
1759 | n/a | |
---|
1760 | n/a | def test_sendto_ready_error_received(self): |
---|
1761 | n/a | self.sock.sendto.side_effect = ConnectionRefusedError |
---|
1762 | n/a | |
---|
1763 | n/a | transport = self.datagram_transport() |
---|
1764 | n/a | transport._fatal_error = mock.Mock() |
---|
1765 | n/a | transport._buffer.append((b'data', ())) |
---|
1766 | n/a | transport._sendto_ready() |
---|
1767 | n/a | |
---|
1768 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
1769 | n/a | |
---|
1770 | n/a | def test_sendto_ready_error_received_connection(self): |
---|
1771 | n/a | self.sock.send.side_effect = ConnectionRefusedError |
---|
1772 | n/a | |
---|
1773 | n/a | transport = self.datagram_transport(address=('0.0.0.0', 1)) |
---|
1774 | n/a | transport._fatal_error = mock.Mock() |
---|
1775 | n/a | transport._buffer.append((b'data', ())) |
---|
1776 | n/a | transport._sendto_ready() |
---|
1777 | n/a | |
---|
1778 | n/a | self.assertFalse(transport._fatal_error.called) |
---|
1779 | n/a | self.assertTrue(self.protocol.error_received.called) |
---|
1780 | n/a | |
---|
1781 | n/a | @mock.patch('asyncio.base_events.logger.error') |
---|
1782 | n/a | def test_fatal_error_connected(self, m_exc): |
---|
1783 | n/a | transport = self.datagram_transport(address=('0.0.0.0', 1)) |
---|
1784 | n/a | err = ConnectionRefusedError() |
---|
1785 | n/a | transport._fatal_error(err) |
---|
1786 | n/a | self.assertFalse(self.protocol.error_received.called) |
---|
1787 | n/a | m_exc.assert_called_with( |
---|
1788 | n/a | test_utils.MockPattern( |
---|
1789 | n/a | 'Fatal error on transport\nprotocol:.*\ntransport:.*'), |
---|
1790 | n/a | exc_info=(ConnectionRefusedError, MOCK_ANY, MOCK_ANY)) |
---|
1791 | n/a | |
---|
1792 | n/a | if __name__ == '__main__': |
---|
1793 | n/a | unittest.main() |
---|