def __init__(self, client_config, credentials_provider=None, retry_policy=None, endpoint_resolver=None): self.product_code = None self.location_service_code = None self.api_version = None self.location_endpoint_type = None self.logger = self._init_logger() # TODO initialize self.handlers = [ RetryHandler(), APIProtocolHandler(), CredentialsHandler(), SignerHandler(), TimeoutConfigReader(), EndpointHandler(), HttpHandler(), ServerErrorHandler(), ] # client_config:ClientConfig,TODO self.config = client_config self.credentials_provider = credentials_provider self._init_endpoint_resolve(endpoint_resolver) self._init_retry(retry_policy)
def test_file_retry(self): config_file = ('[default]\n' 'max_retry_times = 2\n' 'endpoint = somewhere.you.will.never.get\n' 'region_id = us-west-1\n') self.write_config(config_file) api_request = APIRequest('DescribeInstances', 'GET', 'http', 'RPC') client = get_client( 'ecs', credentials_provider=self.init_credentials_provider()) globals()["_test_compute_delay"] = [] def record_sleep(delay): global _test_compute_delay _test_compute_delay.append(delay) from alibabacloud.handlers.api_protocol_handler import APIProtocolHandler from alibabacloud.handlers.credentials_handler import CredentialsHandler from alibabacloud.handlers.signer_handler import SignerHandler from alibabacloud.handlers.timeout_config_reader import TimeoutConfigReader from alibabacloud.handlers.endpoint_handler import EndpointHandler from alibabacloud.handlers.retry_handler import RetryHandler from alibabacloud.handlers.server_error_handler import ServerErrorHandler from alibabacloud.handlers.http_handler import HttpHandler DEFAULT_HANDLERS = [ RetryHandler(), APIProtocolHandler(), CredentialsHandler(), SignerHandler(), TimeoutConfigReader(), EndpointHandler(), ServerErrorHandler(), HttpHandler(), ] client.handlers = DEFAULT_HANDLERS with patch.object(time, "sleep", wraps=record_sleep) as monkey: try: client._handle_request(api_request) assert False except HttpErrorException as e: self.assertTrue( "Max retries exceeded with url" in e.error_message) self.assertEqual(2, monkey.call_count) self.assertEqual(2, len(_test_compute_delay)) os.environ.pop('ALIBABA_CLOUD_CONFIG_FILE')
def test_throttled_backoff(self): def _handle_response(context): context.exception = ServerException("Throttling", "some error") config = self.client_config config.max_retry_times = 10 config.endpoint = "somewhere.you.will.never.get" client = EcsClient(config, self.init_credentials_provider()) api_request = APIRequest('DescribeInstances', 'GET', 'http', 'RPC') globals()["_test_compute_delay"] = [] def record_sleep(delay): global _test_compute_delay _test_compute_delay.append(delay) from alibabacloud.handlers.api_protocol_handler import APIProtocolHandler from alibabacloud.handlers.credentials_handler import CredentialsHandler from alibabacloud.handlers.signer_handler import SignerHandler from alibabacloud.handlers.timeout_config_reader import TimeoutConfigReader from alibabacloud.handlers.endpoint_handler import EndpointHandler from alibabacloud.handlers.retry_handler import RetryHandler from alibabacloud.handlers.server_error_handler import ServerErrorHandler from alibabacloud.handlers.http_handler import HttpHandler DEFAULT_HANDLERS = [ RetryHandler(), APIProtocolHandler(), CredentialsHandler(), SignerHandler(), TimeoutConfigReader(), EndpointHandler(), ServerErrorHandler(), HttpHandler(), ] client.handlers = DEFAULT_HANDLERS client.config = config with patch.object(time, "sleep", wraps=record_sleep) as monkey: with patch.object(ServerErrorHandler, "handle_response", wraps=_handle_response): try: client._handle_request(api_request) assert False except ServerException as e: self.assertEqual("Throttling", e.error_code) self.assertEqual(10, monkey.call_count) self.assertEqual(10, len(_test_compute_delay))
from alibabacloud.handlers.http_handler import HttpHandler from alibabacloud.handlers.retry_handler import RetryHandler from alibabacloud.handlers.server_error_handler import ServerErrorHandler from alibabacloud.handlers.signer_handler import SignerHandler from alibabacloud.handlers.timeout_config_reader import TimeoutConfigReader from alibabacloud.request import APIRequest from base import SDKTestBase DEFAULT_HANDLERS = [ RetryHandler(), APIProtocolHandler(), CredentialsHandler(), SignerHandler(), TimeoutConfigReader(), EndpointHandler(), HttpHandler(), ServerErrorHandler(), ] class AlibabaCloudRetryTest(SDKTestBase): def test_no_retry(self): config = self.client_config config.enable_retry = False config.endpoint = 'somewhere.you.never' client = EcsClient(config, self.init_credentials_provider()) with patch.object(client, "_handle_request", wraps=client._handle_request) as monkey: try: context = client.describe_instances()
from alibabacloud.handlers.http_handler import HttpHandler from alibabacloud.handlers.retry_handler import RetryHandler from alibabacloud.handlers.server_error_handler import ServerErrorHandler from alibabacloud.handlers.signer_handler import SignerHandler from alibabacloud.handlers.timeout_config_reader import TimeoutConfigReader from alibabacloud.request import APIRequest from alibabacloud.vendored import six from alibabacloud.vendored.requests import Response from base import SDKTestBase test_unicode = "\u007b\u0026\u0071\u0075\u006f\u0074\u003b\u006e\u0061\u006d" \ "\u0065\u0026\u0071\u0075\u006f\u0074\u003b\u0020\u003a\u0020" \ "\u0026\u0071\u0075\u006f\u0074\u003b\u0063\u006c\u006f\u0075" \ "\u0064\u0026\u0071\u0075\u006f\u0074\u003b\u007d" http_handler = HttpHandler() DEFAULT_HANDLERS = [ RetryHandler(), APIProtocolHandler(), CredentialsHandler(), SignerHandler(), TimeoutConfigReader(), EndpointHandler(), http_handler, ServerErrorHandler(), ] from alibabacloud import get_client class APIRequestTest(SDKTestBase):