33import json
44import pytest
55import time
6+ import logging
67
78from aiohttp import BasicAuth
89from datetime import datetime
910from urllib .parse import urljoin
1011from uuid import uuid4
1112
1213from pulpcore .client .pulpcore import ApiException
14+ from contextlib import contextmanager
1315
1416from pulpcore .tests .functional .utils import download_file , PulpTaskError
1517from pulpcore .constants import IMMEDIATE_TIMEOUT
@@ -449,13 +451,13 @@ def test_cancel_task_group(pulpcore_bindings, dispatch_task_group, gen_user):
449451
450452
451453LT_TIMEOUT = IMMEDIATE_TIMEOUT / 2
452- GT_TIMEOUT = IMMEDIATE_TIMEOUT + 1
454+ GT_TIMEOUT = IMMEDIATE_TIMEOUT * 2
453455
454456
455- class TestImmediateTaskNoLocking :
457+ class TestImmediateTaskWithNoResource :
456458
457459 @pytest .mark .parallel
458- def test_succeed_on_api_worker (self , pulpcore_bindings , dispatch_task , monitor_task ):
460+ def test_succeeds_on_api_worker (self , pulpcore_bindings , dispatch_task ):
459461 """
460462 GIVEN a task with no resource requirements
461463 AND the task IS an async function
@@ -465,43 +467,47 @@ def test_succeed_on_api_worker(self, pulpcore_bindings, dispatch_task, monitor_t
465467 task_href = dispatch_task (
466468 "pulpcore.app.tasks.test.asleep" , args = (LT_TIMEOUT ,), immediate = True
467469 )
468- task = monitor_task (task_href )
470+ task = pulpcore_bindings . TasksApi . read (task_href )
469471 assert task .state == "completed"
470472 assert task .worker is None
471473
472474 @pytest .mark .parallel
473475 def test_executes_on_api_worker_when_no_async (
474- self , pulpcore_bindings , dispatch_task , monitor_task
476+ self ,
477+ pulpcore_bindings ,
478+ dispatch_task ,
479+ caplog ,
475480 ):
476481 """
477482 GIVEN a task with no resource requirements
478483 AND the task IS NOT an async function
479484 WHEN dispatching a task as immediate
480485 THEN the task completes with no associated worker
481486 """
487+ caplog .set_level (logging .WARNING )
482488 # TODO: on 3.85 this should throw an error
483489 task_href = dispatch_task (
484490 "pulpcore.app.tasks.test.sleep" , args = (LT_TIMEOUT ,), immediate = True
485491 )
486- task = monitor_task (task_href )
492+ task = pulpcore_bindings . TasksApi . read (task_href )
487493 assert task .state == "completed"
488494 assert task .worker is None
489495
490496 @pytest .mark .parallel
491- def test_timeouts_on_api_worker (self , pulpcore_bindings , dispatch_task , monitor_task ):
497+ def test_timeouts_on_api_worker (self , pulpcore_bindings , dispatch_task ):
492498 """
493499 GIVEN a task with no resource requirements
494500 AND the task is an async function
495501 WHEN dispatching a task as immediate
496502 AND it takes longer than timeout
497503 THEN the task fails with a timeout error message
498504 """
499- with pytest . raises ( PulpTaskError ) as ctx :
500- task_href = dispatch_task (
501- "pulpcore.app.tasks.test.asleep" , args = ( GT_TIMEOUT ,), immediate = True
502- )
503- monitor_task ( task_href )
504- assert "task timed out after" in ctx . value . task .error ["description" ]
505+ task_href = dispatch_task (
506+ "pulpcore.app.tasks.test.asleep" , args = ( GT_TIMEOUT ,), immediate = True
507+ )
508+ task = pulpcore_bindings . TasksApi . read ( task_href )
509+ assert task . worker is None
510+ assert "task timed out after" in task .error ["description" ]
505511
506512
507513@pytest .fixture
@@ -515,34 +521,41 @@ def wait_until(state, task_href, timeout=10):
515521 time .sleep (1 )
516522 raise RuntimeError ("Timeout waiting for task to transition" )
517523
524+ @contextmanager
518525 def _dispatch_long_task (required_resources : list [str ], duration = 5 ):
519526 task_href = dispatch_task (
520527 "pulpcore.app.tasks.test.sleep" ,
521528 args = (duration ,),
522529 exclusive_resources = required_resources ,
523530 )
524531 wait_until ("running" , task_href )
532+ yield
533+ pulpcore_bindings .TasksApi .tasks_cancel (task_href , {"state" : "canceled" })
525534
526535 return _dispatch_long_task
527536
528537
529- class TestImmediateTaskWithLocking :
538+ class TestImmediateTaskWithBlockedResource :
530539
531540 @pytest .mark .parallel
532- def test_executes_in_task_worker (self , dispatch_long_task , dispatch_task , monitor_task ):
541+ def test_executes_in_task_worker (
542+ self , dispatch_long_task , dispatch_task , monitor_task , pulpcore_bindings
543+ ):
533544 """
534545 GIVEN an async task requiring busy resources
535546 WHEN dispatching a task as immediate
536547 THEN the task completes with a worker
537548 """
538549 COMMON_RESOURCE = "MMM"
539- dispatch_long_task (required_resources = [COMMON_RESOURCE ])
540- task_href = dispatch_task (
541- "pulpcore.app.tasks.test.asleep" ,
542- args = (LT_TIMEOUT ,),
543- immediate = True ,
544- exclusive_resources = [COMMON_RESOURCE ],
545- )
550+ with dispatch_long_task (required_resources = [COMMON_RESOURCE ]):
551+ task_href = dispatch_task (
552+ "pulpcore.app.tasks.test.asleep" ,
553+ args = (LT_TIMEOUT ,),
554+ immediate = True ,
555+ exclusive_resources = [COMMON_RESOURCE ],
556+ )
557+ task = pulpcore_bindings .TasksApi .read (task_href )
558+ assert task .state == "waiting"
546559 task = monitor_task (task_href )
547560 assert task .state == "completed"
548561 assert task .worker is not None
@@ -557,18 +570,21 @@ def test_throws_when_non_deferrable(
557570 THEN an error is raised
558571 """
559572 COMMON_RESOURCE = "NNN"
560- dispatch_long_task (required_resources = [COMMON_RESOURCE ])
561- with pytest .raises (PulpTaskError ):
573+ with dispatch_long_task (required_resources = [COMMON_RESOURCE ]):
562574 task_href = dispatch_task (
563575 "pulpcore.app.tasks.test.asleep" ,
564576 args = (0 ,),
565577 immediate = True ,
566578 deferred = False ,
567579 exclusive_resources = [COMMON_RESOURCE ],
568580 )
569- monitor_task (task_href )
581+ task = pulpcore_bindings .TasksApi .read (task_href )
582+ assert task .state == "canceled"
583+ assert task .worker is None
584+ assert "Resources temporarily unavailable." in task .error ["reason" ]
570585
571- def test_throws_on_timeout (
586+ @pytest .mark .parallel
587+ def test_timeouts_on_task_worker (
572588 self , dispatch_long_task , pulpcore_bindings , dispatch_task , monitor_task
573589 ):
574590 """
@@ -578,13 +594,15 @@ def test_throws_on_timeout(
578594 THEN an error is raised
579595 """
580596 COMMON_RESOURCE = "PPP"
581- dispatch_long_task (required_resources = [COMMON_RESOURCE ])
582597 with pytest .raises (PulpTaskError ) as ctx :
583- task_href = dispatch_task (
584- "pulpcore.app.tasks.test.asleep" ,
585- args = (GT_TIMEOUT ,),
586- immediate = True ,
587- exclusive_resources = [COMMON_RESOURCE ],
588- )
598+ with dispatch_long_task (required_resources = [COMMON_RESOURCE ]):
599+ task_href = dispatch_task (
600+ "pulpcore.app.tasks.test.asleep" ,
601+ args = (GT_TIMEOUT ,),
602+ immediate = True ,
603+ exclusive_resources = [COMMON_RESOURCE ],
604+ )
605+ task = pulpcore_bindings .TasksApi .read (task_href )
606+ assert task .state == "waiting"
589607 monitor_task (task_href )
590608 assert "task timed out after" in ctx .value .task .error ["description" ]
0 commit comments