def test_success(self): session = clients.Session(executor=self.executor) self.set_mock_response(200) response = kernels.run(session.send(self.REQUEST)) self.assertIsNot(response, self.mock_response) self.assertIsInstance(response, bases.Response) self.mock_session.get.assert_called_once_with(self.URL)
def test_success(self): # TODO: How to test that requests are executed in order? session = clients.Session(executor=self.executor) self.set_mock_response(200) response = kernels.run(session.send(self.REQUEST, priority=1)) self.assertIsNot(response, self.mock_response) self.assertIsInstance(response, bases.Response) self.mock_session.get.assert_called_once_with(self.URL)
def test_error(self): session = clients.Session( executor=self.executor, retry=policies.ExponentialBackoff(2, 0.001), ) self.mock_session.get.side_effect = Exception('some error') with self.assertRaisesRegex(Exception, r'some error'): kernels.run(session.send(self.REQUEST)) self.mock_session.get.assert_called_once_with(self.URL)
def test_http_error_no_retry_4xx(self): session = clients.Session( executor=self.executor, retry=policies.ExponentialBackoff(2, 0.001), ) self.set_mock_response(404) with self.assertRaises(requests.RequestException): kernels.run(session.send(self.REQUEST)) self.mock_session.get.assert_called_once_with(self.URL)
def test_sticky_key(self): session = clients.Session(executor=self.executor) self.set_mock_response(200) response = kernels.run(session.send(self.REQUEST, sticky_key='y')) for _ in range(3): self.assertIs( kernels.run(session.send(self.REQUEST, sticky_key='y')), response, ) self.mock_session.get.assert_called_once_with(self.URL)
def test_circuit_breaker(self, mock_time): mock_monotonic = mock_time.monotonic mock_monotonic.side_effect = [ 99, 100, 101, 102, 103, 104, 105, 106, 107 ] self.mock_session.get.side_effect = Exception('some error') circuit_breakers = policies.TristateBreakers( failure_threshold=2, failure_period=1, failure_timeout=1, success_threshold=2, ) breaker = circuit_breakers.get('localhost') session = clients.Session( executor=self.executor, circuit_breakers=circuit_breakers, ) self.assert_breaker(breaker, policies._States.GREEN, [], 0) with self.assertRaisesRegex(Exception, r'some error'): kernels.run(session.send(self.REQUEST)) self.assert_breaker(breaker, policies._States.GREEN, [99], 0) with self.assertRaisesRegex(Exception, r'some error'): kernels.run(session.send(self.REQUEST)) self.assert_breaker(breaker, policies._States.RED, [100], 0) with self.assertRaisesRegex(policies.Unavailable, r'circuit breaker disconnected'): kernels.run(session.send(self.REQUEST)) self.assert_breaker(breaker, policies._States.RED, [100], 0) with self.assertRaisesRegex(Exception, r'some error'): kernels.run(session.send(self.REQUEST)) self.assert_breaker(breaker, policies._States.RED, [103], 0) self.mock_session.get.side_effect = None self.set_mock_response(404) with self.assertRaisesRegex(policies.Unavailable, r'circuit breaker disconnected'): kernels.run(session.send(self.REQUEST)) self.assert_breaker(breaker, policies._States.RED, [103], 0) with self.assertRaises(requests.RequestException): kernels.run(session.send(self.REQUEST)) self.assert_breaker(breaker, policies._States.YELLOW, [106], 0) self.set_mock_response(200) kernels.run(session.send(self.REQUEST)) self.assert_breaker(breaker, policies._States.GREEN, [], 0)
def test_cache_revalidate(self): session = clients.Session(executor=self.executor) self.set_mock_response(200) for _ in range(3): kernels.run( session.send(self.REQUEST, cache_key='x', cache_revalidate=True)) self.mock_session.get.assert_has_calls([ unittest.mock.call(self.URL), unittest.mock.call().close(), unittest.mock.call(self.URL), unittest.mock.call().close(), unittest.mock.call(self.URL), unittest.mock.call().close(), ])
def test_http_error_retry(self): session = clients.Session( executor=self.executor, retry=policies.ExponentialBackoff(2, 0.001), ) self.set_mock_response(500) with self.assertRaises(requests.RequestException): kernels.run(session.send(self.REQUEST)) self.mock_session.get.assert_has_calls([ unittest.mock.call(self.URL), unittest.mock.call().close(), unittest.mock.call(self.URL), unittest.mock.call().close(), unittest.mock.call(self.URL), unittest.mock.call().close(), ])
def test_recvfile(self): test_path = Path(__file__) session = clients.Session() request = bases.Request( 'GET', 'http://127.0.0.1:%d/%s' % (self.server_port, test_path.name), ) with tempfile.TemporaryFile() as output: with kernels.run(session.send(request, stream=True)) as response: kernels.run(response.recvfile(output)) output.seek(0) self.assertEqual(output.read(), test_path.read_bytes()) # Check that response is closed. self.assertTrue(response.raw._fp.isclosed()) self.assertIsNone(response.raw._fp.fp)
def test_cache_key_and_sticky_key(self): session = clients.Session(executor=self.executor) with self.assertRaisesRegex(AssertionError, r'expect at most one:'): kernels.run( session.send(self.REQUEST, cache_key='x', sticky_key='y'))
def test_http_error_no_retry(self): session = clients.Session(executor=self.executor) self.set_mock_response(404) with self.assertRaises(requests.RequestException): kernels.run(session.send(self.REQUEST)) self.mock_session.get.assert_called_once_with(self.URL)