66from celery .contrib .testing .worker import TestWorkController
77from celery_library .task import register_task
88from faker import Faker
9- from fastapi import FastAPI
9+ from fastapi import FastAPI , status
1010from httpx import AsyncClient , BasicAuth
11+ from models_library .api_schemas_long_running_tasks .tasks import (
12+ TaskGet ,
13+ TaskResult ,
14+ TaskStatus ,
15+ )
1116from models_library .functions import (
1217 FunctionClass ,
1318 FunctionID ,
3439 JobPricingSpecification ,
3540 NodeID ,
3641)
42+ from tenacity import (
43+ AsyncRetrying ,
44+ retry_if_exception_type ,
45+ stop_after_delay ,
46+ wait_exponential ,
47+ )
3748
3849pytest_simcore_core_services_selection = ["postgres" , "rabbit" ]
3950pytest_simcore_ops_services_selection = ["adminer" ]
4051
4152_faker = Faker ()
4253
4354
55+ async def poll_task_until_done (
56+ client : AsyncClient ,
57+ auth : BasicAuth ,
58+ task_id : str ,
59+ timeout : float = 30.0 ,
60+ ) -> TaskResult :
61+
62+ async for attempt in AsyncRetrying (
63+ stop = stop_after_delay (timeout ),
64+ wait = wait_exponential (multiplier = 0.5 , min = 0.5 , max = 2.0 ),
65+ reraise = True ,
66+ retry = retry_if_exception_type (AssertionError ),
67+ ):
68+ with attempt :
69+
70+ response = await client .get (f"/{ API_VTAG } /tasks/{ task_id } " , auth = auth )
71+ response .raise_for_status ()
72+ status = TaskStatus .model_validate (response .json ())
73+ assert status .done is True
74+
75+ assert status .done is True
76+ response = await client .get (f"/{ API_VTAG } /tasks/{ task_id } /result" , auth = auth )
77+ response .raise_for_status ()
78+ return TaskResult .model_validate (response .json ())
79+
80+
4481def _register_fake_run_function_task () -> Callable [[Celery ], None ]:
4582
4683 async def run_function (
@@ -87,7 +124,6 @@ async def test_with_fake_run_function(
87124 auth : BasicAuth ,
88125 with_storage_celery_worker : TestWorkController ,
89126):
90-
91127 app .dependency_overrides [get_function ] = (
92128 lambda : RegisteredProjectFunction .model_validate (
93129 RegisteredProjectFunction .model_config .get ("json_schema_extra" , {}).get (
@@ -107,4 +143,13 @@ async def test_with_fake_run_function(
107143 headers = headers ,
108144 )
109145
110- assert response .status_code == 200
146+ assert response .status_code == status .HTTP_200_OK
147+ task = TaskGet .model_validate (response .json ())
148+
149+ # Poll until task completion and get result
150+ result = await poll_task_until_done (client , auth , task .task_id )
151+
152+ # Verify the result is a RegisteredProjectFunctionJob
153+ assert result is not None
154+ assert isinstance (result , dict )
155+ # Add more specific assertions based on your expected result structure
0 commit comments