@@ -508,61 +508,63 @@ def test_immediate_task_requires_resource(pulpcore_bindings, dispatch_task, moni
508508 THEN an error is raised
509509
510510 3
511+ WHEN dispatching a task as immediate
511512 AND it takes longer than timeout
512513 THEN an error is raised
513514 """
514515 LT_TIMEOUT = IMMEDIATE_TIMEOUT / 2
515516 GT_TIMEOUT = IMMEDIATE_TIMEOUT + 1
516517 LONG_RUNNING = 5
518+ COMMON_RESOURCE = "XYZ"
517519
518520 def wait_until (state , task_href , timeout = 10 ):
519521 for i in range (timeout ):
520522 task = pulpcore_bindings .TasksApi .read (task_href )
521523 if task .state != state :
522524 break
523525 time .sleep (1 )
526+ raise RuntimeError ("Timeout waiting for task to transition" )
527+
528+ def dispatch_long_task (requires_resource ):
529+ task_href = dispatch_task (
530+ "pulpcore.app.tasks.test.sleep" ,
531+ args = (LONG_RUNNING ,),
532+ exclusive_resources = [requires_resource ],
533+ )
534+ wait_until ("running" , task_href )
524535
525536 # Case 1
526- long_href = dispatch_task (
527- "pulpcore.app.tasks.test.sleep" , args = (LONG_RUNNING ,), exclusive_resources = ["XYZ" ]
528- )
529- wait_until ("running" , long_href )
537+ dispatch_long_task (requires_resource = COMMON_RESOURCE )
530538 task_href = dispatch_task (
531539 "pulpcore.app.tasks.test.asleep" ,
532540 args = (LT_TIMEOUT ,),
533541 immediate = True ,
534- exclusive_resources = ["XYZ" ],
542+ exclusive_resources = [COMMON_RESOURCE ],
535543 )
536544 task = monitor_task (task_href )
537545 assert task .state == "completed"
538546 assert task .worker is not None
539547
540548 # Case 2
541- long_href = dispatch_task (
542- "pulpcore.app.tasks.test.sleep" , args = (LONG_RUNNING ,), exclusive_resources = ["XYZ" ]
543- )
544- wait_until ("running" , long_href )
549+ dispatch_long_task (requires_resource = COMMON_RESOURCE )
545550 with pytest .raises (PulpTaskError ):
546551 task_href = dispatch_task (
547552 "pulpcore.app.tasks.test.asleep" ,
548553 args = (0 ,),
549554 immediate = True ,
550555 deferred = False ,
551- exclusive_resources = ["XYZ" ],
556+ exclusive_resources = [COMMON_RESOURCE ],
552557 )
553558 monitor_task (task_href )
554559
555560 # Case 3
556- long_href = dispatch_task (
557- "pulpcore.app.tasks.test.sleep" , args = (LONG_RUNNING ,), exclusive_resources = ["XYZ" ]
558- )
559- wait_until ("running" , long_href )
561+ dispatch_long_task (requires_resource = COMMON_RESOURCE )
560562 with pytest .raises (PulpTaskError ) as ctx :
561563 task_href = dispatch_task (
562564 "pulpcore.app.tasks.test.asleep" ,
563565 args = (GT_TIMEOUT ,),
564566 immediate = True ,
565- exclusive_resources = ["XYZ" ],
567+ exclusive_resources = [COMMON_RESOURCE ],
566568 )
567569 monitor_task (task_href )
568570 assert "task timed out after" in ctx .value .task .error ["description" ]
0 commit comments