Skip to content

Commit

Permalink
Return error message in can_connect
Browse files Browse the repository at this point in the history
  • Loading branch information
jl-wynen committed Jan 19, 2024
1 parent 3eadf09 commit c637477
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
3 changes: 0 additions & 3 deletions src/scitacean/testing/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def test_manual_client(require_scicat_backend, scicat_access):
add_pytest_option
backend_enabled
can_connect
configure
skip_if_not_backend
start_backend
Expand All @@ -73,7 +72,6 @@ def test_manual_client(require_scicat_backend, scicat_access):

from . import config, seed
from ._backend import (
can_connect,
configure,
start_backend,
stop_backend,
Expand All @@ -84,7 +82,6 @@ def test_manual_client(require_scicat_backend, scicat_access):
__all__ = [
"add_pytest_option",
"backend_enabled",
"can_connect",
"config",
"configure",
"seed",
Expand Down
20 changes: 12 additions & 8 deletions src/scitacean/testing/backend/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ def stop_backend(docker_compose_file: _PathLike) -> None:
docker_compose_down(docker_compose_file)


def can_connect() -> bool:
def _can_connect() -> tuple[bool, str]:
"""Test the connection to the testing SciCat backend.
Returns
-------
:
True if the backend is reachable, False otherwise.
The first element indicates whether the connection was successful.
The second element is an error message.
"""
scicat_access = config.local_access("user1")
try:
Expand All @@ -90,9 +91,11 @@ def can_connect() -> bool:
json=scicat_access.user.credentials,
timeout=0.5,
)
except requests.ConnectionError:
return False
return response.ok
except requests.ConnectionError as err:
return False, str(err)
if response.ok:
return True, ""
return False, str(f"{response}: {response.text}")


def wait_until_backend_is_live(max_time: float, n_tries: int) -> None:
Expand All @@ -116,8 +119,9 @@ def wait_until_backend_is_live(max_time: float, n_tries: int) -> None:
If no connection can be made within the time limit.
"""
for _ in range(n_tries):
if can_connect():
if _can_connect()[0]:
return
time.sleep(max_time / n_tries)
if not can_connect():
raise RuntimeError("Cannot connect to backend")
ok, err = _can_connect()
if not ok:
raise RuntimeError(f"Cannot connect to backend: {err}")

0 comments on commit c637477

Please sign in to comment.