Ejemplo n.º 1
0
 def test_non_retryable(self):
     with patch('time.sleep') as mock_time:
         fcn = MagicMock()
         fcn.side_effect = [BadJson('a'), self.response]
         with self.assertRaises(BadJson):
             B2Http._translate_and_retry(fcn, 3)
         self.assertEqual([], mock_time.mock_calls)
Ejemplo n.º 2
0
 def test_too_many_requests(self):
     response = MagicMock()
     response.status_code = 429
     response.headers = {'retry-after': 1}
     response.content = b'{"status": 429, "code": "Too Many requests", "message": "retry after some time"}'
     with self.assertRaises(TooManyRequests):
         B2Http._translate_errors(lambda: response)
Ejemplo n.º 3
0
    def test_broken_pipe(self):
        def fcn():
            raise requests.ConnectionError(
                requests.packages.urllib3.exceptions.ProtocolError(
                    "dummy", socket.error(20, 'Broken pipe')))

        with self.assertRaises(BrokenPipe):
            B2Http._translate_errors(fcn)
Ejemplo n.º 4
0
    def test_unknown_host(self):
        def fcn():
            raise requests.ConnectionError(
                requests.packages.urllib3.exceptions.MaxRetryError(
                    'AAA nodename nor servname provided, or not known AAA',
                    'http://example.com'))

        with self.assertRaises(UnknownHost):
            B2Http._translate_errors(fcn)
Ejemplo n.º 5
0
    def test_connection_reset(self):
        class SysCallError(Exception):
            pass

        def fcn():
            raise SysCallError('(104, ECONNRESET)')

        with self.assertRaises(ConnectionReset):
            B2Http._translate_errors(fcn)
Ejemplo n.º 6
0
 def test_too_many_requests_failed_after_sleep(self):
     with patch('time.sleep') as mock_time:
         fcn = MagicMock()
         fcn.side_effect = [
             TooManyRequests(retry_after_seconds=2),
             TooManyRequests(retry_after_seconds=5),
         ]
         with self.assertRaises(TooManyRequests):
             B2Http._translate_and_retry(fcn, 2)
         self.assertEqual([call(2)], mock_time.mock_calls)
Ejemplo n.º 7
0
 def test_never_works(self):
     with patch('time.sleep') as mock_time:
         fcn = MagicMock()
         fcn.side_effect = [
             ServiceError('a'),
             ServiceError('a'),
             ServiceError('a'), self.response
         ]
         with self.assertRaises(ServiceError):
             B2Http._translate_and_retry(fcn, 3)
         self.assertEqual([call(1.0), call(1.5)], mock_time.mock_calls)
Ejemplo n.º 8
0
 def test_provide_raw_api_v1(self):
     from apiver_deps import B2RawApi  # test for legacy name
     old_style_api = B2Api(
         raw_api=B2RawApi(B2Http(user_agent_append='test append')))
     new_style_api = B2Api(api_config=B2HttpApiConfig(
         user_agent_append='test append'))
     assert old_style_api.session.raw_api.b2_http.user_agent == new_style_api.session.raw_api.b2_http.user_agent
     with pytest.raises(InvalidArgument):
         B2Api(
             raw_api=B2RawApi(B2Http(user_agent_append='test append')),
             api_config=B2HttpApiConfig(user_agent_append='test append'),
         )
Ejemplo n.º 9
0
    def setUp(self):
        self.session = MagicMock()
        self.response = MagicMock()

        requests = MagicMock()
        requests.Session.return_value = self.session

        if apiver_deps.V <= 1:
            self.b2_http = B2Http(requests,
                                  install_clock_skew_hook=False,
                                  user_agent_append=self.UA_APPEND)
        else:
            self.b2_http = B2Http(
                B2HttpApiConfig(requests.Session,
                                install_clock_skew_hook=False,
                                user_agent_append=self.UA_APPEND))
Ejemplo n.º 10
0
 def test_too_many_requests_works_after_sleep(self):
     with patch('time.sleep') as mock_time:
         fcn = MagicMock()
         fcn.side_effect = [
             TooManyRequests(retry_after_seconds=2), self.response
         ]
         self.assertIs(self.response, B2Http._translate_and_retry(fcn, 3))
         self.assertEqual([call(2)], mock_time.mock_calls)
Ejemplo n.º 11
0
    def test_too_many_requests_retry_header_combination_one(self):
        # If the first response didn't have a header, second one has, and third one doesn't have, what should happen?

        with patch('time.sleep') as mock_time:
            fcn = MagicMock()
            fcn.side_effect = [
                TooManyRequests(retry_after_seconds=2),
                TooManyRequests(),
                TooManyRequests(retry_after_seconds=2),
                self.response,
            ]
            self.assertIs(self.response, B2Http._translate_and_retry(fcn, 4))
            self.assertEqual([call(2), call(1.5), call(2)],
                             mock_time.mock_calls)
Ejemplo n.º 12
0
    def test_too_many_requests_retry_header_combination_two(self):
        # If the first response had header, and the second did not, but the third has header again, what should happen?

        with patch('time.sleep') as mock_time:
            fcn = MagicMock()
            fcn.side_effect = [
                TooManyRequests(),
                TooManyRequests(retry_after_seconds=5),
                TooManyRequests(),
                self.response,
            ]
            self.assertIs(self.response, B2Http._translate_and_retry(fcn, 4))
            self.assertEqual(
                [call(1.0), call(5), call(2.25)], mock_time.mock_calls)
Ejemplo n.º 13
0
    def test_unknown_error(self):
        def fcn():
            raise Exception('a message')

        with self.assertRaises(UnknownError):
            B2Http._translate_errors(fcn)
Ejemplo n.º 14
0
 def test_works_second_try(self):
     with patch('time.sleep') as mock_time:
         fcn = MagicMock()
         fcn.side_effect = [ServiceError('a'), self.response]
         self.assertIs(self.response, B2Http._translate_and_retry(fcn, 3))
         self.assertEqual([call(1.0)], mock_time.mock_calls)
Ejemplo n.º 15
0
    def test_connection_error(self):
        def fcn():
            raise requests.ConnectionError('a message')

        with self.assertRaises(B2ConnectionError):
            B2Http._translate_errors(fcn)
Ejemplo n.º 16
0
 def test_b2_error(self):
     response = MagicMock()
     response.status_code = 503
     response.content = b'{"status": 503, "code": "server_busy", "message": "busy"}'
     with self.assertRaises(ServiceError):
         B2Http._translate_errors(lambda: response)
Ejemplo n.º 17
0
 def test_partial_content(self):
     response = MagicMock()
     response.status_code = 206
     actual = B2Http._translate_errors(lambda: response)
     self.assertIs(response, actual)
Ejemplo n.º 18
0
 def test_works_first_try(self):
     fcn = MagicMock()
     fcn.side_effect = [self.response]
     self.assertIs(self.response, B2Http._translate_and_retry(fcn, 3))