From 43c34a2b5cae8f553e24effaff8f9c4949f73a36 Mon Sep 17 00:00:00 2001 From: NotPeopling2day <32708219+NotPeopling2day@users.noreply.github.com> Date: Fri, 3 Jan 2025 15:31:38 -0500 Subject: [PATCH] test: add rate limit test --- tests/test_providers.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_providers.py b/tests/test_providers.py index fb11e42..05dd346 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -4,6 +4,7 @@ from ape.exceptions import ContractLogicError from ape.types import LogFilter from hexbytes import HexBytes +from requests import HTTPError from web3.exceptions import ContractLogicError as Web3ContractLogicError from ape_alchemy.provider import MissingProjectKeyError @@ -179,3 +180,29 @@ def test_get_transaction_trace(networks, alchemy_provider, mock_web3, parity_tra actual = repr(trace.get_calltree()) expected = r"CALL: 0xC17f2C69aE2E66FD87367E3260412EEfF637F70E\.<0x96d373e5\> \[1401584 gas\]" assert re.match(expected, actual) + + +def test_make_request_rate_limiting(mocker, alchemy_provider, mock_web3): + alchemy_provider._web3 = mock_web3 + + class RateLimitTester: + tries = 2 + _try = 0 + tries_made = 0 + + def rate_limit_hook(self, rpc, params): + self.tries_made += 1 + if self._try == self.tries: + self._try = 0 + return {"success": True} + else: + self._try += 1 + response = mocker.MagicMock() + response.status_code = 429 + raise HTTPError(response=response) + + rate_limit_tester = RateLimitTester() + mock_web3.provider.make_request.side_effect = rate_limit_tester.rate_limit_hook + result = alchemy_provider.make_request("ape_testRateLimiting", parameters=[]) + assert rate_limit_tester.tries_made == rate_limit_tester.tries + 1 + assert result == {"success": True}