|
10 | 10 | from uuid import uuid4
|
11 | 11 |
|
12 | 12 | from pulpcore.client.pulpcore import ApiException
|
| 13 | +from contextlib import contextmanager |
13 | 14 |
|
14 |
| -from pulpcore.tests.functional.utils import download_file |
| 15 | +from pulpcore.tests.functional.utils import download_file, PulpTaskError |
| 16 | +from pulpcore.constants import IMMEDIATE_TIMEOUT |
15 | 17 |
|
16 | 18 |
|
17 | 19 | @pytest.fixture(scope="module")
|
@@ -445,3 +447,151 @@ def test_cancel_task_group(pulpcore_bindings, dispatch_task_group, gen_user):
|
445 | 447 |
|
446 | 448 | with gen_user(model_roles=["core.task_owner"]):
|
447 | 449 | pulpcore_bindings.TaskGroupsApi.task_groups_cancel(tgroup_href, {"state": "canceled"})
|
| 450 | + |
| 451 | + |
| 452 | +LT_TIMEOUT = IMMEDIATE_TIMEOUT / 2 |
| 453 | +GT_TIMEOUT = IMMEDIATE_TIMEOUT * 2 |
| 454 | + |
| 455 | + |
| 456 | +class TestImmediateTaskWithNoResource: |
| 457 | + |
| 458 | + @pytest.mark.parallel |
| 459 | + def test_succeeds_on_api_worker(self, pulpcore_bindings, dispatch_task): |
| 460 | + """ |
| 461 | + GIVEN a task with no resource requirements |
| 462 | + AND the task IS an async function |
| 463 | + WHEN dispatching a task as immediate |
| 464 | + THEN the task completes with no associated worker |
| 465 | + """ |
| 466 | + task_href = dispatch_task( |
| 467 | + "pulpcore.app.tasks.test.asleep", args=(LT_TIMEOUT,), immediate=True |
| 468 | + ) |
| 469 | + task = pulpcore_bindings.TasksApi.read(task_href) |
| 470 | + assert task.state == "completed" |
| 471 | + assert task.worker is None |
| 472 | + |
| 473 | + @pytest.mark.parallel |
| 474 | + def test_executes_on_api_worker_when_no_async(self, pulpcore_bindings, dispatch_task, capsys): |
| 475 | + """ |
| 476 | + GIVEN a task with no resource requirements |
| 477 | + AND the task IS NOT an async function |
| 478 | + WHEN dispatching a task as immediate |
| 479 | + THEN the task completes with no associated worker |
| 480 | + """ |
| 481 | + # TODO: on 3.85 this should throw an error |
| 482 | + task_href = dispatch_task( |
| 483 | + "pulpcore.app.tasks.test.sleep", args=(LT_TIMEOUT,), immediate=True |
| 484 | + ) |
| 485 | + stderr_content = capsys.readouterr().err |
| 486 | + task = pulpcore_bindings.TasksApi.read(task_href) |
| 487 | + assert task.state == "completed" |
| 488 | + assert task.worker is None |
| 489 | + assert "Support for non-coroutine immediate tasks will be dropped" in stderr_content |
| 490 | + |
| 491 | + @pytest.mark.parallel |
| 492 | + def test_timeouts_on_api_worker(self, pulpcore_bindings, dispatch_task): |
| 493 | + """ |
| 494 | + GIVEN a task with no resource requirements |
| 495 | + AND the task is an async function |
| 496 | + WHEN dispatching a task as immediate |
| 497 | + AND it takes longer than timeout |
| 498 | + THEN the task fails with a timeout error message |
| 499 | + """ |
| 500 | + task_href = dispatch_task( |
| 501 | + "pulpcore.app.tasks.test.asleep", args=(GT_TIMEOUT,), immediate=True |
| 502 | + ) |
| 503 | + task = pulpcore_bindings.TasksApi.read(task_href) |
| 504 | + assert task.worker is None |
| 505 | + assert "task timed out after" in task.error["description"] |
| 506 | + |
| 507 | + |
| 508 | +@pytest.fixture |
| 509 | +def resource_blocker(pulpcore_bindings, dispatch_task): |
| 510 | + |
| 511 | + @contextmanager |
| 512 | + def _resource_blocker(exclusive_resources: list[str], duration=20): |
| 513 | + task_href = dispatch_task( |
| 514 | + "pulpcore.app.tasks.test.sleep", |
| 515 | + args=(duration,), |
| 516 | + exclusive_resources=exclusive_resources, |
| 517 | + ) |
| 518 | + yield |
| 519 | + # Trying to cancel a finished task will return a 409 code. |
| 520 | + # We can ignore if that's the case, because all we want here is to cut time down. |
| 521 | + # Otherwise it might be a real error. |
| 522 | + try: |
| 523 | + pulpcore_bindings.TasksApi.tasks_cancel(task_href, {"state": "canceled"}) |
| 524 | + except ApiException as e: |
| 525 | + if e.status != 409: |
| 526 | + raise |
| 527 | + |
| 528 | + return _resource_blocker |
| 529 | + |
| 530 | + |
| 531 | +class TestImmediateTaskWithBlockedResource: |
| 532 | + |
| 533 | + @pytest.mark.parallel |
| 534 | + def test_executes_in_task_worker( |
| 535 | + self, resource_blocker, dispatch_task, monitor_task, pulpcore_bindings |
| 536 | + ): |
| 537 | + """ |
| 538 | + GIVEN an async task requiring busy resources |
| 539 | + WHEN dispatching a task as immediate |
| 540 | + THEN the task completes with a worker |
| 541 | + """ |
| 542 | + COMMON_RESOURCE = str(uuid4()) |
| 543 | + with resource_blocker(exclusive_resources=[COMMON_RESOURCE]): |
| 544 | + task_href = dispatch_task( |
| 545 | + "pulpcore.app.tasks.test.asleep", |
| 546 | + args=(LT_TIMEOUT,), |
| 547 | + immediate=True, |
| 548 | + exclusive_resources=[COMMON_RESOURCE], |
| 549 | + ) |
| 550 | + task = monitor_task(task_href) |
| 551 | + assert task.state == "completed" |
| 552 | + assert task.worker is not None |
| 553 | + |
| 554 | + @pytest.mark.parallel |
| 555 | + def test_throws_when_non_deferrable( |
| 556 | + self, resource_blocker, pulpcore_bindings, dispatch_task, monitor_task |
| 557 | + ): |
| 558 | + """ |
| 559 | + GIVEN an async task requiring busy resources |
| 560 | + WHEN dispatching as immediate and not deferrable |
| 561 | + THEN an error is raised |
| 562 | + """ |
| 563 | + COMMON_RESOURCE = str(uuid4()) |
| 564 | + with resource_blocker(exclusive_resources=[COMMON_RESOURCE]): |
| 565 | + task_href = dispatch_task( |
| 566 | + "pulpcore.app.tasks.test.asleep", |
| 567 | + args=(0,), |
| 568 | + immediate=True, |
| 569 | + deferred=False, |
| 570 | + exclusive_resources=[COMMON_RESOURCE], |
| 571 | + ) |
| 572 | + task = pulpcore_bindings.TasksApi.read(task_href) |
| 573 | + assert task.state == "canceled" |
| 574 | + assert task.worker is None |
| 575 | + assert "Resources temporarily unavailable." in task.error["reason"] |
| 576 | + |
| 577 | + @pytest.mark.parallel |
| 578 | + def test_times_out_on_task_worker( |
| 579 | + self, resource_blocker, pulpcore_bindings, dispatch_task, monitor_task |
| 580 | + ): |
| 581 | + """ |
| 582 | + GIVEN an async task requiring busy resources |
| 583 | + WHEN dispatching a task as immediate |
| 584 | + AND it takes longer than timeout |
| 585 | + THEN an error is raised |
| 586 | + """ |
| 587 | + COMMON_RESOURCE = str(uuid4()) |
| 588 | + with pytest.raises(PulpTaskError) as ctx: |
| 589 | + with resource_blocker(exclusive_resources=[COMMON_RESOURCE]): |
| 590 | + task_href = dispatch_task( |
| 591 | + "pulpcore.app.tasks.test.asleep", |
| 592 | + args=(GT_TIMEOUT,), |
| 593 | + immediate=True, |
| 594 | + exclusive_resources=[COMMON_RESOURCE], |
| 595 | + ) |
| 596 | + monitor_task(task_href) |
| 597 | + assert "task timed out after" in ctx.value.task.error["description"] |
0 commit comments