Skip to content

Commit

Permalink
descope fixtures from module to function
Browse files Browse the repository at this point in the history
Due to changes in pytest-asyncio 0.23, the event loop used by
fixtures/tests with different scopes is not the same. For example,
a fixture declared with "module" scope will use a different event loop
to tests, which run with the "function" scope's event loop. This causes
many tests to fail.
Despite being less efficient, the prudent way forward for now is to
modify the fixtures to be recreated for each test. Astacus' tests are
very fast, so this does not slow down CI runs very much at all.
  • Loading branch information
nicois committed Apr 30, 2024
1 parent ce330ea commit 1648340
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
14 changes: 2 additions & 12 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
from astacus.common.utils import build_netloc
from astacus.coordinator.plugins.zookeeper import KazooZooKeeperClient
from collections.abc import AsyncIterator, Iterator, Mapping, Sequence
from collections.abc import AsyncIterator, Mapping, Sequence
from pathlib import Path
from types import MappingProxyType

Expand All @@ -21,16 +21,6 @@
logger = logging.getLogger(__name__)


@pytest.fixture(scope="module", name="event_loop")
def fixture_event_loop() -> Iterator[asyncio.AbstractEventLoop]:
# This is the same as the original `event_loop` fixture from `pytest_asyncio`
# but with a module scope, re-declaring this fixture is their suggested way
# of locally increasing the scope of this fixture.
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


async def get_command_path(name: str) -> Path | None:
process = await asyncio.create_subprocess_exec(
"which", name, stderr=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE
Expand Down Expand Up @@ -147,7 +137,7 @@ def fixture_ports() -> Ports:
return Ports()


@pytest.fixture(scope="module", name="zookeeper")
@pytest.fixture(name="zookeeper")
async def fixture_zookeeper(ports: Ports) -> AsyncIterator[Service]:
async with create_zookeeper(ports) as zookeeper:
yield zookeeper
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/coordinator/plugins/clickhouse/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ClickHouseServiceCluster(ServiceCluster):
ClickHouseCommand = Sequence[str | Path]


@pytest.fixture(scope="module", name="clickhouse_command")
@pytest.fixture(scope="function", name="clickhouse_command")
async def fixture_clickhouse_command(request: FixtureRequest) -> ClickHouseCommand:
clickhouse_path = request.config.getoption(CLICKHOUSE_PATH_OPTION)
if clickhouse_path is None:
Expand All @@ -91,7 +91,7 @@ async def fixture_clickhouse_command(request: FixtureRequest) -> ClickHouseComma
return get_clickhouse_command(clickhouse_path)


@pytest.fixture(scope="module", name="clickhouse_restore_command")
@pytest.fixture(scope="function", name="clickhouse_restore_command")
def fixture_clickhouse_restore_command(request: FixtureRequest, clickhouse_command: ClickHouseCommand) -> ClickHouseCommand:
clickhouse_restore_path = request.config.getoption(CLICKHOUSE_RESTORE_PATH_OPTION)
if clickhouse_restore_path is None:
Expand All @@ -103,7 +103,7 @@ def get_clickhouse_command(clickhouse_path: Path) -> ClickHouseCommand:
return [clickhouse_path] if clickhouse_path.name.endswith("-server") else [clickhouse_path, "server"]


@pytest.fixture(scope="module", name="clickhouse")
@pytest.fixture(scope="function", name="clickhouse")
async def fixture_clickhouse(ports: Ports, clickhouse_command: ClickHouseCommand) -> AsyncIterator[Service]:
async with create_clickhouse_service(ports, clickhouse_command) as service:
yield service
Expand Down Expand Up @@ -192,13 +192,13 @@ def bucket(self, *, bucket_name: str) -> Iterator[MinioBucket]:
s3_client.delete_bucket(Bucket=bucket_name) # type: ignore[attr-defined]


@pytest.fixture(scope="module", name="minio")
@pytest.fixture(scope="function", name="minio")
async def fixture_minio(ports: Ports) -> AsyncIterator[MinioService]:
async with create_minio_service(ports) as service:
yield service


@pytest.fixture(scope="module", name="minio_bucket")
@pytest.fixture(scope="function", name="minio_bucket")
async def fixture_minio_bucket(minio: MinioService) -> AsyncIterator[MinioBucket]:
with minio.bucket(bucket_name="clickhouse-bucket") as bucket:
yield bucket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def restorable_cluster_manager(
yield storage_path


@pytest.fixture(scope="module", name="restorable_cluster")
@pytest.fixture(scope="function", name="restorable_cluster")
async def fixture_restorable_cluster(
ports: Ports,
clickhouse_command: ClickHouseCommand,
Expand Down Expand Up @@ -148,7 +148,7 @@ async def restored_cluster_manager(
yield clients


@pytest.fixture(scope="module", name="restored_cluster", params=[*get_restore_steps_names(), None])
@pytest.fixture(scope="function", name="restored_cluster", params=[*get_restore_steps_names(), None])
async def fixture_restored_cluster(
ports: Ports,
request: SubRequest,
Expand Down

0 comments on commit 1648340

Please sign in to comment.