From 0a5b0033456f1a0fa6b743f6d2635df879dbc630 Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Wed, 25 Jun 2014 20:21:28 -0700 Subject: [PATCH] Fix double request in simple client The SimpleClient.base_request method need only to call urllib2.urlopen once to get a response from the server. Remove the duplicate call. * fixup tests that were poorly mocking urllib2 * mock eventlet sleep because we're all busy Change-Id: I6da93c689200351e9dbd9416b9c4bf9544571f15 --- swift/common/internal_client.py | 1 - test/unit/common/test_internal_client.py | 22 ++++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/swift/common/internal_client.py b/swift/common/internal_client.py index 5d41a1d94e..6e2c217056 100644 --- a/swift/common/internal_client.py +++ b/swift/common/internal_client.py @@ -764,7 +764,6 @@ class SimpleClient(object): req = urllib2.Request(url, headers=headers, data=contents) req.get_method = lambda: method - urllib2.urlopen(req) conn = urllib2.urlopen(req) body = conn.read() try: diff --git a/test/unit/common/test_internal_client.py b/test/unit/common/test_internal_client.py index 0d969b1814..427f46363c 100644 --- a/test/unit/common/test_internal_client.py +++ b/test/unit/common/test_internal_client.py @@ -1130,6 +1130,7 @@ class TestSimpleClient(unittest.TestCase): logger = FakeLogger() retval = sc.retry_request( 'GET', headers={'content-length': '123'}, logger=logger) + self.assertEqual(urlopen.call_count, 1) request.assert_called_with('http://127.0.0.1?format=json', headers={'content-length': '123'}, data=None) @@ -1181,25 +1182,30 @@ class TestSimpleClient(unittest.TestCase): def test_get_with_retries_all_failed(self, request, urlopen): # Simulate a failing request, ensure retries done request.return_value.get_type.return_value = "http" - request.side_effect = urllib2.URLError('') - urlopen.return_value.read.return_value = '' + urlopen.side_effect = urllib2.URLError('') sc = internal_client.SimpleClient(url='http://127.0.0.1', retries=1) - self.assertRaises(urllib2.URLError, sc.retry_request, 'GET') + with mock.patch('swift.common.internal_client.sleep') as mock_sleep: + self.assertRaises(urllib2.URLError, sc.retry_request, 'GET') + self.assertEqual(mock_sleep.call_count, 1) self.assertEqual(request.call_count, 2) + self.assertEqual(urlopen.call_count, 2) @mock.patch('eventlet.green.urllib2.urlopen') @mock.patch('eventlet.green.urllib2.Request') def test_get_with_retries(self, request, urlopen): # First request fails, retry successful request.return_value.get_type.return_value = "http" - urlopen.return_value.read.return_value = '' - req = urllib2.Request('http://127.0.0.1', method='GET') - request.side_effect = [urllib2.URLError(''), req] + mock_resp = mock.MagicMock() + mock_resp.read.return_value = '' + urlopen.side_effect = [urllib2.URLError(''), mock_resp] sc = internal_client.SimpleClient(url='http://127.0.0.1', retries=1, token='token') - retval = sc.retry_request('GET') - self.assertEqual(request.call_count, 3) + with mock.patch('swift.common.internal_client.sleep') as mock_sleep: + retval = sc.retry_request('GET') + self.assertEqual(mock_sleep.call_count, 1) + self.assertEqual(request.call_count, 2) + self.assertEqual(urlopen.call_count, 2) request.assert_called_with('http://127.0.0.1?format=json', data=None, headers={'X-Auth-Token': 'token'}) self.assertEqual([None, None], retval)