Skip to content

Commit bb517d6

Browse files
committed
✨ Refactor: Update background tasks setup to use context manager for improved test handling
1 parent 0c7623c commit bb517d6

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

services/catalog/tests/unit/conftest.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,26 @@ def postgres_setup_disabled(mocker: MockerFixture) -> MockType:
224224
def background_tasks_setup_disabled(mocker: MockerFixture) -> None:
225225
"""patch the setup of the background task so we can call it manually"""
226226

227-
def _factory(name):
228-
async def _side_effect(app: FastAPI):
229-
assert app
227+
class MockedBackgroundTaskContextManager:
228+
async def __aenter__(self):
230229
print(
231230
"TEST",
232231
background_tasks_setup_disabled.__name__,
233-
"Disabled background tasks. Skipping execution of",
234-
name,
232+
"Disabled background tasks. Skipping execution of __aenter__",
235233
)
236234

237-
return _side_effect
235+
async def __aexit__(self, exc_type, exc_value, traceback):
236+
print(
237+
"TEST",
238+
background_tasks_setup_disabled.__name__,
239+
"Disabled background tasks. Skipping execution of __aexit__",
240+
)
238241

239-
for name in ("start_registry_sync_task", "stop_registry_sync_task"):
240-
mocker.patch.object(
241-
simcore_service_catalog.core.events,
242-
name,
243-
side_effect=_factory(name),
244-
autospec=True,
245-
)
242+
mocker.patch.object(
243+
simcore_service_catalog.core.events,
244+
"setup_background_task",
245+
return_value=MockedBackgroundTaskContextManager(),
246+
)
246247

247248

248249
#
@@ -253,10 +254,8 @@ async def _side_effect(app: FastAPI):
253254
@pytest.fixture
254255
def rabbitmq_and_rpc_setup_disabled(mocker: MockerFixture):
255256
# The following services are affected if rabbitmq is not in place
256-
mocker.patch.object(simcore_service_catalog.core.application, "setup_rabbitmq")
257-
mocker.patch.object(
258-
simcore_service_catalog.core.application, "setup_rpc_api_routes"
259-
)
257+
mocker.patch.object(simcore_service_catalog.core.events, "setup_rabbitmq")
258+
mocker.patch.object(simcore_service_catalog.core.events, "setup_rpc_api_routes")
260259

261260

262261
@pytest.fixture
@@ -272,8 +271,8 @@ async def rpc_client(
272271

273272

274273
@pytest.fixture
275-
def director_setup_disabled(monkeypatch: pytest.MonkeyPatch) -> None:
276-
monkeypatch.setenv("CATALOG_DIRECTOR", "null")
274+
def director_setup_disabled(mocker: MockerFixture) -> None:
275+
mocker.patch.object(simcore_service_catalog.core.events, "setup_director")
277276

278277

279278
@pytest.fixture

0 commit comments

Comments
 (0)