Skip to content

Commit 4a6f539

Browse files
author
Andrei Neagu
committed
renaming fixture and fixed flaky tests
1 parent f01ae9c commit 4a6f539

File tree

10 files changed

+164
-134
lines changed

10 files changed

+164
-134
lines changed

services/web/server/tests/integration/01/test_garbage_collection.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from simcore_service_webserver.socketio.plugin import setup_socketio
5757
from simcore_service_webserver.users.plugin import setup_users
5858
from sqlalchemy import func, select
59+
from tenacity import AsyncRetrying, stop_after_delay, wait_fixed
5960

6061
log = logging.getLogger(__name__)
6162

@@ -101,7 +102,9 @@ def osparc_product_name() -> str:
101102

102103

103104
@pytest.fixture
104-
async def director_v2_service_mock() -> AsyncIterable[aioresponses]:
105+
async def director_v2_service_mock(
106+
mocker: MockerFixture,
107+
) -> AsyncIterable[aioresponses]:
105108
"""uses aioresponses to mock all calls of an aiohttpclient
106109
WARNING: any request done through the client will go through aioresponses. It is
107110
unfortunate but that means any valid request (like calling the test server) prefix must be set as passthrough.
@@ -115,9 +118,13 @@ async def director_v2_service_mock() -> AsyncIterable[aioresponses]:
115118
projects_networks_pattern = re.compile(
116119
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/dynamic_services/projects/.*/-/networks$"
117120
)
118-
dynamic_services_list_pattern = re.compile(
119-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/dynamic_services?.*"
121+
122+
mocker.patch(
123+
"simcore_service_webserver.dynamic_scheduler.api.list_dynamic_services",
124+
autospec=True,
125+
return_value={},
120126
)
127+
121128
# NOTE: GitHK I have to copy paste that fixture for some unclear reason for now.
122129
# I think this is due to some conflict between these non-pytest-simcore fixtures and the loop fixture being defined at different locations?? not sure..
123130
# anyway I think this should disappear once the garbage collector moves to its own micro-service
@@ -130,7 +137,6 @@ async def director_v2_service_mock() -> AsyncIterable[aioresponses]:
130137
)
131138
mock.delete(delete_computation_pattern, status=204, repeat=True)
132139
mock.patch(projects_networks_pattern, status=204, repeat=True)
133-
mock.get(dynamic_services_list_pattern, status=200, repeat=True, payload=[])
134140
yield mock
135141

136142

@@ -343,10 +349,18 @@ async def disconnect_user_from_socketio(
343349
socket_registry = get_registry(client.app)
344350
await sio.disconnect()
345351
assert not sio.sid
346-
await asyncio.sleep(0) # just to ensure there is a context switch
347-
assert not await socket_registry.find_keys(("socket_id", sio.get_sid()))
348-
assert sid not in await socket_registry.find_resources(resource_key, "socket_id")
349-
assert not await socket_registry.find_resources(resource_key, "socket_id")
352+
353+
async for attempt in AsyncRetrying(
354+
wait=wait_fixed(0.1),
355+
stop=stop_after_delay(10),
356+
reraise=True,
357+
):
358+
with attempt:
359+
assert not await socket_registry.find_keys(("socket_id", sio.get_sid()))
360+
assert sid not in await socket_registry.find_resources(
361+
resource_key, "socket_id"
362+
)
363+
assert not await socket_registry.find_resources(resource_key, "socket_id")
350364

351365

352366
async def assert_users_count(

services/web/server/tests/unit/with_dbs/02/test_projects_cancellations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async def test_copying_large_project_and_aborting_correctly_removes_new_project(
8989
catalog_subsystem_mock: Callable[[list[ProjectDict]], None],
9090
slow_storage_subsystem_mock: MockedStorageSubsystem,
9191
project_db_cleaner: None,
92-
mocked_director_v2_api: dict[str, MagicMock],
92+
mocked_dynamic_services_interface: dict[str, MagicMock],
9393
):
9494
assert client.app
9595
catalog_subsystem_mock([user_project])
@@ -142,7 +142,7 @@ async def test_copying_large_project_and_retrieving_copy_task(
142142
catalog_subsystem_mock: Callable[[list[ProjectDict]], None],
143143
slow_storage_subsystem_mock: MockedStorageSubsystem,
144144
project_db_cleaner: None,
145-
mocked_director_v2_api: dict[str, MagicMock],
145+
mocked_dynamic_services_interface: dict[str, MagicMock],
146146
):
147147
assert client.app
148148
catalog_subsystem_mock([user_project])

services/web/server/tests/unit/with_dbs/02/test_projects_crud_handlers__delete.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def test_delete_project(
6060
user_project: ProjectDict,
6161
expected: ExpectedResponse,
6262
storage_subsystem_mock: MockedStorageSubsystem,
63-
mocked_director_v2_api: dict[str, MagicMock],
63+
mocked_dynamic_services_interface: dict[str, MagicMock],
6464
catalog_subsystem_mock: Callable[[list[ProjectDict]], None],
6565
fake_services: Callable[..., Awaitable[list[DynamicServiceGet]]],
6666
assert_get_same_project_caller: Callable,
@@ -70,7 +70,7 @@ async def test_delete_project(
7070

7171
# DELETE /v0/projects/{project_id}
7272
fakes = await fake_services(5)
73-
mocked_director_v2_api[
73+
mocked_dynamic_services_interface[
7474
"dynamic_scheduler.api.list_dynamic_services"
7575
].return_value = fakes
7676

@@ -90,7 +90,7 @@ async def test_delete_project(
9090
# might have finished, and therefore there is no need to waith
9191
await tasks[0]
9292

93-
mocked_director_v2_api[
93+
mocked_dynamic_services_interface[
9494
"dynamic_scheduler.api.list_dynamic_services"
9595
].assert_called_once()
9696

@@ -108,7 +108,7 @@ async def test_delete_project(
108108
)
109109
for service in fakes
110110
]
111-
mocked_director_v2_api[
111+
mocked_dynamic_services_interface[
112112
"dynamic_scheduler.api.stop_dynamic_service"
113113
].assert_has_calls(expected_calls)
114114

@@ -141,7 +141,7 @@ async def test_delete_multiple_opened_project_forbidden(
141141
client: TestClient,
142142
logged_user: UserInfoDict,
143143
user_project: ProjectDict,
144-
mocked_director_v2_api,
144+
mocked_dynamic_services_interface,
145145
create_dynamic_service_mock: Callable[..., Awaitable[DynamicServiceGet]],
146146
socketio_client_factory: Callable,
147147
client_session_id_factory: Callable,

services/web/server/tests/unit/with_dbs/02/test_projects_metadata_handlers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
@pytest.mark.parametrize(*standard_user_role_response())
4242
async def test_custom_metadata_handlers(
4343
# for deletion
44-
mocked_director_v2_api: dict[str, MagicMock],
44+
mocked_dynamic_services_interface: dict[str, MagicMock],
4545
storage_subsystem_mock: MockedStorageSubsystem,
4646
#
4747
client: TestClient,
@@ -115,7 +115,7 @@ async def _wait_until_deleted():
115115
@pytest.mark.parametrize(*standard_user_role_response())
116116
async def test_new_project_with_parent_project_node(
117117
# for deletion
118-
mocked_director_v2_api: dict[str, MagicMock],
118+
mocked_dynamic_services_interface: dict[str, MagicMock],
119119
storage_subsystem_mock: MockedStorageSubsystem,
120120
#
121121
client: TestClient,
@@ -192,7 +192,7 @@ async def test_new_project_with_parent_project_node(
192192
@pytest.mark.parametrize(*standard_user_role_response())
193193
async def test_new_project_with_invalid_parent_project_node(
194194
# for deletion
195-
mocked_director_v2_api: dict[str, MagicMock],
195+
mocked_dynamic_services_interface: dict[str, MagicMock],
196196
storage_subsystem_mock: MockedStorageSubsystem,
197197
#
198198
client: TestClient,
@@ -275,7 +275,7 @@ async def test_new_project_with_invalid_parent_project_node(
275275
@pytest.mark.parametrize(*standard_user_role_response())
276276
async def test_set_project_parent_backward_compatibility(
277277
# for deletion
278-
mocked_director_v2_api: dict[str, MagicMock],
278+
mocked_dynamic_services_interface: dict[str, MagicMock],
279279
storage_subsystem_mock: MockedStorageSubsystem,
280280
#
281281
client: TestClient,
@@ -339,7 +339,7 @@ async def test_set_project_parent_backward_compatibility(
339339
@pytest.mark.parametrize(*standard_user_role_response())
340340
async def test_update_project_metadata_backward_compatibility_with_same_project_does_not_raises_and_does_not_work(
341341
# for deletion
342-
mocked_director_v2_api: dict[str, MagicMock],
342+
mocked_dynamic_services_interface: dict[str, MagicMock],
343343
storage_subsystem_mock: MockedStorageSubsystem,
344344
#
345345
client: TestClient,
@@ -394,7 +394,7 @@ async def test_update_project_metadata_backward_compatibility_with_same_project_
394394
@pytest.mark.parametrize(*standard_user_role_response())
395395
async def test_update_project_metadata_s4lacad_backward_compatibility_passing_nil_parent_node_id(
396396
# for deletion
397-
mocked_director_v2_api: dict[str, MagicMock],
397+
mocked_dynamic_services_interface: dict[str, MagicMock],
398398
storage_subsystem_mock: MockedStorageSubsystem,
399399
#
400400
client: TestClient,

0 commit comments

Comments
 (0)