From 36755c772625dbb517fa178c0119cb8503395c96 Mon Sep 17 00:00:00 2001
From: Valentin Lorentz <vlorentz@softwareheritage.org>
Date: Fri, 21 Mar 2025 15:00:24 +0100
Subject: [PATCH 1/2] Tentatively fix 'database "tests" is being accessed by
 other users'

---
 swh/storage/tests/blocking/conftest.py | 13 ++++++++-----
 swh/storage/tests/masking/conftest.py  | 13 ++++++++-----
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/swh/storage/tests/blocking/conftest.py b/swh/storage/tests/blocking/conftest.py
index 8e6a3836d..c6757d965 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/masking/conftest.py b/swh/storage/tests/masking/conftest.py
index 4028ca802..3b1d0ed30 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
-- 
GitLab


From 5e83b055a2eed8ddd99fcb1c432b64b01ef6dc78 Mon Sep 17 00:00:00 2001
From: Valentin Lorentz <vlorentz@softwareheritage.org>
Date: Fri, 21 Mar 2025 15:16:30 +0100
Subject: [PATCH 2/2] moar

---
 swh/storage/pytest_plugin.py                         | 2 ++
 swh/storage/tests/blocking/test_proxy.py             | 6 +++++-
 swh/storage/tests/blocking/test_proxy_blocking.py    | 6 +++++-
 swh/storage/tests/blocking/test_proxy_no_blocking.py | 6 +++++-
 swh/storage/tests/masking/conftest.py                | 6 +++++-
 swh/storage/tests/masking/test_proxy_masking.py      | 6 +++++-
 6 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/swh/storage/pytest_plugin.py b/swh/storage/pytest_plugin.py
index e46b3617f..455bfef65 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/test_proxy.py b/swh/storage/tests/blocking/test_proxy.py
index 2a3d0b606..b86e339f2 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 91c809da9..2f1d83a7c 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 4ba80a2e7..9ed923193 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 3b1d0ed30..ecaa19d2a 100644
--- a/swh/storage/tests/masking/conftest.py
+++ b/swh/storage/tests/masking/conftest.py
@@ -53,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 90fe9a012..efd6bfa32 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):
-- 
GitLab