|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from httpx import Response |
| 3 | +import pytest |
| 4 | + |
| 5 | +from tests.common import get_response_json |
| 6 | + |
| 7 | +from xbox.webapi.common.exceptions import RateLimitExceededException |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.asyncio |
| 11 | +async def test_ratelimits_exceeded_burst_only(respx_mock, xbl_client): |
| 12 | + async def make_request(): |
| 13 | + route = respx_mock.get("https://peoplehub.xboxlive.com").mock( |
| 14 | + return_value=Response(200, json=get_response_json("people_friends_own")) |
| 15 | + ) |
| 16 | + ret = await xbl_client.people.get_friends_own() |
| 17 | + |
| 18 | + assert len(ret.people) == 2 |
| 19 | + assert route.called |
| 20 | + |
| 21 | + # Record the start time to ensure that the timeouts are the correct length |
| 22 | + start_time = datetime.now() |
| 23 | + |
| 24 | + # Make as many requests as possible without exceeding |
| 25 | + max_request_num = xbl_client.people.RATE_LIMITS["burst"] |
| 26 | + for i in range(max_request_num): |
| 27 | + await make_request() |
| 28 | + |
| 29 | + # Make another request, ensure that it raises the exception. |
| 30 | + with pytest.raises(RateLimitExceededException) as exception: |
| 31 | + await make_request() |
| 32 | + |
| 33 | + # Get the error instance from pytest |
| 34 | + ex: RateLimitExceededException = exception.value |
| 35 | + |
| 36 | + # Assert that the counter matches the max request num (should not have incremented above max value) |
| 37 | + assert ex.rate_limit.get_counter() == max_request_num |
| 38 | + |
| 39 | + # Get the timeout we were issued |
| 40 | + try_again_in = ex.rate_limit.get_reset_after() |
| 41 | + |
| 42 | + # Assert that the timeout is the correct length |
| 43 | + delta: timedelta = try_again_in - start_time |
| 44 | + assert delta.seconds == 15 |
0 commit comments