| 1 |
"""Tests for base_events.py""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import contextvars |
| 5 |
import unittest |
| 6 |
|
| 7 |
from unittest import mock |
| 8 |
from asyncio import tasks |
| 9 |
from test.test_asyncio import utils as test_utils |
| 10 |
from test.support.script_helper import assert_python_ok |
| 11 |
|
| 12 |
MOCK_ANY = mock.ANY |
| 13 |
|
| 14 |
|
| 15 |
def tearDownModule(): |
| 16 |
asyncio.events._set_event_loop_policy(None) |
| 17 |
|
| 18 |
|
| 19 |
class EagerTaskFactoryLoopTests: |
| 20 |
|
| 21 |
Task = None |
| 22 |
|
| 23 |
def run_coro(self, coro): |
| 24 |
""" |
| 25 |
Helper method to run the `coro` coroutine in the test event loop. |
| 26 |
It helps with making sure the event loop is running before starting |
| 27 |
to execute `coro`. This is important for testing the eager step |
| 28 |
functionality, since an eager step is taken only if the event loop |
| 29 |
is already running. |
| 30 |
""" |
| 31 |
|
| 32 |
async def coro_runner(): |
| 33 |
self.assertTrue(asyncio.get_event_loop().is_running()) |
| 34 |
return await coro |
| 35 |
|
| 36 |
return self.loop.run_until_complete(coro) |
| 37 |
|
| 38 |
def setUp(self): |
| 39 |
super().setUp() |
| 40 |
self.loop = asyncio.new_event_loop() |
| 41 |
self.eager_task_factory = asyncio.create_eager_task_factory(self.Task) |
| 42 |
self.loop.set_task_factory(self.eager_task_factory) |
| 43 |
self.set_event_loop(self.loop) |
| 44 |
|
| 45 |
def test_eager_task_factory_set(self): |
| 46 |
self.assertIsNotNone(self.eager_task_factory) |
| 47 |
self.assertIs(self.loop.get_task_factory(), self.eager_task_factory) |
| 48 |
|
| 49 |
async def noop(): pass |
| 50 |
|
| 51 |
async def run(): |
| 52 |
t = self.loop.create_task(noop()) |
| 53 |
self.assertIsInstance(t, self.Task) |
| 54 |
await t |
| 55 |
|
| 56 |
self.run_coro(run()) |
| 57 |
|
| 58 |
def test_await_future_during_eager_step(self): |
| 59 |
|
| 60 |
async def set_result(fut, val): |
| 61 |
fut.set_result(val) |
| 62 |
|
| 63 |
async def run(): |
| 64 |
fut = self.loop.create_future() |
| 65 |
t = self.loop.create_task(set_result(fut, 'my message')) |
| 66 |
# assert the eager step completed the task |
| 67 |
self.assertTrue(t.done()) |
| 68 |
return await fut |
| 69 |
|
| 70 |
self.assertEqual(self.run_coro(run()), 'my message') |
| 71 |
|
| 72 |
def test_eager_completion(self): |
| 73 |
|
| 74 |
async def coro(): |
| 75 |
return 'hello' |
| 76 |
|
| 77 |
async def run(): |
| 78 |
t = self.loop.create_task(coro()) |
| 79 |
# assert the eager step completed the task |
| 80 |
self.assertTrue(t.done()) |
| 81 |
return await t |
| 82 |
|
| 83 |
self.assertEqual(self.run_coro(run()), 'hello') |
| 84 |
|
| 85 |
def test_block_after_eager_step(self): |
| 86 |
|
| 87 |
async def coro(): |
| 88 |
await asyncio.sleep(0.1) |
| 89 |
return 'finished after blocking' |
| 90 |
|
| 91 |
async def run(): |
| 92 |
t = self.loop.create_task(coro()) |
| 93 |
self.assertFalse(t.done()) |
| 94 |
result = await t |
| 95 |
self.assertTrue(t.done()) |
| 96 |
return result |
| 97 |
|
| 98 |
self.assertEqual(self.run_coro(run()), 'finished after blocking') |
| 99 |
|
| 100 |
def test_cancellation_after_eager_completion(self): |
| 101 |
|
| 102 |
async def coro(): |
| 103 |
return 'finished without blocking' |
| 104 |
|
| 105 |
async def run(): |
| 106 |
t = self.loop.create_task(coro()) |
| 107 |
t.cancel() |
| 108 |
result = await t |
| 109 |
# finished task can't be cancelled |
| 110 |
self.assertFalse(t.cancelled()) |
| 111 |
return result |
| 112 |
|
| 113 |
self.assertEqual(self.run_coro(run()), 'finished without blocking') |
| 114 |
|
| 115 |
def test_cancellation_after_eager_step_blocks(self): |
| 116 |
|
| 117 |
async def coro(): |
| 118 |
await asyncio.sleep(0.1) |
| 119 |
return 'finished after blocking' |
| 120 |
|
| 121 |
async def run(): |
| 122 |
t = self.loop.create_task(coro()) |
| 123 |
t.cancel('cancellation message') |
| 124 |
self.assertGreater(t.cancelling(), 0) |
| 125 |
result = await t |
| 126 |
|
| 127 |
with self.assertRaises(asyncio.CancelledError) as cm: |
| 128 |
self.run_coro(run()) |
| 129 |
|
| 130 |
self.assertEqual('cancellation message', cm.exception.args[0]) |
| 131 |
|
| 132 |
def test_current_task(self): |
| 133 |
captured_current_task = None |
| 134 |
|
| 135 |
async def coro(): |
| 136 |
nonlocal captured_current_task |
| 137 |
captured_current_task = asyncio.current_task() |
| 138 |
# verify the task before and after blocking is identical |
| 139 |
await asyncio.sleep(0.1) |
| 140 |
self.assertIs(asyncio.current_task(), captured_current_task) |
| 141 |
|
| 142 |
async def run(): |
| 143 |
t = self.loop.create_task(coro()) |
| 144 |
self.assertIs(captured_current_task, t) |
| 145 |
await t |
| 146 |
|
| 147 |
self.run_coro(run()) |
| 148 |
captured_current_task = None |
| 149 |
|
| 150 |
def test_all_tasks_with_eager_completion(self): |
| 151 |
captured_all_tasks = None |
| 152 |
|
| 153 |
async def coro(): |
| 154 |
nonlocal captured_all_tasks |
| 155 |
captured_all_tasks = asyncio.all_tasks() |
| 156 |
|
| 157 |
async def run(): |
| 158 |
t = self.loop.create_task(coro()) |
| 159 |
self.assertIn(t, captured_all_tasks) |
| 160 |
self.assertNotIn(t, asyncio.all_tasks()) |
| 161 |
|
| 162 |
self.run_coro(run()) |
| 163 |
|
| 164 |
def test_all_tasks_with_blocking(self): |
| 165 |
captured_eager_all_tasks = None |
| 166 |
|
| 167 |
async def coro(fut1, fut2): |
| 168 |
nonlocal captured_eager_all_tasks |
| 169 |
captured_eager_all_tasks = asyncio.all_tasks() |
| 170 |
await fut1 |
| 171 |
fut2.set_result(None) |
| 172 |
|
| 173 |
async def run(): |
| 174 |
fut1 = self.loop.create_future() |
| 175 |
fut2 = self.loop.create_future() |
| 176 |
t = self.loop.create_task(coro(fut1, fut2)) |
| 177 |
self.assertIn(t, captured_eager_all_tasks) |
| 178 |
self.assertIn(t, asyncio.all_tasks()) |
| 179 |
fut1.set_result(None) |
| 180 |
await fut2 |
| 181 |
self.assertNotIn(t, asyncio.all_tasks()) |
| 182 |
|
| 183 |
self.run_coro(run()) |
| 184 |
|
| 185 |
def test_context_vars(self): |
| 186 |
cv = contextvars.ContextVar('cv', default=0) |
| 187 |
|
| 188 |
coro_first_step_ran = False |
| 189 |
coro_second_step_ran = False |
| 190 |
|
| 191 |
async def coro(): |
| 192 |
nonlocal coro_first_step_ran |
| 193 |
nonlocal coro_second_step_ran |
| 194 |
self.assertEqual(cv.get(), 1) |
| 195 |
cv.set(2) |
| 196 |
self.assertEqual(cv.get(), 2) |
| 197 |
coro_first_step_ran = True |
| 198 |
await asyncio.sleep(0.1) |
| 199 |
self.assertEqual(cv.get(), 2) |
| 200 |
cv.set(3) |
| 201 |
self.assertEqual(cv.get(), 3) |
| 202 |
coro_second_step_ran = True |
| 203 |
|
| 204 |
async def run(): |
| 205 |
cv.set(1) |
| 206 |
t = self.loop.create_task(coro()) |
| 207 |
self.assertTrue(coro_first_step_ran) |
| 208 |
self.assertFalse(coro_second_step_ran) |
| 209 |
self.assertEqual(cv.get(), 1) |
| 210 |
await t |
| 211 |
self.assertTrue(coro_second_step_ran) |
| 212 |
self.assertEqual(cv.get(), 1) |
| 213 |
|
| 214 |
self.run_coro(run()) |
| 215 |
|
| 216 |
def test_staggered_race_with_eager_tasks(self): |
| 217 |
# See https://github.com/python/cpython/issues/124309 |
| 218 |
|
| 219 |
async def fail(): |
| 220 |
await asyncio.sleep(0) |
| 221 |
raise ValueError("no good") |
| 222 |
|
| 223 |
async def blocked(): |
| 224 |
fut = asyncio.Future() |
| 225 |
await fut |
| 226 |
|
| 227 |
async def run(): |
| 228 |
winner, index, excs = await asyncio.staggered.staggered_race( |
| 229 |
[ |
| 230 |
lambda: blocked(), |
| 231 |
lambda: asyncio.sleep(1, result="sleep1"), |
| 232 |
lambda: fail() |
| 233 |
], |
| 234 |
delay=0.25 |
| 235 |
) |
| 236 |
self.assertEqual(winner, 'sleep1') |
| 237 |
self.assertEqual(index, 1) |
| 238 |
self.assertIsNone(excs[index]) |
| 239 |
self.assertIsInstance(excs[0], asyncio.CancelledError) |
| 240 |
self.assertIsInstance(excs[2], ValueError) |
| 241 |
|
| 242 |
self.run_coro(run()) |
| 243 |
|
| 244 |
def test_staggered_race_with_eager_tasks_no_delay(self): |
| 245 |
# See https://github.com/python/cpython/issues/124309 |
| 246 |
async def fail(): |
| 247 |
raise ValueError("no good") |
| 248 |
|
| 249 |
async def run(): |
| 250 |
winner, index, excs = await asyncio.staggered.staggered_race( |
| 251 |
[ |
| 252 |
lambda: fail(), |
| 253 |
lambda: asyncio.sleep(1, result="sleep1"), |
| 254 |
lambda: asyncio.sleep(0, result="sleep0"), |
| 255 |
], |
| 256 |
delay=None |
| 257 |
) |
| 258 |
self.assertEqual(winner, 'sleep1') |
| 259 |
self.assertEqual(index, 1) |
| 260 |
self.assertIsNone(excs[index]) |
| 261 |
self.assertIsInstance(excs[0], ValueError) |
| 262 |
self.assertEqual(len(excs), 2) |
| 263 |
|
| 264 |
self.run_coro(run()) |
| 265 |
|
| 266 |
def test_eager_start_false(self): |
| 267 |
name = None |
| 268 |
|
| 269 |
async def asyncfn(): |
| 270 |
nonlocal name |
| 271 |
name = asyncio.current_task().get_name() |
| 272 |
|
| 273 |
async def main(): |
| 274 |
t = asyncio.get_running_loop().create_task( |
| 275 |
asyncfn(), eager_start=False, name="example" |
| 276 |
) |
| 277 |
self.assertFalse(t.done()) |
| 278 |
self.assertIsNone(name) |
| 279 |
await t |
| 280 |
self.assertEqual(name, "example") |
| 281 |
|
| 282 |
self.run_coro(main()) |
| 283 |
|
| 284 |
|
| 285 |
class PyEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase): |
| 286 |
Task = tasks._PyTask |
| 287 |
|
| 288 |
def setUp(self): |
| 289 |
self._all_tasks = asyncio.all_tasks |
| 290 |
self._current_task = asyncio.current_task |
| 291 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task |
| 292 |
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._py_all_tasks |
| 293 |
return super().setUp() |
| 294 |
|
| 295 |
def tearDown(self): |
| 296 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 297 |
asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks |
| 298 |
return super().tearDown() |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
@unittest.skipUnless(hasattr(tasks, '_CTask'), |
| 303 |
'requires the C _asyncio module') |
| 304 |
class CEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase): |
| 305 |
Task = getattr(tasks, '_CTask', None) |
| 306 |
|
| 307 |
def setUp(self): |
| 308 |
self._current_task = asyncio.current_task |
| 309 |
self._all_tasks = asyncio.all_tasks |
| 310 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task |
| 311 |
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._c_all_tasks |
| 312 |
return super().setUp() |
| 313 |
|
| 314 |
def tearDown(self): |
| 315 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 316 |
asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks |
| 317 |
return super().tearDown() |
| 318 |
|
| 319 |
def test_issue105987(self): |
| 320 |
code = """if 1: |
| 321 |
from _asyncio import _swap_current_task, _set_running_loop |
| 322 |
|
| 323 |
class DummyTask: |
| 324 |
pass |
| 325 |
|
| 326 |
class DummyLoop: |
| 327 |
pass |
| 328 |
|
| 329 |
l = DummyLoop() |
| 330 |
_set_running_loop(l) |
| 331 |
_swap_current_task(l, DummyTask()) |
| 332 |
t = _swap_current_task(l, None) |
| 333 |
""" |
| 334 |
|
| 335 |
_, out, err = assert_python_ok("-c", code) |
| 336 |
self.assertFalse(err) |
| 337 |
|
| 338 |
def test_issue122332(self): |
| 339 |
async def coro(): |
| 340 |
pass |
| 341 |
|
| 342 |
async def run(): |
| 343 |
task = self.loop.create_task(coro()) |
| 344 |
await task |
| 345 |
self.assertIsNone(task.get_coro()) |
| 346 |
|
| 347 |
self.run_coro(run()) |
| 348 |
|
| 349 |
def test_name(self): |
| 350 |
name = None |
| 351 |
async def coro(): |
| 352 |
nonlocal name |
| 353 |
name = asyncio.current_task().get_name() |
| 354 |
|
| 355 |
async def main(): |
| 356 |
task = self.loop.create_task(coro(), name="test name") |
| 357 |
self.assertEqual(name, "test name") |
| 358 |
await task |
| 359 |
|
| 360 |
self.run_coro(coro()) |
| 361 |
|
| 362 |
class AsyncTaskCounter: |
| 363 |
def __init__(self, loop, *, task_class, eager): |
| 364 |
self.suspense_count = 0 |
| 365 |
self.task_count = 0 |
| 366 |
|
| 367 |
def CountingTask(*args, eager_start=False, **kwargs): |
| 368 |
if not eager_start: |
| 369 |
self.task_count += 1 |
| 370 |
kwargs["eager_start"] = eager_start |
| 371 |
return task_class(*args, **kwargs) |
| 372 |
|
| 373 |
if eager: |
| 374 |
factory = asyncio.create_eager_task_factory(CountingTask) |
| 375 |
else: |
| 376 |
def factory(loop, coro, **kwargs): |
| 377 |
return CountingTask(coro, loop=loop, **kwargs) |
| 378 |
loop.set_task_factory(factory) |
| 379 |
|
| 380 |
def get(self): |
| 381 |
return self.task_count |
| 382 |
|
| 383 |
|
| 384 |
async def awaitable_chain(depth): |
| 385 |
if depth == 0: |
| 386 |
return 0 |
| 387 |
return 1 + await awaitable_chain(depth - 1) |
| 388 |
|
| 389 |
|
| 390 |
async def recursive_taskgroups(width, depth): |
| 391 |
if depth == 0: |
| 392 |
return |
| 393 |
|
| 394 |
async with asyncio.TaskGroup() as tg: |
| 395 |
futures = [ |
| 396 |
tg.create_task(recursive_taskgroups(width, depth - 1)) |
| 397 |
for _ in range(width) |
| 398 |
] |
| 399 |
|
| 400 |
|
| 401 |
async def recursive_gather(width, depth): |
| 402 |
if depth == 0: |
| 403 |
return |
| 404 |
|
| 405 |
await asyncio.gather( |
| 406 |
*[recursive_gather(width, depth - 1) for _ in range(width)] |
| 407 |
) |
| 408 |
|
| 409 |
|
| 410 |
class BaseTaskCountingTests: |
| 411 |
|
| 412 |
Task = None |
| 413 |
eager = None |
| 414 |
expected_task_count = None |
| 415 |
|
| 416 |
def setUp(self): |
| 417 |
super().setUp() |
| 418 |
self.loop = asyncio.new_event_loop() |
| 419 |
self.counter = AsyncTaskCounter(self.loop, task_class=self.Task, eager=self.eager) |
| 420 |
self.set_event_loop(self.loop) |
| 421 |
|
| 422 |
def test_awaitables_chain(self): |
| 423 |
observed_depth = self.loop.run_until_complete(awaitable_chain(100)) |
| 424 |
self.assertEqual(observed_depth, 100) |
| 425 |
self.assertEqual(self.counter.get(), 0 if self.eager else 1) |
| 426 |
|
| 427 |
def test_recursive_taskgroups(self): |
| 428 |
num_tasks = self.loop.run_until_complete(recursive_taskgroups(5, 4)) |
| 429 |
self.assertEqual(self.counter.get(), self.expected_task_count) |
| 430 |
|
| 431 |
def test_recursive_gather(self): |
| 432 |
self.loop.run_until_complete(recursive_gather(5, 4)) |
| 433 |
self.assertEqual(self.counter.get(), self.expected_task_count) |
| 434 |
|
| 435 |
|
| 436 |
class BaseNonEagerTaskFactoryTests(BaseTaskCountingTests): |
| 437 |
eager = False |
| 438 |
expected_task_count = 781 # 1 + 5 + 5^2 + 5^3 + 5^4 |
| 439 |
|
| 440 |
|
| 441 |
class BaseEagerTaskFactoryTests(BaseTaskCountingTests): |
| 442 |
eager = True |
| 443 |
expected_task_count = 0 |
| 444 |
|
| 445 |
|
| 446 |
class NonEagerTests(BaseNonEagerTaskFactoryTests, test_utils.TestCase): |
| 447 |
Task = asyncio.tasks._CTask |
| 448 |
|
| 449 |
def setUp(self): |
| 450 |
self._current_task = asyncio.current_task |
| 451 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task |
| 452 |
return super().setUp() |
| 453 |
|
| 454 |
def tearDown(self): |
| 455 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 456 |
return super().tearDown() |
| 457 |
|
| 458 |
class EagerTests(BaseEagerTaskFactoryTests, test_utils.TestCase): |
| 459 |
Task = asyncio.tasks._CTask |
| 460 |
|
| 461 |
def setUp(self): |
| 462 |
self._current_task = asyncio.current_task |
| 463 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task |
| 464 |
return super().setUp() |
| 465 |
|
| 466 |
def tearDown(self): |
| 467 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 468 |
return super().tearDown() |
| 469 |
|
| 470 |
|
| 471 |
class NonEagerPyTaskTests(BaseNonEagerTaskFactoryTests, test_utils.TestCase): |
| 472 |
Task = tasks._PyTask |
| 473 |
|
| 474 |
def setUp(self): |
| 475 |
self._current_task = asyncio.current_task |
| 476 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task |
| 477 |
return super().setUp() |
| 478 |
|
| 479 |
def tearDown(self): |
| 480 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 481 |
return super().tearDown() |
| 482 |
|
| 483 |
|
| 484 |
class EagerPyTaskTests(BaseEagerTaskFactoryTests, test_utils.TestCase): |
| 485 |
Task = tasks._PyTask |
| 486 |
|
| 487 |
def setUp(self): |
| 488 |
self._current_task = asyncio.current_task |
| 489 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task |
| 490 |
return super().setUp() |
| 491 |
|
| 492 |
def tearDown(self): |
| 493 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 494 |
return super().tearDown() |
| 495 |
|
| 496 |
@unittest.skipUnless(hasattr(tasks, '_CTask'), |
| 497 |
'requires the C _asyncio module') |
| 498 |
class NonEagerCTaskTests(BaseNonEagerTaskFactoryTests, test_utils.TestCase): |
| 499 |
Task = getattr(tasks, '_CTask', None) |
| 500 |
|
| 501 |
def setUp(self): |
| 502 |
self._current_task = asyncio.current_task |
| 503 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task |
| 504 |
return super().setUp() |
| 505 |
|
| 506 |
def tearDown(self): |
| 507 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 508 |
return super().tearDown() |
| 509 |
|
| 510 |
|
| 511 |
@unittest.skipUnless(hasattr(tasks, '_CTask'), |
| 512 |
'requires the C _asyncio module') |
| 513 |
class EagerCTaskTests(BaseEagerTaskFactoryTests, test_utils.TestCase): |
| 514 |
Task = getattr(tasks, '_CTask', None) |
| 515 |
|
| 516 |
def setUp(self): |
| 517 |
self._current_task = asyncio.current_task |
| 518 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task |
| 519 |
return super().setUp() |
| 520 |
|
| 521 |
def tearDown(self): |
| 522 |
asyncio.current_task = asyncio.tasks.current_task = self._current_task |
| 523 |
return super().tearDown() |
| 524 |
|
| 525 |
|
| 526 |
class DefaultTaskFactoryEagerStart(test_utils.TestCase): |
| 527 |
def test_eager_start_true_with_default_factory(self): |
| 528 |
name = None |
| 529 |
|
| 530 |
async def asyncfn(): |
| 531 |
nonlocal name |
| 532 |
name = asyncio.current_task().get_name() |
| 533 |
|
| 534 |
async def main(): |
| 535 |
t = asyncio.get_running_loop().create_task( |
| 536 |
asyncfn(), eager_start=True, name="example" |
| 537 |
) |
| 538 |
self.assertTrue(t.done()) |
| 539 |
self.assertEqual(name, "example") |
| 540 |
await t |
| 541 |
|
| 542 |
asyncio.run(main(), loop_factory=asyncio.EventLoop) |
| 543 |
|
| 544 |
if __name__ == '__main__': |
| 545 |
unittest.main() |