Skip to content

Commit 2f48a64

Browse files
committed
fixed usage of random
1 parent 0104e11 commit 2f48a64

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

services/director-v2/tests/unit/with_dbs/comp_scheduler/test_scheduler_dask.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
assert_comp_runs_empty,
2828
assert_comp_tasks_and_comp_run_snapshot_tasks,
2929
)
30-
from dask_task_models_library.container_tasks.errors import TaskCancelledError
30+
from dask_task_models_library.container_tasks.errors import (
31+
ServiceRuntimeError,
32+
TaskCancelledError,
33+
)
3134
from dask_task_models_library.container_tasks.events import TaskProgressEvent
3235
from dask_task_models_library.container_tasks.io import TaskOutputData
3336
from dask_task_models_library.container_tasks.protocol import TaskOwner
@@ -838,7 +841,13 @@ async def _return_2nd_task_failed(job_ids: list[str]) -> list[RunningState]:
838841
]
839842

840843
mocked_dask_client.get_tasks_status.side_effect = _return_2nd_task_failed
841-
mocked_dask_client.get_task_result.side_effect = None
844+
mocked_dask_client.get_task_result.side_effect = ServiceRuntimeError(
845+
service_key="simcore/services/dynamic/some-service",
846+
service_version="1.0.0",
847+
container_id="some-container-id",
848+
exit_code=1,
849+
service_logs="simulated error",
850+
)
842851
await scheduler_api.apply(
843852
user_id=run_in_db.user_id,
844853
project_id=run_in_db.project_uuid,
@@ -2301,18 +2310,17 @@ async def mocked_get_tasks_status(job_ids: list[str]) -> list[RunningState]:
23012310
computational_tasks = [
23022311
t for t in running_project.tasks if t.node_class is NodeClass.COMPUTATIONAL
23032312
]
2304-
expected_timeouted_tasks = random.choices( # noqa: S311
2313+
expected_timeouted_tasks = random.sample(
23052314
computational_tasks, k=len(computational_tasks) - 1
23062315
)
23072316
successful_tasks = [
23082317
t for t in computational_tasks if t not in expected_timeouted_tasks
23092318
]
23102319

23112320
async def mocked_get_task_result(job_id: str) -> TaskOutputData:
2312-
nonlocal expected_timeouted_tasks
2313-
if job_id in [t.job_id for t in expected_timeouted_tasks]:
2314-
raise ComputationalBackendTaskResultsNotReadyError(job_id=job_id)
2315-
return TaskOutputData.model_validate({"whatever_output": 123})
2321+
if job_id in [t.job_id for t in successful_tasks]:
2322+
return TaskOutputData.model_validate({"whatever_output": 123})
2323+
raise ComputationalBackendTaskResultsNotReadyError(job_id=job_id)
23162324

23172325
mocked_dask_client.get_task_result.side_effect = mocked_get_task_result
23182326
# calling apply should not raise
@@ -2346,6 +2354,7 @@ async def mocked_get_task_result(job_id: str) -> TaskOutputData:
23462354
retrieval_times.append(
23472355
error_dict["ctx"][_TASK_RETRIEVAL_ERROR_CONTEXT_TIME_KEY]
23482356
)
2357+
assert len(retrieval_times) == len(expected_timeouted_tasks)
23492358

23502359
await assert_comp_tasks_and_comp_run_snapshot_tasks(
23512360
sqlalchemy_async_engine,
@@ -2374,14 +2383,7 @@ async def mocked_get_task_result(job_id: str) -> TaskOutputData:
23742383
iteration=1,
23752384
)
23762385
assert mocked_dask_client.get_task_result.call_count == (
2377-
len(
2378-
[
2379-
t
2380-
for t in running_project.tasks
2381-
if t.node_class is NodeClass.COMPUTATIONAL
2382-
]
2383-
)
2384-
- 1
2386+
len(expected_timeouted_tasks)
23852387
)
23862388
mocked_dask_client.get_task_result.reset_mock()
23872389
await asyncio.sleep(0.5) # wait a bit to ensure the retry decorator has reset

0 commit comments

Comments
 (0)