Skip to content

Commit a02f6e3

Browse files
author
Andrei Neagu
committed
fixed issues with loops in test
1 parent 6b13f67 commit a02f6e3

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

services/dynamic-sidecar/tests/unit/test_api_rest_workflow_service_metrics.py

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from aiodocker.utils import clean_filters
1717
from aiodocker.volumes import DockerVolume
1818
from asgi_lifespan import LifespanManager
19+
from common_library.serialization import model_dump_with_secrets
1920
from fastapi import FastAPI
2021
from httpx import ASGITransport, AsyncClient
2122
from models_library.api_schemas_dynamic_sidecar.containers import DockerComposeYamlStr
@@ -36,7 +37,9 @@
3637
from servicelib.fastapi.long_running_tasks.client import setup as client_setup
3738
from servicelib.long_running_tasks.errors import TaskExceptionError
3839
from servicelib.long_running_tasks.models import TaskId
40+
from settings_library.rabbit import RabbitSettings
3941
from simcore_service_dynamic_sidecar._meta import API_VTAG
42+
from simcore_service_dynamic_sidecar.core.application import create_app
4043
from simcore_service_dynamic_sidecar.core.docker_utils import get_container_states
4144
from simcore_service_dynamic_sidecar.models.schemas.containers import (
4245
ContainersComposeSpec,
@@ -47,6 +50,10 @@
4750
from tenacity.stop import stop_after_delay
4851
from tenacity.wait import wait_fixed
4952

53+
pytest_simcore_core_services_selection = [
54+
"rabbit",
55+
]
56+
5057
_FAST_STATUS_POLL: Final[float] = 0.1
5158
_CREATE_SERVICE_CONTAINERS_TIMEOUT: Final[float] = 60
5259
_BASE_HEART_BEAT_INTERVAL: Final[float] = 0.1
@@ -81,26 +88,34 @@ def backend_url() -> AnyHttpUrl:
8188

8289

8390
@pytest.fixture
84-
def mock_environment(
91+
async def mock_environment(
8592
mock_postgres_check: None,
93+
mock_environment: EnvVarsDict,
8694
monkeypatch: pytest.MonkeyPatch,
87-
mock_rabbitmq_envs: EnvVarsDict,
95+
rabbit_service: RabbitSettings,
96+
mock_registry_service: AsyncMock,
8897
) -> EnvVarsDict:
89-
setenvs_from_dict(
98+
return setenvs_from_dict(
9099
monkeypatch,
91-
{"RESOURCE_TRACKING_HEARTBEAT_INTERVAL": f"{_BASE_HEART_BEAT_INTERVAL}"},
100+
{
101+
**mock_environment,
102+
"RESOURCE_TRACKING_HEARTBEAT_INTERVAL": f"{_BASE_HEART_BEAT_INTERVAL}",
103+
"RABBIT_SETTINGS": json.dumps(
104+
model_dump_with_secrets(rabbit_service, show_secrets=True)
105+
),
106+
},
92107
)
93-
return mock_rabbitmq_envs
94108

95109

96110
@pytest.fixture
97-
async def app(app: FastAPI) -> AsyncIterable[FastAPI]:
111+
async def app(mock_environment: EnvVarsDict) -> AsyncIterable[FastAPI]:
112+
lcal_app = create_app()
98113
# add the client setup to the same application
99114
# this is only required for testing, in reality
100115
# this will be in a different process
101-
client_setup(app)
102-
async with LifespanManager(app):
103-
yield app
116+
client_setup(lcal_app)
117+
async with LifespanManager(lcal_app):
118+
yield lcal_app
104119

105120

106121
@pytest.fixture
@@ -122,7 +137,7 @@ async def httpx_async_client(
122137

123138

124139
@pytest.fixture
125-
def client(
140+
async def client(
126141
app: FastAPI, httpx_async_client: AsyncClient, backend_url: AnyHttpUrl
127142
) -> Client:
128143
return Client(app=app, async_client=httpx_async_client, base_url=f"{backend_url}")
@@ -144,6 +159,15 @@ def mock_user_services_fail_to_stop(mocker: MockerFixture) -> None:
144159
)
145160

146161

162+
@pytest.fixture
163+
def mock_post_rabbit_message(mocker: MockerFixture) -> AsyncMock:
164+
return mocker.patch(
165+
"simcore_service_dynamic_sidecar.core.rabbitmq._post_rabbit_message",
166+
return_value=None,
167+
autospec=True,
168+
)
169+
170+
147171
async def _get_task_id_create_service_containers(
148172
httpx_async_client: AsyncClient,
149173
compose_spec: DockerComposeYamlStr,
@@ -173,11 +197,11 @@ async def _get_task_id_docker_compose_down(httpx_async_client: AsyncClient) -> T
173197

174198

175199
def _get_resource_tracking_messages(
176-
mock_core_rabbitmq: dict[str, AsyncMock],
200+
mock_post_rabbit_message: AsyncMock,
177201
) -> list[RabbitResourceTrackingMessages]:
178202
return [
179203
x[0][1]
180-
for x in mock_core_rabbitmq["post_rabbit_message"].call_args_list
204+
for x in mock_post_rabbit_message.call_args_list
181205
if isinstance(x[0][1], RabbitResourceTrackingMessages)
182206
]
183207

@@ -201,7 +225,7 @@ async def _wait_for_containers_to_be_running(app: FastAPI) -> None:
201225

202226

203227
async def test_service_starts_and_closes_as_expected(
204-
mock_core_rabbitmq: dict[str, AsyncMock],
228+
mock_post_rabbit_message: AsyncMock,
205229
app: FastAPI,
206230
httpx_async_client: AsyncClient,
207231
client: Client,
@@ -235,7 +259,9 @@ async def test_service_starts_and_closes_as_expected(
235259
await asyncio.sleep(_BASE_HEART_BEAT_INTERVAL * 10)
236260

237261
# Ensure messages arrive in the expected order
238-
resource_tracking_messages = _get_resource_tracking_messages(mock_core_rabbitmq)
262+
resource_tracking_messages = _get_resource_tracking_messages(
263+
mock_post_rabbit_message
264+
)
239265
assert len(resource_tracking_messages) >= 3
240266

241267
start_message = resource_tracking_messages[0]
@@ -252,7 +278,7 @@ async def test_service_starts_and_closes_as_expected(
252278

253279
@pytest.mark.parametrize("with_compose_down", [True, False])
254280
async def test_user_services_fail_to_start(
255-
mock_core_rabbitmq: dict[str, AsyncMock],
281+
mock_post_rabbit_message: AsyncMock,
256282
app: FastAPI,
257283
httpx_async_client: AsyncClient,
258284
client: Client,
@@ -284,12 +310,14 @@ async def test_user_services_fail_to_start(
284310
assert result is None
285311

286312
# no messages were sent
287-
resource_tracking_messages = _get_resource_tracking_messages(mock_core_rabbitmq)
313+
resource_tracking_messages = _get_resource_tracking_messages(
314+
mock_post_rabbit_message
315+
)
288316
assert len(resource_tracking_messages) == 0
289317

290318

291319
async def test_user_services_fail_to_stop_or_save_data(
292-
mock_core_rabbitmq: dict[str, AsyncMock],
320+
mock_post_rabbit_message: AsyncMock,
293321
app: FastAPI,
294322
httpx_async_client: AsyncClient,
295323
client: Client,
@@ -327,7 +355,9 @@ async def test_user_services_fail_to_stop_or_save_data(
327355
...
328356

329357
# Ensure messages arrive in the expected order
330-
resource_tracking_messages = _get_resource_tracking_messages(mock_core_rabbitmq)
358+
resource_tracking_messages = _get_resource_tracking_messages(
359+
mock_post_rabbit_message
360+
)
331361
assert len(resource_tracking_messages) >= 3
332362

333363
start_message = resource_tracking_messages[0]
@@ -384,7 +414,7 @@ async def _mocked_get_container_states(
384414

385415
@pytest.mark.parametrize("expected_platform_state", SimcorePlatformStatus)
386416
async def test_user_services_crash_when_running(
387-
mock_core_rabbitmq: dict[str, AsyncMock],
417+
mock_post_rabbit_message: AsyncMock,
388418
app: FastAPI,
389419
httpx_async_client: AsyncClient,
390420
client: Client,
@@ -419,7 +449,9 @@ async def test_user_services_crash_when_running(
419449
await _simulate_container_crash(container_names)
420450

421451
# check only start and heartbeats are present
422-
resource_tracking_messages = _get_resource_tracking_messages(mock_core_rabbitmq)
452+
resource_tracking_messages = _get_resource_tracking_messages(
453+
mock_post_rabbit_message
454+
)
423455
assert len(resource_tracking_messages) >= 2
424456

425457
start_message = resource_tracking_messages[0]
@@ -431,11 +463,13 @@ async def test_user_services_crash_when_running(
431463

432464
# reset mock
433465
await asyncio.sleep(_BASE_HEART_BEAT_INTERVAL * 2)
434-
mock_core_rabbitmq["post_rabbit_message"].reset_mock()
466+
mock_post_rabbit_message.reset_mock()
435467

436468
# wait a bit more and check no further heartbeats are sent
437469
await asyncio.sleep(_BASE_HEART_BEAT_INTERVAL * 2)
438-
new_resource_tracking_messages = _get_resource_tracking_messages(mock_core_rabbitmq)
470+
new_resource_tracking_messages = _get_resource_tracking_messages(
471+
mock_post_rabbit_message
472+
)
439473
assert len(new_resource_tracking_messages) == 0
440474

441475
# sending stop events, and since there was an issue multiple stops
@@ -450,7 +484,9 @@ async def test_user_services_crash_when_running(
450484
) as result:
451485
assert result is None
452486

453-
resource_tracking_messages = _get_resource_tracking_messages(mock_core_rabbitmq)
487+
resource_tracking_messages = _get_resource_tracking_messages(
488+
mock_post_rabbit_message
489+
)
454490
# NOTE: only 1 stop event arrives here since the stopping of the containers
455491
# was successful
456492
assert len(resource_tracking_messages) == 1

0 commit comments

Comments
 (0)