From f392459bd417bec8a3ce184ee8f753649bcb77b8 Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Tue, 5 Sep 2023 22:09:33 +0200 Subject: [PATCH] Tests: Make `PsqlDosStorage` profile unload test more robust (#6115) The `test_unload_profile` test verifies that if a loaded profile is unloaded, it properly relinquishes of the session that is maintained by sqlalchemy. It did so by checking that after unloading, there were no sessions being referenced. However, this would fail sometimes, because another session may still be held on to, even though that session had nothing to do with the test. A more robust test is simply to check that after unloading, there is exactly one less session being held on to. Cherry-pick: 1c72eac1f91e02bc464c66328ea74911762b94fd --- tests/storage/psql_dos/test_backend.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/storage/psql_dos/test_backend.py b/tests/storage/psql_dos/test_backend.py index 7c98f801da..1adb1bd5c8 100644 --- a/tests/storage/psql_dos/test_backend.py +++ b/tests/storage/psql_dos/test_backend.py @@ -147,16 +147,23 @@ def test_unload_profile(): This is a regression test for #5506. """ + import gc + from sqlalchemy.orm.session import _sessions # pylint: disable=import-outside-toplevel + # Run the garbage collector to ensure any lingering unrelated sessions do not cause the test to fail. + gc.collect() + # Just running the test suite itself should have opened at least one session - assert len(_sessions) != 0, str(_sessions) + current_sessions = len(_sessions) + assert current_sessions != 0, str(_sessions) manager = get_manager() profile_name = manager.get_profile().name try: manager.unload_profile() - assert len(_sessions) == 0, str(_sessions) + # After unloading, the session should have been cleared, so we should have one less + assert len(_sessions) == current_sessions - 1, str(_sessions) finally: manager.load_profile(profile_name)