| OLD | NEW |
| (Empty) |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import mock | |
| 6 import unittest | |
| 7 | |
| 8 from apiclient.errors import HttpError | |
| 9 from endpoints import endpoints | |
| 10 | |
| 11 | |
| 12 class EndpointsTestCase(unittest.TestCase): | |
| 13 def setUp(self): | |
| 14 super(EndpointsTestCase, self).setUp() | |
| 15 self.build = mock.Mock() | |
| 16 self.cred = mock.Mock() | |
| 17 self.http = mock.Mock() | |
| 18 self.sleep = mock.Mock() | |
| 19 self.patchers = [ | |
| 20 mock.patch('apiclient.discovery.build', self.build), | |
| 21 mock.patch('httplib2.Http', self.http), | |
| 22 mock.patch('oauth2client.appengine.AppAssertionCredentials', self.cred), | |
| 23 mock.patch('time.sleep', self.sleep), | |
| 24 ] | |
| 25 for patcher in self.patchers: | |
| 26 patcher.start() | |
| 27 | |
| 28 def tearDown(self): | |
| 29 super(EndpointsTestCase, self).tearDown() | |
| 30 for patcher in self.patchers: | |
| 31 patcher.stop() | |
| 32 | |
| 33 def test_retries_errors_with_exponential_delay_when_building_client(self): | |
| 34 self.build.side_effect = [ | |
| 35 HttpError(mock.Mock(), 'error'), | |
| 36 HttpError(mock.Mock(), 'error'), | |
| 37 HttpError(mock.Mock(), 'error'), | |
| 38 'client' | |
| 39 ] | |
| 40 self.assertEquals(endpoints.build_client('foo', 'bar', 'baz'), 'client') | |
| 41 self.sleep.assert_has_calls([mock.call(1), mock.call(2), mock.call(4)]) | |
| 42 | |
| 43 def test_fails_to_build_client_after_5_errors(self): | |
| 44 self.build.side_effect = [HttpError(mock.Mock(), 'error')] * 3 | |
| 45 with self.assertRaises(HttpError): | |
| 46 endpoints.build_client('foo', 'bar', 'baz', num_tries=3) | |
| 47 | |
| 48 def test_uses_provided_http(self): | |
| 49 self.cred.return_value.authorize = mock.Mock() | |
| 50 endpoints.build_client('foo', 'bar', 'baz', http='myhttp') | |
| 51 self.cred.return_value.authorize.assert_called_once_with('myhttp') | |
| 52 | |
| 53 def test_retries_supported_errors_on_requests_with_exponential_delay(self): | |
| 54 request = mock.Mock() | |
| 55 request.execute.side_effect = [ | |
| 56 HttpError(mock.Mock(status=500), 'error'), | |
| 57 HttpError(mock.Mock(status=503), 'error'), | |
| 58 HttpError(mock.Mock(status=403), 'error'), | |
| 59 'response' | |
| 60 ] | |
| 61 self.assertEquals(endpoints.retry_request(request), 'response') | |
| 62 self.sleep.assert_has_calls([mock.call(1), mock.call(2), mock.call(4)]) | |
| 63 | |
| 64 def test_fails_requests_with_too_many_retries(self): | |
| 65 request = mock.Mock() | |
| 66 request.execute.side_effect = [HttpError(mock.Mock(status=500), 'err')] * 3 | |
| 67 with self.assertRaises(HttpError): | |
| 68 endpoints.retry_request(request, num_tries=3) | |
| 69 | |
| 70 def test_fails_requests_with_unsupported_retry_errors(self): | |
| 71 request = mock.Mock() | |
| 72 request.execute.side_effect = [ | |
| 73 HttpError(mock.Mock(status=400), 'err'), | |
| 74 'response' | |
| 75 ] | |
| 76 with self.assertRaises(HttpError): | |
| 77 endpoints.retry_request(request) | |
| OLD | NEW |