diff --git a/swh/storage/pytest_plugin.py b/swh/storage/pytest_plugin.py index e46b3617f7d7c93fa7203fdbdafd8feba648c743..455bfef65801a89640d0a665acc9f7b494303e58 100644 --- a/swh/storage/pytest_plugin.py +++ b/swh/storage/pytest_plugin.py @@ -400,6 +400,8 @@ def swh_storage_backend(swh_storage_backend_config): if hasattr(backend, "_cql_runner") and hasattr(backend._cql_runner, "_cluster"): backend._cql_runner._cluster.shutdown() + if hasattr(backend, "_pool"): + backend._pool.close() @pytest.fixture diff --git a/swh/storage/tests/blocking/conftest.py b/swh/storage/tests/blocking/conftest.py index 8e6a3836d4acb5b78b3737957704dd6a6f6f8d8c..c6757d96557a32dfc2cc0a25bdbfb26232603528 100644 --- a/swh/storage/tests/blocking/conftest.py +++ b/swh/storage/tests/blocking/conftest.py @@ -1,9 +1,10 @@ -# Copyright (C) 2024 The Software Heritage developers +# Copyright (C) 2024-2025 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from functools import partial +from typing import Iterator import pytest from pytest_postgresql import factories @@ -28,10 +29,12 @@ blocking_db_postgresql = factories.postgresql( @pytest.fixture -def blocking_admin(blocking_db_postgresql) -> BlockingAdmin: - return BlockingAdmin.connect(blocking_db_postgresql.info.dsn) +def blocking_admin(blocking_db_postgresql) -> Iterator[BlockingAdmin]: + with BlockingAdmin.connect(blocking_db_postgresql.info.dsn) as db: + yield db @pytest.fixture -def blocking_query(blocking_db_postgresql) -> BlockingQuery: - return BlockingQuery.connect(blocking_db_postgresql.info.dsn) +def blocking_query(blocking_db_postgresql) -> Iterator[BlockingQuery]: + with BlockingQuery.connect(blocking_db_postgresql.info.dsn) as db: + yield db diff --git a/swh/storage/tests/blocking/test_proxy.py b/swh/storage/tests/blocking/test_proxy.py index 2a3d0b6066485c2ca32eeeaf84a73fe3f312e1b2..b86e339f27ae432e5f35020aeeedf2f2d596edff 100644 --- a/swh/storage/tests/blocking/test_proxy.py +++ b/swh/storage/tests/blocking/test_proxy.py @@ -31,9 +31,13 @@ def swh_storage_backend_config(): @pytest.fixture def swh_storage(blocking_db_postgresql, swh_storage_backend): - return BlockingProxyStorage( + storage = BlockingProxyStorage( db=blocking_db_postgresql.info.dsn, storage=swh_storage_backend ) + try: + yield storage + finally: + storage._blocking_pool.close() def set_origin_visibility(blocking_admin, slug="foo", reason="bar"): diff --git a/swh/storage/tests/blocking/test_proxy_blocking.py b/swh/storage/tests/blocking/test_proxy_blocking.py index 91c809da92523c56d2e7968f3886707c867caea6..2f1d83a7c90ef0a6418011e5892c4121e65331bf 100644 --- a/swh/storage/tests/blocking/test_proxy_blocking.py +++ b/swh/storage/tests/blocking/test_proxy_blocking.py @@ -43,9 +43,13 @@ def swh_storage(blocking_db_postgresql, blocking_admin, swh_storage_backend): urls=list(BLOCKED_ORIGINS), ) - return BlockingProxyStorage( + storage = BlockingProxyStorage( db=blocking_db_postgresql.info.dsn, storage=swh_storage_backend ) + try: + yield storage + finally: + storage._blocking_pool.close() class TestStorage(_TestStorage): diff --git a/swh/storage/tests/blocking/test_proxy_no_blocking.py b/swh/storage/tests/blocking/test_proxy_no_blocking.py index 4ba80a2e7266d61d9d396f8f14a8983d81a1cf7c..9ed9231931a268c1634cda905261b6bc9ea64469 100644 --- a/swh/storage/tests/blocking/test_proxy_no_blocking.py +++ b/swh/storage/tests/blocking/test_proxy_no_blocking.py @@ -25,9 +25,13 @@ def swh_storage_backend_config(): @pytest.fixture def swh_storage(blocking_db_postgresql, swh_storage_backend): - return BlockingProxyStorage( + storage = BlockingProxyStorage( db=blocking_db_postgresql.info.dsn, storage=swh_storage_backend ) + try: + yield storage + finally: + storage._blocking_pool.close() class TestStorage(_TestStorage): diff --git a/swh/storage/tests/masking/conftest.py b/swh/storage/tests/masking/conftest.py index 4028ca802f7c54d9fb5c8293e4147060082e432e..ecaa19d2a70189c4427798dffba3a7ae976d2d60 100644 --- a/swh/storage/tests/masking/conftest.py +++ b/swh/storage/tests/masking/conftest.py @@ -1,9 +1,10 @@ -# Copyright (C) 2024 The Software Heritage developers +# Copyright (C) 2024-2025 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from functools import partial +from typing import Iterator import pytest from pytest_postgresql import factories @@ -29,13 +30,15 @@ masking_db_postgresql = factories.postgresql( @pytest.fixture -def masking_admin(masking_db_postgresql) -> MaskingAdmin: - return MaskingAdmin.connect(masking_db_postgresql.info.dsn) +def masking_admin(masking_db_postgresql) -> Iterator[MaskingAdmin]: + with MaskingAdmin.connect(masking_db_postgresql.info.dsn) as db: + yield db @pytest.fixture -def masking_query(masking_db_postgresql) -> MaskingQuery: - return MaskingQuery.connect(masking_db_postgresql.info.dsn) +def masking_query(masking_db_postgresql) -> Iterator[MaskingQuery]: + with MaskingQuery.connect(masking_db_postgresql.info.dsn) as db: + yield db @pytest.fixture @@ -50,6 +53,10 @@ def swh_storage_backend_config(): @pytest.fixture def swh_storage(masking_db_postgresql, swh_storage_backend): - return MaskingProxyStorage( + storage = MaskingProxyStorage( db=masking_db_postgresql.info.dsn, storage=swh_storage_backend ) + try: + yield storage + finally: + storage._masking_pool.close() diff --git a/swh/storage/tests/masking/test_proxy_masking.py b/swh/storage/tests/masking/test_proxy_masking.py index 90fe9a0122b1421b860f46a3042eeaa8bcb10e34..efd6bfa3212eb937cc3e628bb4c08ee831042527 100644 --- a/swh/storage/tests/masking/test_proxy_masking.py +++ b/swh/storage/tests/masking/test_proxy_masking.py @@ -34,9 +34,13 @@ def swh_storage(masking_db_postgresql, masking_admin, swh_storage_backend): swhids=list(MASKED_SWHIDS), ) - return MaskingProxyStorage( + storage = MaskingProxyStorage( db=masking_db_postgresql.info.dsn, storage=swh_storage_backend ) + try: + yield storage + finally: + storage._masking_pool.close() class TestStorage(_TestStorage):