Skip to content

Commit

Permalink
Tests: Make PsqlDosStorage profile unload test more robust (#6115)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sphuber committed Sep 5, 2023
1 parent f11598d commit 1c72eac
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tests/storage/psql_dos/test_backend.py
Expand Up @@ -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)

0 comments on commit 1c72eac

Please sign in to comment.