mirror of
https://github.com/hanzoai/bootnode.git
synced 2026-10-25 02:31:30 +00:00
Hanzo CI/CD / cicd (push) Successful in 8m30s
CI/CD / cicd (push) Successful in 8m30s
The cache client is now the KV client (core/cache/kv.py, kv_client), configured by KV_URL. rate_limit_check allows the request and logs when KV is unreachable, and the RPC cache treats an outage as a miss. cloud_billing reads KV directly instead of a missing usage_tracker.redis attribute; the NFT refresh enqueues through an arq pool. Co-authored-by: Hanzo Dev <[email protected]>
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
"""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)
|