first commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from sync_state_machine.datasource.api.client import ApiClient
|
||||
|
||||
|
||||
class _FakeResponse:
|
||||
status_code = 200
|
||||
|
||||
def raise_for_status(self) -> None:
|
||||
return None
|
||||
|
||||
def json(self) -> dict:
|
||||
return {"code": 200, "data": []}
|
||||
|
||||
|
||||
class _FakeAsyncClient:
|
||||
def __init__(self) -> None:
|
||||
self.call_times: list[float] = []
|
||||
|
||||
async def request(self, **kwargs):
|
||||
self.call_times.append(time.monotonic())
|
||||
return _FakeResponse()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_client_global_rate_limit_applies_to_concurrent_requests() -> None:
|
||||
client = ApiClient(
|
||||
base_url="http://example.com",
|
||||
uid="uid",
|
||||
secret="secret",
|
||||
rate_limit_max_requests=2,
|
||||
rate_limit_window_seconds=0.2,
|
||||
)
|
||||
fake_client = _FakeAsyncClient()
|
||||
client._client = fake_client
|
||||
|
||||
await asyncio.gather(
|
||||
client.request("GET", "/ping"),
|
||||
client.request("GET", "/ping"),
|
||||
client.request("GET", "/ping"),
|
||||
)
|
||||
|
||||
assert len(fake_client.call_times) == 3
|
||||
third_delay = fake_client.call_times[2] - fake_client.call_times[0]
|
||||
assert third_delay >= 0.15
|
||||
Reference in New Issue
Block a user