Skip to content

Commit a847424

Browse files
author
Andrei Neagu
committed
simplified tests
1 parent ad0ca26 commit a847424

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

services/dynamic-sidecar/tests/unit/api/rpc/test__containers_long_running_tasks.py

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import asyncio
77
import json
8-
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator
9-
from contextlib import asynccontextmanager
8+
from collections.abc import Awaitable, Callable, Iterator
109
from pathlib import Path
1110
from typing import Any, Final, NamedTuple
1211
from unittest.mock import AsyncMock
@@ -32,7 +31,8 @@
3231
from pytest_mock.plugin import MockerFixture
3332
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict, setenvs_from_dict
3433
from servicelib.fastapi.long_running_tasks._manager import FastAPILongRunningManager
35-
from servicelib.fastapi.long_running_tasks.client import BaseClient, RPCClient
34+
from servicelib.long_running_tasks import lrt_api
35+
from servicelib.long_running_tasks.errors import TaskNotFoundError
3636
from servicelib.long_running_tasks.models import LRTNamespace, TaskId
3737
from servicelib.long_running_tasks.task import TaskRegistry
3838
from servicelib.rabbitmq import RabbitMQRPCClient
@@ -73,15 +73,6 @@ class ContainerTimes(NamedTuple):
7373
finished_at: Any
7474

7575

76-
@asynccontextmanager
77-
async def auto_remove_task(client: BaseClient, task_id: TaskId) -> AsyncIterator[None]:
78-
"""clenup pending tasks"""
79-
try:
80-
yield
81-
finally:
82-
await client.remove_task(task_id)
83-
84-
8576
async def _get_container_timestamps(
8677
container_names: list[str],
8778
) -> dict[str, ContainerTimes]:
@@ -197,13 +188,6 @@ def lrt_namespace(app: FastAPI) -> LRTNamespace:
197188
return long_running_manager.lrt_namespace
198189

199190

