| 1 |
import socket |
| 2 |
import asyncio |
| 3 |
import sys |
| 4 |
import unittest |
| 5 |
|
| 6 |
from asyncio import proactor_events |
| 7 |
from itertools import cycle, islice |
| 8 |
from unittest.mock import Mock |
| 9 |
from test.test_asyncio import utils as test_utils |
| 10 |
from test import support |
| 11 |
from test.support import socket_helper |
| 12 |
|
| 13 |
if socket_helper.tcp_blackhole(): |
| 14 |
raise unittest.SkipTest('Not relevant to ProactorEventLoop') |
| 15 |
|
| 16 |
|
| 17 |
def tearDownModule(): |
| 18 |
asyncio.events._set_event_loop_policy(None) |
| 19 |
|
| 20 |
|
| 21 |
class MyProto(asyncio.Protocol): |
| 22 |
connected = None |
| 23 |
done = None |
| 24 |
|
| 25 |
def __init__(self, loop=None): |
| 26 |
self.transport = None |
| 27 |
self.state = 'INITIAL' |
| 28 |
self.nbytes = 0 |
| 29 |
if loop is not None: |
| 30 |
self.connected = loop.create_future() |
| 31 |
self.done = loop.create_future() |
| 32 |
|
| 33 |
def _assert_state(self, *expected): |
| 34 |
if self.state not in expected: |
| 35 |
raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') |
| 36 |
|
| 37 |
def connection_made(self, transport): |
| 38 |
self.transport = transport |
| 39 |
self._assert_state('INITIAL') |
| 40 |
self.state = 'CONNECTED' |
| 41 |
if self.connected: |
| 42 |
self.connected.set_result(None) |
| 43 |
transport.write(b'GET / HTTP/1.0\r\nHost: example.com\r\n\r\n') |
| 44 |
|
| 45 |
def data_received(self, data): |
| 46 |
self._assert_state('CONNECTED') |
| 47 |
self.nbytes += len(data) |
| 48 |
|
| 49 |
def eof_received(self): |
| 50 |
self._assert_state('CONNECTED') |
| 51 |
self.state = 'EOF' |
| 52 |
|
| 53 |
def connection_lost(self, exc): |
| 54 |
self._assert_state('CONNECTED', 'EOF') |
| 55 |
self.state = 'CLOSED' |
| 56 |
if self.done: |
| 57 |
self.done.set_result(None) |
| 58 |
|
| 59 |
|
| 60 |
class BaseSockTestsMixin: |
| 61 |
|
| 62 |
def create_event_loop(self): |
| 63 |
raise NotImplementedError |
| 64 |
|
| 65 |
def setUp(self): |
| 66 |
self.loop = self.create_event_loop() |
| 67 |
self.set_event_loop(self.loop) |
| 68 |
super().setUp() |
| 69 |
|
| 70 |
def tearDown(self): |
| 71 |
# just in case if we have transport close callbacks |
| 72 |
if not self.loop.is_closed(): |
| 73 |
test_utils.run_briefly(self.loop) |
| 74 |
|
| 75 |
self.doCleanups() |
| 76 |
support.gc_collect() |
| 77 |
super().tearDown() |
| 78 |
|
| 79 |
def _basetest_sock_client_ops(self, httpd, sock): |
| 80 |
if not isinstance(self.loop, proactor_events.BaseProactorEventLoop): |
| 81 |
# in debug mode, socket operations must fail |
| 82 |
# if the socket is not in blocking mode |
| 83 |
self.loop.set_debug(True) |
| 84 |
sock.setblocking(True) |
| 85 |
with self.assertRaises(ValueError): |
| 86 |
self.loop.run_until_complete( |
| 87 |
self.loop.sock_connect(sock, httpd.address)) |
| 88 |
with self.assertRaises(ValueError): |
| 89 |
self.loop.run_until_complete( |
| 90 |
self.loop.sock_sendall(sock, b'GET / HTTP/1.0\r\n\r\n')) |
| 91 |
with self.assertRaises(ValueError): |
| 92 |
self.loop.run_until_complete( |
| 93 |
self.loop.sock_recv(sock, 1024)) |
| 94 |
with self.assertRaises(ValueError): |
| 95 |
self.loop.run_until_complete( |
| 96 |
self.loop.sock_recv_into(sock, bytearray())) |
| 97 |
with self.assertRaises(ValueError): |
| 98 |
self.loop.run_until_complete( |
| 99 |
self.loop.sock_accept(sock)) |
| 100 |
|
| 101 |
# test in non-blocking mode |
| 102 |
sock.setblocking(False) |
| 103 |
self.loop.run_until_complete( |
| 104 |
self.loop.sock_connect(sock, httpd.address)) |
| 105 |
self.loop.run_until_complete( |
| 106 |
self.loop.sock_sendall(sock, b'GET / HTTP/1.0\r\n\r\n')) |
| 107 |
data = self.loop.run_until_complete( |
| 108 |
self.loop.sock_recv(sock, 1024)) |
| 109 |
# consume data |
| 110 |
self.loop.run_until_complete( |
| 111 |
self.loop.sock_recv(sock, 1024)) |
| 112 |
sock.close() |
| 113 |
self.assertStartsWith(data, b'HTTP/1.0 200 OK') |
| 114 |
|
| 115 |
def _basetest_sock_recv_into(self, httpd, sock): |
| 116 |
# same as _basetest_sock_client_ops, but using sock_recv_into |
| 117 |
sock.setblocking(False) |
| 118 |
self.loop.run_until_complete( |
| 119 |
self.loop.sock_connect(sock, httpd.address)) |
| 120 |
self.loop.run_until_complete( |
| 121 |
self.loop.sock_sendall(sock, b'GET / HTTP/1.0\r\n\r\n')) |
| 122 |
data = bytearray(1024) |
| 123 |
with memoryview(data) as buf: |
| 124 |
nbytes = self.loop.run_until_complete( |
| 125 |
self.loop.sock_recv_into(sock, buf[:1024])) |
| 126 |
# consume data |
| 127 |
self.loop.run_until_complete( |
| 128 |
self.loop.sock_recv_into(sock, buf[nbytes:])) |
| 129 |
sock.close() |
| 130 |
self.assertStartsWith(data, b'HTTP/1.0 200 OK') |
| 131 |
|
| 132 |
def test_sock_client_ops(self): |
| 133 |
with test_utils.run_test_server() as httpd: |
| 134 |
sock = socket.socket() |
| 135 |
self._basetest_sock_client_ops(httpd, sock) |
| 136 |
sock = socket.socket() |
| 137 |
self._basetest_sock_recv_into(httpd, sock) |
| 138 |
|
| 139 |
async def _basetest_sock_recv_racing(self, httpd, sock): |
| 140 |
sock.setblocking(False) |
| 141 |
await self.loop.sock_connect(sock, httpd.address) |
| 142 |
|
| 143 |
task = asyncio.create_task(self.loop.sock_recv(sock, 1024)) |
| 144 |
await asyncio.sleep(0) |
| 145 |
task.cancel() |
| 146 |
|
| 147 |
asyncio.create_task( |
| 148 |
self.loop.sock_sendall(sock, b'GET / HTTP/1.0\r\n\r\n')) |
| 149 |
data = await self.loop.sock_recv(sock, 1024) |
| 150 |
# consume data |
| 151 |
await self.loop.sock_recv(sock, 1024) |
| 152 |
|
| 153 |
self.assertStartsWith(data, b'HTTP/1.0 200 OK') |
| 154 |
|
| 155 |
async def _basetest_sock_recv_into_racing(self, httpd, sock): |
| 156 |
sock.setblocking(False) |
| 157 |
await self.loop.sock_connect(sock, httpd.address) |
| 158 |
|
| 159 |
data = bytearray(1024) |
| 160 |
with memoryview(data) as buf: |
| 161 |
task = asyncio.create_task( |
| 162 |
self.loop.sock_recv_into(sock, buf[:1024])) |
| 163 |
await asyncio.sleep(0) |
| 164 |
task.cancel() |
| 165 |
|
| 166 |
task = asyncio.create_task( |
| 167 |
self.loop.sock_sendall(sock, b'GET / HTTP/1.0\r\n\r\n')) |
| 168 |
nbytes = await self.loop.sock_recv_into(sock, buf[:1024]) |
| 169 |
# consume data |
| 170 |
await self.loop.sock_recv_into(sock, buf[nbytes:]) |
| 171 |
self.assertStartsWith(data, b'HTTP/1.0 200 OK') |
| 172 |
|
| 173 |
await task |
| 174 |
|
| 175 |
async def _basetest_sock_send_racing(self, listener, sock): |
| 176 |
listener.bind(('127.0.0.1', 0)) |
| 177 |
listener.listen(1) |
| 178 |
|
| 179 |
# make connection |
| 180 |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024) |
| 181 |
sock.setblocking(False) |
| 182 |
task = asyncio.create_task( |
| 183 |
self.loop.sock_connect(sock, listener.getsockname())) |
| 184 |
await asyncio.sleep(0) |
| 185 |
server = listener.accept()[0] |
| 186 |
server.setblocking(False) |
| 187 |
|
| 188 |
with server: |
| 189 |
await task |
| 190 |
|
| 191 |
# fill the buffer until sending 5 chars would block |
| 192 |
size = 8192 |
| 193 |
while size >= 4: |
| 194 |
with self.assertRaises(BlockingIOError): |
| 195 |
while True: |
| 196 |
sock.send(b' ' * size) |
| 197 |
size = int(size / 2) |
| 198 |
|
| 199 |
# cancel a blocked sock_sendall |
| 200 |
task = asyncio.create_task( |
| 201 |
self.loop.sock_sendall(sock, b'hello')) |
| 202 |
await asyncio.sleep(0) |
| 203 |
task.cancel() |
| 204 |
|
| 205 |
# receive everything that is not a space |
| 206 |
async def recv_all(): |
| 207 |
rv = b'' |
| 208 |
while True: |
| 209 |
buf = await self.loop.sock_recv(server, 8192) |
| 210 |
if not buf: |
| 211 |
return rv |
| 212 |
rv += buf.strip() |
| 213 |
task = asyncio.create_task(recv_all()) |
| 214 |
|
| 215 |
# immediately make another sock_sendall call |
| 216 |
await self.loop.sock_sendall(sock, b'world') |
| 217 |
sock.shutdown(socket.SHUT_WR) |
| 218 |
data = await task |
| 219 |
# ProactorEventLoop could deliver hello, so endswith is necessary |
| 220 |
self.assertEndsWith(data, b'world') |
| 221 |
|
| 222 |
# After the first connect attempt before the listener is ready, |
| 223 |
# the socket needs time to "recover" to make the next connect call. |
| 224 |
# On Linux, a second retry will do. On Windows, the waiting time is |
| 225 |
# unpredictable; and on FreeBSD the socket may never come back |
| 226 |
# because it's a loopback address. Here we'll just retry for a few |
| 227 |
# times, and have to skip the test if it's not working. See also: |
| 228 |
# https://stackoverflow.com/a/54437602/3316267 |
| 229 |
# https://lists.freebsd.org/pipermail/freebsd-current/2005-May/049876.html |
| 230 |
async def _basetest_sock_connect_racing(self, listener, sock): |
| 231 |
listener.bind(('127.0.0.1', 0)) |
| 232 |
addr = listener.getsockname() |
| 233 |
sock.setblocking(False) |
| 234 |
|
| 235 |
task = asyncio.create_task(self.loop.sock_connect(sock, addr)) |
| 236 |
await asyncio.sleep(0) |
| 237 |
task.cancel() |
| 238 |
|
| 239 |
listener.listen(1) |
| 240 |
|
| 241 |
skip_reason = "Max retries reached" |
| 242 |
for i in range(128): |
| 243 |
try: |
| 244 |
await self.loop.sock_connect(sock, addr) |
| 245 |
except ConnectionRefusedError as e: |
| 246 |
skip_reason = e |
| 247 |
except OSError as e: |
| 248 |
skip_reason = e |
| 249 |
|
| 250 |
# Retry only for this error: |
| 251 |
# [WinError 10022] An invalid argument was supplied |
| 252 |
if getattr(e, 'winerror', 0) != 10022: |
| 253 |
break |
| 254 |
else: |
| 255 |
# success |
| 256 |
return |
| 257 |
|
| 258 |
self.skipTest(skip_reason) |
| 259 |
|
| 260 |
def test_sock_client_racing(self): |
| 261 |
with test_utils.run_test_server() as httpd: |
| 262 |
sock = socket.socket() |
| 263 |
with sock: |
| 264 |
self.loop.run_until_complete(asyncio.wait_for( |
| 265 |
self._basetest_sock_recv_racing(httpd, sock), 10)) |
| 266 |
sock = socket.socket() |
| 267 |
with sock: |
| 268 |
self.loop.run_until_complete(asyncio.wait_for( |
| 269 |
self._basetest_sock_recv_into_racing(httpd, sock), 10)) |
| 270 |
listener = socket.socket() |
| 271 |
sock = socket.socket() |
| 272 |
with listener, sock: |
| 273 |
self.loop.run_until_complete(asyncio.wait_for( |
| 274 |
self._basetest_sock_send_racing(listener, sock), 10)) |
| 275 |
|
| 276 |
def test_sock_client_connect_racing(self): |
| 277 |
listener = socket.socket() |
| 278 |
sock = socket.socket() |
| 279 |
with listener, sock: |
| 280 |
self.loop.run_until_complete(asyncio.wait_for( |
| 281 |
self._basetest_sock_connect_racing(listener, sock), 10)) |
| 282 |
|
| 283 |
async def _basetest_huge_content(self, address): |
| 284 |
sock = socket.socket() |
| 285 |
sock.setblocking(False) |
| 286 |
DATA_SIZE = 10_000_00 |
| 287 |
|
| 288 |
chunk = b'0123456789' * (DATA_SIZE // 10) |
| 289 |
|
| 290 |
await self.loop.sock_connect(sock, address) |
| 291 |
await self.loop.sock_sendall(sock, |
| 292 |
(b'POST /loop HTTP/1.0\r\n' + |
| 293 |
b'Content-Length: %d\r\n' % DATA_SIZE + |
| 294 |
b'\r\n')) |
| 295 |
|
| 296 |
task = asyncio.create_task(self.loop.sock_sendall(sock, chunk)) |
| 297 |
|
| 298 |
data = await self.loop.sock_recv(sock, DATA_SIZE) |
| 299 |
# HTTP headers size is less than MTU, |
| 300 |
# they are sent by the first packet always |
| 301 |
self.assertStartsWith(data, b'HTTP/1.0 200 OK') |
| 302 |
while data.find(b'\r\n\r\n') == -1: |
| 303 |
data += await self.loop.sock_recv(sock, DATA_SIZE) |
| 304 |
# Strip headers |
| 305 |
headers = data[:data.index(b'\r\n\r\n') + 4] |
| 306 |
data = data[len(headers):] |
| 307 |
|
| 308 |
size = DATA_SIZE |
| 309 |
checker = cycle(b'0123456789') |
| 310 |
|
| 311 |
expected = bytes(islice(checker, len(data))) |
| 312 |
self.assertEqual(data, expected) |
| 313 |
size -= len(data) |
| 314 |
|
| 315 |
while True: |
| 316 |
data = await self.loop.sock_recv(sock, DATA_SIZE) |
| 317 |
if not data: |
| 318 |
break |
| 319 |
expected = bytes(islice(checker, len(data))) |
| 320 |
self.assertEqual(data, expected) |
| 321 |
size -= len(data) |
| 322 |
self.assertEqual(size, 0) |
| 323 |
|
| 324 |
await task |
| 325 |
sock.close() |
| 326 |
|
| 327 |
def test_huge_content(self): |
| 328 |
with test_utils.run_test_server() as httpd: |
| 329 |
self.loop.run_until_complete( |
| 330 |
self._basetest_huge_content(httpd.address)) |
| 331 |
|
| 332 |
async def _basetest_huge_content_recvinto(self, address): |
| 333 |
sock = socket.socket() |
| 334 |
sock.setblocking(False) |
| 335 |
DATA_SIZE = 10_000_00 |
| 336 |
|
| 337 |
chunk = b'0123456789' * (DATA_SIZE // 10) |
| 338 |
|
| 339 |
await self.loop.sock_connect(sock, address) |
| 340 |
await self.loop.sock_sendall(sock, |
| 341 |
(b'POST /loop HTTP/1.0\r\n' + |
| 342 |
b'Content-Length: %d\r\n' % DATA_SIZE + |
| 343 |
b'\r\n')) |
| 344 |
|
| 345 |
task = asyncio.create_task(self.loop.sock_sendall(sock, chunk)) |
| 346 |
|
| 347 |
array = bytearray(DATA_SIZE) |
| 348 |
buf = memoryview(array) |
| 349 |
|
| 350 |
nbytes = await self.loop.sock_recv_into(sock, buf) |
| 351 |
data = bytes(buf[:nbytes]) |
| 352 |
# HTTP headers size is less than MTU, |
| 353 |
# they are sent by the first packet always |
| 354 |
self.assertStartsWith(data, b'HTTP/1.0 200 OK') |
| 355 |
while data.find(b'\r\n\r\n') == -1: |
| 356 |
nbytes = await self.loop.sock_recv_into(sock, buf) |
| 357 |
data = bytes(buf[:nbytes]) |
| 358 |
# Strip headers |
| 359 |
headers = data[:data.index(b'\r\n\r\n') + 4] |
| 360 |
data = data[len(headers):] |
| 361 |
|
| 362 |
size = DATA_SIZE |
| 363 |
checker = cycle(b'0123456789') |
| 364 |
|
| 365 |
expected = bytes(islice(checker, len(data))) |
| 366 |
self.assertEqual(data, expected) |
| 367 |
size -= len(data) |
| 368 |
|
| 369 |
while True: |
| 370 |
nbytes = await self.loop.sock_recv_into(sock, buf) |
| 371 |
data = buf[:nbytes] |
| 372 |
if not data: |
| 373 |
break |
| 374 |
expected = bytes(islice(checker, len(data))) |
| 375 |
self.assertEqual(data, expected) |
| 376 |
size -= len(data) |
| 377 |
self.assertEqual(size, 0) |
| 378 |
|
| 379 |
await task |
| 380 |
sock.close() |
| 381 |
|
| 382 |
def test_huge_content_recvinto(self): |
| 383 |
with test_utils.run_test_server() as httpd: |
| 384 |
self.loop.run_until_complete( |
| 385 |
self._basetest_huge_content_recvinto(httpd.address)) |
| 386 |
|
| 387 |
async def _basetest_datagram_recvfrom(self, server_address): |
| 388 |
# Happy path, sock.sendto() returns immediately |
| 389 |
data = b'\x01' * 4096 |
| 390 |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: |
| 391 |
sock.setblocking(False) |
| 392 |
await self.loop.sock_sendto(sock, data, server_address) |
| 393 |
received_data, from_addr = await self.loop.sock_recvfrom( |
| 394 |
sock, 4096) |
| 395 |
self.assertEqual(received_data, data) |
| 396 |
self.assertEqual(from_addr, server_address) |
| 397 |
|
| 398 |
def test_recvfrom(self): |
| 399 |
with test_utils.run_udp_echo_server() as server_address: |
| 400 |
self.loop.run_until_complete( |
| 401 |
self._basetest_datagram_recvfrom(server_address)) |
| 402 |
|
| 403 |
async def _basetest_datagram_recvfrom_into(self, server_address): |
| 404 |
# Happy path, sock.sendto() returns immediately |
| 405 |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: |
| 406 |
sock.setblocking(False) |
| 407 |
|
| 408 |
buf = bytearray(4096) |
| 409 |
data = b'\x01' * 4096 |
| 410 |
await self.loop.sock_sendto(sock, data, server_address) |
| 411 |
num_bytes, from_addr = await self.loop.sock_recvfrom_into( |
| 412 |
sock, buf) |
| 413 |
self.assertEqual(num_bytes, 4096) |
| 414 |
self.assertEqual(buf, data) |
| 415 |
self.assertEqual(from_addr, server_address) |
| 416 |
|
| 417 |
buf = bytearray(8192) |
| 418 |
await self.loop.sock_sendto(sock, data, server_address) |
| 419 |
num_bytes, from_addr = await self.loop.sock_recvfrom_into( |
| 420 |
sock, buf, 4096) |
| 421 |
self.assertEqual(num_bytes, 4096) |
| 422 |
self.assertEqual(buf[:4096], data[:4096]) |
| 423 |
self.assertEqual(from_addr, server_address) |
| 424 |
|
| 425 |
def test_recvfrom_into(self): |
| 426 |
with test_utils.run_udp_echo_server() as server_address: |
| 427 |
self.loop.run_until_complete( |
| 428 |
self._basetest_datagram_recvfrom_into(server_address)) |
| 429 |
|
| 430 |
async def _basetest_datagram_recvfrom_into_wrong_size(self, server_address): |
| 431 |
# Call sock_sendto() with a size larger than the buffer |
| 432 |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: |
| 433 |
sock.setblocking(False) |
| 434 |
|
| 435 |
buf = bytearray(5000) |
| 436 |
data = b'\x01' * 4096 |
| 437 |
wrong_size = len(buf) + 1 |
| 438 |
await self.loop.sock_sendto(sock, data, server_address) |
| 439 |
with self.assertRaises(ValueError): |
| 440 |
await self.loop.sock_recvfrom_into( |
| 441 |
sock, buf, wrong_size) |
| 442 |
|
| 443 |
size, addr = await self.loop.sock_recvfrom_into(sock, buf) |
| 444 |
self.assertEqual(buf[:size], data) |
| 445 |
|
| 446 |
def test_recvfrom_into_wrong_size(self): |
| 447 |
with test_utils.run_udp_echo_server() as server_address: |
| 448 |
self.loop.run_until_complete( |
| 449 |
self._basetest_datagram_recvfrom_into_wrong_size(server_address)) |
| 450 |
|
| 451 |
async def _basetest_datagram_sendto_blocking(self, server_address): |
| 452 |
# Sad path, sock.sendto() raises BlockingIOError |
| 453 |
# This involves patching sock.sendto() to raise BlockingIOError but |
| 454 |
# sendto() is not used by the proactor event loop |
| 455 |
data = b'\x01' * 4096 |
| 456 |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: |
| 457 |
sock.setblocking(False) |
| 458 |
mock_sock = Mock(sock) |
| 459 |
mock_sock.gettimeout = sock.gettimeout |
| 460 |
mock_sock.sendto.configure_mock(side_effect=BlockingIOError) |
| 461 |
mock_sock.fileno = sock.fileno |
| 462 |
self.loop.call_soon( |
| 463 |
lambda: setattr(mock_sock, 'sendto', sock.sendto) |
| 464 |
) |
| 465 |
await self.loop.sock_sendto(mock_sock, data, server_address) |
| 466 |
|
| 467 |
received_data, from_addr = await self.loop.sock_recvfrom( |
| 468 |
sock, 4096) |
| 469 |
self.assertEqual(received_data, data) |
| 470 |
self.assertEqual(from_addr, server_address) |
| 471 |
|
| 472 |
def test_sendto_blocking(self): |
| 473 |
if sys.platform == 'win32': |
| 474 |
if isinstance(self.loop, asyncio.ProactorEventLoop): |
| 475 |
raise unittest.SkipTest('Not relevant to ProactorEventLoop') |
| 476 |
|
| 477 |
with test_utils.run_udp_echo_server() as server_address: |
| 478 |
self.loop.run_until_complete( |
| 479 |
self._basetest_datagram_sendto_blocking(server_address)) |
| 480 |
|
| 481 |
@socket_helper.skip_unless_bind_unix_socket |
| 482 |
def test_unix_sock_client_ops(self): |
| 483 |
with test_utils.run_test_unix_server() as httpd: |
| 484 |
sock = socket.socket(socket.AF_UNIX) |
| 485 |
self._basetest_sock_client_ops(httpd, sock) |
| 486 |
sock = socket.socket(socket.AF_UNIX) |
| 487 |
self._basetest_sock_recv_into(httpd, sock) |
| 488 |
|
| 489 |
def test_sock_client_fail(self): |
| 490 |
# Make sure that we will get an unused port |
| 491 |
address = None |
| 492 |
try: |
| 493 |
s = socket.socket() |
| 494 |
s.bind(('127.0.0.1', 0)) |
| 495 |
address = s.getsockname() |
| 496 |
finally: |
| 497 |
s.close() |
| 498 |
|
| 499 |
sock = socket.socket() |
| 500 |
sock.setblocking(False) |
| 501 |
with self.assertRaises(ConnectionRefusedError): |
| 502 |
self.loop.run_until_complete( |
| 503 |
self.loop.sock_connect(sock, address)) |
| 504 |
sock.close() |
| 505 |
|
| 506 |
def test_sock_accept(self): |
| 507 |
listener = socket.socket() |
| 508 |
listener.setblocking(False) |
| 509 |
listener.bind(('127.0.0.1', 0)) |
| 510 |
listener.listen(1) |
| 511 |
client = socket.socket() |
| 512 |
client.connect(listener.getsockname()) |
| 513 |
|
| 514 |
f = self.loop.sock_accept(listener) |
| 515 |
conn, addr = self.loop.run_until_complete(f) |
| 516 |
self.assertEqual(conn.gettimeout(), 0) |
| 517 |
self.assertEqual(addr, client.getsockname()) |
| 518 |
self.assertEqual(client.getpeername(), listener.getsockname()) |
| 519 |
client.close() |
| 520 |
conn.close() |
| 521 |
listener.close() |
| 522 |
|
| 523 |
def test_cancel_sock_accept(self): |
| 524 |
listener = socket.socket() |
| 525 |
listener.setblocking(False) |
| 526 |
listener.bind(('127.0.0.1', 0)) |
| 527 |
listener.listen(1) |
| 528 |
sockaddr = listener.getsockname() |
| 529 |
f = asyncio.wait_for(self.loop.sock_accept(listener), 0.1) |
| 530 |
with self.assertRaises(asyncio.TimeoutError): |
| 531 |
self.loop.run_until_complete(f) |
| 532 |
|
| 533 |
listener.close() |
| 534 |
client = socket.socket() |
| 535 |
client.setblocking(False) |
| 536 |
f = self.loop.sock_connect(client, sockaddr) |
| 537 |
with self.assertRaises(ConnectionRefusedError): |
| 538 |
self.loop.run_until_complete(f) |
| 539 |
|
| 540 |
client.close() |
| 541 |
|
| 542 |
def test_create_connection_sock(self): |
| 543 |
with test_utils.run_test_server() as httpd: |
| 544 |
sock = None |
| 545 |
infos = self.loop.run_until_complete( |
| 546 |
self.loop.getaddrinfo( |
| 547 |
*httpd.address, type=socket.SOCK_STREAM)) |
| 548 |
for family, type, proto, cname, address in infos: |
| 549 |
try: |
| 550 |
sock = socket.socket(family=family, type=type, proto=proto) |
| 551 |
sock.setblocking(False) |
| 552 |
self.loop.run_until_complete( |
| 553 |
self.loop.sock_connect(sock, address)) |
| 554 |
except BaseException: |
| 555 |
pass |
| 556 |
else: |
| 557 |
break |
| 558 |
else: |
| 559 |
self.fail('Can not create socket.') |
| 560 |
|
| 561 |
f = self.loop.create_connection( |
| 562 |
lambda: MyProto(loop=self.loop), sock=sock) |
| 563 |
tr, pr = self.loop.run_until_complete(f) |
| 564 |
self.assertIsInstance(tr, asyncio.Transport) |
| 565 |
self.assertIsInstance(pr, asyncio.Protocol) |
| 566 |
self.loop.run_until_complete(pr.done) |
| 567 |
self.assertGreater(pr.nbytes, 0) |
| 568 |
tr.close() |
| 569 |
|
| 570 |
|
| 571 |
if sys.platform == 'win32': |
| 572 |
|
| 573 |
class SelectEventLoopTests(BaseSockTestsMixin, |
| 574 |
test_utils.TestCase): |
| 575 |
|
| 576 |
def create_event_loop(self): |
| 577 |
return asyncio.SelectorEventLoop() |
| 578 |
|
| 579 |
|
| 580 |
class ProactorEventLoopTests(BaseSockTestsMixin, |
| 581 |
test_utils.TestCase): |
| 582 |
|
| 583 |
def create_event_loop(self): |
| 584 |
return asyncio.ProactorEventLoop() |
| 585 |
|
| 586 |
|
| 587 |
async def _basetest_datagram_send_to_non_listening_address(self, |
| 588 |
recvfrom): |
| 589 |
# see: |
| 590 |
# https://github.com/python/cpython/issues/91227 |
| 591 |
# https://github.com/python/cpython/issues/88906 |
| 592 |
# https://bugs.python.org/issue47071 |
| 593 |
# https://bugs.python.org/issue44743 |
| 594 |
# The Proactor event loop would fail to receive datagram messages |
| 595 |
# after sending a message to an address that wasn't listening. |
| 596 |
|
| 597 |
def create_socket(): |
| 598 |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 599 |
sock.setblocking(False) |
| 600 |
sock.bind(('127.0.0.1', 0)) |
| 601 |
return sock |
| 602 |
|
| 603 |
socket_1 = create_socket() |
| 604 |
addr_1 = socket_1.getsockname() |
| 605 |
|
| 606 |
socket_2 = create_socket() |
| 607 |
addr_2 = socket_2.getsockname() |
| 608 |
|
| 609 |
# creating and immediately closing this to try to get an address |
| 610 |
# that is not listening |
| 611 |
socket_3 = create_socket() |
| 612 |
addr_3 = socket_3.getsockname() |
| 613 |
socket_3.shutdown(socket.SHUT_RDWR) |
| 614 |
socket_3.close() |
| 615 |
|
| 616 |
socket_1_recv_task = self.loop.create_task(recvfrom(socket_1)) |
| 617 |
socket_2_recv_task = self.loop.create_task(recvfrom(socket_2)) |
| 618 |
await asyncio.sleep(0) |
| 619 |
|
| 620 |
await self.loop.sock_sendto(socket_1, b'a', addr_2) |
| 621 |
self.assertEqual(await socket_2_recv_task, b'a') |
| 622 |
|
| 623 |
await self.loop.sock_sendto(socket_2, b'b', addr_1) |
| 624 |
self.assertEqual(await socket_1_recv_task, b'b') |
| 625 |
socket_1_recv_task = self.loop.create_task(recvfrom(socket_1)) |
| 626 |
await asyncio.sleep(0) |
| 627 |
|
| 628 |
# this should send to an address that isn't listening |
| 629 |
await self.loop.sock_sendto(socket_1, b'c', addr_3) |
| 630 |
self.assertEqual(await socket_1_recv_task, b'') |
| 631 |
socket_1_recv_task = self.loop.create_task(recvfrom(socket_1)) |
| 632 |
await asyncio.sleep(0) |
| 633 |
|
| 634 |
# socket 1 should still be able to receive messages after sending |
| 635 |
# to an address that wasn't listening |
| 636 |
socket_2.sendto(b'd', addr_1) |
| 637 |
self.assertEqual(await socket_1_recv_task, b'd') |
| 638 |
|
| 639 |
socket_1.shutdown(socket.SHUT_RDWR) |
| 640 |
socket_1.close() |
| 641 |
socket_2.shutdown(socket.SHUT_RDWR) |
| 642 |
socket_2.close() |
| 643 |
|
| 644 |
|
| 645 |
def test_datagram_send_to_non_listening_address_recvfrom(self): |
| 646 |
async def recvfrom(socket): |
| 647 |
data, _ = await self.loop.sock_recvfrom(socket, 4096) |
| 648 |
return data |
| 649 |
|
| 650 |
self.loop.run_until_complete( |
| 651 |
self._basetest_datagram_send_to_non_listening_address( |
| 652 |
recvfrom)) |
| 653 |
|
| 654 |
|
| 655 |
def test_datagram_send_to_non_listening_address_recvfrom_into(self): |
| 656 |
async def recvfrom_into(socket): |
| 657 |
buf = bytearray(4096) |
| 658 |
length, _ = await self.loop.sock_recvfrom_into(socket, buf, |
| 659 |
4096) |
| 660 |
return buf[:length] |
| 661 |
|
| 662 |
self.loop.run_until_complete( |
| 663 |
self._basetest_datagram_send_to_non_listening_address( |
| 664 |
recvfrom_into)) |
| 665 |
|
| 666 |
else: |
| 667 |
import selectors |
| 668 |
|
| 669 |
if hasattr(selectors, 'KqueueSelector'): |
| 670 |
class KqueueEventLoopTests(BaseSockTestsMixin, |
| 671 |
test_utils.TestCase): |
| 672 |
|
| 673 |
def create_event_loop(self): |
| 674 |
return asyncio.SelectorEventLoop( |
| 675 |
selectors.KqueueSelector()) |
| 676 |
|
| 677 |
if hasattr(selectors, 'EpollSelector'): |
| 678 |
class EPollEventLoopTests(BaseSockTestsMixin, |
| 679 |
test_utils.TestCase): |
| 680 |
|
| 681 |
def create_event_loop(self): |
| 682 |
return asyncio.SelectorEventLoop(selectors.EpollSelector()) |
| 683 |
|
| 684 |
if hasattr(selectors, 'PollSelector'): |
| 685 |
class PollEventLoopTests(BaseSockTestsMixin, |
| 686 |
test_utils.TestCase): |
| 687 |
|
| 688 |
def create_event_loop(self): |
| 689 |
return asyncio.SelectorEventLoop(selectors.PollSelector()) |
| 690 |
|
| 691 |
# Should always exist. |
| 692 |
class SelectEventLoopTests(BaseSockTestsMixin, |
| 693 |
test_utils.TestCase): |
| 694 |
|
| 695 |
def create_event_loop(self): |
| 696 |
return asyncio.SelectorEventLoop(selectors.SelectSelector()) |
| 697 |
|
| 698 |
|
| 699 |
if __name__ == '__main__': |
| 700 |
unittest.main() |