@@ -471,25 +471,22 @@ def test_succeeds_on_api_worker(self, pulpcore_bindings, dispatch_task):
471471 assert task .worker is None
472472
473473 @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 ):
479475 """
480476 GIVEN a task with no resource requirements
481477 AND the task IS NOT an async function
482478 WHEN dispatching a task as immediate
483479 THEN the task completes with no associated worker
484480 """
485481 # TODO: on 3.85 this should throw an error
486- task_href , stderr = dispatch_task_with_stderr (
482+ task_href = dispatch_task (
487483 "pulpcore.app.tasks.test.sleep" , args = (LT_TIMEOUT ,), immediate = True
488484 )
485+ stderr_content = capsys .readouterr ().err
489486 task = pulpcore_bindings .TasksApi .read (task_href )
490487 assert task .state == "completed"
491488 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
493490
494491 @pytest .mark .parallel
495492 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):
509506
510507
511508@pytest .fixture
512- def dispatch_long_task (pulpcore_bindings , dispatch_task ):
509+ def resource_blocker (pulpcore_bindings , dispatch_task ):
513510
514511 def wait_until (state , task_href , timeout = 10 ):
515512 for i in range (timeout ):
@@ -520,34 +517,39 @@ def wait_until(state, task_href, timeout=10):
520517 raise RuntimeError ("Timeout waiting for task to transition" )
521518
522519 @contextmanager
523- def _dispatch_long_task ( required_resources : list [str ], duration = 5 ):
520+ def _resource_blocker ( exclusive_resources : list [str ], duration = 5 ):
524521 task_href = dispatch_task (
525522 "pulpcore.app.tasks.test.sleep" ,
526523 args = (duration ,),
527- exclusive_resources = required_resources ,
524+ exclusive_resources = exclusive_resources ,
528525 )
529526 wait_until ("running" , task_href )
530527 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 :
533532 pulpcore_bindings .TasksApi .tasks_cancel (task_href , {"state" : "canceled" })
533+ except ApiException as e :
534+ if e .status != 409 :
535+ raise
534536
535- return _dispatch_long_task
537+ return _resource_blocker
536538
537539
538540class TestImmediateTaskWithBlockedResource :
539541
540542 @pytest .mark .parallel
541543 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
543545 ):
544546 """
545547 GIVEN an async task requiring busy resources
546548 WHEN dispatching a task as immediate
547549 THEN the task completes with a worker
548550 """
549551 COMMON_RESOURCE = str (uuid4 ())
550- with dispatch_long_task ( required_resources = [COMMON_RESOURCE ]):
552+ with resource_blocker ( exclusive_resources = [COMMON_RESOURCE ]):
551553 task_href = dispatch_task (
552554 "pulpcore.app.tasks.test.asleep" ,
553555 args = (LT_TIMEOUT ,),
@@ -560,15 +562,15 @@ def test_executes_in_task_worker(
560562
561563 @pytest .mark .parallel
562564 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
564566 ):
565567 """
566568 GIVEN an async task requiring busy resources
567569 WHEN dispatching as immediate and not deferrable
568570 THEN an error is raised
569571 """
570572 COMMON_RESOURCE = str (uuid4 ())
571- with dispatch_long_task ( required_resources = [COMMON_RESOURCE ]):
573+ with resource_blocker ( exclusive_resources = [COMMON_RESOURCE ]):
572574 task_href = dispatch_task (
573575 "pulpcore.app.tasks.test.asleep" ,
574576 args = (0 ,),
@@ -582,8 +584,8 @@ def test_throws_when_non_deferrable(
582584 assert "Resources temporarily unavailable." in task .error ["reason" ]
583585
584586 @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
587589 ):
588590 """
589591 GIVEN an async task requiring busy resources
@@ -593,7 +595,7 @@ def test_timeouts_on_task_worker(
593595 """
594596 COMMON_RESOURCE = str (uuid4 ())
595597 with pytest .raises (PulpTaskError ) as ctx :
596- with dispatch_long_task ( required_resources = [COMMON_RESOURCE ]):
598+ with resource_blocker ( exclusive_resources = [COMMON_RESOURCE ]):
597599 task_href = dispatch_task (
598600 "pulpcore.app.tasks.test.asleep" ,
599601 args = (GT_TIMEOUT ,),
0 commit comments