200-
@pytest.fixture
201-
def client(
202-
app: FastAPI, rpc_client: RabbitMQRPCClient, lrt_namespace: LRTNamespace
203-
) -> BaseClient:
204-
return RPCClient(rpc_client, lrt_namespace)
205-
206-
207191
@pytest.fixture
208192
def shared_store(app: FastAPI) -> SharedStore:
209193
return app.state.shared_store
@@ -433,7 +417,6 @@ async def test_create_containers_task(
433417
rpc_client: RabbitMQRPCClient,
434418
node_id: NodeID,
435419
lrt_namespace: LRTNamespace,
436-
client: BaseClient,
437420
compose_spec: str,
438421
mock_stop_heart_beat_task: AsyncMock,
439422
mock_metrics_params: CreateServiceMetricsAdditionalParams,
@@ -460,7 +443,6 @@ async def test_pull_user_servcices_docker_images(
460443
rpc_client: RabbitMQRPCClient,
461444
node_id: NodeID,
462445
lrt_namespace: LRTNamespace,
463-
client: BaseClient,
464446
compose_spec: str,
465447
mock_stop_heart_beat_task: AsyncMock,
466448
mock_metrics_params: CreateServiceMetricsAdditionalParams,
@@ -501,7 +483,6 @@ async def test_create_containers_task_invalid_yaml_spec(
501483
rpc_client: RabbitMQRPCClient,
502484
node_id: NodeID,
503485
lrt_namespace: LRTNamespace,
504-
client: BaseClient,
505486
mock_stop_heart_beat_task: AsyncMock,
506487
mock_metrics_params: CreateServiceMetricsAdditionalParams,
507488
):
@@ -538,7 +519,6 @@ async def test_same_task_id_is_returned_if_task_exists(
538519
rpc_client: RabbitMQRPCClient,
539520
node_id: NodeID,
540521
lrt_namespace: LRTNamespace,
541-
client: BaseClient,
542522
mocker: MockerFixture,
543523
get_task_id_callable: Callable[..., Awaitable[TaskId]],
544524
mock_stop_heart_beat_task: AsyncMock,
@@ -555,26 +535,33 @@ def _get_awaitable() -> Awaitable[TaskId]:
555535
port_keys=None,
556536
)
557537

538+
async def _assert_task_removed(task_id: TaskId) -> None:
539+
await lrt_api.remove_task(
540+
rpc_client, lrt_namespace, {}, task_id, wait_for_removal=True
541+
)
542+
with pytest.raises(TaskNotFoundError):
543+
await lrt_api.get_task_status(rpc_client, lrt_namespace, {}, task_id)
544+
558545
task_id = await _get_awaitable()
559546
assert task_id.endswith("unique")
560-
async with auto_remove_task(client, task_id):
561-
assert await _get_awaitable() == task_id
547+
assert await _get_awaitable() == task_id
548+
549+
await _assert_task_removed(task_id)
562550

563551
# since the previous task was already removed it is again possible
564552
# to create a task and it will share the same task_id
565553
new_task_id = await _get_awaitable()
566554
assert new_task_id.endswith("unique")
567555
assert new_task_id == task_id
568-
async with auto_remove_task(client, task_id):
569-
pass
556+
557+
await _assert_task_removed(task_id)
570558

571559

572560
async def test_containers_down_after_starting(
573561
mock_ensure_read_permissions_on_user_service_data: None,
574562
rpc_client: RabbitMQRPCClient,
575563
node_id: NodeID,
576564
lrt_namespace: LRTNamespace,
577-
client: BaseClient,
578565
compose_spec: str,
579566
mock_stop_heart_beat_task: AsyncMock,
580567
mock_metrics_params: CreateServiceMetricsAdditionalParams,
@@ -613,7 +600,6 @@ async def test_containers_down_missing_spec(
613600
rpc_client: RabbitMQRPCClient,
614601
node_id: NodeID,
615602
lrt_namespace: LRTNamespace,
616-
client: BaseClient,
617603
caplog_info_debug: pytest.LogCaptureFixture,
618604
):
619605
result = await get_lrt_result(
@@ -634,7 +620,6 @@ async def test_container_restore_state(
634620
rpc_client: RabbitMQRPCClient,
635621
node_id: NodeID,
636622
lrt_namespace: LRTNamespace,
637-
client: BaseClient,
638623
mock_data_manager: None,
639624
):
640625
result = await get_lrt_result(
@@ -654,7 +639,6 @@ async def test_container_save_state(
654639
rpc_client: RabbitMQRPCClient,
655640
node_id: NodeID,
656641
lrt_namespace: LRTNamespace,
657-
client: BaseClient,
658642
mock_data_manager: None,
659643
):
660644
result = await get_lrt_result(
@@ -673,7 +657,6 @@ async def test_container_pull_input_ports(
673657
rpc_client: RabbitMQRPCClient,
674658
node_id: NodeID,
675659
lrt_namespace: LRTNamespace,
676-
client: BaseClient,
677660
inputs_pulling_enabled: bool,
678661
app: FastAPI,
679662
mock_port_keys: list[str] | None,
@@ -699,7 +682,6 @@ async def test_container_pull_output_ports(
699682
rpc_client: RabbitMQRPCClient,
700683
node_id: NodeID,
701684
lrt_namespace: LRTNamespace,
702-
client: BaseClient,
703685
mock_port_keys: list[str] | None,
704686
mock_nodeports: None,
705687
):
@@ -720,7 +702,6 @@ async def test_container_push_output_ports(
720702
rpc_client: RabbitMQRPCClient,
721703
node_id: NodeID,
722704
lrt_namespace: LRTNamespace,
723-
client: BaseClient,
724705
mock_port_keys: list[str] | None,
725706
mock_nodeports: None,
726707
):
@@ -741,7 +722,6 @@ async def test_container_push_output_ports_missing_node(
741722
rpc_client: RabbitMQRPCClient,
742723
node_id: NodeID,
743724
lrt_namespace: LRTNamespace,
744-
client: BaseClient,
745725
mock_port_keys: list[str] | None,
746726
missing_node_uuid: str,
747727
mock_node_missing: None,
@@ -774,7 +754,6 @@ async def test_containers_restart(
774754
rpc_client: RabbitMQRPCClient,
775755
node_id: NodeID,
776756
lrt_namespace: LRTNamespace,
777-
client: BaseClient,
778757
compose_spec: str,
779758
mock_stop_heart_beat_task: AsyncMock,
780759
mock_metrics_params: CreateServiceMetricsAdditionalParams,

0 commit comments

Comments
 (0)