test_bitstamp_rate_limit.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from __future__ import annotations
  2. from exec_mcp import bitstamp_fx, bitstamp_metadata, bitstamp_rate_limit
  3. class _FakeResponse:
  4. def __init__(self, payload):
  5. self._payload = payload
  6. def raise_for_status(self):
  7. return None
  8. def json(self):
  9. return self._payload
  10. def test_global_rate_limiter_spaces_calls(monkeypatch):
  11. limiter = bitstamp_rate_limit.GlobalRateLimiter(default_delay_ms=250)
  12. monkeypatch.setenv("BITSTAMP_CALL_DELAY_MS", "250")
  13. now = [100.0]
  14. sleeps: list[float] = []
  15. monkeypatch.setattr(bitstamp_rate_limit.time, "monotonic", lambda: now[0])
  16. monkeypatch.setattr(bitstamp_rate_limit.time, "sleep", lambda seconds: sleeps.append(seconds))
  17. first = limiter.acquire()
  18. second = limiter.acquire()
  19. assert first == 0.0
  20. assert second == 0.25
  21. assert sleeps == [0.25]
  22. def test_bitstamp_public_helpers_call_throttle(monkeypatch):
  23. calls: list[str] = []
  24. monkeypatch.setattr(bitstamp_metadata, "throttle_bitstamp_request", lambda: calls.append("meta"))
  25. monkeypatch.setattr(bitstamp_fx, "throttle_bitstamp_request", lambda: calls.append("fx"))
  26. monkeypatch.setattr("exec_mcp.repo.cache_get", lambda *args, **kwargs: None)
  27. monkeypatch.setattr("exec_mcp.repo.cache_put", lambda *args, **kwargs: None)
  28. def fake_get(url, *args, **kwargs):
  29. if url.endswith("/currencies/"):
  30. return _FakeResponse([{"code": "USD"}])
  31. if url.endswith("/eur_usd/"):
  32. return _FakeResponse({"buy": "1.0", "sell": "1.0"})
  33. raise AssertionError(f"unexpected url: {url}")
  34. monkeypatch.setattr(bitstamp_metadata.requests, "get", fake_get)
  35. assert bitstamp_metadata.fetch_currencies() == [{"code": "USD"}]
  36. assert bitstamp_fx.fetch_eur_usd() == {"buy": "1.0", "sell": "1.0"}
  37. assert calls == ["meta", "fx"]