| 1 |
import _thread |
| 2 |
import asyncio |
| 3 |
import contextvars |
| 4 |
import re |
| 5 |
import signal |
| 6 |
import sys |
| 7 |
import threading |
| 8 |
import unittest |
| 9 |
from test.test_asyncio import utils as test_utils |
| 10 |
from unittest import mock |
| 11 |
from unittest.mock import patch |
| 12 |
|
| 13 |
|
| 14 |
def tearDownModule(): |
| 15 |
asyncio.events._set_event_loop_policy(None) |
| 16 |
|
| 17 |
|
| 18 |
def interrupt_self(): |
| 19 |
_thread.interrupt_main() |
| 20 |
|
| 21 |
|
| 22 |
class TestPolicy(asyncio.events._AbstractEventLoopPolicy): |
| 23 |
|
| 24 |
def __init__(self, loop_factory): |
| 25 |
self.loop_factory = loop_factory |
| 26 |
self.loop = None |
| 27 |
|
| 28 |
def get_event_loop(self): |
| 29 |
# shouldn't ever be called by asyncio.run() |
| 30 |
raise RuntimeError |
| 31 |
|
| 32 |
def new_event_loop(self): |
| 33 |
return self.loop_factory() |
| 34 |
|
| 35 |
def set_event_loop(self, loop): |
| 36 |
if loop is not None: |
| 37 |
# we want to check if the loop is closed |
| 38 |
# in BaseTest.tearDown |
| 39 |
self.loop = loop |
| 40 |
|
| 41 |
|
| 42 |
class BaseTest(unittest.TestCase): |
| 43 |
|
| 44 |
def new_loop(self): |
| 45 |
loop = asyncio.BaseEventLoop() |
| 46 |
loop._process_events = mock.Mock() |
| 47 |
# Mock waking event loop from select |
| 48 |
loop._write_to_self = mock.Mock() |
| 49 |
loop._write_to_self.return_value = None |
| 50 |
loop._selector = mock.Mock() |
| 51 |
loop._selector.select.return_value = () |
| 52 |
loop.shutdown_ag_run = False |
| 53 |
|
| 54 |
async def shutdown_asyncgens(): |
| 55 |
loop.shutdown_ag_run = True |
| 56 |
loop.shutdown_asyncgens = shutdown_asyncgens |
| 57 |
|
| 58 |
return loop |
| 59 |
|
| 60 |
def setUp(self): |
| 61 |
super().setUp() |
| 62 |
|
| 63 |
policy = TestPolicy(self.new_loop) |
| 64 |
asyncio.events._set_event_loop_policy(policy) |
| 65 |
|
| 66 |
def tearDown(self): |
| 67 |
policy = asyncio.events._get_event_loop_policy() |
| 68 |
if policy.loop is not None: |
| 69 |
self.assertTrue(policy.loop.is_closed()) |
| 70 |
self.assertTrue(policy.loop.shutdown_ag_run) |
| 71 |
|
| 72 |
asyncio.events._set_event_loop_policy(None) |
| 73 |
super().tearDown() |
| 74 |
|
| 75 |
|
| 76 |
class RunTests(BaseTest): |
| 77 |
|
| 78 |
def test_asyncio_run_return(self): |
| 79 |
async def main(): |
| 80 |
await asyncio.sleep(0) |
| 81 |
return 42 |
| 82 |
|
| 83 |
self.assertEqual(asyncio.run(main()), 42) |
| 84 |
|
| 85 |
def test_asyncio_run_raises(self): |
| 86 |
async def main(): |
| 87 |
await asyncio.sleep(0) |
| 88 |
raise ValueError('spam') |
| 89 |
|
| 90 |
with self.assertRaisesRegex(ValueError, 'spam'): |
| 91 |
asyncio.run(main()) |
| 92 |
|
| 93 |
def test_asyncio_run_only_coro(self): |
| 94 |
for o in {1, lambda: None}: |
| 95 |
with self.subTest(obj=o), \ |
| 96 |
self.assertRaisesRegex(TypeError, |
| 97 |
'an awaitable is required'): |
| 98 |
asyncio.run(o) |
| 99 |
|
| 100 |
def test_asyncio_run_debug(self): |
| 101 |
async def main(expected): |
| 102 |
loop = asyncio.get_event_loop() |
| 103 |
self.assertIs(loop.get_debug(), expected) |
| 104 |
|
| 105 |
asyncio.run(main(False), debug=False) |
| 106 |
asyncio.run(main(True), debug=True) |
| 107 |
with mock.patch('asyncio.coroutines._is_debug_mode', lambda: True): |
| 108 |
asyncio.run(main(True)) |
| 109 |
asyncio.run(main(False), debug=False) |
| 110 |
with mock.patch('asyncio.coroutines._is_debug_mode', lambda: False): |
| 111 |
asyncio.run(main(True), debug=True) |
| 112 |
asyncio.run(main(False)) |
| 113 |
|
| 114 |
def test_asyncio_run_from_running_loop(self): |
| 115 |
async def main(): |
| 116 |
coro = main() |
| 117 |
try: |
| 118 |
asyncio.run(coro) |
| 119 |
finally: |
| 120 |
coro.close() # Suppress ResourceWarning |
| 121 |
|
| 122 |
with self.assertRaisesRegex(RuntimeError, |
| 123 |
'cannot be called from a running'): |
| 124 |
asyncio.run(main()) |
| 125 |
|
| 126 |
def test_asyncio_run_cancels_hanging_tasks(self): |
| 127 |
lo_task = None |
| 128 |
|
| 129 |
async def leftover(): |
| 130 |
await asyncio.sleep(0.1) |
| 131 |
|
| 132 |
async def main(): |
| 133 |
nonlocal lo_task |
| 134 |
lo_task = asyncio.create_task(leftover()) |
| 135 |
return 123 |
| 136 |
|
| 137 |
self.assertEqual(asyncio.run(main()), 123) |
| 138 |
self.assertTrue(lo_task.done()) |
| 139 |
|
| 140 |
def test_asyncio_run_reports_hanging_tasks_errors(self): |
| 141 |
lo_task = None |
| 142 |
call_exc_handler_mock = mock.Mock() |
| 143 |
|
| 144 |
async def leftover(): |
| 145 |
try: |
| 146 |
await asyncio.sleep(0.1) |
| 147 |
except asyncio.CancelledError: |
| 148 |
1 / 0 |
| 149 |
|
| 150 |
async def main(): |
| 151 |
loop = asyncio.get_running_loop() |
| 152 |
loop.call_exception_handler = call_exc_handler_mock |
| 153 |
|
| 154 |
nonlocal lo_task |
| 155 |
lo_task = asyncio.create_task(leftover()) |
| 156 |
return 123 |
| 157 |
|
| 158 |
self.assertEqual(asyncio.run(main()), 123) |
| 159 |
self.assertTrue(lo_task.done()) |
| 160 |
|
| 161 |
call_exc_handler_mock.assert_called_with({ |
| 162 |
'message': test_utils.MockPattern(r'asyncio.run.*shutdown'), |
| 163 |
'task': lo_task, |
| 164 |
'exception': test_utils.MockInstanceOf(ZeroDivisionError) |
| 165 |
}) |
| 166 |
|
| 167 |
def test_asyncio_run_closes_gens_after_hanging_tasks_errors(self): |
| 168 |
spinner = None |
| 169 |
lazyboy = None |
| 170 |
|
| 171 |
class FancyExit(Exception): |
| 172 |
pass |
| 173 |
|
| 174 |
async def fidget(): |
| 175 |
while True: |
| 176 |
yield 1 |
| 177 |
await asyncio.sleep(1) |
| 178 |
|
| 179 |
async def spin(): |
| 180 |
nonlocal spinner |
| 181 |
spinner = fidget() |
| 182 |
try: |
| 183 |
async for the_meaning_of_life in spinner: # NoQA |
| 184 |
pass |
| 185 |
except asyncio.CancelledError: |
| 186 |
1 / 0 |
| 187 |
|
| 188 |
async def main(): |
| 189 |
loop = asyncio.get_running_loop() |
| 190 |
loop.call_exception_handler = mock.Mock() |
| 191 |
|
| 192 |
nonlocal lazyboy |
| 193 |
lazyboy = asyncio.create_task(spin()) |
| 194 |
raise FancyExit |
| 195 |
|
| 196 |
with self.assertRaises(FancyExit): |
| 197 |
asyncio.run(main()) |
| 198 |
|
| 199 |
self.assertTrue(lazyboy.done()) |
| 200 |
|
| 201 |
self.assertIsNone(spinner.ag_frame) |
| 202 |
self.assertFalse(spinner.ag_running) |
| 203 |
|
| 204 |
def test_asyncio_run_set_event_loop(self): |
| 205 |
#See https://github.com/python/cpython/issues/93896 |
| 206 |
|
| 207 |
async def main(): |
| 208 |
await asyncio.sleep(0) |
| 209 |
return 42 |
| 210 |
|
| 211 |
policy = asyncio.events._get_event_loop_policy() |
| 212 |
policy.set_event_loop = mock.Mock() |
| 213 |
asyncio.run(main()) |
| 214 |
self.assertTrue(policy.set_event_loop.called) |
| 215 |
|
| 216 |
def test_asyncio_run_without_uncancel(self): |
| 217 |
# See https://github.com/python/cpython/issues/95097 |
| 218 |
class Task: |
| 219 |
def __init__(self, loop, coro, **kwargs): |
| 220 |
self._task = asyncio.Task(coro, loop=loop, **kwargs) |
| 221 |
|
| 222 |
def cancel(self, *args, **kwargs): |
| 223 |
return self._task.cancel(*args, **kwargs) |
| 224 |
|
| 225 |
def add_done_callback(self, *args, **kwargs): |
| 226 |
return self._task.add_done_callback(*args, **kwargs) |
| 227 |
|
| 228 |
def remove_done_callback(self, *args, **kwargs): |
| 229 |
return self._task.remove_done_callback(*args, **kwargs) |
| 230 |
|
| 231 |
@property |
| 232 |
def _asyncio_future_blocking(self): |
| 233 |
return self._task._asyncio_future_blocking |
| 234 |
|
| 235 |
def result(self, *args, **kwargs): |
| 236 |
return self._task.result(*args, **kwargs) |
| 237 |
|
| 238 |
def done(self, *args, **kwargs): |
| 239 |
return self._task.done(*args, **kwargs) |
| 240 |
|
| 241 |
def cancelled(self, *args, **kwargs): |
| 242 |
return self._task.cancelled(*args, **kwargs) |
| 243 |
|
| 244 |
def exception(self, *args, **kwargs): |
| 245 |
return self._task.exception(*args, **kwargs) |
| 246 |
|
| 247 |
def get_loop(self, *args, **kwargs): |
| 248 |
return self._task.get_loop(*args, **kwargs) |
| 249 |
|
| 250 |
def set_name(self, *args, **kwargs): |
| 251 |
return self._task.set_name(*args, **kwargs) |
| 252 |
|
| 253 |
async def main(): |
| 254 |
interrupt_self() |
| 255 |
await asyncio.Event().wait() |
| 256 |
|
| 257 |
def new_event_loop(): |
| 258 |
loop = self.new_loop() |
| 259 |
loop.set_task_factory(Task) |
| 260 |
return loop |
| 261 |
|
| 262 |
asyncio.events._set_event_loop_policy(TestPolicy(new_event_loop)) |
| 263 |
with self.assertRaises(asyncio.CancelledError): |
| 264 |
asyncio.run(main()) |
| 265 |
|
| 266 |
def test_asyncio_run_loop_factory(self): |
| 267 |
factory = mock.Mock() |
| 268 |
loop = factory.return_value = self.new_loop() |
| 269 |
|
| 270 |
async def main(): |
| 271 |
self.assertEqual(asyncio.get_running_loop(), loop) |
| 272 |
|
| 273 |
asyncio.run(main(), loop_factory=factory) |
| 274 |
factory.assert_called_once_with() |
| 275 |
|
| 276 |
def test_loop_factory_default_event_loop(self): |
| 277 |
async def main(): |
| 278 |
if sys.platform == "win32": |
| 279 |
self.assertIsInstance(asyncio.get_running_loop(), asyncio.ProactorEventLoop) |
| 280 |
else: |
| 281 |
self.assertIsInstance(asyncio.get_running_loop(), asyncio.SelectorEventLoop) |
| 282 |
|
| 283 |
|
| 284 |
asyncio.run(main(), loop_factory=asyncio.EventLoop) |
| 285 |
|
| 286 |
|
| 287 |
class RunnerTests(BaseTest): |
| 288 |
|
| 289 |
def test_non_debug(self): |
| 290 |
with asyncio.Runner(debug=False) as runner: |
| 291 |
self.assertFalse(runner.get_loop().get_debug()) |
| 292 |
|
| 293 |
def test_debug(self): |
| 294 |
with asyncio.Runner(debug=True) as runner: |
| 295 |
self.assertTrue(runner.get_loop().get_debug()) |
| 296 |
|
| 297 |
def test_custom_factory(self): |
| 298 |
loop = mock.Mock() |
| 299 |
with asyncio.Runner(loop_factory=lambda: loop) as runner: |
| 300 |
self.assertIs(runner.get_loop(), loop) |
| 301 |
|
| 302 |
def test_run(self): |
| 303 |
async def f(): |
| 304 |
await asyncio.sleep(0) |
| 305 |
return 'done' |
| 306 |
|
| 307 |
with asyncio.Runner() as runner: |
| 308 |
self.assertEqual('done', runner.run(f())) |
| 309 |
loop = runner.get_loop() |
| 310 |
|
| 311 |
with self.assertRaisesRegex( |
| 312 |
RuntimeError, |
| 313 |
"Runner is closed" |
| 314 |
): |
| 315 |
runner.get_loop() |
| 316 |
|
| 317 |
self.assertTrue(loop.is_closed()) |
| 318 |
|
| 319 |
def test_run_non_coro(self): |
| 320 |
with asyncio.Runner() as runner: |
| 321 |
with self.assertRaisesRegex( |
| 322 |
TypeError, |
| 323 |
"an awaitable is required" |
| 324 |
): |
| 325 |
runner.run(123) |
| 326 |
|
| 327 |
def test_run_future(self): |
| 328 |
with asyncio.Runner() as runner: |
| 329 |
fut = runner.get_loop().create_future() |
| 330 |
fut.set_result('done') |
| 331 |
self.assertEqual('done', runner.run(fut)) |
| 332 |
|
| 333 |
def test_run_awaitable(self): |
| 334 |
class MyAwaitable: |
| 335 |
def __await__(self): |
| 336 |
return self.run().__await__() |
| 337 |
|
| 338 |
@staticmethod |
| 339 |
async def run(): |
| 340 |
return 'done' |
| 341 |
|
| 342 |
with asyncio.Runner() as runner: |
| 343 |
self.assertEqual('done', runner.run(MyAwaitable())) |
| 344 |
|
| 345 |
def test_explicit_close(self): |
| 346 |
runner = asyncio.Runner() |
| 347 |
loop = runner.get_loop() |
| 348 |
runner.close() |
| 349 |
with self.assertRaisesRegex( |
| 350 |
RuntimeError, |
| 351 |
"Runner is closed" |
| 352 |
): |
| 353 |
runner.get_loop() |
| 354 |
|
| 355 |
self.assertTrue(loop.is_closed()) |
| 356 |
|
| 357 |
def test_double_close(self): |
| 358 |
runner = asyncio.Runner() |
| 359 |
loop = runner.get_loop() |
| 360 |
|
| 361 |
runner.close() |
| 362 |
self.assertTrue(loop.is_closed()) |
| 363 |
|
| 364 |
# the second call is no-op |
| 365 |
runner.close() |
| 366 |
self.assertTrue(loop.is_closed()) |
| 367 |
|
| 368 |
def test_second_with_block_raises(self): |
| 369 |
ret = [] |
| 370 |
|
| 371 |
async def f(arg): |
| 372 |
ret.append(arg) |
| 373 |
|
| 374 |
runner = asyncio.Runner() |
| 375 |
with runner: |
| 376 |
runner.run(f(1)) |
| 377 |
|
| 378 |
with self.assertRaisesRegex( |
| 379 |
RuntimeError, |
| 380 |
"Runner is closed" |
| 381 |
): |
| 382 |
with runner: |
| 383 |
runner.run(f(2)) |
| 384 |
|
| 385 |
self.assertEqual([1], ret) |
| 386 |
|
| 387 |
def test_run_keeps_context(self): |
| 388 |
cvar = contextvars.ContextVar("cvar", default=-1) |
| 389 |
|
| 390 |
async def f(val): |
| 391 |
old = cvar.get() |
| 392 |
await asyncio.sleep(0) |
| 393 |
cvar.set(val) |
| 394 |
return old |
| 395 |
|
| 396 |
async def get_context(): |
| 397 |
return contextvars.copy_context() |
| 398 |
|
| 399 |
with asyncio.Runner() as runner: |
| 400 |
self.assertEqual(-1, runner.run(f(1))) |
| 401 |
self.assertEqual(1, runner.run(f(2))) |
| 402 |
|
| 403 |
self.assertEqual(2, runner.run(get_context()).get(cvar)) |
| 404 |
|
| 405 |
def test_recursive_run(self): |
| 406 |
async def g(): |
| 407 |
pass |
| 408 |
|
| 409 |
async def f(): |
| 410 |
runner.run(g()) |
| 411 |
|
| 412 |
with asyncio.Runner() as runner: |
| 413 |
with self.assertWarnsRegex( |
| 414 |
RuntimeWarning, |
| 415 |
"coroutine .+ was never awaited", |
| 416 |
): |
| 417 |
with self.assertRaisesRegex( |
| 418 |
RuntimeError, |
| 419 |
re.escape( |
| 420 |
"Runner.run() cannot be called from a running event loop" |
| 421 |
), |
| 422 |
): |
| 423 |
runner.run(f()) |
| 424 |
|
| 425 |
def test_interrupt_call_soon(self): |
| 426 |
# The only case when task is not suspended by waiting a future |
| 427 |
# or another task |
| 428 |
assert threading.current_thread() is threading.main_thread() |
| 429 |
|
| 430 |
async def coro(): |
| 431 |
with self.assertRaises(asyncio.CancelledError): |
| 432 |
while True: |
| 433 |
await asyncio.sleep(0) |
| 434 |
raise asyncio.CancelledError() |
| 435 |
|
| 436 |
with asyncio.Runner() as runner: |
| 437 |
runner.get_loop().call_later(0.1, interrupt_self) |
| 438 |
with self.assertRaises(KeyboardInterrupt): |
| 439 |
runner.run(coro()) |
| 440 |
|
| 441 |
def test_interrupt_wait(self): |
| 442 |
# interrupting when waiting a future cancels both future and main task |
| 443 |
assert threading.current_thread() is threading.main_thread() |
| 444 |
|
| 445 |
async def coro(fut): |
| 446 |
with self.assertRaises(asyncio.CancelledError): |
| 447 |
await fut |
| 448 |
raise asyncio.CancelledError() |
| 449 |
|
| 450 |
with asyncio.Runner() as runner: |
| 451 |
fut = runner.get_loop().create_future() |
| 452 |
runner.get_loop().call_later(0.1, interrupt_self) |
| 453 |
|
| 454 |
with self.assertRaises(KeyboardInterrupt): |
| 455 |
runner.run(coro(fut)) |
| 456 |
|
| 457 |
self.assertTrue(fut.cancelled()) |
| 458 |
|
| 459 |
def test_interrupt_cancelled_task(self): |
| 460 |
# interrupting cancelled main task doesn't raise KeyboardInterrupt |
| 461 |
assert threading.current_thread() is threading.main_thread() |
| 462 |
|
| 463 |
async def subtask(task): |
| 464 |
await asyncio.sleep(0) |
| 465 |
task.cancel() |
| 466 |
interrupt_self() |
| 467 |
|
| 468 |
async def coro(): |
| 469 |
asyncio.create_task(subtask(asyncio.current_task())) |
| 470 |
await asyncio.sleep(10) |
| 471 |
|
| 472 |
with asyncio.Runner() as runner: |
| 473 |
with self.assertRaises(asyncio.CancelledError): |
| 474 |
runner.run(coro()) |
| 475 |
|
| 476 |
def test_signal_install_not_supported_ok(self): |
| 477 |
# signal.signal() can throw if the "main thread" doesn't have signals enabled |
| 478 |
assert threading.current_thread() is threading.main_thread() |
| 479 |
|
| 480 |
async def coro(): |
| 481 |
pass |
| 482 |
|
| 483 |
with asyncio.Runner() as runner: |
| 484 |
with patch.object( |
| 485 |
signal, |
| 486 |
"signal", |
| 487 |
side_effect=ValueError( |
| 488 |
"signal only works in main thread of the main interpreter" |
| 489 |
) |
| 490 |
): |
| 491 |
runner.run(coro()) |
| 492 |
|
| 493 |
def test_set_event_loop_called_once(self): |
| 494 |
# See https://github.com/python/cpython/issues/95736 |
| 495 |
async def coro(): |
| 496 |
pass |
| 497 |
|
| 498 |
policy = asyncio.events._get_event_loop_policy() |
| 499 |
policy.set_event_loop = mock.Mock() |
| 500 |
runner = asyncio.Runner() |
| 501 |
runner.run(coro()) |
| 502 |
runner.run(coro()) |
| 503 |
|
| 504 |
self.assertEqual(1, policy.set_event_loop.call_count) |
| 505 |
runner.close() |
| 506 |
|
| 507 |
def test_no_repr_is_call_on_the_task_result(self): |
| 508 |
# See https://github.com/python/cpython/issues/112559. |
| 509 |
class MyResult: |
| 510 |
def __init__(self): |
| 511 |
self.repr_count = 0 |
| 512 |
def __repr__(self): |
| 513 |
self.repr_count += 1 |
| 514 |
return super().__repr__() |
| 515 |
|
| 516 |
async def coro(): |
| 517 |
return MyResult() |
| 518 |
|
| 519 |
|
| 520 |
with asyncio.Runner() as runner: |
| 521 |
result = runner.run(coro()) |
| 522 |
|
| 523 |
self.assertEqual(0, result.repr_count) |
| 524 |
|
| 525 |
|
| 526 |
if __name__ == '__main__': |
| 527 |
unittest.main() |