@@ -471,25 +471,22 @@ def test_succeeds_on_api_worker(self, pulpcore_bindings, dispatch_task):
471
471
assert task .worker is None
472
472
473
473
@pytest .mark .parallel
474
- def test_executes_on_api_worker_when_no_async (
475
- self ,
476
- pulpcore_bindings ,
477
- dispatch_task_with_stderr ,
478
- ):
474
+ def test_executes_on_api_worker_when_no_async (self , pulpcore_bindings , dispatch_task , capsys ):
479
475
"""
480
476
GIVEN a task with no resource requirements
481
477
AND the task IS NOT an async function
482
478
WHEN dispatching a task as immediate
483
479
THEN the task completes with no associated worker
484
480
"""
485
481
# TODO: on 3.85 this should throw an error
486
- task_href , stderr = dispatch_task_with_stderr (
482
+ task_href = dispatch_task (
487
483
"pulpcore.app.tasks.test.sleep" , args = (LT_TIMEOUT ,), immediate = True
488
484
)
485
+ stderr_content = capsys .readouterr ().err
489
486
task = pulpcore_bindings .TasksApi .read (task_href )
490
487
assert task .state == "completed"
491
488
assert task .worker is None
492
- assert "Support for non-coroutine immediate tasks will be dropped" in stderr
489
+ assert "Support for non-coroutine immediate tasks will be dropped" in stderr_content
493
490
494
491
@pytest .mark .parallel
495
492
def test_timeouts_on_api_worker (self , pulpcore_bindings , dispatch_task ):
@@ -509,7 +506,7 @@ def test_timeouts_on_api_worker(self, pulpcore_bindings, dispatch_task):
509
506
510
507
511
508
@pytest .fixture
512
- def dispatch_long_task (pulpcore_bindings , dispatch_task ):
509
+ def resource_blocker (pulpcore_bindings , dispatch_task ):
513
510
514
511
def wait_until (state , task_href , timeout = 10 ):
515
512
for i in range (timeout ):
@@ -520,34 +517,39 @@ def wait_until(state, task_href, timeout=10):
520
517
raise RuntimeError ("Timeout waiting for task to transition" )
521
518
522
519
@contextmanager
523
- def _dispatch_long_task ( required_resources : list [str ], duration = 5 ):
520
+ def _resource_blocker ( exclusive_resources : list [str ], duration = 5 ):
524
521
task_href = dispatch_task (
525
522
"pulpcore.app.tasks.test.sleep" ,
526
523
args = (duration ,),
527
- exclusive_resources = required_resources ,
524
+ exclusive_resources = exclusive_resources ,
528
525
)
529
526
wait_until ("running" , task_href )
530
527
yield
531
- task = pulpcore_bindings .TasksApi .read (task_href )
532
- if task .state == "running" :
528
+ # Trying to cancel a finished task will return a 409 code.
529
+ # We can ignore if that's the case, because all we want here is to cut time down.
530
+ # Otherwise it might be a real error.
531
+ try :
533
532
pulpcore_bindings .TasksApi .tasks_cancel (task_href , {"state" : "canceled" })
533
+ except ApiException as e :
534
+ if e .status != 409 :
535
+ raise
534
536
535
- return _dispatch_long_task
537
+ return _resource_blocker
536
538
537
539
538
540
class TestImmediateTaskWithBlockedResource :
539
541
540
542
@pytest .mark .parallel
541
543
def test_executes_in_task_worker (
542
- self , dispatch_long_task , dispatch_task , monitor_task , pulpcore_bindings
544
+ self , resource_blocker , dispatch_task , monitor_task , pulpcore_bindings
543
545
):
544
546
"""
545
547
GIVEN an async task requiring busy resources
546
548
WHEN dispatching a task as immediate
547
549
THEN the task completes with a worker
548
550
"""
549
551
COMMON_RESOURCE = str (uuid4 ())
550
- with dispatch_long_task ( required_resources = [COMMON_RESOURCE ]):
552
+ with resource_blocker ( exclusive_resources = [COMMON_RESOURCE ]):
551
553
task_href = dispatch_task (
552
554
"pulpcore.app.tasks.test.asleep" ,
553
555
args = (LT_TIMEOUT ,),
@@ -560,15 +562,15 @@ def test_executes_in_task_worker(
560
562
561
563
@pytest .mark .parallel
562
564
def test_throws_when_non_deferrable (
563
- self , dispatch_long_task , pulpcore_bindings , dispatch_task , monitor_task
565
+ self , resource_blocker , pulpcore_bindings , dispatch_task , monitor_task
564
566
):
565
567
"""
566
568
GIVEN an async task requiring busy resources
567
569
WHEN dispatching as immediate and not deferrable
568
570
THEN an error is raised
569
571
"""
570
572
COMMON_RESOURCE = str (uuid4 ())
571
- with dispatch_long_task ( required_resources = [COMMON_RESOURCE ]):
573
+ with resource_blocker ( exclusive_resources = [COMMON_RESOURCE ]):
572
574
task_href = dispatch_task (
573
575
"pulpcore.app.tasks.test.asleep" ,
574
576
args = (0 ,),
@@ -582,8 +584,8 @@ def test_throws_when_non_deferrable(
582
584
assert "Resources temporarily unavailable." in task .error ["reason" ]
583
585
584
586
@pytest .mark .parallel
585
- def test_timeouts_on_task_worker (
586
- self , dispatch_long_task , pulpcore_bindings , dispatch_task , monitor_task
587
+ def test_times_out_on_task_worker (
588
+ self , resource_blocker , pulpcore_bindings , dispatch_task , monitor_task
587
589
):
588
590
"""
589
591
GIVEN an async task requiring busy resources
@@ -593,7 +595,7 @@ def test_timeouts_on_task_worker(
593
595
"""
594
596
COMMON_RESOURCE = str (uuid4 ())
595
597
with pytest .raises (PulpTaskError ) as ctx :
596
- with dispatch_long_task ( required_resources = [COMMON_RESOURCE ]):
598
+ with resource_blocker ( exclusive_resources = [COMMON_RESOURCE ]):
597
599
task_href = dispatch_task (
598
600
"pulpcore.app.tasks.test.asleep" ,
599
601
args = (GT_TIMEOUT ,),
0 commit comments