Repository: rucio/rucio

Issue:
Nightly builds fail for SQLLite
### Description

For the sqllite based auto tests the nightly builds are failing.

### Steps to reproduce

https://github.com/rucio/rucio/actions/runs/4486048949

### Rucio Version

1.31.0

### Additional Information

_No response_


Discussion:

Resolving patch:
diff --git a/lib/rucio/core/replica.py b/lib/rucio/core/replica.py
--- a/lib/rucio/core/replica.py
+++ b/lib/rucio/core/replica.py
@@ -1111,7 +1111,7 @@ def _list_replicas_with_temp_tables(
         resolve_parents: bool = False,
         nrandom: "Optional[int]" = None,
         updated_after: "Optional[datetime]" = None,
-        by_rse_name: bool = True,
+        by_rse_name: bool = False,
         *,
         session: "Session",
 ):
@@ -4441,7 +4441,7 @@ def _list_replicas_wo_temp_tables(
         resolve_parents: bool = False,
         nrandom: "Optional[int]" = None,
         updated_after: "Optional[datetime]" = None,
-        by_rse_name: bool = True,
+        by_rse_name: bool = False,
         *,
         session: "Session",
 ):
diff --git a/lib/rucio/daemons/conveyor/preparer.py b/lib/rucio/daemons/conveyor/preparer.py
--- a/lib/rucio/daemons/conveyor/preparer.py
+++ b/lib/rucio/daemons/conveyor/preparer.py
@@ -157,13 +157,13 @@ def run_once(
             reqs_no_source.update(reqs_no_transfertool)
             if reqs_no_source:
                 logger(logging.INFO, "Marking requests as no-sources: %s", reqs_no_source)
-                set_requests_state_if_possible(reqs_no_source, RequestState.NO_SOURCES, logger=logger)
+                set_requests_state_if_possible(reqs_no_source, RequestState.NO_SOURCES, logger=logger, session=session)
             if reqs_only_tape_source:
                 logger(logging.INFO, "Marking requests as only-tape-sources: %s", reqs_only_tape_source)
-                set_requests_state_if_possible(reqs_only_tape_source, RequestState.ONLY_TAPE_SOURCES, logger=logger)
+                set_requests_state_if_possible(reqs_only_tape_source, RequestState.ONLY_TAPE_SOURCES, logger=logger, session=session)
             if reqs_scheme_mismatch:
                 logger(logging.INFO, "Marking requests as scheme-mismatch: %s", reqs_scheme_mismatch)
-                set_requests_state_if_possible(reqs_scheme_mismatch, RequestState.MISMATCH_SCHEME, logger=logger)
+                set_requests_state_if_possible(reqs_scheme_mismatch, RequestState.MISMATCH_SCHEME, logger=logger, session=session)
     except RucioException:
         logger(logging.ERROR, 'errored with a RucioException, retrying later', exc_info=True)
         updated_msg = 'errored'


Test patch:
diff --git a/lib/rucio/tests/conftest.py b/lib/rucio/tests/conftest.py
--- a/lib/rucio/tests/conftest.py
+++ b/lib/rucio/tests/conftest.py
@@ -251,10 +251,14 @@ def containerized_rses(rucio_client):
 
 
 @pytest.fixture
-def rse_factory(vo, function_scope_prefix):
+def rse_factory(request, vo, function_scope_prefix):
     from rucio.tests.temp_factories import TemporaryRSEFactory
 
-    with TemporaryRSEFactory(vo=vo, name_prefix=function_scope_prefix) as factory:
+    session = None
+    if 'db_session' in request.fixturenames:
+        session = request.getfixturevalue('db_session')
+
+    with TemporaryRSEFactory(vo=vo, name_prefix=function_scope_prefix, db_session=session) as factory:
         yield factory
 
 
@@ -271,10 +275,15 @@ def rse_factory_unittest(request, vo, class_scope_prefix):
 
 
 @pytest.fixture
-def did_factory(vo, mock_scope, function_scope_prefix, file_factory):
+def did_factory(request, vo, mock_scope, function_scope_prefix, file_factory, root_account):
     from rucio.tests.temp_factories import TemporaryDidFactory
 
-    with TemporaryDidFactory(vo=vo, default_scope=mock_scope, name_prefix=function_scope_prefix, file_factory=file_factory) as factory:
+    session = None
+    if 'db_session' in request.fixturenames:
+        session = request.getfixturevalue('db_session')
+
+    with TemporaryDidFactory(vo=vo, default_scope=mock_scope, name_prefix=function_scope_prefix, file_factory=file_factory,
+                             default_account=root_account, db_session=session) as factory:
         yield factory
 
 
@@ -326,7 +335,7 @@ def tag_factory_class(class_scope_prefix):
 
 
 @pytest.fixture
-def db_session(did_factory, rse_factory):
+def db_session():
     from rucio.db.sqla import session
 
     db_session = session.get_session()
diff --git a/lib/rucio/tests/temp_factories.py b/lib/rucio/tests/temp_factories.py
--- a/lib/rucio/tests/temp_factories.py
+++ b/lib/rucio/tests/temp_factories.py
@@ -25,7 +25,9 @@
 from rucio.common.types import InternalScope
 from rucio.common.utils import execute, generate_uuid
 from rucio.core import rse as rse_core
+from rucio.core import did as did_core
 from rucio.db.sqla import models
+from rucio.db.sqla.constants import DIDType
 from rucio.db.sqla.session import transactional_session
 from rucio.tests.common import did_name_generator
 from rucio.tests.common_server import cleanup_db_deps
@@ -45,10 +47,11 @@ class TemporaryRSEFactory:
     Factory which keeps track of created RSEs and cleans up everything related to these RSEs at the end
     """
 
-    def __init__(self, vo, name_prefix, **kwargs):
+    def __init__(self, vo, name_prefix, db_session=None, **kwargs):
         self.vo = vo
         self.name_prefix = name_prefix.upper().replace('_', '-')
         self.created_rses = set()
+        self.db_session = db_session
 
     def __enter__(self):
         return self
@@ -56,11 +59,11 @@ def __enter__(self):
     def __exit__(self, exc_type, exc_val, exc_tb):
         self.cleanup()
 
-    def cleanup(self):
+    def cleanup(self, session=None):
         if not self.created_rses:
             return
 
-        self._cleanup_db_deps()
+        self._cleanup_db_deps(session=session or self.db_session)
 
     @transactional_session
     def _cleanup_db_deps(self, *, session=None):
@@ -76,12 +79,13 @@ def _cleanup_rses(self):
             # So running test in parallel results in some tests failing on foreign key errors.
             rse_core.del_rse(rse_id)
 
-    def _make_rse(self, scheme, protocol_impl, parameters=None, add_rse_kwargs=None) -> "Tuple[str, str]":
+    def _make_rse(self, scheme, protocol_impl, parameters=None, add_rse_kwargs=None, session=None) -> "Tuple[str, str]":
+        session = session or self.db_session
         rse_name = self.name_prefix + ''.join(choice(ascii_uppercase) for _ in range(6))
         if add_rse_kwargs and 'vo' in add_rse_kwargs:
-            rse_id = rse_core.add_rse(rse_name, **add_rse_kwargs)
+            rse_id = rse_core.add_rse(rse_name, session=session, **add_rse_kwargs)
         else:
-            rse_id = rse_core.add_rse(rse_name, vo=self.vo, **(add_rse_kwargs or {}))
+            rse_id = rse_core.add_rse(rse_name, vo=self.vo, session=session, **(add_rse_kwargs or {}))
         if scheme and protocol_impl:
             prefix = '/test_%s/' % rse_id
             if protocol_impl == 'rucio.rse.protocols.posix.Default':
@@ -108,27 +112,27 @@ def _make_rse(self, scheme, protocol_impl, parameters=None, add_rse_kwargs=None)
                 }
             }
             protocol_parameters.update(parameters or {})
-            rse_core.add_protocol(rse_id=rse_id, parameter=protocol_parameters)
+            rse_core.add_protocol(rse_id=rse_id, parameter=protocol_parameters, session=session)
         self.created_rses.add(rse_id)
         return rse_name, rse_id
 
     def make_rse(self, scheme=None, protocol_impl=None, **kwargs):
         return self._make_rse(scheme=scheme, protocol_impl=protocol_impl, add_rse_kwargs=kwargs)
 
-    def make_posix_rse(self, **kwargs):
-        return self._make_rse(scheme='file', protocol_impl='rucio.rse.protocols.posix.Default', add_rse_kwargs=kwargs)
+    def make_posix_rse(self, session=None, **kwargs):
+        return self._make_rse(scheme='file', protocol_impl='rucio.rse.protocols.posix.Default', add_rse_kwargs=kwargs, session=session)
 
-    def make_mock_rse(self, **kwargs):
-        return self._make_rse(scheme='mock', protocol_impl='rucio.rse.protocols.mock.Default', add_rse_kwargs=kwargs)
+    def make_mock_rse(self, session=None, **kwargs):
+        return self._make_rse(scheme='mock', protocol_impl='rucio.rse.protocols.mock.Default', add_rse_kwargs=kwargs, session=session)
 
-    def make_xroot_rse(self, **kwargs):
-        return self._make_rse(scheme='root', protocol_impl='rucio.rse.protocols.xrootd.Default', add_rse_kwargs=kwargs)
+    def make_xroot_rse(self, session=None, **kwargs):
+        return self._make_rse(scheme='root', protocol_impl='rucio.rse.protocols.xrootd.Default', add_rse_kwargs=kwargs, session=session)
 
-    def make_srm_rse(self, **kwargs):
+    def make_srm_rse(self, session=None, **kwargs):
         parameters = {
             "extended_attributes": {"web_service_path": "/srm/managerv2?SFN=", "space_token": "RUCIODISK"},
         }
-        return self._make_rse(scheme='srm', protocol_impl='rucio.rse.protocols.srm.Default', parameters=parameters, add_rse_kwargs=kwargs)
+        return self._make_rse(scheme='srm', protocol_impl='rucio.rse.protocols.srm.Default', parameters=parameters, add_rse_kwargs=kwargs, session=session)
 
 
 class TemporaryDidFactory:
@@ -137,11 +141,13 @@ class TemporaryDidFactory:
     All files related to the same test will have the same uuid in the name for easier debugging.
     """
 
-    def __init__(self, default_scope, vo, name_prefix, file_factory):
+    def __init__(self, default_scope, vo, name_prefix, file_factory, default_account, db_session=None):
+        self.default_account = default_account
         self.default_scope = default_scope
         self.vo = vo
         self.name_prefix = name_prefix
         self.file_factory = file_factory
+        self.db_session = db_session
 
         self._client = None
         self._upload_client = None
@@ -154,23 +160,21 @@ def __enter__(self):
     def __exit__(self, exc_type, exc_val, exc_tb):
         self.cleanup()
 
-    @property
-    def client(self):
-        if not self._client:
-            self._client = Client(vo=self.vo)
-        return self._client
-
     @property
     def upload_client(self):
         if not self._upload_client:
-            self._upload_client = UploadClient(self.client)
+            client = Client(vo=self.vo)
+            self._upload_client = UploadClient(client)
         return self._upload_client
 
-    @transactional_session
-    def cleanup(self, *, session=None):
+    def cleanup(self, session=None):
         if not self.created_dids:
             return
 
+        self._cleanup_db_deps(session=session or self.db_session)
+
+    @transactional_session
+    def _cleanup_db_deps(self, *, session=None):
         select_dids_stmt = or_(and_(models.DataIdentifier.scope == scope,
                                     models.DataIdentifier.name == name)
                                for scope, name in self.created_dids)
@@ -221,17 +225,19 @@ def random_container_did(self, scope=None, *, external=False):
         did = self._random_did(did_type='container', scope=scope)
         return _to_external(did) if external else did
 
-    def make_dataset(self, scope=None, *, external=False):
+    def make_dataset(self, scope=None, account=None, session=None, *, external=False):
+        account = account or self.default_account
+        session = session or self.db_session
         did = self.random_dataset_did(scope=scope)
-        external_did = _to_external(did)
-        self.client.add_dataset(**external_did)
-        return external_did if external else did
+        did_core.add_did(**did, did_type=DIDType.DATASET, account=account, session=session)
+        return _to_external(did) if external else did
 
-    def make_container(self, scope=None, *, external=False):
+    def make_container(self, scope=None, account=None, session=None, *, external=False):
+        account = account or self.default_account
+        session = session or self.db_session
         did = self.random_container_did(scope=scope)
-        external_did = _to_external(did)
-        self.client.add_container(**external_did)
-        return external_did if external else did
+        did_core.add_did(**did, did_type=DIDType.CONTAINER, account=account, session=session)
+        return _to_external(did) if external else did
 
     def upload_test_file(self, rse_name, scope=None, name=None, path=None, size=2, return_full_item=False):
         scope = self._sanitize_or_set_scope(scope)
diff --git a/lib/rucio/tests/test_account_limits.py b/lib/rucio/tests/test_account_limits.py
--- a/lib/rucio/tests/test_account_limits.py
+++ b/lib/rucio/tests/test_account_limits.py
@@ -67,8 +67,8 @@ def test_get_global_account_usage(self, account, rse_factory, db_session):
         rse4, _ = rse_factory.make_mock_rse()
         limit1 = 10
         limit2 = 20
-        account_limit.set_global_account_limit(account, f'{rse1}|{rse2}', limit1)
-        account_limit.set_global_account_limit(account, f'{rse3}|{rse4}', limit2)
+        account_limit.set_global_account_limit(account, f'{rse1}|{rse2}', limit1, session=db_session)
+        account_limit.set_global_account_limit(account, f'{rse3}|{rse4}', limit2, session=db_session)
         results = account_limit.get_global_account_usage(account=account, session=db_session)
         assert len(results) == 2
 
diff --git a/lib/rucio/tests/test_counter.py b/lib/rucio/tests/test_counter.py
--- a/lib/rucio/tests/test_counter.py
+++ b/lib/rucio/tests/test_counter.py
@@ -90,8 +90,10 @@ class TestCoreAccountCounter:
 
     def test_inc_dec_get_counter(self, jdoe_account, rse_factory, db_session):
         """ACCOUNT COUNTER (CORE): Increase, decrease and get counter """
+        db_session.commit()
         account_update(once=True)
-        _, rse_id = rse_factory.make_mock_rse()
+        _, rse_id = rse_factory.make_mock_rse(session=db_session)
+        db_session.commit()
         account = jdoe_account
         account_counter.del_counter(rse_id=rse_id, account=account)
         account_counter.add_counter(rse_id=rse_id, account=account)
diff --git a/lib/rucio/tests/test_download.py b/lib/rucio/tests/test_download.py
--- a/lib/rucio/tests/test_download.py
+++ b/lib/rucio/tests/test_download.py
@@ -562,12 +562,12 @@ def test_nrandom_respected(rse_factory, did_factory, download_client, root_accou
         assert len(result) == nrandom
 
 
-def test_download_blocklisted_replicas(rse_factory, did_factory, download_client):
+def test_download_blocklisted_replicas(rse_factory, did_factory, download_client, rse_client):
     rse, _ = rse_factory.make_posix_rse()
     did = did_factory.upload_test_file(rse)
     did_str = '%s:%s' % (did['scope'], did['name'])
 
-    did_factory.client.update_rse(rse, {'availability_read': False})
+    rse_client.update_rse(rse, {'availability_read': False})
 
     with TemporaryDirectory() as tmp_dir:
         with pytest.raises(NoFilesDownloaded):
diff --git a/lib/rucio/tests/test_preparer.py b/lib/rucio/tests/test_preparer.py
--- a/lib/rucio/tests/test_preparer.py
+++ b/lib/rucio/tests/test_preparer.py
@@ -27,15 +27,15 @@
 
 
 @pytest.fixture
-def dest_rse(vo, rse_factory):
-    rse_name, rse_id = rse_factory.make_mock_rse()
+def dest_rse(vo, rse_factory, db_session):
+    rse_name, rse_id = rse_factory.make_mock_rse(session=db_session)
     yield {'name': rse_name, 'id': rse_id}
 
 
 @pytest.fixture
-def source_rse(vo, rse_factory, dest_rse):
-    rse_name, rse_id = rse_factory.make_mock_rse()
-    add_distance(rse_id, dest_rse['id'], distance=5)
+def source_rse(vo, rse_factory, dest_rse, db_session):
+    rse_name, rse_id = rse_factory.make_mock_rse(session=db_session)
+    add_distance(rse_id, dest_rse['id'], distance=5, session=db_session)
     yield {'name': rse_name, 'id': rse_id}
 
 
@@ -47,7 +47,7 @@ def file(vo, did_factory):
 
 @pytest.fixture
 def dataset(db_session, did_factory, vo):
-    return did_factory.make_dataset()
+    return did_factory.make_dataset(session=db_session)
 
 
 @pytest.fixture
@@ -84,8 +84,8 @@ def mock_request_no_source(db_session, dest_rse, dataset, root_account):
     yield request_dict
 
 
-def test_listing_preparing_transfers(mock_request):
-    req_sources = list_transfer_requests_and_source_replicas(request_state=RequestState.PREPARING)
+def test_listing_preparing_transfers(mock_request, db_session):
+    req_sources = list_transfer_requests_and_source_replicas(request_state=RequestState.PREPARING, session=db_session)
 
     assert len(req_sources) != 0
     found_requests = list(filter(lambda rws: rws.request_id == mock_request['id'], req_sources.values()))
@@ -93,19 +93,20 @@ def test_listing_preparing_transfers(mock_request):
 
 
 @pytest.mark.noparallel(reason='uses preparer')
-@pytest.mark.parametrize("core_config_mock", [{"table_content": [
+@pytest.mark.parametrize("file_config_mock", [{"overrides": [
     ('throttler', 'mode', 'DEST_PER_ACT')
 ]}], indirect=True)
-def test_preparer_setting_request_state_waiting(db_session, dest_rse, mock_request, core_config_mock):
+def test_preparer_setting_request_state_waiting(db_session, dest_rse, mock_request, file_config_mock):
     set_transfer_limit(
         dest_rse['name'],
         activity=mock_request['activity'],
         max_transfers=1,
         strategy='fifo',
+        session=db_session,
     )
-    list(list_transfer_limits())
+    list(list_transfer_limits(session=db_session))
 
-    preparer.run_once(logger=print)
+    preparer.run_once(logger=print, session=db_session)
 
     updated_mock_request = db_session.query(models.Request).filter_by(id=mock_request['id']).one()  # type: models.Request
 
@@ -114,7 +115,7 @@ def test_preparer_setting_request_state_waiting(db_session, dest_rse, mock_reque
 
 @pytest.mark.noparallel(reason='uses preparer')
 def test_preparer_setting_request_state_queued(db_session, mock_request):
-    preparer.run_once(logger=print)
+    preparer.run_once(logger=print, session=db_session)
 
     updated_mock_request = db_session.query(models.Request).filter_by(id=mock_request['id']).one()  # type: models.Request
 
@@ -123,7 +124,7 @@ def test_preparer_setting_request_state_queued(db_session, mock_request):
 
 @pytest.mark.noparallel(reason='uses preparer')
 def test_preparer_setting_request_source(db_session, vo, source_rse, mock_request):
-    preparer.run_once(logger=print)
+    preparer.run_once(logger=print, session=db_session)
 
     updated_mock_request = db_session.query(models.Request).filter_by(id=mock_request['id']).one()  # type: models.Request
 
@@ -133,7 +134,7 @@ def test_preparer_setting_request_source(db_session, vo, source_rse, mock_reques
 
 @pytest.mark.noparallel(reason='uses preparer')
 def test_preparer_for_request_without_source(db_session, mock_request_no_source):
-    preparer.run_once(logger=print)
+    preparer.run_once(logger=print, session=db_session)
 
     updated_mock_request: "models.Request" = (
         db_session.query(models.Request).filter_by(id=mock_request_no_source['id']).one()
@@ -151,7 +152,7 @@ def test_preparer_without_and_with_mat(db_session, source_rse, dest_rse, mock_re
 
     REGION.invalidate()
 
-    preparer.run_once(logger=print, transfertools=['fts3', 'globus'])
+    preparer.run_once(logger=print, transfertools=['fts3', 'globus'], session=db_session)
 
     db_session.expunge_all()
     updated_mock_request = db_session.query(models.Request).filter_by(id=mock_request['id']).one()  # type: models.Request
@@ -161,21 +162,22 @@ def test_preparer_without_and_with_mat(db_session, source_rse, dest_rse, mock_re
 
 @pytest.mark.noparallel(reason='uses preparer')
 def test_two_sources_one_destination(rse_factory, source_rse, db_session, vo, file, mock_request):
-    _, source_rse2_id = rse_factory.make_mock_rse()
-    add_distance(source_rse2_id, mock_request['dest_rse_id'], distance=2)
-    add_replicas(rse_id=source_rse2_id, files=[file], account=mock_request['account'])
+    _, source_rse2_id = rse_factory.make_mock_rse(session=db_session)
+    add_distance(source_rse2_id, mock_request['dest_rse_id'], distance=2, session=db_session)
+    add_replicas(rse_id=source_rse2_id, files=[file], account=mock_request['account'], session=db_session)
 
     src1_distance, src2_distance = (
         get_distances(
             src_rse_id=src_rse,
             dest_rse_id=mock_request['dest_rse_id'],
+            session=db_session,
         )
         for src_rse in (source_rse['id'], source_rse2_id)
     )
     assert src1_distance and len(src1_distance) == 1 and src1_distance[0]['distance'] == 5
     assert src2_distance and len(src2_distance) == 1 and src2_distance[0]['distance'] == 2
 
-    preparer.run_once(logger=print)
+    preparer.run_once(logger=print, session=db_session)
 
     db_session.expunge_all()
     updated_mock_request = (
diff --git a/lib/rucio/tests/test_replica.py b/lib/rucio/tests/test_replica.py
--- a/lib/rucio/tests/test_replica.py
+++ b/lib/rucio/tests/test_replica.py
@@ -882,7 +882,7 @@ def __add_file_did_without_replica(*, session=None):
     # assert list(replica_client.list_replicas(dids=[container]))
 
 
-def test_client_list_blocklisted_replicas(rse_factory, did_factory, replica_client, did_client):
+def test_client_list_blocklisted_replicas(rse_factory, did_factory, replica_client, did_client, rse_client):
     """ REPLICA (CLIENT): Blocklisted replicas are filtered in list replicas"""
 
     rse, _ = rse_factory.make_posix_rse()
@@ -897,14 +897,14 @@ def test_client_list_blocklisted_replicas(rse_factory, did_factory, replica_clie
     did_client.add_datasets_to_container(dsns=[dataset], **container)
 
     # availability_write will not have any impact on listing replicas
-    did_factory.client.update_rse(rse, {'availability_write': False})
+    rse_client.update_rse(rse, {'availability_write': False})
     for did in (file, dataset, container):
         replicas = list(replica_client.list_replicas(dids=[did]))
         assert len(replicas) == 1
         assert len(replicas[0]['rses']) == 1
 
     # if availability_read is set to false, the replicas from the given rse will not be listed
-    did_factory.client.update_rse(rse, {'availability_read': False})
+    rse_client.update_rse(rse, {'availability_read': False})
     replicas = list(replica_client.list_replicas(dids=[file], ignore_availability=False))
     assert len(replicas) == 1
     assert not replicas[0]['rses'] and not replicas[0]['pfns']
diff --git a/lib/rucio/tests/test_replica_recoverer.py b/lib/rucio/tests/test_replica_recoverer.py
--- a/lib/rucio/tests/test_replica_recoverer.py
+++ b/lib/rucio/tests/test_replica_recoverer.py
@@ -128,10 +128,10 @@ def setup_obj(self, vo, rse_factory, replica_client, mock_scope, file_factory, s
         rse_expression_parser.REGION.invalidate()
 
         # Gather replica info
-        replicalist_mock = list_replicas(dids=self.listdids_mock)
-        replicalist_declarebad = list_replicas(dids=self.listdids_declarebad)
-        replicalist_nopolicy = list_replicas(dids=self.listdids_nopolicy)
-        replicalist_ignore = list_replicas(dids=self.listdids_ignore)
+        replicalist_mock = list(list_replicas(dids=self.listdids_mock))
+        replicalist_declarebad = list(list_replicas(dids=self.listdids_declarebad))
+        replicalist_nopolicy = list(list_replicas(dids=self.listdids_nopolicy))
+        replicalist_ignore = list(list_replicas(dids=self.listdids_ignore))
 
         # Changing the replica statuses as follows:
         # ----------------------------------------------------------------------------------------------------------------------------------------------------
@@ -213,10 +213,10 @@ def setup_obj(self, vo, rse_factory, replica_client, mock_scope, file_factory, s
                 update_replica_state(self.rse4recovery_id, self.scope_ignore, self.tmp_file10.name, ReplicaState.UNAVAILABLE)
 
         # Gather replica info after setting initial replica statuses
-        replicalist_mock = list_replicas(dids=self.listdids_mock)
-        replicalist_declarebad = list_replicas(dids=self.listdids_declarebad)
-        replicalist_nopolicy = list_replicas(dids=self.listdids_nopolicy)
-        replicalist_ignore = list_replicas(dids=self.listdids_ignore)
+        replicalist_mock = list(list_replicas(dids=self.listdids_mock))
+        replicalist_declarebad = list(list_replicas(dids=self.listdids_declarebad))
+        replicalist_nopolicy = list(list_replicas(dids=self.listdids_nopolicy))
+        replicalist_ignore = list(list_replicas(dids=self.listdids_ignore))
 
         # Checking if the status changes were effective
         for replica in replicalist_mock:
@@ -392,10 +392,10 @@ def test_replica_recoverer(self, vo):
         # ----------------------------------------------------------------------------------------------------------------------------------------------------
 
         # Gather replica info after replica_recoverer has run.
-        replicalist_mock = list_replicas(dids=self.listdids_mock)
-        replicalist_declarebad = list_replicas(dids=self.listdids_declarebad)
-        replicalist_nopolicy = list_replicas(dids=self.listdids_nopolicy)
-        replicalist_ignore = list_replicas(dids=self.listdids_ignore)
+        replicalist_mock = list(list_replicas(dids=self.listdids_mock))
+        replicalist_declarebad = list(list_replicas(dids=self.listdids_declarebad))
+        replicalist_nopolicy = list(list_replicas(dids=self.listdids_nopolicy))
+        replicalist_ignore = list(list_replicas(dids=self.listdids_ignore))
 
         for replica in replicalist_mock:
             if replica['name'] == self.tmp_file1.name or replica['name'] == self.tmp_file2.name:
diff --git a/lib/rucio/tests/test_request.py b/lib/rucio/tests/test_request.py
--- a/lib/rucio/tests/test_request.py
+++ b/lib/rucio/tests/test_request.py
@@ -43,13 +43,13 @@
 def test_queue_requests_state(vo, file_config_mock, rse_factory, mock_scope, root_account, db_session):
     """ REQUEST (CORE): test queuing requests """
 
-    source_rse, source_rse_id = rse_factory.make_mock_rse()
-    source_rse2, source_rse_id2 = rse_factory.make_mock_rse()
-    dest_rse, dest_rse_id = rse_factory.make_mock_rse()
-    dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse()
+    source_rse, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+    source_rse2, source_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+    dest_rse, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+    dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
 
     user_activity = 'User Subscription'
-    use_preparer = config_get_bool('conveyor', 'use_preparer')
+    use_preparer = config_get_bool('conveyor', 'use_preparer', session=db_session)
     target_state = RequestState.PREPARING if use_preparer else RequestState.QUEUED
 
     name = generate_uuid()
@@ -126,10 +126,10 @@ class TestRequestCoreList:
 
     def test_list_requests(self, rse_factory, db_session):
         """ REQUEST (CORE): list requests """
-        _, source_rse_id = rse_factory.make_mock_rse()
-        _, source_rse_id2 = rse_factory.make_mock_rse()
-        _, dest_rse_id = rse_factory.make_mock_rse()
-        _, dest_rse_id2 = rse_factory.make_mock_rse()
+        _, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+        _, source_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
         models.Request(state=constants.RequestState.WAITING, source_rse_id=source_rse_id, dest_rse_id=dest_rse_id).save(session=db_session)
         models.Request(state=constants.RequestState.SUBMITTED, source_rse_id=source_rse_id2, dest_rse_id=dest_rse_id).save(session=db_session)
         models.Request(state=constants.RequestState.SUBMITTED, source_rse_id=source_rse_id, dest_rse_id=dest_rse_id2).save(session=db_session)
@@ -149,10 +149,10 @@ def test_list_requests(self, rse_factory, db_session):
 class TestRequestHistoryCoreList:
 
     def test_list_requests_history(self, rse_factory, db_session):
-        _, source_rse_id = rse_factory.make_mock_rse()
-        _, source_rse_id2 = rse_factory.make_mock_rse()
-        _, dest_rse_id = rse_factory.make_mock_rse()
-        _, dest_rse_id2 = rse_factory.make_mock_rse()
+        _, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+        _, source_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
         """ REQUEST (CORE): list requests history"""
         models.RequestHistory(state=constants.RequestState.WAITING, source_rse_id=source_rse_id, dest_rse_id=dest_rse_id).save(session=db_session)
         models.RequestHistory(state=constants.RequestState.SUBMITTED, source_rse_id=source_rse_id2, dest_rse_id=dest_rse_id).save(session=db_session)
@@ -172,20 +172,20 @@ def test_list_requests_history(self, rse_factory, db_session):
 
 def test_list_requests(vo, rest_client, auth_token, rse_factory, tag_factory, db_session):
     """ REQUEST (REST): list requests """
-    source_rse, source_rse_id = rse_factory.make_mock_rse()
-    source_rse2, source_rse_id2 = rse_factory.make_mock_rse()
-    source_rse3, source_rse_id3 = rse_factory.make_mock_rse()
-    dest_rse, dest_rse_id = rse_factory.make_mock_rse()
-    dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse()
+    source_rse, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+    source_rse2, source_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+    source_rse3, source_rse_id3 = rse_factory.make_mock_rse(session=db_session)
+    dest_rse, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+    dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
     source_site = tag_factory.new_tag()
     source_site2 = tag_factory.new_tag()
     dst_site = tag_factory.new_tag()
     dst_site2 = tag_factory.new_tag()
-    add_rse_attribute(source_rse_id, 'site', source_site)
-    add_rse_attribute(source_rse_id2, 'site', source_site2)
-    add_rse_attribute(source_rse_id3, 'site', source_site)
-    add_rse_attribute(dest_rse_id, 'site', dst_site)
-    add_rse_attribute(dest_rse_id2, 'site', dst_site2)
+    add_rse_attribute(source_rse_id, 'site', source_site, session=db_session)
+    add_rse_attribute(source_rse_id2, 'site', source_site2, session=db_session)
+    add_rse_attribute(source_rse_id3, 'site', source_site, session=db_session)
+    add_rse_attribute(dest_rse_id, 'site', dst_site, session=db_session)
+    add_rse_attribute(dest_rse_id2, 'site', dst_site2, session=db_session)
 
     name1 = generate_uuid()
     name2 = generate_uuid()
@@ -294,20 +294,20 @@ def check_error_api(params, exception_class, exception_message, code):
 
 def test_list_requests_history(vo, rest_client, auth_token, rse_factory, tag_factory, db_session):
     """ REQUEST (REST): list requests history """
-    source_rse, source_rse_id = rse_factory.make_mock_rse()
-    source_rse2, source_rse_id2 = rse_factory.make_mock_rse()
-    source_rse3, source_rse_id3 = rse_factory.make_mock_rse()
-    dest_rse, dest_rse_id = rse_factory.make_mock_rse()
-    dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse()
+    source_rse, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+    source_rse2, source_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+    source_rse3, source_rse_id3 = rse_factory.make_mock_rse(session=db_session)
+    dest_rse, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+    dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
     source_site = tag_factory.new_tag()
     source_site2 = tag_factory.new_tag()
     dst_site = tag_factory.new_tag()
     dst_site2 = tag_factory.new_tag()
-    add_rse_attribute(source_rse_id, 'site', source_site)
-    add_rse_attribute(source_rse_id2, 'site', source_site2)
-    add_rse_attribute(source_rse_id3, 'site', source_site)
-    add_rse_attribute(dest_rse_id, 'site', dst_site)
-    add_rse_attribute(dest_rse_id2, 'site', dst_site2)
+    add_rse_attribute(source_rse_id, 'site', source_site, session=db_session)
+    add_rse_attribute(source_rse_id2, 'site', source_site2, session=db_session)
+    add_rse_attribute(source_rse_id3, 'site', source_site, session=db_session)
+    add_rse_attribute(dest_rse_id, 'site', dst_site, session=db_session)
+    add_rse_attribute(dest_rse_id2, 'site', dst_site2, session=db_session)
 
     name1 = generate_uuid()
     name2 = generate_uuid()
diff --git a/lib/rucio/tests/test_throttler.py b/lib/rucio/tests/test_throttler.py
--- a/lib/rucio/tests/test_throttler.py
+++ b/lib/rucio/tests/test_throttler.py
@@ -54,9 +54,9 @@ def wrapped_fnc(*args, **kwargs):
 
 @pytest.fixture
 def connected_rse_pair(vo, rse_factory, db_session):
-    source_rse, source_rse_id = rse_factory.make_mock_rse()
-    dest_rse, dest_rse_id = rse_factory.make_mock_rse()
-    add_distance(source_rse_id, dest_rse_id, distance=10)
+    source_rse, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+    dest_rse, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+    add_distance(source_rse_id, dest_rse_id, distance=10, session=db_session)
 
     return source_rse, source_rse_id, dest_rse, dest_rse_id
 
@@ -362,16 +362,16 @@ def test_source_per_act_fifo_release_subset(self, rse_factory, mock_scope, vo, r
         if db_session.bind.dialect.name == 'mysql':
             return True
 
-        source_rse, source_rse_id = rse_factory.make_mock_rse()
-        _, dest_rse_id = rse_factory.make_mock_rse()
-        _, dest_rse_id2 = rse_factory.make_mock_rse()
-        _, dest_rse_id3 = rse_factory.make_mock_rse()
+        source_rse, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id3 = rse_factory.make_mock_rse(session=db_session)
         for rse_id in (dest_rse_id, dest_rse_id2, dest_rse_id3):
-            add_distance(source_rse_id, rse_id, distance=10)
+            add_distance(source_rse_id, rse_id, distance=10, session=db_session)
         # two waiting requests and no active requests but threshold is 1 for one activity
         # one waiting request and no active requests but threshold is 0 for other activity -> release only 1 request for one activity
-        transfer_limit_factory(source_rse, activity=self.user_activity, max_transfers=1, strategy='fifo', direction=TransferLimitDirection.SOURCE)
-        transfer_limit_factory(source_rse, activity=self.user_activity2, max_transfers=0, strategy='fifo', direction=TransferLimitDirection.SOURCE)
+        transfer_limit_factory(source_rse, activity=self.user_activity, max_transfers=1, strategy='fifo', direction=TransferLimitDirection.SOURCE, session=db_session)
+        transfer_limit_factory(source_rse, activity=self.user_activity2, max_transfers=0, strategy='fifo', direction=TransferLimitDirection.SOURCE, session=db_session)
         name1, name2, name3 = _add_test_replicas_and_request(
             scope=mock_scope, account=root_account, session=db_session,
             request_configs=[
@@ -437,11 +437,11 @@ def test_dest_all_act_fifo_release_subset(self, rse_factory, mock_scope, db_sess
 
         source_rse, source_rse_id, dest_rse, dest_rse_id = connected_rse_pair
 
-        _, source_rse_id2 = rse_factory.make_mock_rse()
-        dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse()
-        add_distance(source_rse_id, dest_rse_id2, distance=10)
-        add_distance(source_rse_id2, dest_rse_id, distance=10)
-        add_distance(source_rse_id2, dest_rse_id2, distance=10)
+        _, source_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+        dest_rse2, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+        add_distance(source_rse_id, dest_rse_id2, distance=10, session=db_session)
+        add_distance(source_rse_id2, dest_rse_id, distance=10, session=db_session)
+        add_distance(source_rse_id2, dest_rse_id2, distance=10, session=db_session)
 
         # two waiting requests and no active requests but threshold 1 for one dest RSE
         # one waiting request and no active requests but threshold 0 for another dest RSE -> release only 1 request on one dest RSE
@@ -489,8 +489,8 @@ def test_source_all_act_grouped_fifo_subset(self, rse_factory, mock_scope, db_se
 
         source_rse, source_rse_id, dest_rse, dest_rse_id = connected_rse_pair
 
-        _, dest_rse_id2 = rse_factory.make_mock_rse()
-        add_distance(source_rse_id, dest_rse_id2, distance=10)
+        _, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+        add_distance(source_rse_id, dest_rse_id2, distance=10, session=db_session)
 
         transfer_limit_factory(source_rse, self.all_activities, volume=10, max_transfers=1, deadline=0, direction=TransferLimitDirection.SOURCE, strategy='grouped_fifo', session=db_session)
         name1, name2, name3, name4 = _add_test_replicas_and_request(
@@ -568,18 +568,18 @@ def test_source_dest_mixed_act(self, rse_factory, mock_scope, vo, root_account,
         if db_session.bind.dialect.name == 'mysql':
             return True
 
-        source_rse, source_rse_id = rse_factory.make_mock_rse()
-        dest_rse, dest_rse_id = rse_factory.make_mock_rse()
-        _, dest_rse_id2 = rse_factory.make_mock_rse()
-        _, dest_rse_id3 = rse_factory.make_mock_rse()
+        source_rse, source_rse_id = rse_factory.make_mock_rse(session=db_session)
+        dest_rse, dest_rse_id = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id2 = rse_factory.make_mock_rse(session=db_session)
+        _, dest_rse_id3 = rse_factory.make_mock_rse(session=db_session)
         for rse_id in (dest_rse_id, dest_rse_id2, dest_rse_id3):
-            add_distance(source_rse_id, rse_id, distance=10)
+            add_distance(source_rse_id, rse_id, distance=10, session=db_session)
         # two waiting requests and no active requests but threshold is 1 for one activity
         # one waiting request and no active requests but threshold is 0 for other activity -> release only 1 request for one activity
-        transfer_limit_factory(source_rse, activity=self.user_activity, max_transfers=1, strategy='fifo', direction=TransferLimitDirection.SOURCE)
-        transfer_limit_factory(source_rse, activity=self.all_activities, max_transfers=3, strategy='fifo', direction=TransferLimitDirection.SOURCE)
-        transfer_limit_factory(dest_rse, activity=self.user_activity2, max_transfers=1, strategy='fifo', direction=TransferLimitDirection.DESTINATION)
-        transfer_limit_factory(dest_rse, activity=self.all_activities, max_transfers=4, strategy='fifo', direction=TransferLimitDirection.DESTINATION)
+        transfer_limit_factory(source_rse, activity=self.user_activity, max_transfers=1, strategy='fifo', direction=TransferLimitDirection.SOURCE, session=db_session)
+        transfer_limit_factory(source_rse, activity=self.all_activities, max_transfers=3, strategy='fifo', direction=TransferLimitDirection.SOURCE, session=db_session)
+        transfer_limit_factory(dest_rse, activity=self.user_activity2, max_transfers=1, strategy='fifo', direction=TransferLimitDirection.DESTINATION, session=db_session)
+        transfer_limit_factory(dest_rse, activity=self.all_activities, max_transfers=4, strategy='fifo', direction=TransferLimitDirection.DESTINATION, session=db_session)
         name1, name2, name3, name4, name5 = _add_test_replicas_and_request(
             scope=mock_scope, account=root_account, session=db_session,
             request_configs=[