Skip to content

Commit 15f4890

Browse files
committed
fix(backend, tests): too many db connections in integration tests
Since the neo4j driver upgrade, we must ensure the neo4j driver is properly closed and that a closed driver is not re-used. We disabled singletons during DI to get that behavior. A side effect is that during some integration tests that leverage local workers, some tasks/flows depend on the injected database instance, which would trigger new driver instances with new db connections. Those instances would not get cleaned since it's not using a singleton nor used within a context manager that would close the underlying db driver. Fix this by introducing a new class level fixture for the db instance and use it within the injected dependency. Signed-off-by: Fatih Acar <fatih@opsmill.com>
1 parent 0ed21ad commit 15f4890

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

backend/tests/helpers/test_app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,16 @@ async def register_core_schema(
120120
await default_branch.save(db=db)
121121
return schema_branch
122122

123+
@pytest.fixture(scope="class")
124+
async def class_db(self) -> InfrahubDatabase:
125+
return await build_database(singleton=False)
126+
123127
@pytest.fixture(scope="class")
124128
async def test_client(
125129
self,
126130
dependency_provider: Provider,
127131
db: InfrahubDatabase,
132+
class_db: InfrahubDatabase,
128133
initialize_registry: None,
129134
redis: dict[int, int] | None,
130135
nats: dict[int, int] | None,
@@ -135,7 +140,7 @@ async def test_client(
135140
# NOTE 2: FastAPI does not have an asynchronous TestClient, thus we rely on httpx.AsyncClient which does not trigger
136141
# lifespan events (see https://fastapi.tiangolo.com/advanced/async-tests/#in-detail).
137142
async def _db(singleton: bool = True) -> InfrahubDatabase:
138-
return await build_database(singleton=False)
143+
return class_db
139144

140145
with dependency_provider.scope(build_database, _db):
141146
async with lifespan(app):

backend/tests/integration/git/test_git_repository.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from pathlib import Path
2+
from typing import AsyncGenerator
23

34
import pytest
45
import yaml
6+
from fast_depends import dependency_provider
57
from infrahub_sdk import Config, InfrahubClient
68
from infrahub_sdk.exceptions import NodeNotFoundError
79
from infrahub_sdk.protocols import CoreCheckDefinition, CoreGraphQLQuery, CoreTransformJinja2, CoreTransformPython
@@ -15,9 +17,10 @@
1517
from infrahub.core.utils import count_relationships, delete_all_nodes
1618
from infrahub.database import InfrahubDatabase
1719
from infrahub.git import InfrahubRepository
18-
from infrahub.server import app, app_initialization
20+
from infrahub.server import app, lifespan
1921
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
2022
from infrahub.utils import get_models_dir
23+
from infrahub.workers.dependencies import build_database
2124
from infrahub.workflows.initialization import setup_task_manager
2225
from tests.helpers.file_repo import FileRepo
2326
from tests.helpers.test_app import TestInfrahubApp
@@ -60,14 +63,23 @@ async def base_dataset(self, db: InfrahubDatabase, redis, nats) -> None:
6063
await load_infrastructure_schema(db=db)
6164
await initialization(db=db)
6265

66+
@pytest.fixture(scope="class")
67+
async def class_db(self) -> InfrahubDatabase:
68+
return await build_database(singleton=False)
69+
6370
@pytest.fixture(scope="class")
6471
async def test_client(
6572
self,
6673
base_dataset,
6774
workflow_local,
68-
) -> InfrahubTestClient:
69-
await app_initialization(app)
70-
return InfrahubTestClient(app=app)
75+
class_db,
76+
) -> AsyncGenerator[InfrahubTestClient, None]:
77+
async def _db(singleton: bool = True) -> InfrahubDatabase:
78+
return class_db
79+
80+
with dependency_provider.scope(build_database, _db):
81+
async with lifespan(app):
82+
yield InfrahubTestClient(app=app)
7183

7284
@pytest.fixture
7385
async def client(self, test_client: InfrahubTestClient, integration_helper) -> InfrahubClient:

0 commit comments

Comments
 (0)