Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 2, 2025
1 parent eb6e1b4 commit 9b82abd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/aiida/manage/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from __future__ import annotations

import codecs
import contextlib
import io
import json
import os
import uuid
Expand Down Expand Up @@ -528,12 +526,12 @@ def create_profile(
LOGGER.report('Initialising the storage backend.')
try:
# not sure if scope is good, at least put it in log
#with contextlib.redirect_stdout(io.StringIO()):
# with contextlib.redirect_stdout(io.StringIO()):
profile.storage_cls.initialise(profile)
except Exception as exception:
raise StorageMigrationError(
f'During initialisation of storage backend following excepion was raised: {exception}.\n Please check above for full traceback.'
)
f'During initialisation of storage backend following excepion was raised: {exception}.\n Please check above for full traceback.'
)
LOGGER.report('Storage initialisation completed.')

self.add_profile(profile)
Expand Down
5 changes: 2 additions & 3 deletions src/aiida/storage/psql_dos/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""SqlAlchemy implementation of `aiida.orm.implementation.backends.Backend`."""

import functools
import gc
import pathlib
from contextlib import contextmanager, nullcontext
from typing import TYPE_CHECKING, Iterator, List, Optional, Sequence, Set, Union
Expand Down Expand Up @@ -187,8 +186,8 @@ def close(self) -> None:
# close the connection

# PR QUESTION: can we have multiple sessons? Then we should close all of them
#from sqlalchemy.orm import close_all_sessions
#close_all_sessions()
# from sqlalchemy.orm import close_all_sessions
# close_all_sessions()

engine = self._session_factory.bind
self._session_factory.expunge_all()
Expand Down
4 changes: 1 addition & 3 deletions src/aiida/storage/psql_dos/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ def get_repository_uuid(self) -> str:
except Exception as exception:
# PR COMMENT removed the printing of exception in message since we use from exception
if container is None:
msg = (
'During creation of the container context for the disk-objectstore the following error was raised'
)
msg = 'During creation of the container context for the disk-objectstore the following error was raised'
else:
msg = f'During access of disk-objectstore {container} error was raised.'

Expand Down
7 changes: 5 additions & 2 deletions tests/manage/configuration/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def test_create_profile_raises(config_with_profile, monkeypatch, entry_points):
profile_name = uuid.uuid4().hex

def raise_storage_migration_error(*_, **__):
raise exceptions.StorageMigrationError("Monkey patchted error")
raise exceptions.StorageMigrationError('Monkey patchted error')

monkeypatch.setattr(SqliteTempBackend, 'initialise', raise_storage_migration_error)
entry_points.add(InvalidBaseStorage, 'aiida.storage:core.invalid_base')
Expand All @@ -460,7 +460,10 @@ def raise_storage_migration_error(*_, **__):
with pytest.raises(ValueError, match=r'The entry point `.*` could not be loaded'):
config.create_profile(profile_name, 'core.non_existant', {})

with pytest.raises(exceptions.StorageMigrationError, match=r'During initialisation of storage backend following excepion was raised: Monkey patchted error.*'):
with pytest.raises(
exceptions.StorageMigrationError,
match=r'During initialisation of storage backend following excepion was raised: Monkey patchted error.*',
):
config.create_profile(profile_name, 'core.sqlite_temp', {})


Expand Down
27 changes: 20 additions & 7 deletions tests/storage/sqlite/test_container.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
"""Test container initialization."""
import psutil, os

import os

import psutil


def test_file_descriptor_closed(aiida_profile):
"""Checks if the number of open file descriptors change during a reset."""

def list_open_file_descriptors():
process = psutil.Process(os.getpid())
return process.open_files()

# We have some connections active due to aiida profile during the first
# reset these are destroyed. We check the second time changes the number of
# open file descriptors.
from aiida.manage import get_manager

storage_backend = get_manager().get_profile_storage()
migrator_context = storage_backend.migrator_context
migrator_context = storage_backend.migrator_context
open_file_descriptors_before = list_open_file_descriptors()
with migrator_context(aiida_profile) as migrator:
migrator.initialise_repository()
migrator.reset_repository()
open_file_descriptors_after = list_open_file_descriptors()
assert len(open_file_descriptors_before) == len(open_file_descriptors_after), f"Before these file descriptor were open:\n{open_file_descriptors_before}\n Now these are open:\n{open_file_descriptors_after}"
assert (
len(open_file_descriptors_before) == len(open_file_descriptors_after)
), f'Before these file descriptor were open:\n{open_file_descriptors_before}\n Now these are open:\n{open_file_descriptors_after}'


# PR COMMENT this is just a sanity check for me, I don' think that the test should be included in final PR
def test_reset_storage_file_descriptor_closed(aiida_profile):
"""Checks if the number of open file descriptors change during a reset."""

def list_open_file_descriptors():
process = psutil.Process(os.getpid())
return process.open_files()

# We have some connections active due to aiida profile during the first
# reset these are destroyed. We check the second time changes the number of
# open file descriptors.
# TODO The fix should keep the existing connections alive and just reuse them
# then one does not need to do two resets.
from aiida.manage import get_manager
aiida_profile.reset_storage()

aiida_profile.reset_storage()
open_file_descriptors_before = list_open_file_descriptors()
aiida_profile.reset_storage()
aiida_profile.reset_storage()
open_file_descriptors_after = list_open_file_descriptors()
assert len(open_file_descriptors_before) == len(open_file_descriptors_after), f"Before these file descriptor were open:\n{open_file_descriptors_before}\n Now these are open:\n{open_file_descriptors_after}"
assert (
len(open_file_descriptors_before) == len(open_file_descriptors_after)
), f'Before these file descriptor were open:\n{open_file_descriptors_before}\n Now these are open:\n{open_file_descriptors_after}'

0 comments on commit 9b82abd

Please sign in to comment.