Skip to content

Commit

Permalink
try_enkf_reset
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Oct 11, 2024
1 parent 6d9750d commit 84948e6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/ert/dark_storage/endpoints/updates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from fastapi import APIRouter, Depends

from ert.dark_storage.enkf import get_storage

DEFAULT_STORAGE = Depends(get_storage)
from fastapi import APIRouter

router = APIRouter(tags=["ensemble"])

Expand Down
15 changes: 5 additions & 10 deletions src/ert/dark_storage/enkf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@


def get_storage() -> Storage:
global _storage # noqa
global _storage
if _storage is None:
_storage = update_storage()
try:
return (_storage := open_storage(os.environ["ERT_STORAGE_ENS_PATH"]))
except RuntimeError as err:
raise InternalServerError(f"{err!s}") from err
_storage.refresh()
return _storage


def update_storage() -> Storage:
global _storage
try:
return (_storage := open_storage(os.environ["ERT_STORAGE_ENS_PATH"]))
except RuntimeError as err:
raise InternalServerError(f"{err!s}") from err
4 changes: 0 additions & 4 deletions tests/ert/unit_tests/dark_storage/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from ert.cli.main import run_cli
from ert.dark_storage import enkf
from ert.dark_storage.app import app
from ert.dark_storage.enkf import update_storage
from ert.mode_definitions import ENSEMBLE_SMOOTHER_MODE


Expand Down Expand Up @@ -53,7 +52,6 @@ def poly_example_tmp_dir(poly_example_tmp_dir_shared):
def dark_storage_client(monkeypatch):
with dark_storage_app_(monkeypatch) as dark_app:
monkeypatch.setenv("ERT_STORAGE_ENS_PATH", "storage")
update_storage()
with TestClient(dark_app) as client:
yield client

Expand All @@ -62,7 +60,6 @@ def dark_storage_client(monkeypatch):
def dark_storage_client_snake_oil(monkeypatch):
with dark_storage_app_(monkeypatch) as dark_app:
monkeypatch.setenv("ERT_STORAGE_ENS_PATH", "storage/snake_oil/ensemble")
update_storage()
with TestClient(dark_app) as client:
yield client

Expand All @@ -83,7 +80,6 @@ def reset_enkf():
def dark_storage_app_(monkeypatch):
monkeypatch.setenv("ERT_STORAGE_NO_TOKEN", "yup")
monkeypatch.setenv("ERT_STORAGE_ENS_PATH", "storage")
update_storage()

yield app
reset_enkf()
Expand Down
8 changes: 6 additions & 2 deletions tests/ert/unit_tests/dark_storage/test_dark_storage_state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
import io
import os
from urllib.parse import quote
Expand All @@ -10,8 +11,8 @@
from hypothesis.stateful import rule
from starlette.testclient import TestClient

from ert.dark_storage import enkf
from ert.dark_storage.app import app
from ert.dark_storage.enkf import update_storage
from tests.ert.unit_tests.storage.test_local_storage import StatefulStorageTest


Expand All @@ -26,7 +27,6 @@ def __init__(self):
self.prev_ens_path = os.environ.get("ERT_STORAGE_ENS_PATH")
os.environ["ERT_STORAGE_NO_TOKEN"] = "yup"
os.environ["ERT_STORAGE_ENS_PATH"] = str(self.storage.path)
update_storage()
self.client = TestClient(app)

@rule()
Expand Down Expand Up @@ -96,6 +96,10 @@ def get_response_csv_through_client(self, model_ensemble, data):

def teardown(self):
super().teardown()
if enkf._storage is not None:
enkf._storage.close()
enkf._storage = None
gc.collect()
if self.prev_no_token is not None:
os.environ["ERT_STORAGE_NO_TOKEN"] = self.prev_no_token
else:
Expand Down

0 comments on commit 84948e6

Please sign in to comment.