| 1 |
"""Tests for base_events.py""" |
| 2 |
|
| 3 |
import concurrent.futures |
| 4 |
import errno |
| 5 |
import math |
| 6 |
import platform |
| 7 |
import socket |
| 8 |
import sys |
| 9 |
import threading |
| 10 |
import time |
| 11 |
import unittest |
| 12 |
from unittest import mock |
| 13 |
|
| 14 |
import asyncio |
| 15 |
from asyncio import base_events |
| 16 |
from asyncio import constants |
| 17 |
from test.test_asyncio import utils as test_utils |
| 18 |
from test import support |
| 19 |
from test.support.script_helper import assert_python_ok |
| 20 |
from test.support import os_helper |
| 21 |
from test.support import socket_helper |
| 22 |
import warnings |
| 23 |
|
| 24 |
MOCK_ANY = mock.ANY |
| 25 |
|
| 26 |
|
| 27 |
class CustomError(Exception): |
| 28 |
pass |
| 29 |
|
| 30 |
|
| 31 |
def tearDownModule(): |
| 32 |
asyncio.events._set_event_loop_policy(None) |
| 33 |
|
| 34 |
|
| 35 |
def mock_socket_module(): |
| 36 |
m_socket = mock.MagicMock(spec=socket) |
| 37 |
for name in ( |
| 38 |
'AF_INET', 'AF_INET6', 'AF_UNSPEC', 'IPPROTO_TCP', 'IPPROTO_UDP', |
| 39 |
'SOCK_STREAM', 'SOCK_DGRAM', 'SOL_SOCKET', 'SO_REUSEADDR', 'inet_pton' |
| 40 |
): |
| 41 |
if hasattr(socket, name): |
| 42 |
setattr(m_socket, name, getattr(socket, name)) |
| 43 |
else: |
| 44 |
delattr(m_socket, name) |
| 45 |
|
| 46 |
m_socket.socket = mock.MagicMock() |
| 47 |
m_socket.socket.return_value = test_utils.mock_nonblocking_socket() |
| 48 |
|
| 49 |
return m_socket |
| 50 |
|
| 51 |
|
| 52 |
def patch_socket(f): |
| 53 |
return mock.patch('asyncio.base_events.socket', |
| 54 |
new_callable=mock_socket_module)(f) |
| 55 |
|
| 56 |
|
| 57 |
class BaseEventTests(test_utils.TestCase): |
| 58 |
|
| 59 |
def test_ipaddr_info(self): |
| 60 |
UNSPEC = socket.AF_UNSPEC |
| 61 |
INET = socket.AF_INET |
| 62 |
INET6 = socket.AF_INET6 |
| 63 |
STREAM = socket.SOCK_STREAM |
| 64 |
DGRAM = socket.SOCK_DGRAM |
| 65 |
TCP = socket.IPPROTO_TCP |
| 66 |
UDP = socket.IPPROTO_UDP |
| 67 |
|
| 68 |
self.assertEqual( |
| 69 |
(INET, STREAM, TCP, '', ('1.2.3.4', 1)), |
| 70 |
base_events._ipaddr_info('1.2.3.4', 1, INET, STREAM, TCP)) |
| 71 |
|
| 72 |
self.assertEqual( |
| 73 |
(INET, STREAM, TCP, '', ('1.2.3.4', 1)), |
| 74 |
base_events._ipaddr_info(b'1.2.3.4', 1, INET, STREAM, TCP)) |
| 75 |
|
| 76 |
self.assertEqual( |
| 77 |
(INET, STREAM, TCP, '', ('1.2.3.4', 1)), |
| 78 |
base_events._ipaddr_info('1.2.3.4', 1, UNSPEC, STREAM, TCP)) |
| 79 |
|
| 80 |
self.assertEqual( |
| 81 |
(INET, DGRAM, UDP, '', ('1.2.3.4', 1)), |
| 82 |
base_events._ipaddr_info('1.2.3.4', 1, UNSPEC, DGRAM, UDP)) |
| 83 |
|
| 84 |
# Socket type STREAM implies TCP protocol. |
| 85 |
self.assertEqual( |
| 86 |
(INET, STREAM, TCP, '', ('1.2.3.4', 1)), |
| 87 |
base_events._ipaddr_info('1.2.3.4', 1, UNSPEC, STREAM, 0)) |
| 88 |
|
| 89 |
# Socket type DGRAM implies UDP protocol. |
| 90 |
self.assertEqual( |
| 91 |
(INET, DGRAM, UDP, '', ('1.2.3.4', 1)), |
| 92 |
base_events._ipaddr_info('1.2.3.4', 1, UNSPEC, DGRAM, 0)) |
| 93 |
|
| 94 |
# No socket type. |
| 95 |
self.assertIsNone( |
| 96 |
base_events._ipaddr_info('1.2.3.4', 1, UNSPEC, 0, 0)) |
| 97 |
|
| 98 |
if socket_helper.IPV6_ENABLED: |
| 99 |
# IPv4 address with family IPv6. |
| 100 |
self.assertIsNone( |
| 101 |
base_events._ipaddr_info('1.2.3.4', 1, INET6, STREAM, TCP)) |
| 102 |
|
| 103 |
self.assertEqual( |
| 104 |
(INET6, STREAM, TCP, '', ('::3', 1, 0, 0)), |
| 105 |
base_events._ipaddr_info('::3', 1, INET6, STREAM, TCP)) |
| 106 |
|
| 107 |
self.assertEqual( |
| 108 |
(INET6, STREAM, TCP, '', ('::3', 1, 0, 0)), |
| 109 |
base_events._ipaddr_info('::3', 1, UNSPEC, STREAM, TCP)) |
| 110 |
|
| 111 |
# IPv6 address with family IPv4. |
| 112 |
self.assertIsNone( |
| 113 |
base_events._ipaddr_info('::3', 1, INET, STREAM, TCP)) |
| 114 |
|
| 115 |
# IPv6 address with zone index. |
| 116 |
self.assertIsNone( |
| 117 |
base_events._ipaddr_info('::3%lo0', 1, INET6, STREAM, TCP)) |
| 118 |
|
| 119 |
def test_port_parameter_types(self): |
| 120 |
# Test obscure kinds of arguments for "port". |
| 121 |
INET = socket.AF_INET |
| 122 |
STREAM = socket.SOCK_STREAM |
| 123 |
TCP = socket.IPPROTO_TCP |
| 124 |
|
| 125 |
self.assertEqual( |
| 126 |
(INET, STREAM, TCP, '', ('1.2.3.4', 0)), |
| 127 |
base_events._ipaddr_info('1.2.3.4', None, INET, STREAM, TCP)) |
| 128 |
|
| 129 |
self.assertEqual( |
| 130 |
(INET, STREAM, TCP, '', ('1.2.3.4', 0)), |
| 131 |
base_events._ipaddr_info('1.2.3.4', b'', INET, STREAM, TCP)) |
| 132 |
|
| 133 |
self.assertEqual( |
| 134 |
(INET, STREAM, TCP, '', ('1.2.3.4', 0)), |
| 135 |
base_events._ipaddr_info('1.2.3.4', '', INET, STREAM, TCP)) |
| 136 |
|
| 137 |
self.assertEqual( |
| 138 |
(INET, STREAM, TCP, '', ('1.2.3.4', 1)), |
| 139 |
base_events._ipaddr_info('1.2.3.4', '1', INET, STREAM, TCP)) |
| 140 |
|
| 141 |
self.assertEqual( |
| 142 |
(INET, STREAM, TCP, '', ('1.2.3.4', 1)), |
| 143 |
base_events._ipaddr_info('1.2.3.4', b'1', INET, STREAM, TCP)) |
| 144 |
|
| 145 |
@patch_socket |
| 146 |
def test_ipaddr_info_no_inet_pton(self, m_socket): |
| 147 |
del m_socket.inet_pton |
| 148 |
self.assertIsNone(base_events._ipaddr_info('1.2.3.4', 1, |
| 149 |
socket.AF_INET, |
| 150 |
socket.SOCK_STREAM, |
| 151 |
socket.IPPROTO_TCP)) |
| 152 |
|
| 153 |
|
| 154 |
class BaseEventLoopTests(test_utils.TestCase): |
| 155 |
|
| 156 |
def setUp(self): |
| 157 |
super().setUp() |
| 158 |
self.loop = base_events.BaseEventLoop() |
| 159 |
self.loop._selector = mock.Mock() |
| 160 |
self.loop._selector.select.return_value = () |
| 161 |
self.set_event_loop(self.loop) |
| 162 |
|
| 163 |
def test_not_implemented(self): |
| 164 |
m = mock.Mock() |
| 165 |
self.assertRaises( |
| 166 |
NotImplementedError, |
| 167 |
self.loop._make_socket_transport, m, m) |
| 168 |
self.assertRaises( |
| 169 |
NotImplementedError, |
| 170 |
self.loop._make_ssl_transport, m, m, m, m) |
| 171 |
self.assertRaises( |
| 172 |
NotImplementedError, |
| 173 |
self.loop._make_datagram_transport, m, m) |
| 174 |
self.assertRaises( |
| 175 |
NotImplementedError, self.loop._process_events, []) |
| 176 |
self.assertRaises( |
| 177 |
NotImplementedError, self.loop._write_to_self) |
| 178 |
self.assertRaises( |
| 179 |
NotImplementedError, |
| 180 |
self.loop._make_read_pipe_transport, m, m) |
| 181 |
self.assertRaises( |
| 182 |
NotImplementedError, |
| 183 |
self.loop._make_write_pipe_transport, m, m) |
| 184 |
gen = self.loop._make_subprocess_transport(m, m, m, m, m, m, m) |
| 185 |
with self.assertRaises(NotImplementedError): |
| 186 |
gen.send(None) |
| 187 |
|
| 188 |
def test_close(self): |
| 189 |
self.assertFalse(self.loop.is_closed()) |
| 190 |
self.loop.close() |
| 191 |
self.assertTrue(self.loop.is_closed()) |
| 192 |
|
| 193 |
# it should be possible to call close() more than once |
| 194 |
self.loop.close() |
| 195 |
self.loop.close() |
| 196 |
|
| 197 |
# operation blocked when the loop is closed |
| 198 |
f = self.loop.create_future() |
| 199 |
self.assertRaises(RuntimeError, self.loop.run_forever) |
| 200 |
self.assertRaises(RuntimeError, self.loop.run_until_complete, f) |
| 201 |
|
| 202 |
def test__add_callback_handle(self): |
| 203 |
h = asyncio.Handle(lambda: False, (), self.loop, None) |
| 204 |
|
| 205 |
self.loop._add_callback(h) |
| 206 |
self.assertFalse(self.loop._scheduled) |
| 207 |
self.assertIn(h, self.loop._ready) |
| 208 |
|
| 209 |
def test__add_callback_cancelled_handle(self): |
| 210 |
h = asyncio.Handle(lambda: False, (), self.loop, None) |
| 211 |
h.cancel() |
| 212 |
|
| 213 |
self.loop._add_callback(h) |
| 214 |
self.assertFalse(self.loop._scheduled) |
| 215 |
self.assertFalse(self.loop._ready) |
| 216 |
|
| 217 |
def test_set_default_executor(self): |
| 218 |
class DummyExecutor(concurrent.futures.ThreadPoolExecutor): |
| 219 |
def submit(self, fn, *args, **kwargs): |
| 220 |
raise NotImplementedError( |
| 221 |
'cannot submit into a dummy executor') |
| 222 |
|
| 223 |
self.loop._process_events = mock.Mock() |
| 224 |
self.loop._write_to_self = mock.Mock() |
| 225 |
|
| 226 |
executor = DummyExecutor() |
| 227 |
self.loop.set_default_executor(executor) |
| 228 |
self.assertIs(executor, self.loop._default_executor) |
| 229 |
|
| 230 |
def test_set_default_executor_error(self): |
| 231 |
executor = mock.Mock() |
| 232 |
|
| 233 |
msg = 'executor must be ThreadPoolExecutor instance' |
| 234 |
with self.assertRaisesRegex(TypeError, msg): |
| 235 |
self.loop.set_default_executor(executor) |
| 236 |
|
| 237 |
self.assertIsNone(self.loop._default_executor) |
| 238 |
|
| 239 |
def test_shutdown_default_executor_timeout(self): |
| 240 |
event = threading.Event() |
| 241 |
|
| 242 |
class DummyExecutor(concurrent.futures.ThreadPoolExecutor): |
| 243 |
def shutdown(self, wait=True, *, cancel_futures=False): |
| 244 |
if wait: |
| 245 |
event.wait() |
| 246 |
|
| 247 |
self.loop._process_events = mock.Mock() |
| 248 |
self.loop._write_to_self = mock.Mock() |
| 249 |
executor = DummyExecutor() |
| 250 |
self.loop.set_default_executor(executor) |
| 251 |
|
| 252 |
try: |
| 253 |
with self.assertWarnsRegex(RuntimeWarning, |
| 254 |
"The executor did not finishing joining"): |
| 255 |
self.loop.run_until_complete( |
| 256 |
self.loop.shutdown_default_executor(timeout=0.01)) |
| 257 |
finally: |
| 258 |
event.set() |
| 259 |
|
| 260 |
def test_call_soon(self): |
| 261 |
def cb(): |
| 262 |
pass |
| 263 |
|
| 264 |
h = self.loop.call_soon(cb) |
| 265 |
self.assertEqual(h._callback, cb) |
| 266 |
self.assertIsInstance(h, asyncio.Handle) |
| 267 |
self.assertIn(h, self.loop._ready) |
| 268 |
|
| 269 |
def test_call_soon_non_callable(self): |
| 270 |
self.loop.set_debug(True) |
| 271 |
with self.assertRaisesRegex(TypeError, 'a callable object'): |
| 272 |
self.loop.call_soon(1) |
| 273 |
|
| 274 |
def test_call_later(self): |
| 275 |
def cb(): |
| 276 |
pass |
| 277 |
|
| 278 |
h = self.loop.call_later(10.0, cb) |
| 279 |
self.assertIsInstance(h, asyncio.TimerHandle) |
| 280 |
self.assertIn(h, self.loop._scheduled) |
| 281 |
self.assertNotIn(h, self.loop._ready) |
| 282 |
with self.assertRaises(TypeError, msg="delay must not be None"): |
| 283 |
self.loop.call_later(None, cb) |
| 284 |
|
| 285 |
def test_call_later_negative_delays(self): |
| 286 |
calls = [] |
| 287 |
|
| 288 |
def cb(arg): |
| 289 |
calls.append(arg) |
| 290 |
|
| 291 |
self.loop._process_events = mock.Mock() |
| 292 |
self.loop.call_later(-1, cb, 'a') |
| 293 |
self.loop.call_later(-2, cb, 'b') |
| 294 |
test_utils.run_briefly(self.loop) |
| 295 |
self.assertEqual(calls, ['b', 'a']) |
| 296 |
|
| 297 |
def test_time_and_call_at(self): |
| 298 |
def cb(): |
| 299 |
self.loop.stop() |
| 300 |
|
| 301 |
self.loop._process_events = mock.Mock() |
| 302 |
delay = 0.100 |
| 303 |
|
| 304 |
when = self.loop.time() + delay |
| 305 |
self.loop.call_at(when, cb) |
| 306 |
t0 = self.loop.time() |
| 307 |
self.loop.run_forever() |
| 308 |
dt = self.loop.time() - t0 |
| 309 |
|
| 310 |
# 50 ms: maximum granularity of the event loop |
| 311 |
self.assertGreaterEqual(dt, delay - test_utils.CLOCK_RES) |
| 312 |
with self.assertRaises(TypeError, msg="when cannot be None"): |
| 313 |
self.loop.call_at(None, cb) |
| 314 |
|
| 315 |
def check_thread(self, loop, debug): |
| 316 |
def cb(): |
| 317 |
pass |
| 318 |
|
| 319 |
loop.set_debug(debug) |
| 320 |
if debug: |
| 321 |
msg = ("Non-thread-safe operation invoked on an event loop other " |
| 322 |
"than the current one") |
| 323 |
with self.assertRaisesRegex(RuntimeError, msg): |
| 324 |
loop.call_soon(cb) |
| 325 |
with self.assertRaisesRegex(RuntimeError, msg): |
| 326 |
loop.call_later(60, cb) |
| 327 |
with self.assertRaisesRegex(RuntimeError, msg): |
| 328 |
loop.call_at(loop.time() + 60, cb) |
| 329 |
else: |
| 330 |
loop.call_soon(cb) |
| 331 |
loop.call_later(60, cb) |
| 332 |
loop.call_at(loop.time() + 60, cb) |
| 333 |
|
| 334 |
def test_check_thread(self): |
| 335 |
def check_in_thread(loop, event, debug, create_loop, fut): |
| 336 |
# wait until the event loop is running |
| 337 |
event.wait() |
| 338 |
|
| 339 |
try: |
| 340 |
if create_loop: |
| 341 |
loop2 = base_events.BaseEventLoop() |
| 342 |
try: |
| 343 |
asyncio.set_event_loop(loop2) |
| 344 |
self.check_thread(loop, debug) |
| 345 |
finally: |
| 346 |
asyncio.set_event_loop(None) |
| 347 |
loop2.close() |
| 348 |
else: |
| 349 |
self.check_thread(loop, debug) |
| 350 |
except Exception as exc: |
| 351 |
loop.call_soon_threadsafe(fut.set_exception, exc) |
| 352 |
else: |
| 353 |
loop.call_soon_threadsafe(fut.set_result, None) |
| 354 |
|
| 355 |
def test_thread(loop, debug, create_loop=False): |
| 356 |
event = threading.Event() |
| 357 |
fut = loop.create_future() |
| 358 |
loop.call_soon(event.set) |
| 359 |
args = (loop, event, debug, create_loop, fut) |
| 360 |
thread = threading.Thread(target=check_in_thread, args=args) |
| 361 |
thread.start() |
| 362 |
loop.run_until_complete(fut) |
| 363 |
thread.join() |
| 364 |
|
| 365 |
self.loop._process_events = mock.Mock() |
| 366 |
self.loop._write_to_self = mock.Mock() |
| 367 |
|
| 368 |
# raise RuntimeError if the thread has no event loop |
| 369 |
test_thread(self.loop, True) |
| 370 |
|
| 371 |
# check disabled if debug mode is disabled |
| 372 |
test_thread(self.loop, False) |
| 373 |
|
| 374 |
# raise RuntimeError if the event loop of the thread is not the called |
| 375 |
# event loop |
| 376 |
test_thread(self.loop, True, create_loop=True) |
| 377 |
|
| 378 |
# check disabled if debug mode is disabled |
| 379 |
test_thread(self.loop, False, create_loop=True) |
| 380 |
|
| 381 |
def test__run_once(self): |
| 382 |
h1 = asyncio.TimerHandle(time.monotonic() + 5.0, lambda: True, (), |
| 383 |
self.loop, None) |
| 384 |
h2 = asyncio.TimerHandle(time.monotonic() + 10.0, lambda: True, (), |
| 385 |
self.loop, None) |
| 386 |
|
| 387 |
h1.cancel() |
| 388 |
|
| 389 |
self.loop._process_events = mock.Mock() |
| 390 |
self.loop._scheduled.append(h1) |
| 391 |
self.loop._scheduled.append(h2) |
| 392 |
self.loop._run_once() |
| 393 |
|
| 394 |
t = self.loop._selector.select.call_args[0][0] |
| 395 |
self.assertTrue(9.5 < t < 10.5, t) |
| 396 |
self.assertEqual([h2], self.loop._scheduled) |
| 397 |
self.assertTrue(self.loop._process_events.called) |
| 398 |
|
| 399 |
def test_set_debug(self): |
| 400 |
self.loop.set_debug(True) |
| 401 |
self.assertTrue(self.loop.get_debug()) |
| 402 |
self.loop.set_debug(False) |
| 403 |
self.assertFalse(self.loop.get_debug()) |
| 404 |
|
| 405 |
def test__run_once_schedule_handle(self): |
| 406 |
handle = None |
| 407 |
processed = False |
| 408 |
|
| 409 |
def cb(loop): |
| 410 |
nonlocal processed, handle |
| 411 |
processed = True |
| 412 |
handle = loop.call_soon(lambda: True) |
| 413 |
|
| 414 |
h = asyncio.TimerHandle(time.monotonic() - 1, cb, (self.loop,), |
| 415 |
self.loop, None) |
| 416 |
|
| 417 |
self.loop._process_events = mock.Mock() |
| 418 |
self.loop._scheduled.append(h) |
| 419 |
self.loop._run_once() |
| 420 |
|
| 421 |
self.assertTrue(processed) |
| 422 |
self.assertEqual([handle], list(self.loop._ready)) |
| 423 |
|
| 424 |
def test__run_once_cancelled_event_cleanup(self): |
| 425 |
self.loop._process_events = mock.Mock() |
| 426 |
|
| 427 |
self.assertTrue( |
| 428 |
0 < base_events._MIN_CANCELLED_TIMER_HANDLES_FRACTION < 1.0) |
| 429 |
|
| 430 |
def cb(): |
| 431 |
pass |
| 432 |
|
| 433 |
# Set up one "blocking" event that will not be cancelled to |
| 434 |
# ensure later cancelled events do not make it to the head |
| 435 |
# of the queue and get cleaned. |
| 436 |
not_cancelled_count = 1 |
| 437 |
self.loop.call_later(3000, cb) |
| 438 |
|
| 439 |
# Add less than threshold (base_events._MIN_SCHEDULED_TIMER_HANDLES) |
| 440 |
# cancelled handles, ensure they aren't removed |
| 441 |
|
| 442 |
cancelled_count = 2 |
| 443 |
for x in range(2): |
| 444 |
h = self.loop.call_later(3600, cb) |
| 445 |
h.cancel() |
| 446 |
|
| 447 |
# Add some cancelled events that will be at head and removed |
| 448 |
cancelled_count += 2 |
| 449 |
for x in range(2): |
| 450 |
h = self.loop.call_later(100, cb) |
| 451 |
h.cancel() |
| 452 |
|
| 453 |
# This test is invalid if _MIN_SCHEDULED_TIMER_HANDLES is too low |
| 454 |
self.assertLessEqual(cancelled_count + not_cancelled_count, |
| 455 |
base_events._MIN_SCHEDULED_TIMER_HANDLES) |
| 456 |
|
| 457 |
self.assertEqual(self.loop._timer_cancelled_count, cancelled_count) |
| 458 |
|
| 459 |
self.loop._run_once() |
| 460 |
|
| 461 |
cancelled_count -= 2 |
| 462 |
|
| 463 |
self.assertEqual(self.loop._timer_cancelled_count, cancelled_count) |
| 464 |
|
| 465 |
self.assertEqual(len(self.loop._scheduled), |
| 466 |
cancelled_count + not_cancelled_count) |
| 467 |
|
| 468 |
# Need enough events to pass _MIN_CANCELLED_TIMER_HANDLES_FRACTION |
| 469 |
# so that deletion of cancelled events will occur on next _run_once |
| 470 |
add_cancel_count = int(math.ceil( |
| 471 |
base_events._MIN_SCHEDULED_TIMER_HANDLES * |
| 472 |
base_events._MIN_CANCELLED_TIMER_HANDLES_FRACTION)) + 1 |
| 473 |
|
| 474 |
add_not_cancel_count = max(base_events._MIN_SCHEDULED_TIMER_HANDLES - |
| 475 |
add_cancel_count, 0) |
| 476 |
|
| 477 |
# Add some events that will not be cancelled |
| 478 |
not_cancelled_count += add_not_cancel_count |
| 479 |
for x in range(add_not_cancel_count): |
| 480 |
self.loop.call_later(3600, cb) |
| 481 |
|
| 482 |
# Add enough cancelled events |
| 483 |
cancelled_count += add_cancel_count |
| 484 |
for x in range(add_cancel_count): |
| 485 |
h = self.loop.call_later(3600, cb) |
| 486 |
h.cancel() |
| 487 |
|
| 488 |
# Ensure all handles are still scheduled |
| 489 |
self.assertEqual(len(self.loop._scheduled), |
| 490 |
cancelled_count + not_cancelled_count) |
| 491 |
|
| 492 |
self.loop._run_once() |
| 493 |
|
| 494 |
# Ensure cancelled events were removed |
| 495 |
self.assertEqual(len(self.loop._scheduled), not_cancelled_count) |
| 496 |
|
| 497 |
# Ensure only uncancelled events remain scheduled |
| 498 |
self.assertTrue(all([not x._cancelled for x in self.loop._scheduled])) |
| 499 |
|
| 500 |
def test_run_until_complete_type_error(self): |
| 501 |
self.assertRaises(TypeError, |
| 502 |
self.loop.run_until_complete, 'blah') |
| 503 |
|
| 504 |
def test_run_until_complete_loop(self): |
| 505 |
task = self.loop.create_future() |
| 506 |
other_loop = self.new_test_loop() |
| 507 |
self.addCleanup(other_loop.close) |
| 508 |
self.assertRaises(ValueError, |
| 509 |
other_loop.run_until_complete, task) |
| 510 |
|
| 511 |
def test_run_until_complete_loop_orphan_future_close_loop(self): |
| 512 |
class ShowStopper(SystemExit): |
| 513 |
pass |
| 514 |
|
| 515 |
async def foo(delay): |
| 516 |
await asyncio.sleep(delay) |
| 517 |
|
| 518 |
def throw(): |
| 519 |
raise ShowStopper |
| 520 |
|
| 521 |
self.loop._process_events = mock.Mock() |
| 522 |
self.loop.call_soon(throw) |
| 523 |
with self.assertRaises(ShowStopper): |
| 524 |
self.loop.run_until_complete(foo(0.1)) |
| 525 |
|
| 526 |
# This call fails if run_until_complete does not clean up |
| 527 |
# done-callback for the previous future. |
| 528 |
self.loop.run_until_complete(foo(0.2)) |
| 529 |
|
| 530 |
def test_subprocess_exec_invalid_args(self): |
| 531 |
args = [sys.executable, '-c', 'pass'] |
| 532 |
|
| 533 |
# missing program parameter (empty args) |
| 534 |
self.assertRaises(TypeError, |
| 535 |
self.loop.run_until_complete, self.loop.subprocess_exec, |
| 536 |
asyncio.SubprocessProtocol) |
| 537 |
|
| 538 |
# expected multiple arguments, not a list |
| 539 |
self.assertRaises(TypeError, |
| 540 |
self.loop.run_until_complete, self.loop.subprocess_exec, |
| 541 |
asyncio.SubprocessProtocol, args) |
| 542 |
|
| 543 |
# program arguments must be strings, not int |
| 544 |
self.assertRaises(TypeError, |
| 545 |
self.loop.run_until_complete, self.loop.subprocess_exec, |
| 546 |
asyncio.SubprocessProtocol, sys.executable, 123) |
| 547 |
|
| 548 |
# universal_newlines, shell, bufsize must not be set |
| 549 |
self.assertRaises(TypeError, |
| 550 |
self.loop.run_until_complete, self.loop.subprocess_exec, |
| 551 |
asyncio.SubprocessProtocol, *args, universal_newlines=True) |
| 552 |
self.assertRaises(TypeError, |
| 553 |
self.loop.run_until_complete, self.loop.subprocess_exec, |
| 554 |
asyncio.SubprocessProtocol, *args, shell=True) |
| 555 |
self.assertRaises(TypeError, |
| 556 |
self.loop.run_until_complete, self.loop.subprocess_exec, |
| 557 |
asyncio.SubprocessProtocol, *args, bufsize=4096) |
| 558 |
|
| 559 |
def test_subprocess_shell_invalid_args(self): |
| 560 |
# expected a string, not an int or a list |
| 561 |
self.assertRaises(TypeError, |
| 562 |
self.loop.run_until_complete, self.loop.subprocess_shell, |
| 563 |
asyncio.SubprocessProtocol, 123) |
| 564 |
self.assertRaises(TypeError, |
| 565 |
self.loop.run_until_complete, self.loop.subprocess_shell, |
| 566 |
asyncio.SubprocessProtocol, [sys.executable, '-c', 'pass']) |
| 567 |
|
| 568 |
# universal_newlines, shell, bufsize must not be set |
| 569 |
self.assertRaises(TypeError, |
| 570 |
self.loop.run_until_complete, self.loop.subprocess_shell, |
| 571 |
asyncio.SubprocessProtocol, 'exit 0', universal_newlines=True) |
| 572 |
self.assertRaises(TypeError, |
| 573 |
self.loop.run_until_complete, self.loop.subprocess_shell, |
| 574 |
asyncio.SubprocessProtocol, 'exit 0', shell=True) |
| 575 |
self.assertRaises(TypeError, |
| 576 |
self.loop.run_until_complete, self.loop.subprocess_shell, |
| 577 |
asyncio.SubprocessProtocol, 'exit 0', bufsize=4096) |
| 578 |
|
| 579 |
def test_default_exc_handler_callback(self): |
| 580 |
self.loop._process_events = mock.Mock() |
| 581 |
|
| 582 |
def zero_error(fut): |
| 583 |
fut.set_result(True) |
| 584 |
1/0 |
| 585 |
|
| 586 |
# Test call_soon (events.Handle) |
| 587 |
with mock.patch('asyncio.base_events.logger') as log: |
| 588 |
fut = self.loop.create_future() |
| 589 |
self.loop.call_soon(zero_error, fut) |
| 590 |
fut.add_done_callback(lambda fut: self.loop.stop()) |
| 591 |
self.loop.run_forever() |
| 592 |
log.error.assert_called_with( |
| 593 |
test_utils.MockPattern('Exception in callback.*zero'), |
| 594 |
exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY)) |
| 595 |
|
| 596 |
# Test call_later (events.TimerHandle) |
| 597 |
with mock.patch('asyncio.base_events.logger') as log: |
| 598 |
fut = self.loop.create_future() |
| 599 |
self.loop.call_later(0.01, zero_error, fut) |
| 600 |
fut.add_done_callback(lambda fut: self.loop.stop()) |
| 601 |
self.loop.run_forever() |
| 602 |
log.error.assert_called_with( |
| 603 |
test_utils.MockPattern('Exception in callback.*zero'), |
| 604 |
exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY)) |
| 605 |
|
| 606 |
def test_default_exc_handler_coro(self): |
| 607 |
self.loop._process_events = mock.Mock() |
| 608 |
|
| 609 |
async def zero_error_coro(): |
| 610 |
await asyncio.sleep(0.01) |
| 611 |
1/0 |
| 612 |
|
| 613 |
# Test Future.__del__ |
| 614 |
with mock.patch('asyncio.base_events.logger') as log: |
| 615 |
fut = asyncio.ensure_future(zero_error_coro(), loop=self.loop) |
| 616 |
fut.add_done_callback(lambda *args: self.loop.stop()) |
| 617 |
self.loop.run_forever() |
| 618 |
fut = None # Trigger Future.__del__ or futures._TracebackLogger |
| 619 |
support.gc_collect() |
| 620 |
# Future.__del__ in logs error with an actual exception context |
| 621 |
log.error.assert_called_with( |
| 622 |
test_utils.MockPattern('.*exception was never retrieved'), |
| 623 |
exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY)) |
| 624 |
|
| 625 |
def test_set_exc_handler_invalid(self): |
| 626 |
with self.assertRaisesRegex(TypeError, 'A callable object or None'): |
| 627 |
self.loop.set_exception_handler('spam') |
| 628 |
|
| 629 |
def test_set_exc_handler_custom(self): |
| 630 |
def zero_error(): |
| 631 |
1/0 |
| 632 |
|
| 633 |
def run_loop(): |
| 634 |
handle = self.loop.call_soon(zero_error) |
| 635 |
self.loop._run_once() |
| 636 |
return handle |
| 637 |
|
| 638 |
self.loop.set_debug(True) |
| 639 |
self.loop._process_events = mock.Mock() |
| 640 |
|
| 641 |
self.assertIsNone(self.loop.get_exception_handler()) |
| 642 |
mock_handler = mock.Mock() |
| 643 |
self.loop.set_exception_handler(mock_handler) |
| 644 |
self.assertIs(self.loop.get_exception_handler(), mock_handler) |
| 645 |
handle = run_loop() |
| 646 |
mock_handler.assert_called_with(self.loop, { |
| 647 |
'exception': MOCK_ANY, |
| 648 |
'message': test_utils.MockPattern( |
| 649 |
'Exception in callback.*zero_error'), |
| 650 |
'handle': handle, |
| 651 |
'source_traceback': handle._source_traceback, |
| 652 |
}) |
| 653 |
mock_handler.reset_mock() |
| 654 |
|
| 655 |
self.loop.set_exception_handler(None) |
| 656 |
with mock.patch('asyncio.base_events.logger') as log: |
| 657 |
run_loop() |
| 658 |
log.error.assert_called_with( |
| 659 |
test_utils.MockPattern( |
| 660 |
'Exception in callback.*zero'), |
| 661 |
exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY)) |
| 662 |
|
| 663 |
self.assertFalse(mock_handler.called) |
| 664 |
|
| 665 |
def test_set_exc_handler_broken(self): |
| 666 |
def run_loop(): |
| 667 |
def zero_error(): |
| 668 |
1/0 |
| 669 |
self.loop.call_soon(zero_error) |
| 670 |
self.loop._run_once() |
| 671 |
|
| 672 |
def handler(loop, context): |
| 673 |
raise AttributeError('spam') |
| 674 |
|
| 675 |
self.loop._process_events = mock.Mock() |
| 676 |
|
| 677 |
self.loop.set_exception_handler(handler) |
| 678 |
|
| 679 |
with mock.patch('asyncio.base_events.logger') as log: |
| 680 |
run_loop() |
| 681 |
log.error.assert_called_with( |
| 682 |
test_utils.MockPattern( |
| 683 |
'Unhandled error in exception handler'), |
| 684 |
exc_info=(AttributeError, MOCK_ANY, MOCK_ANY)) |
| 685 |
|
| 686 |
def test_default_exc_handler_broken(self): |
| 687 |
_context = None |
| 688 |
|
| 689 |
class Loop(base_events.BaseEventLoop): |
| 690 |
|
| 691 |
_selector = mock.Mock() |
| 692 |
_process_events = mock.Mock() |
| 693 |
|
| 694 |
def default_exception_handler(self, context): |
| 695 |
nonlocal _context |
| 696 |
_context = context |
| 697 |
# Simulates custom buggy "default_exception_handler" |
| 698 |
raise ValueError('spam') |
| 699 |
|
| 700 |
loop = Loop() |
| 701 |
self.addCleanup(loop.close) |
| 702 |
asyncio.set_event_loop(loop) |
| 703 |
|
| 704 |
def run_loop(): |
| 705 |
def zero_error(): |
| 706 |
1/0 |
| 707 |
loop.call_soon(zero_error) |
| 708 |
loop._run_once() |
| 709 |
|
| 710 |
with mock.patch('asyncio.base_events.logger') as log: |
| 711 |
run_loop() |
| 712 |
log.error.assert_called_with( |
| 713 |
'Exception in default exception handler', |
| 714 |
exc_info=True) |
| 715 |
|
| 716 |
def custom_handler(loop, context): |
| 717 |
raise ValueError('ham') |
| 718 |
|
| 719 |
_context = None |
| 720 |
loop.set_exception_handler(custom_handler) |
| 721 |
with mock.patch('asyncio.base_events.logger') as log: |
| 722 |
run_loop() |
| 723 |
log.error.assert_called_with( |
| 724 |
test_utils.MockPattern('Exception in default exception.*' |
| 725 |
'while handling.*in custom'), |
| 726 |
exc_info=True) |
| 727 |
|
| 728 |
# Check that original context was passed to default |
| 729 |
# exception handler. |
| 730 |
self.assertIn('context', _context) |
| 731 |
self.assertIs(type(_context['context']['exception']), |
| 732 |
ZeroDivisionError) |
| 733 |
|
| 734 |
def test_set_task_factory_invalid(self): |
| 735 |
with self.assertRaisesRegex( |
| 736 |
TypeError, 'task factory must be a callable or None'): |
| 737 |
|
| 738 |
self.loop.set_task_factory(1) |
| 739 |
|
| 740 |
self.assertIsNone(self.loop.get_task_factory()) |
| 741 |
|
| 742 |
def test_set_task_factory(self): |
| 743 |
self.loop._process_events = mock.Mock() |
| 744 |
|
| 745 |
class MyTask(asyncio.Task): |
| 746 |
pass |
| 747 |
|
| 748 |
async def coro(): |
| 749 |
pass |
| 750 |
|
| 751 |
factory = lambda loop, coro: MyTask(coro, loop=loop) |
| 752 |
|
| 753 |
self.assertIsNone(self.loop.get_task_factory()) |
| 754 |
self.loop.set_task_factory(factory) |
| 755 |
self.assertIs(self.loop.get_task_factory(), factory) |
| 756 |
|
| 757 |
task = self.loop.create_task(coro()) |
| 758 |
self.assertTrue(isinstance(task, MyTask)) |
| 759 |
self.loop.run_until_complete(task) |
| 760 |
|
| 761 |
self.loop.set_task_factory(None) |
| 762 |
self.assertIsNone(self.loop.get_task_factory()) |
| 763 |
|
| 764 |
task = self.loop.create_task(coro()) |
| 765 |
self.assertTrue(isinstance(task, asyncio.Task)) |
| 766 |
self.assertFalse(isinstance(task, MyTask)) |
| 767 |
self.loop.run_until_complete(task) |
| 768 |
|
| 769 |
def test_env_var_debug(self): |
| 770 |
code = '\n'.join(( |
| 771 |
'import asyncio', |
| 772 |
'loop = asyncio.new_event_loop()', |
| 773 |
'print(loop.get_debug())')) |
| 774 |
|
| 775 |
# Test with -E to not fail if the unit test was run with |
| 776 |
# PYTHONASYNCIODEBUG set to a non-empty string |
| 777 |
sts, stdout, stderr = assert_python_ok('-E', '-c', code) |
| 778 |
self.assertEqual(stdout.rstrip(), b'False') |
| 779 |
|
| 780 |
sts, stdout, stderr = assert_python_ok('-c', code, |
| 781 |
PYTHONASYNCIODEBUG='', |
| 782 |
PYTHONDEVMODE='') |
| 783 |
self.assertEqual(stdout.rstrip(), b'False') |
| 784 |
|
| 785 |
sts, stdout, stderr = assert_python_ok('-c', code, |
| 786 |
PYTHONASYNCIODEBUG='1', |
| 787 |
PYTHONDEVMODE='') |
| 788 |
self.assertEqual(stdout.rstrip(), b'True') |
| 789 |
|
| 790 |
sts, stdout, stderr = assert_python_ok('-E', '-c', code, |
| 791 |
PYTHONASYNCIODEBUG='1') |
| 792 |
self.assertEqual(stdout.rstrip(), b'False') |
| 793 |
|
| 794 |
# -X dev |
| 795 |
sts, stdout, stderr = assert_python_ok('-E', '-X', 'dev', |
| 796 |
'-c', code) |
| 797 |
self.assertEqual(stdout.rstrip(), b'True') |
| 798 |
|
| 799 |
def test_create_task(self): |
| 800 |
class MyTask(asyncio.Task): |
| 801 |
pass |
| 802 |
|
| 803 |
async def test(): |
| 804 |
pass |
| 805 |
|
| 806 |
class EventLoop(base_events.BaseEventLoop): |
| 807 |
def create_task(self, coro): |
| 808 |
return MyTask(coro, loop=loop) |
| 809 |
|
| 810 |
loop = EventLoop() |
| 811 |
self.set_event_loop(loop) |
| 812 |
|
| 813 |
coro = test() |
| 814 |
task = asyncio.ensure_future(coro, loop=loop) |
| 815 |
self.assertIsInstance(task, MyTask) |
| 816 |
|
| 817 |
# make warnings quiet |
| 818 |
task._log_destroy_pending = False |
| 819 |
coro.close() |
| 820 |
|
| 821 |
def test_create_task_error_closes_coro(self): |
| 822 |
async def test(): |
| 823 |
pass |
| 824 |
loop = asyncio.new_event_loop() |
| 825 |
loop.close() |
| 826 |
with warnings.catch_warnings(record=True) as w: |
| 827 |
with self.assertRaises(RuntimeError): |
| 828 |
asyncio.ensure_future(test(), loop=loop) |
| 829 |
self.assertEqual(len(w), 0) |
| 830 |
|
| 831 |
|
| 832 |
def test_create_named_task_with_default_factory(self): |
| 833 |
async def test(): |
| 834 |
pass |
| 835 |
|
| 836 |
loop = asyncio.new_event_loop() |
| 837 |
task = loop.create_task(test(), name='test_task') |
| 838 |
try: |
| 839 |
self.assertEqual(task.get_name(), 'test_task') |
| 840 |
finally: |
| 841 |
loop.run_until_complete(task) |
| 842 |
loop.close() |
| 843 |
|
| 844 |
def test_create_named_task_with_custom_factory(self): |
| 845 |
def task_factory(loop, coro, **kwargs): |
| 846 |
return asyncio.Task(coro, loop=loop, **kwargs) |
| 847 |
|
| 848 |
async def test(): |
| 849 |
pass |
| 850 |
|
| 851 |
loop = asyncio.new_event_loop() |
| 852 |
loop.set_task_factory(task_factory) |
| 853 |
task = loop.create_task(test(), name='test_task') |
| 854 |
try: |
| 855 |
self.assertEqual(task.get_name(), 'test_task') |
| 856 |
finally: |
| 857 |
loop.run_until_complete(task) |
| 858 |
loop.close() |
| 859 |
|
| 860 |
def test_run_forever_keyboard_interrupt(self): |
| 861 |
# Python issue #22601: ensure that the temporary task created by |
| 862 |
# run_forever() consumes the KeyboardInterrupt and so don't log |
| 863 |
# a warning |
| 864 |
async def raise_keyboard_interrupt(): |
| 865 |
raise KeyboardInterrupt |
| 866 |
|
| 867 |
self.loop._process_events = mock.Mock() |
| 868 |
self.loop.call_exception_handler = mock.Mock() |
| 869 |
|
| 870 |
try: |
| 871 |
self.loop.run_until_complete(raise_keyboard_interrupt()) |
| 872 |
except KeyboardInterrupt: |
| 873 |
pass |
| 874 |
self.loop.close() |
| 875 |
support.gc_collect() |
| 876 |
|
| 877 |
self.assertFalse(self.loop.call_exception_handler.called) |
| 878 |
|
| 879 |
def test_run_until_complete_baseexception(self): |
| 880 |
# Python issue #22429: run_until_complete() must not schedule a pending |
| 881 |
# call to stop() if the future raised a BaseException |
| 882 |
async def raise_keyboard_interrupt(): |
| 883 |
raise KeyboardInterrupt |
| 884 |
|
| 885 |
self.loop._process_events = mock.Mock() |
| 886 |
|
| 887 |
with self.assertRaises(KeyboardInterrupt): |
| 888 |
self.loop.run_until_complete(raise_keyboard_interrupt()) |
| 889 |
|
| 890 |
def func(): |
| 891 |
self.loop.stop() |
| 892 |
func.called = True |
| 893 |
func.called = False |
| 894 |
self.loop.call_soon(self.loop.call_soon, func) |
| 895 |
self.loop.run_forever() |
| 896 |
self.assertTrue(func.called) |
| 897 |
|
| 898 |
def test_single_selecter_event_callback_after_stopping(self): |
| 899 |
# Python issue #25593: A stopped event loop may cause event callbacks |
| 900 |
# to run more than once. |
| 901 |
event_sentinel = object() |
| 902 |
callcount = 0 |
| 903 |
doer = None |
| 904 |
|
| 905 |
def proc_events(event_list): |
| 906 |
nonlocal doer |
| 907 |
if event_sentinel in event_list: |
| 908 |
doer = self.loop.call_soon(do_event) |
| 909 |
|
| 910 |
def do_event(): |
| 911 |
nonlocal callcount |
| 912 |
callcount += 1 |
| 913 |
self.loop.call_soon(clear_selector) |
| 914 |
|
| 915 |
def clear_selector(): |
| 916 |
doer.cancel() |
| 917 |
self.loop._selector.select.return_value = () |
| 918 |
|
| 919 |
self.loop._process_events = proc_events |
| 920 |
self.loop._selector.select.return_value = (event_sentinel,) |
| 921 |
|
| 922 |
for i in range(1, 3): |
| 923 |
with self.subTest('Loop %d/2' % i): |
| 924 |
self.loop.call_soon(self.loop.stop) |
| 925 |
self.loop.run_forever() |
| 926 |
self.assertEqual(callcount, 1) |
| 927 |
|
| 928 |
def test_run_once(self): |
| 929 |
# Simple test for test_utils.run_once(). It may seem strange |
| 930 |
# to have a test for this (the function isn't even used!) but |
| 931 |
# it's a de-factor standard API for library tests. This tests |
| 932 |
# the idiom: loop.call_soon(loop.stop); loop.run_forever(). |
| 933 |
count = 0 |
| 934 |
|
| 935 |
def callback(): |
| 936 |
nonlocal count |
| 937 |
count += 1 |
| 938 |
|
| 939 |
self.loop._process_events = mock.Mock() |
| 940 |
self.loop.call_soon(callback) |
| 941 |
test_utils.run_once(self.loop) |
| 942 |
self.assertEqual(count, 1) |
| 943 |
|
| 944 |
def test_run_forever_pre_stopped(self): |
| 945 |
# Test that the old idiom for pre-stopping the loop works. |
| 946 |
self.loop._process_events = mock.Mock() |
| 947 |
self.loop.stop() |
| 948 |
self.loop.run_forever() |
| 949 |
self.loop._selector.select.assert_called_once_with(0) |
| 950 |
|
| 951 |
def test_custom_run_forever_integration(self): |
| 952 |
# Test that the run_forever_setup() and run_forever_cleanup() primitives |
| 953 |
# can be used to implement a custom run_forever loop. |
| 954 |
self.loop._process_events = mock.Mock() |
| 955 |
|
| 956 |
count = 0 |
| 957 |
|
| 958 |
def callback(): |
| 959 |
nonlocal count |
| 960 |
count += 1 |
| 961 |
|
| 962 |
self.loop.call_soon(callback) |
| 963 |
|
| 964 |
# Set up the custom event loop |
| 965 |
self.loop._run_forever_setup() |
| 966 |
|
| 967 |
# Confirm the loop has been started |
| 968 |
self.assertEqual(asyncio.get_running_loop(), self.loop) |
| 969 |
self.assertTrue(self.loop.is_running()) |
| 970 |
|
| 971 |
# Our custom "event loop" just iterates 10 times before exiting. |
| 972 |
for i in range(10): |
| 973 |
self.loop._run_once() |
| 974 |
|
| 975 |
# Clean up the event loop |
| 976 |
self.loop._run_forever_cleanup() |
| 977 |
|
| 978 |
# Confirm the loop has been cleaned up |
| 979 |
with self.assertRaises(RuntimeError): |
| 980 |
asyncio.get_running_loop() |
| 981 |
self.assertFalse(self.loop.is_running()) |
| 982 |
|
| 983 |
# Confirm the loop actually did run, processing events 10 times, |
| 984 |
# and invoking the callback once. |
| 985 |
self.assertEqual(self.loop._process_events.call_count, 10) |
| 986 |
self.assertEqual(count, 1) |
| 987 |
|
| 988 |
async def leave_unfinalized_asyncgen(self): |
| 989 |
# Create an async generator, iterate it partially, and leave it |
| 990 |
# to be garbage collected. |
| 991 |
# Used in async generator finalization tests. |
| 992 |
# Depends on implementation details of garbage collector. Changes |
| 993 |
# in gc may break this function. |
| 994 |
status = {'started': False, |
| 995 |
'stopped': False, |
| 996 |
'finalized': False} |
| 997 |
|
| 998 |
async def agen(): |
| 999 |
status['started'] = True |
| 1000 |
try: |
| 1001 |
for item in ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR']: |
| 1002 |
yield item |
| 1003 |
finally: |
| 1004 |
status['finalized'] = True |
| 1005 |
|
| 1006 |
ag = agen() |
| 1007 |
ai = ag.__aiter__() |
| 1008 |
|
| 1009 |
async def iter_one(): |
| 1010 |
try: |
| 1011 |
item = await ai.__anext__() |
| 1012 |
except StopAsyncIteration: |
| 1013 |
return |
| 1014 |
if item == 'THREE': |
| 1015 |
status['stopped'] = True |
| 1016 |
return |
| 1017 |
asyncio.create_task(iter_one()) |
| 1018 |
|
| 1019 |
asyncio.create_task(iter_one()) |
| 1020 |
return status |
| 1021 |
|
| 1022 |
def test_asyncgen_finalization_by_gc(self): |
| 1023 |
# Async generators should be finalized when garbage collected. |
| 1024 |
self.loop._process_events = mock.Mock() |
| 1025 |
self.loop._write_to_self = mock.Mock() |
| 1026 |
with support.disable_gc(): |
| 1027 |
status = self.loop.run_until_complete(self.leave_unfinalized_asyncgen()) |
| 1028 |
while not status['stopped']: |
| 1029 |
test_utils.run_briefly(self.loop) |
| 1030 |
self.assertTrue(status['started']) |
| 1031 |
self.assertTrue(status['stopped']) |
| 1032 |
self.assertFalse(status['finalized']) |
| 1033 |
support.gc_collect() |
| 1034 |
test_utils.run_briefly(self.loop) |
| 1035 |
self.assertTrue(status['finalized']) |
| 1036 |
|
| 1037 |
def test_asyncgen_finalization_by_gc_in_other_thread(self): |
| 1038 |
# Python issue 34769: If garbage collector runs in another |
| 1039 |
# thread, async generators will not finalize in debug |
| 1040 |
# mode. |
| 1041 |
self.loop._process_events = mock.Mock() |
| 1042 |
self.loop._write_to_self = mock.Mock() |
| 1043 |
self.loop.set_debug(True) |
| 1044 |
with support.disable_gc(): |
| 1045 |
status = self.loop.run_until_complete(self.leave_unfinalized_asyncgen()) |
| 1046 |
while not status['stopped']: |
| 1047 |
test_utils.run_briefly(self.loop) |
| 1048 |
self.assertTrue(status['started']) |
| 1049 |
self.assertTrue(status['stopped']) |
| 1050 |
self.assertFalse(status['finalized']) |
| 1051 |
self.loop.run_until_complete( |
| 1052 |
self.loop.run_in_executor(None, support.gc_collect)) |
| 1053 |
test_utils.run_briefly(self.loop) |
| 1054 |
self.assertTrue(status['finalized']) |
| 1055 |
|
| 1056 |
|
| 1057 |
class MyProto(asyncio.Protocol): |
| 1058 |
done = None |
| 1059 |
|
| 1060 |
def __init__(self, create_future=False): |
| 1061 |
self.state = 'INITIAL' |
| 1062 |
self.nbytes = 0 |
| 1063 |
if create_future: |
| 1064 |
self.done = asyncio.get_running_loop().create_future() |
| 1065 |
|
| 1066 |
def _assert_state(self, *expected): |
| 1067 |
if self.state not in expected: |
| 1068 |
raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') |
| 1069 |
|
| 1070 |
def connection_made(self, transport): |
| 1071 |
self.transport = transport |
| 1072 |
self._assert_state('INITIAL') |
| 1073 |
self.state = 'CONNECTED' |
| 1074 |
transport.write(b'GET / HTTP/1.0\r\nHost: example.com\r\n\r\n') |
| 1075 |
|
| 1076 |
def data_received(self, data): |
| 1077 |
self._assert_state('CONNECTED') |
| 1078 |
self.nbytes += len(data) |
| 1079 |
|
| 1080 |
def eof_received(self): |
| 1081 |
self._assert_state('CONNECTED') |
| 1082 |
self.state = 'EOF' |
| 1083 |
|
| 1084 |
def connection_lost(self, exc): |
| 1085 |
self._assert_state('CONNECTED', 'EOF') |
| 1086 |
self.state = 'CLOSED' |
| 1087 |
if self.done: |
| 1088 |
self.done.set_result(None) |
| 1089 |
|
| 1090 |
|
| 1091 |
class MyDatagramProto(asyncio.DatagramProtocol): |
| 1092 |
done = None |
| 1093 |
|
| 1094 |
def __init__(self, create_future=False, loop=None): |
| 1095 |
self.state = 'INITIAL' |
| 1096 |
self.nbytes = 0 |
| 1097 |
if create_future: |
| 1098 |
self.done = loop.create_future() |
| 1099 |
|
| 1100 |
def _assert_state(self, expected): |
| 1101 |
if self.state != expected: |
| 1102 |
raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') |
| 1103 |
|
| 1104 |
def connection_made(self, transport): |
| 1105 |
self.transport = transport |
| 1106 |
self._assert_state('INITIAL') |
| 1107 |
self.state = 'INITIALIZED' |
| 1108 |
|
| 1109 |
def datagram_received(self, data, addr): |
| 1110 |
self._assert_state('INITIALIZED') |
| 1111 |
self.nbytes += len(data) |
| 1112 |
|
| 1113 |
def error_received(self, exc): |
| 1114 |
self._assert_state('INITIALIZED') |
| 1115 |
|
| 1116 |
def connection_lost(self, exc): |
| 1117 |
self._assert_state('INITIALIZED') |
| 1118 |
self.state = 'CLOSED' |
| 1119 |
if self.done: |
| 1120 |
self.done.set_result(None) |
| 1121 |
|
| 1122 |
|
| 1123 |
class BaseEventLoopWithSelectorTests(test_utils.TestCase): |
| 1124 |
|
| 1125 |
def setUp(self): |
| 1126 |
super().setUp() |
| 1127 |
self.loop = asyncio.SelectorEventLoop() |
| 1128 |
self.set_event_loop(self.loop) |
| 1129 |
|
| 1130 |
@mock.patch('socket.getnameinfo') |
| 1131 |
def test_getnameinfo(self, m_gai): |
| 1132 |
m_gai.side_effect = lambda *args: 42 |
| 1133 |
r = self.loop.run_until_complete(self.loop.getnameinfo(('abc', 123))) |
| 1134 |
self.assertEqual(r, 42) |
| 1135 |
|
| 1136 |
@patch_socket |
| 1137 |
def test_create_connection_multiple_errors(self, m_socket): |
| 1138 |
|
| 1139 |
class MyProto(asyncio.Protocol): |
| 1140 |
pass |
| 1141 |
|
| 1142 |
async def getaddrinfo(*args, **kw): |
| 1143 |
return [(2, 1, 6, '', ('107.6.106.82', 80)), |
| 1144 |
(2, 1, 6, '', ('107.6.106.82', 80))] |
| 1145 |
|
| 1146 |
def getaddrinfo_task(*args, **kwds): |
| 1147 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1148 |
|
| 1149 |
idx = -1 |
| 1150 |
errors = ['err1', 'err2'] |
| 1151 |
|
| 1152 |
def _socket(*args, **kw): |
| 1153 |
nonlocal idx, errors |
| 1154 |
idx += 1 |
| 1155 |
raise OSError(errors[idx]) |
| 1156 |
|
| 1157 |
m_socket.socket = _socket |
| 1158 |
|
| 1159 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1160 |
|
| 1161 |
coro = self.loop.create_connection(MyProto, 'example.com', 80) |
| 1162 |
with self.assertRaises(OSError) as cm: |
| 1163 |
self.loop.run_until_complete(coro) |
| 1164 |
|
| 1165 |
self.assertEqual(str(cm.exception), 'Multiple exceptions: err1, err2') |
| 1166 |
|
| 1167 |
idx = -1 |
| 1168 |
coro = self.loop.create_connection(MyProto, 'example.com', 80, all_errors=True) |
| 1169 |
with self.assertRaises(ExceptionGroup) as cm: |
| 1170 |
self.loop.run_until_complete(coro) |
| 1171 |
|
| 1172 |
self.assertIsInstance(cm.exception, ExceptionGroup) |
| 1173 |
for e in cm.exception.exceptions: |
| 1174 |
self.assertIsInstance(e, OSError) |
| 1175 |
|
| 1176 |
@patch_socket |
| 1177 |
def test_create_connection_timeout(self, m_socket): |
| 1178 |
# Ensure that the socket is closed on timeout |
| 1179 |
sock = mock.Mock() |
| 1180 |
m_socket.socket.return_value = sock |
| 1181 |
|
| 1182 |
def getaddrinfo(*args, **kw): |
| 1183 |
fut = self.loop.create_future() |
| 1184 |
addr = (socket.AF_INET, socket.SOCK_STREAM, 0, '', |
| 1185 |
('127.0.0.1', 80)) |
| 1186 |
fut.set_result([addr]) |
| 1187 |
return fut |
| 1188 |
self.loop.getaddrinfo = getaddrinfo |
| 1189 |
|
| 1190 |
with mock.patch.object(self.loop, 'sock_connect', |
| 1191 |
side_effect=asyncio.TimeoutError): |
| 1192 |
coro = self.loop.create_connection(MyProto, '127.0.0.1', 80) |
| 1193 |
with self.assertRaises(asyncio.TimeoutError): |
| 1194 |
self.loop.run_until_complete(coro) |
| 1195 |
self.assertTrue(sock.close.called) |
| 1196 |
|
| 1197 |
@patch_socket |
| 1198 |
def test_create_connection_happy_eyeballs_empty_exceptions(self, m_socket): |
| 1199 |
# See gh-135836: Fix IndexError when Happy Eyeballs algorithm |
| 1200 |
# results in empty exceptions list |
| 1201 |
|
| 1202 |
async def getaddrinfo(*args, **kw): |
| 1203 |
return [(socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 80)), |
| 1204 |
(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 80))] |
| 1205 |
|
| 1206 |
def getaddrinfo_task(*args, **kwds): |
| 1207 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1208 |
|
| 1209 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1210 |
|
| 1211 |
# Mock staggered_race to return empty exceptions list |
| 1212 |
# This simulates the scenario where Happy Eyeballs algorithm |
| 1213 |
# cancels all attempts but doesn't properly collect exceptions |
| 1214 |
with mock.patch('asyncio.staggered.staggered_race') as mock_staggered: |
| 1215 |
# Return (None, []) - no winner, empty exceptions list |
| 1216 |
async def mock_race(coro_fns, delay, loop): |
| 1217 |
return None, [] |
| 1218 |
mock_staggered.side_effect = mock_race |
| 1219 |
|
| 1220 |
coro = self.loop.create_connection( |
| 1221 |
MyProto, 'example.com', 80, happy_eyeballs_delay=0.1) |
| 1222 |
|
| 1223 |
# Should raise TimeoutError instead of IndexError |
| 1224 |
with self.assertRaisesRegex(TimeoutError, "create_connection failed"): |
| 1225 |
self.loop.run_until_complete(coro) |
| 1226 |
|
| 1227 |
def test_create_connection_host_port_sock(self): |
| 1228 |
coro = self.loop.create_connection( |
| 1229 |
MyProto, 'example.com', 80, sock=object()) |
| 1230 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1231 |
|
| 1232 |
def test_create_connection_wrong_sock(self): |
| 1233 |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 1234 |
with sock: |
| 1235 |
coro = self.loop.create_connection(MyProto, sock=sock) |
| 1236 |
with self.assertRaisesRegex(ValueError, |
| 1237 |
'A Stream Socket was expected'): |
| 1238 |
self.loop.run_until_complete(coro) |
| 1239 |
|
| 1240 |
def test_create_server_wrong_sock(self): |
| 1241 |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 1242 |
with sock: |
| 1243 |
coro = self.loop.create_server(MyProto, sock=sock) |
| 1244 |
with self.assertRaisesRegex(ValueError, |
| 1245 |
'A Stream Socket was expected'): |
| 1246 |
self.loop.run_until_complete(coro) |
| 1247 |
|
| 1248 |
def test_create_server_ssl_timeout_for_plain_socket(self): |
| 1249 |
coro = self.loop.create_server( |
| 1250 |
MyProto, 'example.com', 80, ssl_handshake_timeout=1) |
| 1251 |
with self.assertRaisesRegex( |
| 1252 |
ValueError, |
| 1253 |
'ssl_handshake_timeout is only meaningful with ssl'): |
| 1254 |
self.loop.run_until_complete(coro) |
| 1255 |
|
| 1256 |
@unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'), |
| 1257 |
'no socket.SOCK_NONBLOCK (linux only)') |
| 1258 |
def test_create_server_stream_bittype(self): |
| 1259 |
sock = socket.socket( |
| 1260 |
socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) |
| 1261 |
with sock: |
| 1262 |
coro = self.loop.create_server(lambda: None, sock=sock) |
| 1263 |
srv = self.loop.run_until_complete(coro) |
| 1264 |
srv.close() |
| 1265 |
self.loop.run_until_complete(srv.wait_closed()) |
| 1266 |
|
| 1267 |
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'no IPv6 support') |
| 1268 |
def test_create_server_ipv6(self): |
| 1269 |
async def main(): |
| 1270 |
srv = await asyncio.start_server(lambda: None, '::1', 0) |
| 1271 |
try: |
| 1272 |
self.assertGreater(len(srv.sockets), 0) |
| 1273 |
finally: |
| 1274 |
srv.close() |
| 1275 |
await srv.wait_closed() |
| 1276 |
|
| 1277 |
try: |
| 1278 |
self.loop.run_until_complete(main()) |
| 1279 |
except OSError as ex: |
| 1280 |
if (hasattr(errno, 'EADDRNOTAVAIL') and |
| 1281 |
ex.errno == errno.EADDRNOTAVAIL): |
| 1282 |
self.skipTest('failed to bind to ::1') |
| 1283 |
else: |
| 1284 |
raise |
| 1285 |
|
| 1286 |
def test_create_datagram_endpoint_wrong_sock(self): |
| 1287 |
sock = socket.socket(socket.AF_INET) |
| 1288 |
with sock: |
| 1289 |
coro = self.loop.create_datagram_endpoint(MyProto, sock=sock) |
| 1290 |
with self.assertRaisesRegex(ValueError, |
| 1291 |
'A datagram socket was expected'): |
| 1292 |
self.loop.run_until_complete(coro) |
| 1293 |
|
| 1294 |
def test_create_connection_no_host_port_sock(self): |
| 1295 |
coro = self.loop.create_connection(MyProto) |
| 1296 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1297 |
|
| 1298 |
def test_create_connection_no_getaddrinfo(self): |
| 1299 |
async def getaddrinfo(*args, **kw): |
| 1300 |
return [] |
| 1301 |
|
| 1302 |
def getaddrinfo_task(*args, **kwds): |
| 1303 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1304 |
|
| 1305 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1306 |
coro = self.loop.create_connection(MyProto, 'example.com', 80) |
| 1307 |
self.assertRaises( |
| 1308 |
OSError, self.loop.run_until_complete, coro) |
| 1309 |
|
| 1310 |
def test_create_connection_connect_err(self): |
| 1311 |
async def getaddrinfo(*args, **kw): |
| 1312 |
return [(2, 1, 6, '', ('107.6.106.82', 80))] |
| 1313 |
|
| 1314 |
def getaddrinfo_task(*args, **kwds): |
| 1315 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1316 |
|
| 1317 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1318 |
self.loop.sock_connect = mock.Mock() |
| 1319 |
self.loop.sock_connect.side_effect = OSError |
| 1320 |
|
| 1321 |
coro = self.loop.create_connection(MyProto, 'example.com', 80) |
| 1322 |
self.assertRaises( |
| 1323 |
OSError, self.loop.run_until_complete, coro) |
| 1324 |
|
| 1325 |
coro = self.loop.create_connection(MyProto, 'example.com', 80, all_errors=True) |
| 1326 |
with self.assertRaises(ExceptionGroup) as cm: |
| 1327 |
self.loop.run_until_complete(coro) |
| 1328 |
|
| 1329 |
self.assertIsInstance(cm.exception, ExceptionGroup) |
| 1330 |
self.assertEqual(len(cm.exception.exceptions), 1) |
| 1331 |
self.assertIsInstance(cm.exception.exceptions[0], OSError) |
| 1332 |
|
| 1333 |
@patch_socket |
| 1334 |
def test_create_connection_connect_non_os_err_close_err(self, m_socket): |
| 1335 |
# Test the case when sock_connect() raises non-OSError exception |
| 1336 |
# and sock.close() raises OSError. |
| 1337 |
async def getaddrinfo(*args, **kw): |
| 1338 |
return [(2, 1, 6, '', ('107.6.106.82', 80))] |
| 1339 |
|
| 1340 |
def getaddrinfo_task(*args, **kwds): |
| 1341 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1342 |
|
| 1343 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1344 |
self.loop.sock_connect = mock.Mock() |
| 1345 |
self.loop.sock_connect.side_effect = CustomError |
| 1346 |
sock = mock.Mock() |
| 1347 |
m_socket.socket.return_value = sock |
| 1348 |
sock.close.side_effect = OSError |
| 1349 |
|
| 1350 |
coro = self.loop.create_connection(MyProto, 'example.com', 80) |
| 1351 |
self.assertRaises( |
| 1352 |
CustomError, self.loop.run_until_complete, coro) |
| 1353 |
|
| 1354 |
coro = self.loop.create_connection(MyProto, 'example.com', 80, all_errors=True) |
| 1355 |
self.assertRaises( |
| 1356 |
CustomError, self.loop.run_until_complete, coro) |
| 1357 |
|
| 1358 |
def test_create_connection_multiple(self): |
| 1359 |
async def getaddrinfo(*args, **kw): |
| 1360 |
return [(2, 1, 6, '', ('0.0.0.1', 80)), |
| 1361 |
(2, 1, 6, '', ('0.0.0.2', 80))] |
| 1362 |
|
| 1363 |
def getaddrinfo_task(*args, **kwds): |
| 1364 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1365 |
|
| 1366 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1367 |
self.loop.sock_connect = mock.Mock() |
| 1368 |
self.loop.sock_connect.side_effect = OSError |
| 1369 |
|
| 1370 |
coro = self.loop.create_connection( |
| 1371 |
MyProto, 'example.com', 80, family=socket.AF_INET) |
| 1372 |
with self.assertRaises(OSError): |
| 1373 |
self.loop.run_until_complete(coro) |
| 1374 |
|
| 1375 |
coro = self.loop.create_connection( |
| 1376 |
MyProto, 'example.com', 80, family=socket.AF_INET, all_errors=True) |
| 1377 |
with self.assertRaises(ExceptionGroup) as cm: |
| 1378 |
self.loop.run_until_complete(coro) |
| 1379 |
|
| 1380 |
self.assertIsInstance(cm.exception, ExceptionGroup) |
| 1381 |
for e in cm.exception.exceptions: |
| 1382 |
self.assertIsInstance(e, OSError) |
| 1383 |
|
| 1384 |
@patch_socket |
| 1385 |
def test_create_connection_multiple_errors_local_addr(self, m_socket): |
| 1386 |
|
| 1387 |
def bind(addr): |
| 1388 |
if addr[0] == '0.0.0.1': |
| 1389 |
err = OSError('Err') |
| 1390 |
err.strerror = 'Err' |
| 1391 |
raise err |
| 1392 |
|
| 1393 |
m_socket.socket.return_value.bind = bind |
| 1394 |
|
| 1395 |
async def getaddrinfo(*args, **kw): |
| 1396 |
return [(2, 1, 6, '', ('0.0.0.1', 80)), |
| 1397 |
(2, 1, 6, '', ('0.0.0.2', 80))] |
| 1398 |
|
| 1399 |
def getaddrinfo_task(*args, **kwds): |
| 1400 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1401 |
|
| 1402 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1403 |
self.loop.sock_connect = mock.Mock() |
| 1404 |
self.loop.sock_connect.side_effect = OSError('Err2') |
| 1405 |
|
| 1406 |
coro = self.loop.create_connection( |
| 1407 |
MyProto, 'example.com', 80, family=socket.AF_INET, |
| 1408 |
local_addr=(None, 8080)) |
| 1409 |
with self.assertRaises(OSError) as cm: |
| 1410 |
self.loop.run_until_complete(coro) |
| 1411 |
|
| 1412 |
self.assertStartsWith(str(cm.exception), 'Multiple exceptions: ') |
| 1413 |
self.assertTrue(m_socket.socket.return_value.close.called) |
| 1414 |
|
| 1415 |
coro = self.loop.create_connection( |
| 1416 |
MyProto, 'example.com', 80, family=socket.AF_INET, |
| 1417 |
local_addr=(None, 8080), all_errors=True) |
| 1418 |
with self.assertRaises(ExceptionGroup) as cm: |
| 1419 |
self.loop.run_until_complete(coro) |
| 1420 |
|
| 1421 |
self.assertIsInstance(cm.exception, ExceptionGroup) |
| 1422 |
for e in cm.exception.exceptions: |
| 1423 |
self.assertIsInstance(e, OSError) |
| 1424 |
|
| 1425 |
def _test_create_connection_ip_addr(self, m_socket, allow_inet_pton): |
| 1426 |
# Test the fallback code, even if this system has inet_pton. |
| 1427 |
if not allow_inet_pton: |
| 1428 |
del m_socket.inet_pton |
| 1429 |
|
| 1430 |
m_socket.getaddrinfo = socket.getaddrinfo |
| 1431 |
sock = m_socket.socket.return_value |
| 1432 |
|
| 1433 |
self.loop._add_reader = mock.Mock() |
| 1434 |
self.loop._add_writer = mock.Mock() |
| 1435 |
|
| 1436 |
coro = self.loop.create_connection(asyncio.Protocol, '1.2.3.4', 80) |
| 1437 |
t, p = self.loop.run_until_complete(coro) |
| 1438 |
try: |
| 1439 |
sock.connect.assert_called_with(('1.2.3.4', 80)) |
| 1440 |
_, kwargs = m_socket.socket.call_args |
| 1441 |
self.assertEqual(kwargs['family'], m_socket.AF_INET) |
| 1442 |
self.assertEqual(kwargs['type'], m_socket.SOCK_STREAM) |
| 1443 |
finally: |
| 1444 |
t.close() |
| 1445 |
test_utils.run_briefly(self.loop) # allow transport to close |
| 1446 |
|
| 1447 |
if socket_helper.IPV6_ENABLED: |
| 1448 |
sock.family = socket.AF_INET6 |
| 1449 |
coro = self.loop.create_connection(asyncio.Protocol, '::1', 80) |
| 1450 |
t, p = self.loop.run_until_complete(coro) |
| 1451 |
try: |
| 1452 |
# Without inet_pton we use getaddrinfo, which transforms |
| 1453 |
# ('::1', 80) to ('::1', 80, 0, 0). The last 0s are flow info, |
| 1454 |
# scope id. |
| 1455 |
[address] = sock.connect.call_args[0] |
| 1456 |
host, port = address[:2] |
| 1457 |
self.assertRegex(host, r'::(0\.)*1') |
| 1458 |
self.assertEqual(port, 80) |
| 1459 |
_, kwargs = m_socket.socket.call_args |
| 1460 |
self.assertEqual(kwargs['family'], m_socket.AF_INET6) |
| 1461 |
self.assertEqual(kwargs['type'], m_socket.SOCK_STREAM) |
| 1462 |
finally: |
| 1463 |
t.close() |
| 1464 |
test_utils.run_briefly(self.loop) # allow transport to close |
| 1465 |
|
| 1466 |
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'no IPv6 support') |
| 1467 |
@unittest.skipIf(sys.platform.startswith('aix'), |
| 1468 |
"bpo-25545: IPv6 scope id and getaddrinfo() behave differently on AIX") |
| 1469 |
@patch_socket |
| 1470 |
def test_create_connection_ipv6_scope(self, m_socket): |
| 1471 |
m_socket.getaddrinfo = socket.getaddrinfo |
| 1472 |
sock = m_socket.socket.return_value |
| 1473 |
sock.family = socket.AF_INET6 |
| 1474 |
|
| 1475 |
self.loop._add_reader = mock.Mock() |
| 1476 |
self.loop._add_writer = mock.Mock() |
| 1477 |
|
| 1478 |
coro = self.loop.create_connection(asyncio.Protocol, 'fe80::1%1', 80) |
| 1479 |
t, p = self.loop.run_until_complete(coro) |
| 1480 |
try: |
| 1481 |
sock.connect.assert_called_with(('fe80::1', 80, 0, 1)) |
| 1482 |
_, kwargs = m_socket.socket.call_args |
| 1483 |
self.assertEqual(kwargs['family'], m_socket.AF_INET6) |
| 1484 |
self.assertEqual(kwargs['type'], m_socket.SOCK_STREAM) |
| 1485 |
finally: |
| 1486 |
t.close() |
| 1487 |
test_utils.run_briefly(self.loop) # allow transport to close |
| 1488 |
|
| 1489 |
@patch_socket |
| 1490 |
def test_create_connection_ip_addr(self, m_socket): |
| 1491 |
self._test_create_connection_ip_addr(m_socket, True) |
| 1492 |
|
| 1493 |
@patch_socket |
| 1494 |
def test_create_connection_no_inet_pton(self, m_socket): |
| 1495 |
self._test_create_connection_ip_addr(m_socket, False) |
| 1496 |
|
| 1497 |
@patch_socket |
| 1498 |
@unittest.skipIf( |
| 1499 |
support.is_android and platform.android_ver().api_level < 23, |
| 1500 |
"Issue gh-71123: this fails on Android before API level 23" |
| 1501 |
) |
| 1502 |
def test_create_connection_service_name(self, m_socket): |
| 1503 |
m_socket.getaddrinfo = socket.getaddrinfo |
| 1504 |
sock = m_socket.socket.return_value |
| 1505 |
|
| 1506 |
self.loop._add_reader = mock.Mock() |
| 1507 |
self.loop._add_writer = mock.Mock() |
| 1508 |
|
| 1509 |
for service, port in ('http', 80), (b'http', 80): |
| 1510 |
coro = self.loop.create_connection(asyncio.Protocol, |
| 1511 |
'127.0.0.1', service) |
| 1512 |
|
| 1513 |
t, p = self.loop.run_until_complete(coro) |
| 1514 |
try: |
| 1515 |
sock.connect.assert_called_with(('127.0.0.1', port)) |
| 1516 |
_, kwargs = m_socket.socket.call_args |
| 1517 |
self.assertEqual(kwargs['family'], m_socket.AF_INET) |
| 1518 |
self.assertEqual(kwargs['type'], m_socket.SOCK_STREAM) |
| 1519 |
finally: |
| 1520 |
t.close() |
| 1521 |
test_utils.run_briefly(self.loop) # allow transport to close |
| 1522 |
|
| 1523 |
for service in 'nonsense', b'nonsense': |
| 1524 |
coro = self.loop.create_connection(asyncio.Protocol, |
| 1525 |
'127.0.0.1', service) |
| 1526 |
|
| 1527 |
with self.assertRaises(OSError): |
| 1528 |
self.loop.run_until_complete(coro) |
| 1529 |
|
| 1530 |
def test_create_connection_no_local_addr(self): |
| 1531 |
async def getaddrinfo(host, *args, **kw): |
| 1532 |
if host == 'example.com': |
| 1533 |
return [(2, 1, 6, '', ('107.6.106.82', 80)), |
| 1534 |
(2, 1, 6, '', ('107.6.106.82', 80))] |
| 1535 |
else: |
| 1536 |
return [] |
| 1537 |
|
| 1538 |
def getaddrinfo_task(*args, **kwds): |
| 1539 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1540 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1541 |
|
| 1542 |
coro = self.loop.create_connection( |
| 1543 |
MyProto, 'example.com', 80, family=socket.AF_INET, |
| 1544 |
local_addr=(None, 8080)) |
| 1545 |
self.assertRaises( |
| 1546 |
OSError, self.loop.run_until_complete, coro) |
| 1547 |
|
| 1548 |
@patch_socket |
| 1549 |
def test_create_connection_bluetooth(self, m_socket): |
| 1550 |
# See http://bugs.python.org/issue27136, fallback to getaddrinfo when |
| 1551 |
# we can't recognize an address is resolved, e.g. a Bluetooth address. |
| 1552 |
addr = ('00:01:02:03:04:05', 1) |
| 1553 |
|
| 1554 |
def getaddrinfo(host, port, *args, **kw): |
| 1555 |
self.assertEqual((host, port), addr) |
| 1556 |
return [(999, 1, 999, '', (addr, 1))] |
| 1557 |
|
| 1558 |
m_socket.getaddrinfo = getaddrinfo |
| 1559 |
sock = m_socket.socket() |
| 1560 |
coro = self.loop.sock_connect(sock, addr) |
| 1561 |
self.loop.run_until_complete(coro) |
| 1562 |
|
| 1563 |
def test_create_connection_ssl_server_hostname_default(self): |
| 1564 |
self.loop.getaddrinfo = mock.Mock() |
| 1565 |
|
| 1566 |
def mock_getaddrinfo(*args, **kwds): |
| 1567 |
f = self.loop.create_future() |
| 1568 |
f.set_result([(socket.AF_INET, socket.SOCK_STREAM, |
| 1569 |
socket.SOL_TCP, '', ('1.2.3.4', 80))]) |
| 1570 |
return f |
| 1571 |
|
| 1572 |
self.loop.getaddrinfo.side_effect = mock_getaddrinfo |
| 1573 |
self.loop.sock_connect = mock.Mock() |
| 1574 |
self.loop.sock_connect.return_value = self.loop.create_future() |
| 1575 |
self.loop.sock_connect.return_value.set_result(None) |
| 1576 |
self.loop._make_ssl_transport = mock.Mock() |
| 1577 |
|
| 1578 |
class _SelectorTransportMock: |
| 1579 |
_sock = None |
| 1580 |
|
| 1581 |
def get_extra_info(self, key): |
| 1582 |
return mock.Mock() |
| 1583 |
|
| 1584 |
def close(self): |
| 1585 |
self._sock.close() |
| 1586 |
|
| 1587 |
def mock_make_ssl_transport(sock, protocol, sslcontext, waiter, |
| 1588 |
**kwds): |
| 1589 |
waiter.set_result(None) |
| 1590 |
transport = _SelectorTransportMock() |
| 1591 |
transport._sock = sock |
| 1592 |
return transport |
| 1593 |
|
| 1594 |
self.loop._make_ssl_transport.side_effect = mock_make_ssl_transport |
| 1595 |
ANY = mock.ANY |
| 1596 |
handshake_timeout = object() |
| 1597 |
shutdown_timeout = object() |
| 1598 |
# First try the default server_hostname. |
| 1599 |
self.loop._make_ssl_transport.reset_mock() |
| 1600 |
coro = self.loop.create_connection( |
| 1601 |
MyProto, 'python.org', 80, ssl=True, |
| 1602 |
ssl_handshake_timeout=handshake_timeout, |
| 1603 |
ssl_shutdown_timeout=shutdown_timeout) |
| 1604 |
transport, _ = self.loop.run_until_complete(coro) |
| 1605 |
transport.close() |
| 1606 |
self.loop._make_ssl_transport.assert_called_with( |
| 1607 |
ANY, ANY, ANY, ANY, |
| 1608 |
server_side=False, |
| 1609 |
server_hostname='python.org', |
| 1610 |
ssl_handshake_timeout=handshake_timeout, |
| 1611 |
ssl_shutdown_timeout=shutdown_timeout) |
| 1612 |
# Next try an explicit server_hostname. |
| 1613 |
self.loop._make_ssl_transport.reset_mock() |
| 1614 |
coro = self.loop.create_connection( |
| 1615 |
MyProto, 'python.org', 80, ssl=True, |
| 1616 |
server_hostname='perl.com', |
| 1617 |
ssl_handshake_timeout=handshake_timeout, |
| 1618 |
ssl_shutdown_timeout=shutdown_timeout) |
| 1619 |
transport, _ = self.loop.run_until_complete(coro) |
| 1620 |
transport.close() |
| 1621 |
self.loop._make_ssl_transport.assert_called_with( |
| 1622 |
ANY, ANY, ANY, ANY, |
| 1623 |
server_side=False, |
| 1624 |
server_hostname='perl.com', |
| 1625 |
ssl_handshake_timeout=handshake_timeout, |
| 1626 |
ssl_shutdown_timeout=shutdown_timeout) |
| 1627 |
# Finally try an explicit empty server_hostname. |
| 1628 |
self.loop._make_ssl_transport.reset_mock() |
| 1629 |
coro = self.loop.create_connection( |
| 1630 |
MyProto, 'python.org', 80, ssl=True, |
| 1631 |
server_hostname='', |
| 1632 |
ssl_handshake_timeout=handshake_timeout, |
| 1633 |
ssl_shutdown_timeout=shutdown_timeout) |
| 1634 |
transport, _ = self.loop.run_until_complete(coro) |
| 1635 |
transport.close() |
| 1636 |
self.loop._make_ssl_transport.assert_called_with( |
| 1637 |
ANY, ANY, ANY, ANY, |
| 1638 |
server_side=False, |
| 1639 |
server_hostname='', |
| 1640 |
ssl_handshake_timeout=handshake_timeout, |
| 1641 |
ssl_shutdown_timeout=shutdown_timeout) |
| 1642 |
|
| 1643 |
def test_create_connection_no_ssl_server_hostname_errors(self): |
| 1644 |
# When not using ssl, server_hostname must be None. |
| 1645 |
coro = self.loop.create_connection(MyProto, 'python.org', 80, |
| 1646 |
server_hostname='') |
| 1647 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1648 |
coro = self.loop.create_connection(MyProto, 'python.org', 80, |
| 1649 |
server_hostname='python.org') |
| 1650 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1651 |
|
| 1652 |
def test_create_connection_ssl_server_hostname_errors(self): |
| 1653 |
# When using ssl, server_hostname may be None if host is non-empty. |
| 1654 |
coro = self.loop.create_connection(MyProto, '', 80, ssl=True) |
| 1655 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1656 |
coro = self.loop.create_connection(MyProto, None, 80, ssl=True) |
| 1657 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1658 |
sock = socket.socket() |
| 1659 |
coro = self.loop.create_connection(MyProto, None, None, |
| 1660 |
ssl=True, sock=sock) |
| 1661 |
self.addCleanup(sock.close) |
| 1662 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1663 |
|
| 1664 |
def test_create_connection_ssl_timeout_for_plain_socket(self): |
| 1665 |
coro = self.loop.create_connection( |
| 1666 |
MyProto, 'example.com', 80, ssl_handshake_timeout=1) |
| 1667 |
with self.assertRaisesRegex( |
| 1668 |
ValueError, |
| 1669 |
'ssl_handshake_timeout is only meaningful with ssl'): |
| 1670 |
self.loop.run_until_complete(coro) |
| 1671 |
|
| 1672 |
def test_create_server_empty_host(self): |
| 1673 |
# if host is empty string use None instead |
| 1674 |
host = object() |
| 1675 |
|
| 1676 |
async def getaddrinfo(*args, **kw): |
| 1677 |
nonlocal host |
| 1678 |
host = args[0] |
| 1679 |
return [] |
| 1680 |
|
| 1681 |
def getaddrinfo_task(*args, **kwds): |
| 1682 |
return self.loop.create_task(getaddrinfo(*args, **kwds)) |
| 1683 |
|
| 1684 |
self.loop.getaddrinfo = getaddrinfo_task |
| 1685 |
fut = self.loop.create_server(MyProto, '', 0) |
| 1686 |
self.assertRaises(OSError, self.loop.run_until_complete, fut) |
| 1687 |
self.assertIsNone(host) |
| 1688 |
|
| 1689 |
def test_create_server_host_port_sock(self): |
| 1690 |
fut = self.loop.create_server( |
| 1691 |
MyProto, '0.0.0.0', 0, sock=object()) |
| 1692 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1693 |
|
| 1694 |
def test_create_server_no_host_port_sock(self): |
| 1695 |
fut = self.loop.create_server(MyProto) |
| 1696 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1697 |
|
| 1698 |
def test_create_server_no_getaddrinfo(self): |
| 1699 |
getaddrinfo = self.loop.getaddrinfo = mock.Mock() |
| 1700 |
getaddrinfo.return_value = self.loop.create_future() |
| 1701 |
getaddrinfo.return_value.set_result(None) |
| 1702 |
|
| 1703 |
f = self.loop.create_server(MyProto, 'python.org', 0) |
| 1704 |
self.assertRaises(OSError, self.loop.run_until_complete, f) |
| 1705 |
|
| 1706 |
@patch_socket |
| 1707 |
def test_create_server_nosoreuseport(self, m_socket): |
| 1708 |
m_socket.getaddrinfo = socket.getaddrinfo |
| 1709 |
del m_socket.SO_REUSEPORT |
| 1710 |
m_socket.socket.return_value = mock.Mock() |
| 1711 |
|
| 1712 |
f = self.loop.create_server( |
| 1713 |
MyProto, '0.0.0.0', 0, reuse_port=True) |
| 1714 |
|
| 1715 |
self.assertRaises(ValueError, self.loop.run_until_complete, f) |
| 1716 |
|
| 1717 |
@patch_socket |
| 1718 |
def test_create_server_soreuseport_only_defined(self, m_socket): |
| 1719 |
m_socket.getaddrinfo = socket.getaddrinfo |
| 1720 |
m_socket.socket.return_value = mock.Mock() |
| 1721 |
m_socket.SO_REUSEPORT = -1 |
| 1722 |
|
| 1723 |
f = self.loop.create_server( |
| 1724 |
MyProto, '0.0.0.0', 0, reuse_port=True) |
| 1725 |
|
| 1726 |
self.assertRaises(ValueError, self.loop.run_until_complete, f) |
| 1727 |
|
| 1728 |
@patch_socket |
| 1729 |
def test_create_server_cant_bind(self, m_socket): |
| 1730 |
|
| 1731 |
class Err(OSError): |
| 1732 |
strerror = 'error' |
| 1733 |
|
| 1734 |
m_socket.getaddrinfo.return_value = [ |
| 1735 |
(2, 1, 6, '', ('127.0.0.1', 10100))] |
| 1736 |
m_sock = m_socket.socket.return_value = mock.Mock() |
| 1737 |
m_sock.bind.side_effect = Err |
| 1738 |
|
| 1739 |
fut = self.loop.create_server(MyProto, '0.0.0.0', 0) |
| 1740 |
self.assertRaises(OSError, self.loop.run_until_complete, fut) |
| 1741 |
self.assertTrue(m_sock.close.called) |
| 1742 |
|
| 1743 |
@patch_socket |
| 1744 |
def test_create_datagram_endpoint_no_addrinfo(self, m_socket): |
| 1745 |
m_socket.getaddrinfo.return_value = [] |
| 1746 |
|
| 1747 |
coro = self.loop.create_datagram_endpoint( |
| 1748 |
MyDatagramProto, local_addr=('localhost', 0)) |
| 1749 |
self.assertRaises( |
| 1750 |
OSError, self.loop.run_until_complete, coro) |
| 1751 |
|
| 1752 |
def test_create_datagram_endpoint_addr_error(self): |
| 1753 |
coro = self.loop.create_datagram_endpoint( |
| 1754 |
MyDatagramProto, local_addr='localhost') |
| 1755 |
self.assertRaises( |
| 1756 |
TypeError, self.loop.run_until_complete, coro) |
| 1757 |
coro = self.loop.create_datagram_endpoint( |
| 1758 |
MyDatagramProto, local_addr=('localhost', 1, 2, 3)) |
| 1759 |
self.assertRaises( |
| 1760 |
TypeError, self.loop.run_until_complete, coro) |
| 1761 |
|
| 1762 |
def test_create_datagram_endpoint_connect_err(self): |
| 1763 |
self.loop.sock_connect = mock.Mock() |
| 1764 |
self.loop.sock_connect.side_effect = OSError |
| 1765 |
|
| 1766 |
coro = self.loop.create_datagram_endpoint( |
| 1767 |
asyncio.DatagramProtocol, remote_addr=('127.0.0.1', 0)) |
| 1768 |
self.assertRaises( |
| 1769 |
OSError, self.loop.run_until_complete, coro) |
| 1770 |
|
| 1771 |
def test_create_datagram_endpoint_allow_broadcast(self): |
| 1772 |
protocol = MyDatagramProto(create_future=True, loop=self.loop) |
| 1773 |
self.loop.sock_connect = sock_connect = mock.Mock() |
| 1774 |
sock_connect.return_value = [] |
| 1775 |
|
| 1776 |
coro = self.loop.create_datagram_endpoint( |
| 1777 |
lambda: protocol, |
| 1778 |
remote_addr=('127.0.0.1', 0), |
| 1779 |
allow_broadcast=True) |
| 1780 |
|
| 1781 |
transport, _ = self.loop.run_until_complete(coro) |
| 1782 |
self.assertFalse(sock_connect.called) |
| 1783 |
|
| 1784 |
transport.close() |
| 1785 |
self.loop.run_until_complete(protocol.done) |
| 1786 |
self.assertEqual('CLOSED', protocol.state) |
| 1787 |
|
| 1788 |
@patch_socket |
| 1789 |
def test_create_datagram_endpoint_socket_err(self, m_socket): |
| 1790 |
m_socket.getaddrinfo = socket.getaddrinfo |
| 1791 |
m_socket.socket.side_effect = OSError |
| 1792 |
|
| 1793 |
coro = self.loop.create_datagram_endpoint( |
| 1794 |
asyncio.DatagramProtocol, family=socket.AF_INET) |
| 1795 |
self.assertRaises( |
| 1796 |
OSError, self.loop.run_until_complete, coro) |
| 1797 |
|
| 1798 |
coro = self.loop.create_datagram_endpoint( |
| 1799 |
asyncio.DatagramProtocol, local_addr=('127.0.0.1', 0)) |
| 1800 |
self.assertRaises( |
| 1801 |
OSError, self.loop.run_until_complete, coro) |
| 1802 |
|
| 1803 |
@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 not supported or enabled') |
| 1804 |
def test_create_datagram_endpoint_no_matching_family(self): |
| 1805 |
coro = self.loop.create_datagram_endpoint( |
| 1806 |
asyncio.DatagramProtocol, |
| 1807 |
remote_addr=('127.0.0.1', 0), local_addr=('::1', 0)) |
| 1808 |
self.assertRaises( |
| 1809 |
ValueError, self.loop.run_until_complete, coro) |
| 1810 |
|
| 1811 |
@patch_socket |
| 1812 |
def test_create_datagram_endpoint_setblk_err(self, m_socket): |
| 1813 |
m_socket.socket.return_value.setblocking.side_effect = OSError |
| 1814 |
|
| 1815 |
coro = self.loop.create_datagram_endpoint( |
| 1816 |
asyncio.DatagramProtocol, family=socket.AF_INET) |
| 1817 |
self.assertRaises( |
| 1818 |
OSError, self.loop.run_until_complete, coro) |
| 1819 |
self.assertTrue( |
| 1820 |
m_socket.socket.return_value.close.called) |
| 1821 |
|
| 1822 |
def test_create_datagram_endpoint_noaddr_nofamily(self): |
| 1823 |
coro = self.loop.create_datagram_endpoint( |
| 1824 |
asyncio.DatagramProtocol) |
| 1825 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1826 |
|
| 1827 |
@patch_socket |
| 1828 |
def test_create_datagram_endpoint_cant_bind(self, m_socket): |
| 1829 |
class Err(OSError): |
| 1830 |
pass |
| 1831 |
|
| 1832 |
m_socket.getaddrinfo = socket.getaddrinfo |
| 1833 |
m_sock = m_socket.socket.return_value = mock.Mock() |
| 1834 |
m_sock.bind.side_effect = Err |
| 1835 |
|
| 1836 |
fut = self.loop.create_datagram_endpoint( |
| 1837 |
MyDatagramProto, |
| 1838 |
local_addr=('127.0.0.1', 0), family=socket.AF_INET) |
| 1839 |
self.assertRaises(Err, self.loop.run_until_complete, fut) |
| 1840 |
self.assertTrue(m_sock.close.called) |
| 1841 |
|
| 1842 |
def test_create_datagram_endpoint_sock(self): |
| 1843 |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 1844 |
sock.bind(('127.0.0.1', 0)) |
| 1845 |
fut = self.loop.create_datagram_endpoint( |
| 1846 |
lambda: MyDatagramProto(create_future=True, loop=self.loop), |
| 1847 |
sock=sock) |
| 1848 |
transport, protocol = self.loop.run_until_complete(fut) |
| 1849 |
transport.close() |
| 1850 |
self.loop.run_until_complete(protocol.done) |
| 1851 |
self.assertEqual('CLOSED', protocol.state) |
| 1852 |
|
| 1853 |
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'No UNIX Sockets') |
| 1854 |
def test_create_datagram_endpoint_sock_unix(self): |
| 1855 |
fut = self.loop.create_datagram_endpoint( |
| 1856 |
lambda: MyDatagramProto(create_future=True, loop=self.loop), |
| 1857 |
family=socket.AF_UNIX) |
| 1858 |
transport, protocol = self.loop.run_until_complete(fut) |
| 1859 |
self.assertEqual(transport._sock.family, socket.AF_UNIX) |
| 1860 |
transport.close() |
| 1861 |
self.loop.run_until_complete(protocol.done) |
| 1862 |
self.assertEqual('CLOSED', protocol.state) |
| 1863 |
|
| 1864 |
@socket_helper.skip_unless_bind_unix_socket |
| 1865 |
def test_create_datagram_endpoint_existing_sock_unix(self): |
| 1866 |
with test_utils.unix_socket_path() as path: |
| 1867 |
sock = socket.socket(socket.AF_UNIX, type=socket.SOCK_DGRAM) |
| 1868 |
sock.bind(path) |
| 1869 |
sock.close() |
| 1870 |
|
| 1871 |
coro = self.loop.create_datagram_endpoint( |
| 1872 |
lambda: MyDatagramProto(create_future=True, loop=self.loop), |
| 1873 |
path, family=socket.AF_UNIX) |
| 1874 |
transport, protocol = self.loop.run_until_complete(coro) |
| 1875 |
transport.close() |
| 1876 |
self.loop.run_until_complete(protocol.done) |
| 1877 |
|
| 1878 |
def test_create_datagram_endpoint_sock_sockopts(self): |
| 1879 |
class FakeSock: |
| 1880 |
type = socket.SOCK_DGRAM |
| 1881 |
|
| 1882 |
fut = self.loop.create_datagram_endpoint( |
| 1883 |
MyDatagramProto, local_addr=('127.0.0.1', 0), sock=FakeSock()) |
| 1884 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1885 |
|
| 1886 |
fut = self.loop.create_datagram_endpoint( |
| 1887 |
MyDatagramProto, remote_addr=('127.0.0.1', 0), sock=FakeSock()) |
| 1888 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1889 |
|
| 1890 |
fut = self.loop.create_datagram_endpoint( |
| 1891 |
MyDatagramProto, family=1, sock=FakeSock()) |
| 1892 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1893 |
|
| 1894 |
fut = self.loop.create_datagram_endpoint( |
| 1895 |
MyDatagramProto, proto=1, sock=FakeSock()) |
| 1896 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1897 |
|
| 1898 |
fut = self.loop.create_datagram_endpoint( |
| 1899 |
MyDatagramProto, flags=1, sock=FakeSock()) |
| 1900 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1901 |
|
| 1902 |
fut = self.loop.create_datagram_endpoint( |
| 1903 |
MyDatagramProto, reuse_port=True, sock=FakeSock()) |
| 1904 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1905 |
|
| 1906 |
fut = self.loop.create_datagram_endpoint( |
| 1907 |
MyDatagramProto, allow_broadcast=True, sock=FakeSock()) |
| 1908 |
self.assertRaises(ValueError, self.loop.run_until_complete, fut) |
| 1909 |
|
| 1910 |
@unittest.skipIf(sys.platform == 'vxworks', |
| 1911 |
"SO_BROADCAST is enabled by default on VxWorks") |
| 1912 |
def test_create_datagram_endpoint_sockopts(self): |
| 1913 |
# Socket options should not be applied unless asked for. |
| 1914 |
# SO_REUSEPORT is not available on all platforms. |
| 1915 |
|
| 1916 |
coro = self.loop.create_datagram_endpoint( |
| 1917 |
lambda: MyDatagramProto(create_future=True, loop=self.loop), |
| 1918 |
local_addr=('127.0.0.1', 0)) |
| 1919 |
transport, protocol = self.loop.run_until_complete(coro) |
| 1920 |
sock = transport.get_extra_info('socket') |
| 1921 |
|
| 1922 |
reuseport_supported = hasattr(socket, 'SO_REUSEPORT') |
| 1923 |
|
| 1924 |
if reuseport_supported: |
| 1925 |
self.assertFalse( |
| 1926 |
sock.getsockopt( |
| 1927 |
socket.SOL_SOCKET, socket.SO_REUSEPORT)) |
| 1928 |
self.assertFalse( |
| 1929 |
sock.getsockopt( |
| 1930 |
socket.SOL_SOCKET, socket.SO_BROADCAST)) |
| 1931 |
|
| 1932 |
transport.close() |
| 1933 |
self.loop.run_until_complete(protocol.done) |
| 1934 |
self.assertEqual('CLOSED', protocol.state) |
| 1935 |
|
| 1936 |
coro = self.loop.create_datagram_endpoint( |
| 1937 |
lambda: MyDatagramProto(create_future=True, loop=self.loop), |
| 1938 |
local_addr=('127.0.0.1', 0), |
| 1939 |
reuse_port=reuseport_supported, |
| 1940 |
allow_broadcast=True) |
| 1941 |
transport, protocol = self.loop.run_until_complete(coro) |
| 1942 |
sock = transport.get_extra_info('socket') |
| 1943 |
|
| 1944 |
self.assertFalse( |
| 1945 |
sock.getsockopt( |
| 1946 |
socket.SOL_SOCKET, socket.SO_REUSEADDR)) |
| 1947 |
if reuseport_supported: |
| 1948 |
self.assertTrue( |
| 1949 |
sock.getsockopt( |
| 1950 |
socket.SOL_SOCKET, socket.SO_REUSEPORT)) |
| 1951 |
self.assertTrue( |
| 1952 |
sock.getsockopt( |
| 1953 |
socket.SOL_SOCKET, socket.SO_BROADCAST)) |
| 1954 |
|
| 1955 |
transport.close() |
| 1956 |
self.loop.run_until_complete(protocol.done) |
| 1957 |
self.assertEqual('CLOSED', protocol.state) |
| 1958 |
|
| 1959 |
@patch_socket |
| 1960 |
def test_create_datagram_endpoint_nosoreuseport(self, m_socket): |
| 1961 |
del m_socket.SO_REUSEPORT |
| 1962 |
m_socket.socket.return_value = mock.Mock() |
| 1963 |
|
| 1964 |
coro = self.loop.create_datagram_endpoint( |
| 1965 |
lambda: MyDatagramProto(loop=self.loop), |
| 1966 |
local_addr=('127.0.0.1', 0), |
| 1967 |
reuse_port=True) |
| 1968 |
|
| 1969 |
self.assertRaises(ValueError, self.loop.run_until_complete, coro) |
| 1970 |
|
| 1971 |
@patch_socket |
| 1972 |
def test_create_datagram_endpoint_ip_addr(self, m_socket): |
| 1973 |
def getaddrinfo(*args, **kw): |
| 1974 |
self.fail('should not have called getaddrinfo') |
| 1975 |
|
| 1976 |
m_socket.getaddrinfo = getaddrinfo |
| 1977 |
m_socket.socket.return_value.bind = bind = mock.Mock() |
| 1978 |
self.loop._add_reader = mock.Mock() |
| 1979 |
|
| 1980 |
reuseport_supported = hasattr(socket, 'SO_REUSEPORT') |
| 1981 |
coro = self.loop.create_datagram_endpoint( |
| 1982 |
lambda: MyDatagramProto(loop=self.loop), |
| 1983 |
local_addr=('1.2.3.4', 0), |
| 1984 |
reuse_port=reuseport_supported) |
| 1985 |
|
| 1986 |
t, p = self.loop.run_until_complete(coro) |
| 1987 |
try: |
| 1988 |
bind.assert_called_with(('1.2.3.4', 0)) |
| 1989 |
m_socket.socket.assert_called_with(family=m_socket.AF_INET, |
| 1990 |
proto=m_socket.IPPROTO_UDP, |
| 1991 |
type=m_socket.SOCK_DGRAM) |
| 1992 |
finally: |
| 1993 |
t.close() |
| 1994 |
test_utils.run_briefly(self.loop) # allow transport to close |
| 1995 |
|
| 1996 |
def test_accept_connection_retry(self): |
| 1997 |
sock = mock.Mock() |
| 1998 |
sock.accept.side_effect = BlockingIOError() |
| 1999 |
|
| 2000 |
self.loop._accept_connection(MyProto, sock) |
| 2001 |
self.assertFalse(sock.close.called) |
| 2002 |
|
| 2003 |
@mock.patch('asyncio.base_events.logger') |
| 2004 |
def test_accept_connection_exception(self, m_log): |
| 2005 |
sock = mock.Mock() |
| 2006 |
sock.fileno.return_value = 10 |
| 2007 |
sock.accept.side_effect = OSError(errno.EMFILE, 'Too many open files') |
| 2008 |
self.loop._remove_reader = mock.Mock() |
| 2009 |
self.loop.call_later = mock.Mock() |
| 2010 |
|
| 2011 |
self.loop._accept_connection(MyProto, sock) |
| 2012 |
self.assertTrue(m_log.error.called) |
| 2013 |
self.assertFalse(sock.close.called) |
| 2014 |
self.loop._remove_reader.assert_called_with(10) |
| 2015 |
self.loop.call_later.assert_called_with( |
| 2016 |
constants.ACCEPT_RETRY_DELAY, |
| 2017 |
# self.loop._start_serving |
| 2018 |
mock.ANY, |
| 2019 |
MyProto, sock, None, None, mock.ANY, mock.ANY, mock.ANY) |
| 2020 |
|
| 2021 |
def test_call_coroutine(self): |
| 2022 |
async def simple_coroutine(): |
| 2023 |
pass |
| 2024 |
|
| 2025 |
self.loop.set_debug(True) |
| 2026 |
coro_func = simple_coroutine |
| 2027 |
coro_obj = coro_func() |
| 2028 |
self.addCleanup(coro_obj.close) |
| 2029 |
for func in (coro_func, coro_obj): |
| 2030 |
with self.assertRaises(TypeError): |
| 2031 |
self.loop.call_soon(func) |
| 2032 |
with self.assertRaises(TypeError): |
| 2033 |
self.loop.call_soon_threadsafe(func) |
| 2034 |
with self.assertRaises(TypeError): |
| 2035 |
self.loop.call_later(60, func) |
| 2036 |
with self.assertRaises(TypeError): |
| 2037 |
self.loop.call_at(self.loop.time() + 60, func) |
| 2038 |
with self.assertRaises(TypeError): |
| 2039 |
self.loop.run_until_complete( |
| 2040 |
self.loop.run_in_executor(None, func)) |
| 2041 |
|
| 2042 |
@mock.patch('asyncio.base_events.logger') |
| 2043 |
def test_log_slow_callbacks(self, m_logger): |
| 2044 |
def stop_loop_cb(loop): |
| 2045 |
loop.stop() |
| 2046 |
|
| 2047 |
async def stop_loop_coro(loop): |
| 2048 |
loop.stop() |
| 2049 |
|
| 2050 |
asyncio.set_event_loop(self.loop) |
| 2051 |
self.loop.set_debug(True) |
| 2052 |
self.loop.slow_callback_duration = 0.0 |
| 2053 |
|
| 2054 |
# slow callback |
| 2055 |
self.loop.call_soon(stop_loop_cb, self.loop) |
| 2056 |
self.loop.run_forever() |
| 2057 |
fmt, *args = m_logger.warning.call_args[0] |
| 2058 |
self.assertRegex(fmt % tuple(args), |
| 2059 |
"^Executing <Handle.*stop_loop_cb.*> " |
| 2060 |
"took .* seconds$") |
| 2061 |
|
| 2062 |
# slow task |
| 2063 |
asyncio.ensure_future(stop_loop_coro(self.loop), loop=self.loop) |
| 2064 |
self.loop.run_forever() |
| 2065 |
fmt, *args = m_logger.warning.call_args[0] |
| 2066 |
self.assertRegex(fmt % tuple(args), |
| 2067 |
"^Executing <Task.*stop_loop_coro.*> " |
| 2068 |
"took .* seconds$") |
| 2069 |
|
| 2070 |
|
| 2071 |
class RunningLoopTests(unittest.TestCase): |
| 2072 |
|
| 2073 |
def test_running_loop_within_a_loop(self): |
| 2074 |
async def runner(loop): |
| 2075 |
loop.run_forever() |
| 2076 |
|
| 2077 |
loop = asyncio.new_event_loop() |
| 2078 |
outer_loop = asyncio.new_event_loop() |
| 2079 |
try: |
| 2080 |
with self.assertRaisesRegex(RuntimeError, |
| 2081 |
'while another loop is running'): |
| 2082 |
outer_loop.run_until_complete(runner(loop)) |
| 2083 |
finally: |
| 2084 |
loop.close() |
| 2085 |
outer_loop.close() |
| 2086 |
|
| 2087 |
|
| 2088 |
class BaseLoopSockSendfileTests(test_utils.TestCase): |
| 2089 |
|
| 2090 |
DATA = b"12345abcde" * 16 * 1024 # 160 KiB |
| 2091 |
|
| 2092 |
class MyProto(asyncio.Protocol): |
| 2093 |
|
| 2094 |
def __init__(self, loop): |
| 2095 |
self.started = False |
| 2096 |
self.closed = False |
| 2097 |
self.data = bytearray() |
| 2098 |
self.fut = loop.create_future() |
| 2099 |
self.transport = None |
| 2100 |
|
| 2101 |
def connection_made(self, transport): |
| 2102 |
self.started = True |
| 2103 |
self.transport = transport |
| 2104 |
|
| 2105 |
def data_received(self, data): |
| 2106 |
self.data.extend(data) |
| 2107 |
|
| 2108 |
def connection_lost(self, exc): |
| 2109 |
self.closed = True |
| 2110 |
self.fut.set_result(None) |
| 2111 |
self.transport = None |
| 2112 |
|
| 2113 |
async def wait_closed(self): |
| 2114 |
await self.fut |
| 2115 |
|
| 2116 |
@classmethod |
| 2117 |
def setUpClass(cls): |
| 2118 |
cls.__old_bufsize = constants.SENDFILE_FALLBACK_READBUFFER_SIZE |
| 2119 |
constants.SENDFILE_FALLBACK_READBUFFER_SIZE = 1024 * 16 |
| 2120 |
with open(os_helper.TESTFN, 'wb') as fp: |
| 2121 |
fp.write(cls.DATA) |
| 2122 |
super().setUpClass() |
| 2123 |
|
| 2124 |
@classmethod |
| 2125 |
def tearDownClass(cls): |
| 2126 |
constants.SENDFILE_FALLBACK_READBUFFER_SIZE = cls.__old_bufsize |
| 2127 |
os_helper.unlink(os_helper.TESTFN) |
| 2128 |
super().tearDownClass() |
| 2129 |
|
| 2130 |
def setUp(self): |
| 2131 |
from asyncio.selector_events import BaseSelectorEventLoop |
| 2132 |
# BaseSelectorEventLoop() has no native implementation |
| 2133 |
self.loop = BaseSelectorEventLoop() |
| 2134 |
self.set_event_loop(self.loop) |
| 2135 |
self.file = open(os_helper.TESTFN, 'rb') |
| 2136 |
self.addCleanup(self.file.close) |
| 2137 |
super().setUp() |
| 2138 |
|
| 2139 |
def make_socket(self, blocking=False): |
| 2140 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 2141 |
sock.setblocking(blocking) |
| 2142 |
self.addCleanup(sock.close) |
| 2143 |
return sock |
| 2144 |
|
| 2145 |
def run_loop(self, coro): |
| 2146 |
return self.loop.run_until_complete(coro) |
| 2147 |
|
| 2148 |
def prepare(self): |
| 2149 |
sock = self.make_socket() |
| 2150 |
proto = self.MyProto(self.loop) |
| 2151 |
server = self.run_loop(self.loop.create_server( |
| 2152 |
lambda: proto, socket_helper.HOST, 0, family=socket.AF_INET)) |
| 2153 |
addr = server.sockets[0].getsockname() |
| 2154 |
|
| 2155 |
for _ in range(10): |
| 2156 |
try: |
| 2157 |
self.run_loop(self.loop.sock_connect(sock, addr)) |
| 2158 |
except OSError: |
| 2159 |
self.run_loop(asyncio.sleep(0.5)) |
| 2160 |
continue |
| 2161 |
else: |
| 2162 |
break |
| 2163 |
else: |
| 2164 |
# One last try, so we get the exception |
| 2165 |
self.run_loop(self.loop.sock_connect(sock, addr)) |
| 2166 |
|
| 2167 |
def cleanup(): |
| 2168 |
server.close() |
| 2169 |
sock.close() |
| 2170 |
if proto.transport is not None: |
| 2171 |
proto.transport.close() |
| 2172 |
self.run_loop(proto.wait_closed()) |
| 2173 |
self.run_loop(server.wait_closed()) |
| 2174 |
|
| 2175 |
self.addCleanup(cleanup) |
| 2176 |
|
| 2177 |
return sock, proto |
| 2178 |
|
| 2179 |
def test__sock_sendfile_native_failure(self): |
| 2180 |
sock, proto = self.prepare() |
| 2181 |
|
| 2182 |
with self.assertRaisesRegex(asyncio.SendfileNotAvailableError, |
| 2183 |
"sendfile is not available"): |
| 2184 |
self.run_loop(self.loop._sock_sendfile_native(sock, self.file, |
| 2185 |
0, None)) |
| 2186 |
|
| 2187 |
self.assertEqual(proto.data, b'') |
| 2188 |
self.assertEqual(self.file.tell(), 0) |
| 2189 |
|
| 2190 |
def test_sock_sendfile_no_fallback(self): |
| 2191 |
sock, proto = self.prepare() |
| 2192 |
|
| 2193 |
with self.assertRaisesRegex(asyncio.SendfileNotAvailableError, |
| 2194 |
"sendfile is not available"): |
| 2195 |
self.run_loop(self.loop.sock_sendfile(sock, self.file, |
| 2196 |
fallback=False)) |
| 2197 |
|
| 2198 |
self.assertEqual(self.file.tell(), 0) |
| 2199 |
self.assertEqual(proto.data, b'') |
| 2200 |
|
| 2201 |
def test_sock_sendfile_fallback(self): |
| 2202 |
sock, proto = self.prepare() |
| 2203 |
|
| 2204 |
ret = self.run_loop(self.loop.sock_sendfile(sock, self.file)) |
| 2205 |
sock.close() |
| 2206 |
self.run_loop(proto.wait_closed()) |
| 2207 |
|
| 2208 |
self.assertEqual(ret, len(self.DATA)) |
| 2209 |
self.assertEqual(self.file.tell(), len(self.DATA)) |
| 2210 |
self.assertEqual(proto.data, self.DATA) |
| 2211 |
|
| 2212 |
def test_sock_sendfile_fallback_offset_and_count(self): |
| 2213 |
sock, proto = self.prepare() |
| 2214 |
|
| 2215 |
ret = self.run_loop(self.loop.sock_sendfile(sock, self.file, |
| 2216 |
1000, 2000)) |
| 2217 |
sock.close() |
| 2218 |
self.run_loop(proto.wait_closed()) |
| 2219 |
|
| 2220 |
self.assertEqual(ret, 2000) |
| 2221 |
self.assertEqual(self.file.tell(), 3000) |
| 2222 |
self.assertEqual(proto.data, self.DATA[1000:3000]) |
| 2223 |
|
| 2224 |
def test_blocking_socket(self): |
| 2225 |
self.loop.set_debug(True) |
| 2226 |
sock = self.make_socket(blocking=True) |
| 2227 |
with self.assertRaisesRegex(ValueError, "must be non-blocking"): |
| 2228 |
self.run_loop(self.loop.sock_sendfile(sock, self.file)) |
| 2229 |
|
| 2230 |
def test_nonbinary_file(self): |
| 2231 |
sock = self.make_socket() |
| 2232 |
with open(os_helper.TESTFN, encoding="utf-8") as f: |
| 2233 |
with self.assertRaisesRegex(ValueError, "binary mode"): |
| 2234 |
self.run_loop(self.loop.sock_sendfile(sock, f)) |
| 2235 |
|
| 2236 |
def test_nonstream_socket(self): |
| 2237 |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 2238 |
sock.setblocking(False) |
| 2239 |
self.addCleanup(sock.close) |
| 2240 |
with self.assertRaisesRegex(ValueError, "only SOCK_STREAM type"): |
| 2241 |
self.run_loop(self.loop.sock_sendfile(sock, self.file)) |
| 2242 |
|
| 2243 |
def test_notint_count(self): |
| 2244 |
sock = self.make_socket() |
| 2245 |
with self.assertRaisesRegex(TypeError, |
| 2246 |
"count must be a positive integer"): |
| 2247 |
self.run_loop(self.loop.sock_sendfile(sock, self.file, 0, 'count')) |
| 2248 |
|
| 2249 |
def test_negative_count(self): |
| 2250 |
sock = self.make_socket() |
| 2251 |
with self.assertRaisesRegex(ValueError, |
| 2252 |
"count must be a positive integer"): |
| 2253 |
self.run_loop(self.loop.sock_sendfile(sock, self.file, 0, -1)) |
| 2254 |
|
| 2255 |
def test_notint_offset(self): |
| 2256 |
sock = self.make_socket() |
| 2257 |
with self.assertRaisesRegex(TypeError, |
| 2258 |
"offset must be a non-negative integer"): |
| 2259 |
self.run_loop(self.loop.sock_sendfile(sock, self.file, 'offset')) |
| 2260 |
|
| 2261 |
def test_negative_offset(self): |
| 2262 |
sock = self.make_socket() |
| 2263 |
with self.assertRaisesRegex(ValueError, |
| 2264 |
"offset must be a non-negative integer"): |
| 2265 |
self.run_loop(self.loop.sock_sendfile(sock, self.file, -1)) |
| 2266 |
|
| 2267 |
|
| 2268 |
class TestSelectorUtils(test_utils.TestCase): |
| 2269 |
def check_set_nodelay(self, sock): |
| 2270 |
opt = sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY) |
| 2271 |
self.assertFalse(opt) |
| 2272 |
|
| 2273 |
base_events._set_nodelay(sock) |
| 2274 |
|
| 2275 |
opt = sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY) |
| 2276 |
self.assertTrue(opt) |
| 2277 |
|
| 2278 |
@unittest.skipUnless(hasattr(socket, 'TCP_NODELAY'), |
| 2279 |
'need socket.TCP_NODELAY') |
| 2280 |
def test_set_nodelay(self): |
| 2281 |
sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM, |
| 2282 |
proto=socket.IPPROTO_TCP) |
| 2283 |
with sock: |
| 2284 |
self.check_set_nodelay(sock) |
| 2285 |
|
| 2286 |
sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM, |
| 2287 |
proto=socket.IPPROTO_TCP) |
| 2288 |
with sock: |
| 2289 |
sock.setblocking(False) |
| 2290 |
self.check_set_nodelay(sock) |
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
if __name__ == '__main__': |
| 2295 |
unittest.main() |