"""Hanzo KV client: configured by KV_URL, and an outage never fails a request.""" import pytest from bootnode.config import Settings from bootnode.core.cache import kv as kv_module from bootnode.core.cache.kv import KVClient # Nothing listens on port 1, so every command fails with a connection error. DOWN = "redis://127.0.0.1:1/0" def test_kv_url_is_read_from_env(monkeypatch): monkeypatch.setenv("KV_URL", "redis://kv.hanzo.svc.cluster.local:6379/0") monkeypatch.setenv("REDIS_URL", "redis://ignored:6379/0") assert Settings().kv_url == "redis://kv.hanzo.svc.cluster.local:6379/0" @pytest.fixture async def down(monkeypatch): monkeypatch.setattr(kv_module.settings, "kv_url", DOWN) client = KVClient() await client.initialize() yield client await client.close() async def test_rate_limit_allows_when_kv_down(down): assert await down.rate_limit_check("rate:test", 100) == (True, 100) async def test_rpc_cache_misses_when_kv_down(down): assert await down.get_rpc_response("ethereum", "mainnet", "eth_chainId", "h") is None await down.set_rpc_response("ethereum", "mainnet", "eth_chainId", "h", "0x1") class _Pipe: def __init__(self, count: int): self._count = count def incr(self, key): pass def expire(self, key, seconds): pass async def execute(self): return [self._count, True] class _KV: def __init__(self, count: int): self._count = count def pipeline(self): return _Pipe(self._count) async def test_rate_limit_enforced_when_kv_up(): client = KVClient() client._client = _KV(101) assert await client.rate_limit_check("rate:test", 100) == (False, 0) client._client = _KV(40) assert await client.rate_limit_check("rate:test", 100) == (True, 60)