def setUp(self): super(TestRetryInterface, self).setUp() self.total_calls = 0 self.auth = Mock() self.session = Session(include_builtin_handlers=False) self.service = Mock() self.service.endpoint_prefix = 'ec2' self.service.session = self.session self.endpoint = QueryEndpoint( self.service, 'us-west-2', 'https://ec2.us-west-2.amazonaws.com/', auth=self.auth) self.http_session = Mock() self.endpoint.http_session = self.http_session self.get_response_patch = patch('botocore.response.get_response') self.get_response = self.get_response_patch.start() self.retried_on_exception = None
def setUp(self): super(TestRetryInterface, self).setUp() self.total_calls = 0 self.auth = Mock() self.session = Session(include_builtin_handlers=False) self.service = Mock() self.service.endpoint_prefix = "ec2" self.service.session = self.session self.endpoint = QueryEndpoint(self.service, "us-west-2", "https://ec2.us-west-2.amazonaws.com/", auth=self.auth) self.http_session = Mock() self.endpoint.http_session = self.http_session self.get_response_patch = patch("botocore.response.get_response") self.get_response = self.get_response_patch.start() self.retried_on_exception = None
class TestRetryInterface(BaseSessionTest): def setUp(self): super(TestRetryInterface, self).setUp() self.total_calls = 0 self.auth = Mock() self.session = Session(include_builtin_handlers=False) self.service = Mock() self.service.endpoint_prefix = 'ec2' self.service.session = self.session self.endpoint = QueryEndpoint( self.service, 'us-west-2', 'https://ec2.us-west-2.amazonaws.com/', auth=self.auth) self.http_session = Mock() self.endpoint.http_session = self.http_session self.get_response_patch = patch('botocore.response.get_response') self.get_response = self.get_response_patch.start() self.retried_on_exception = None def tearDown(self): self.get_response_patch.stop() def max_attempts_retry_handler(self, attempts, **kwargs): # Simulate a max requests of 3. self.total_calls += 1 if attempts == 3: return None else: # Returning anything non-None will trigger a retry, # but 0 here is so that time.sleep(0) happens. return 0 def connection_error_handler(self, attempts, caught_exception, **kwargs): self.total_calls += 1 if attempts == 3: return None elif isinstance(caught_exception, ConnectionError): # Returning anything non-None will trigger a retry, # but 0 here is so that time.sleep(0) happens. return 0 else: return None def test_retry_events_are_emitted(self): emitted_events = [] self.session.register('needs-retry.ec2.DescribeInstances', lambda **kwargs: emitted_events.append(kwargs)) op = Mock() op.name = 'DescribeInstances' self.endpoint.make_request(op, {}) self.assertEqual(len(emitted_events), 1) self.assertEqual(emitted_events[0]['event_name'], 'needs-retry.ec2.DescribeInstances') def test_retry_events_can_alter_behavior(self): self.session.register('needs-retry.ec2.DescribeInstances', self.max_attempts_retry_handler) op = Mock() op.name = 'DescribeInstances' self.endpoint.make_request(op, {}) self.assertEqual(self.total_calls, 3) def test_retry_on_socket_errors(self): self.session.register('needs-retry.ec2.DescribeInstances', self.connection_error_handler) op = Mock() op.name = 'DescribeInstances' self.http_session.send.side_effect = ConnectionError() self.endpoint.make_request(op, {}) self.assertEqual(self.total_calls, 3)