def test_web_utils_retries(self, mock_api, retry_sleep_time_mock): retry_sleep_time_mock.side_effect = lambda *args, **kwargs: 0 url, regex_url = self._get_regex_url(CONSTANTS.ACCOUNTS_PATH_URL, return_url=True) mock_response = "invalid" for i in range(CONSTANTS.API_MAX_RETRIES * 2): mock_api.get(regex_url, body=mock_response) request = web_utils.CoinflexRESTRequest( method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNTS_PATH_URL) with self.assertRaises(web_utils.CoinflexAPIError): self.async_run_with_timeout( web_utils.api_call_with_retries( request=request, rest_assistant=self.rest_assistant, throttler=self.throttler, logger=self.logger)) with self.assertRaises(web_utils.CoinflexAPIError): self.async_run_with_timeout( web_utils.api_call_with_retries( request=request, rest_assistant=self.rest_assistant, throttler=self.throttler)) self.assertTrue( self._is_logged( "NETWORK", f"Error fetching data from {url}. HTTP status is 200. Retrying in 0s. invalid" ))
def test_web_utils_error_request_data_success_false(self, mock_api): regex_url = self._get_regex_url(CONSTANTS.ACCOUNTS_PATH_URL) mock_response = {"data": [{"success": "false", "message": "Error"}]} mock_api.get(regex_url, body=json.dumps(mock_response)) request = web_utils.CoinflexRESTRequest( method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNTS_PATH_URL) with self.assertRaises(web_utils.CoinflexAPIError): self.async_run_with_timeout( web_utils.api_call_with_retries( request=request, rest_assistant=self.rest_assistant, throttler=self.throttler, logger=self.logger))
def test_web_utils_error_request_truncated_string(self, mock_api, retry_sleep_time_mock): retry_sleep_time_mock.side_effect = lambda *args, **kwargs: 0 regex_url = self._get_regex_url(CONSTANTS.ACCOUNTS_PATH_URL) mock_api.get(regex_url, body="a" * 101) request = web_utils.CoinflexRESTRequest( method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNTS_PATH_URL) with self.assertRaises(web_utils.CoinflexAPIError): self.async_run_with_timeout( web_utils.api_call_with_retries( request=request, rest_assistant=self.rest_assistant, throttler=self.throttler, logger=self.logger))
def test_web_utils_error_request_error(self, mock_api): regex_url = self._get_regex_url(CONSTANTS.ACCOUNTS_PATH_URL) mock_response = {"error": "Error"} mock_api.get(regex_url, body=json.dumps(mock_response)) request = web_utils.CoinflexRESTRequest( method=RESTMethod.GET, endpoint=CONSTANTS.ACCOUNTS_PATH_URL) expected_error = {**mock_response, "errors": "Error"} with self.assertRaisesRegex(web_utils.CoinflexAPIError, str(expected_error)): self.async_run_with_timeout( web_utils.api_call_with_retries( request=request, rest_assistant=self.rest_assistant, throttler=self.throttler, logger=self.logger))