Strengthen is_connected with cached health probes.
Convert is_connected to a method that verifies backend liveness via TTL-cached ping (Redis) or bucket_exists (MinIO), with cache invalidation on connect/disconnect. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
co-authored by
Cursor
parent
fef9552cbf
commit
5149299c1b
@@ -15,7 +15,6 @@ def test_instantiation_fails_when_connect_not_implemented() -> None:
|
||||
def disconnect(self) -> None:
|
||||
pass
|
||||
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
return False
|
||||
|
||||
@@ -32,7 +31,6 @@ def test_instantiation_fails_when_disconnect_not_implemented() -> None:
|
||||
def connect(self) -> None:
|
||||
pass
|
||||
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ def test_should_have_logger_when_instantiated() -> None:
|
||||
def test_should_not_be_connected_when_instantiated() -> None:
|
||||
"""Test that the MinioAdapter is not connected when instantiated."""
|
||||
adapter = MinioAdapter()
|
||||
assert not adapter.is_connected
|
||||
assert not adapter.is_connected()
|
||||
|
||||
|
||||
def test_should_log_info_when_already_connected(
|
||||
@@ -137,7 +137,7 @@ def test_should_raise_connection_error_when_unable_to_connect(
|
||||
adapter = MinioAdapter()
|
||||
with pytest.raises(ConnectionError):
|
||||
adapter.connect()
|
||||
assert not adapter.is_connected
|
||||
assert not adapter.is_connected()
|
||||
|
||||
|
||||
def test_should_log_info_when_creating_expected_bucket(
|
||||
@@ -163,7 +163,7 @@ def test_should_log_error_on_exception_during_exit(
|
||||
"""Test that the MinioAdapter logs an error if an exception occurs during __exit__."""
|
||||
try:
|
||||
with MinioAdapter() as adapter:
|
||||
assert adapter.is_connected
|
||||
assert adapter.is_connected()
|
||||
raise ValueError("Simulated error")
|
||||
except ValueError:
|
||||
pass # Expected
|
||||
|
||||
@@ -9,7 +9,7 @@ from python_repositories.interfaces import JsonRepositoryInterface
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def data() -> Generator[dict[str, str]]:
|
||||
def data() -> Generator[dict[str, str], None, None]:
|
||||
"""Provide a sample data dictionary for tests."""
|
||||
yield {"foo": "bar"}
|
||||
|
||||
@@ -18,7 +18,7 @@ def data() -> Generator[dict[str, str]]:
|
||||
def data_in_redis(
|
||||
raw_redis_client: redis.Redis,
|
||||
data: dict[str, str],
|
||||
) -> Generator[tuple[str, dict[str, str]]]:
|
||||
) -> Generator[tuple[str, dict[str, str]], None, None]:
|
||||
"""Fixture to set up a known value in Redis before each test."""
|
||||
key = "test_key"
|
||||
path = RedisPath.root_path()
|
||||
@@ -31,7 +31,7 @@ def data_in_redis(
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def redis_adapter(redis_container: str) -> Generator[RedisAdapter]:
|
||||
def redis_adapter(redis_container: str) -> Generator[RedisAdapter, None, None]:
|
||||
"""Fixture to provide a connected RedisAdapter instance."""
|
||||
adapter = RedisAdapter()
|
||||
adapter.connect()
|
||||
@@ -63,7 +63,7 @@ def test_should_not_be_connected_when_instantiated(redis_container: str) -> None
|
||||
"""Test that the RedisAdapter is not connected when instantiated."""
|
||||
adapter = RedisAdapter()
|
||||
assert adapter._client is None
|
||||
assert not adapter.is_connected
|
||||
assert not adapter.is_connected()
|
||||
|
||||
|
||||
def test_should_raise_connection_error_when_unable_to_connect(
|
||||
@@ -77,7 +77,7 @@ def test_should_raise_connection_error_when_unable_to_connect(
|
||||
with pytest.raises(ConnectionError):
|
||||
adapter.connect()
|
||||
assert adapter._client is None
|
||||
assert not adapter.is_connected
|
||||
assert not adapter.is_connected()
|
||||
|
||||
|
||||
def test_connect_raises_connection_error_when_unable_to_ping(
|
||||
@@ -109,7 +109,7 @@ def test_should_log_error_on_exception_during_exit(
|
||||
"""Test that the RedisAdapter logs an error when an exception occurs during context exit."""
|
||||
try:
|
||||
with RedisAdapter() as adapter:
|
||||
assert adapter.is_connected
|
||||
assert adapter.is_connected()
|
||||
raise ValueError("Simulated error")
|
||||
except ValueError:
|
||||
pass # Expected
|
||||
|
||||
Reference in New Issue
Block a user