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
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
"""Tests for TTL-cached connection health checks on adapters."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
import redis
|
||||
|
||||
from python_repositories.adapters.minio_adapter import MinioAdapter
|
||||
from python_repositories.adapters.redis_adapter import RedisAdapter
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def redis_adapter(monkeypatch: pytest.MonkeyPatch) -> RedisAdapter:
|
||||
monkeypatch.setenv("REDIS_URI", "redis://localhost:6379")
|
||||
return RedisAdapter()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def minio_adapter(monkeypatch: pytest.MonkeyPatch) -> MinioAdapter:
|
||||
monkeypatch.setenv("MINIO_ENDPOINT", "localhost:9000")
|
||||
monkeypatch.setenv("MINIO_ACCESS_KEY", "minioadmin")
|
||||
monkeypatch.setenv("MINIO_SECRET_KEY", "minioadmin")
|
||||
monkeypatch.setenv("MINIO_BUCKET", "test-bucket")
|
||||
return MinioAdapter()
|
||||
|
||||
|
||||
class TestRedisConnectionHealth:
|
||||
def test_not_connected_when_no_client(self, redis_adapter: RedisAdapter) -> None:
|
||||
assert not redis_adapter.is_connected()
|
||||
|
||||
def test_connected_when_probe_succeeds(self, redis_adapter: RedisAdapter) -> None:
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
mock_client.ping.return_value = True
|
||||
redis_adapter._client = mock_client
|
||||
|
||||
assert redis_adapter.is_connected()
|
||||
mock_client.ping.assert_called_once()
|
||||
|
||||
def test_stale_connection_when_probe_fails(
|
||||
self, redis_adapter: RedisAdapter
|
||||
) -> None:
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
mock_client.ping.side_effect = redis.ConnectionError("connection lost")
|
||||
redis_adapter._client = mock_client
|
||||
|
||||
assert not redis_adapter.is_connected()
|
||||
|
||||
def test_cache_hit_avoids_second_probe(self, redis_adapter: RedisAdapter) -> None:
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
mock_client.ping.return_value = True
|
||||
redis_adapter._client = mock_client
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.redis_adapter.time.monotonic",
|
||||
return_value=100.0,
|
||||
):
|
||||
assert redis_adapter.is_connected()
|
||||
assert redis_adapter.is_connected()
|
||||
|
||||
mock_client.ping.assert_called_once()
|
||||
|
||||
def test_cache_miss_runs_probe_again(self, redis_adapter: RedisAdapter) -> None:
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
mock_client.ping.return_value = True
|
||||
redis_adapter._client = mock_client
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.redis_adapter.time.monotonic",
|
||||
side_effect=[100.0, 102.0],
|
||||
):
|
||||
assert redis_adapter.is_connected()
|
||||
assert redis_adapter.is_connected()
|
||||
|
||||
assert mock_client.ping.call_count == 2
|
||||
|
||||
def test_disconnect_clears_cache(self, redis_adapter: RedisAdapter) -> None:
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
mock_client.ping.return_value = True
|
||||
redis_adapter._client = mock_client
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.redis_adapter.time.monotonic",
|
||||
return_value=100.0,
|
||||
):
|
||||
assert redis_adapter.is_connected()
|
||||
|
||||
redis_adapter.disconnect()
|
||||
redis_adapter._client = mock_client
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.redis_adapter.time.monotonic",
|
||||
return_value=100.0,
|
||||
):
|
||||
assert redis_adapter.is_connected()
|
||||
|
||||
assert mock_client.ping.call_count == 2
|
||||
|
||||
|
||||
class TestMinioConnectionHealth:
|
||||
def test_not_connected_when_no_client(self, minio_adapter: MinioAdapter) -> None:
|
||||
assert not minio_adapter.is_connected()
|
||||
|
||||
def test_not_connected_when_bucket_name_missing(
|
||||
self, minio_adapter: MinioAdapter
|
||||
) -> None:
|
||||
minio_adapter._client = MagicMock()
|
||||
minio_adapter._bucket_name = None
|
||||
|
||||
assert not minio_adapter.is_connected()
|
||||
|
||||
def test_connected_when_probe_succeeds(self, minio_adapter: MinioAdapter) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client.bucket_exists.return_value = True
|
||||
minio_adapter._client = mock_client
|
||||
minio_adapter._bucket_name = "test-bucket"
|
||||
|
||||
assert minio_adapter.is_connected()
|
||||
mock_client.bucket_exists.assert_called_once_with("test-bucket")
|
||||
|
||||
def test_stale_connection_when_probe_fails(
|
||||
self, minio_adapter: MinioAdapter
|
||||
) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client.bucket_exists.side_effect = Exception("connection lost")
|
||||
minio_adapter._client = mock_client
|
||||
minio_adapter._bucket_name = "test-bucket"
|
||||
|
||||
assert not minio_adapter.is_connected()
|
||||
|
||||
def test_cache_hit_avoids_second_probe(self, minio_adapter: MinioAdapter) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client.bucket_exists.return_value = True
|
||||
minio_adapter._client = mock_client
|
||||
minio_adapter._bucket_name = "test-bucket"
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.minio_adapter.time.monotonic",
|
||||
return_value=100.0,
|
||||
):
|
||||
assert minio_adapter.is_connected()
|
||||
assert minio_adapter.is_connected()
|
||||
|
||||
mock_client.bucket_exists.assert_called_once()
|
||||
|
||||
def test_cache_miss_runs_probe_again(self, minio_adapter: MinioAdapter) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client.bucket_exists.return_value = True
|
||||
minio_adapter._client = mock_client
|
||||
minio_adapter._bucket_name = "test-bucket"
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.minio_adapter.time.monotonic",
|
||||
side_effect=[100.0, 102.0],
|
||||
):
|
||||
assert minio_adapter.is_connected()
|
||||
assert minio_adapter.is_connected()
|
||||
|
||||
assert mock_client.bucket_exists.call_count == 2
|
||||
|
||||
def test_disconnect_clears_cache(self, minio_adapter: MinioAdapter) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client.bucket_exists.return_value = True
|
||||
minio_adapter._client = mock_client
|
||||
minio_adapter._bucket_name = "test-bucket"
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.minio_adapter.time.monotonic",
|
||||
return_value=100.0,
|
||||
):
|
||||
assert minio_adapter.is_connected()
|
||||
|
||||
minio_adapter.disconnect()
|
||||
minio_adapter._client = mock_client
|
||||
minio_adapter._bucket_name = "test-bucket"
|
||||
|
||||
with patch(
|
||||
"python_repositories.adapters.minio_adapter.time.monotonic",
|
||||
return_value=100.0,
|
||||
):
|
||||
assert minio_adapter.is_connected()
|
||||
|
||||
assert mock_client.bucket_exists.call_count == 2
|
||||
Reference in New Issue
Block a user