From bb07ac816fd675983688cb5d083320b494305d40 Mon Sep 17 00:00:00 2001 From: zanewalker Date: Wed, 4 Feb 2026 01:33:44 +0000 Subject: [PATCH] fix: resolve flaky test and JSON config validation --- fastapi_traffic/core/config_loader.py | 9 ++++++--- tests/test_algorithms.py | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/fastapi_traffic/core/config_loader.py b/fastapi_traffic/core/config_loader.py index 59447f1..3f767b6 100644 --- a/fastapi_traffic/core/config_loader.py +++ b/fastapi_traffic/core/config_loader.py @@ -217,14 +217,14 @@ class ConfigLoader: return env_vars - def _load_json_file(self, file_path: Path) -> dict[str, Any]: + def _load_json_file(self, file_path: Path) -> Any: """Load configuration from a JSON file. Args: file_path: Path to the JSON file. Returns: - Configuration dictionary. + Parsed JSON data (could be any JSON type). Raises: ConfigurationError: If the file cannot be read or parsed. @@ -235,7 +235,7 @@ class ConfigLoader: try: with file_path.open(encoding="utf-8") as f: - data: dict[str, Any] = json.load(f) + data: Any = json.load(f) except json.JSONDecodeError as e: msg = f"Invalid JSON in {file_path}: {e}" raise ConfigurationError(msg) from e @@ -350,6 +350,9 @@ class ConfigLoader: """ path = Path(file_path) raw_config = self._load_json_file(path) + if not isinstance(raw_config, dict): + msg = "JSON root must be an object" + raise ConfigurationError(msg) config_dict = self._validate_and_convert(raw_config, _RATE_LIMIT_FIELD_TYPES) # Apply overrides diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index c7e9f09..ce5a425 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -426,7 +426,8 @@ class TestSlidingWindowCounterAdvanced: allowed, _ = await algo.check("precision_key") assert not allowed - await asyncio.sleep(0.5) + # Wait for the full window to pass to ensure tokens are fully replenished + await asyncio.sleep(1.1) allowed, _ = await algo.check("precision_key") assert allowed