Skip to content

Tests: Only reset database connection at end of suite #5641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions aiida/manage/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_manager() -> 'Manager':
return MANAGER


class Manager:
class Manager: # pylint: disable=too-many-public-methods
"""Manager singleton for globally loaded resources.

AiiDA can have the following global resources loaded:
Expand Down Expand Up @@ -143,17 +143,33 @@ def load_profile(self, profile: Union[None, str, 'Profile'] = None, allow_switch

def reset_profile(self) -> None:
"""Close and reset any associated resources for the current profile."""
self.reset_profile_storage()
self.reset_communicator()
self.reset_runner()

self._daemon_client = None
self._persister = None

def reset_profile_storage(self) -> None:
"""Reset the profile storage.

This will close any connections to the services used by the storage, such as database connections.
"""
if self._profile_storage is not None:
self._profile_storage.close()
self._profile_storage = None

def reset_communicator(self) -> None:
"""Reset the communicator."""
if self._communicator is not None:
self._communicator.close()
if self._runner is not None:
self._runner.stop()
self._profile_storage = None
self._communicator = None
self._daemon_client = None
self._process_controller = None
self._persister = None

def reset_runner(self) -> None:
"""Reset the process runner."""
if self._runner is not None:
self._runner.close()
self._runner = None

def unload_profile(self) -> None:
Expand Down
9 changes: 6 additions & 3 deletions aiida/manage/tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@ def clear_profile():
"""Reset the global profile, clearing all its data and closing any open resources."""
manager = get_manager()
manager.get_profile_storage()._clear(recreate_user=True) # pylint: disable=protected-access
manager.reset_profile()
manager.get_profile_storage() # reload the storage connection
manager.reset_communicator()
manager.reset_runner()

def has_profile_open(self):
return self._profile is not None

def destroy_all(self):
pass
def destroy_all(self): # pylint: disable=no-self-use
manager = get_manager()
manager.reset_profile()


class TemporaryProfileManager(ProfileManager):
Expand Down Expand Up @@ -396,6 +398,7 @@ def root_dir_ok(self):

def destroy_all(self):
"""Remove all traces of the tests run"""
super().destroy_all()
if self.root_dir:
shutil.rmtree(self.root_dir)
self.root_dir = None
Expand Down
1 change: 1 addition & 0 deletions aiida/storage/psql_dos/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def _clear(self, recreate_user: bool = True) -> None:
):
session.execute(table(table_name).delete())
session.expunge_all()
self._default_user = None

# restore the default user
if recreate_user and default_user_kwargs:
Expand Down