From 0d0694d7020cc98daaeb1e88592aa6d603e1a5a6 Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Wed, 22 Jan 2014 13:39:00 +0100 Subject: [PATCH] Don't use `override_time` but mock instead Closes-bug: #1266962 We're moving away from using the set_override_time function in timeutils. The right way to do it should be by using mock. Change-Id: Ic69c5c5d66cb39d668015c368add10b108684549 --- .../unit/queues/storage/test_impl_mongodb.py | 16 +++--- .../unit/queues/transport/wsgi/test_claims.py | 14 ++--- .../queues/transport/wsgi/test_messages.py | 51 ++++++++++--------- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/tests/unit/queues/storage/test_impl_mongodb.py b/tests/unit/queues/storage/test_impl_mongodb.py index fb310c7b0..b6ad61064 100644 --- a/tests/unit/queues/storage/test_impl_mongodb.py +++ b/tests/unit/queues/storage/test_impl_mongodb.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import datetime import time import uuid @@ -209,15 +210,12 @@ class MongodbMessageTests(base.MessageControllerTest): unchanged = self.queue_controller._inc_counter(queue_name, window=10) self.assertIsNone(unchanged) - # TODO(kgriffs): Pass utcnow to work around bug - # in set_time_override until we merge the fix in - # from upstream. - timeutils.set_time_override(timeutils.utcnow()) - - timeutils.advance_time_seconds(10) - changed = self.queue_controller._inc_counter(queue_name, window=5) - self.assertEqual(changed, reference_value + 1) - timeutils.clear_time_override() + now = timeutils.utcnow() + datetime.timedelta(seconds=10) + timeutils_utcnow = 'marconi.openstack.common.timeutils.utcnow' + with mock.patch(timeutils_utcnow) as mock_utcnow: + mock_utcnow.return_value = now + changed = self.queue_controller._inc_counter(queue_name, window=5) + self.assertEqual(changed, reference_value + 1) def test_race_condition_on_post(self): queue_name = 'marker_test' diff --git a/tests/unit/queues/transport/wsgi/test_claims.py b/tests/unit/queues/transport/wsgi/test_claims.py index b47317a4f..144e74190 100644 --- a/tests/unit/queues/transport/wsgi/test_claims.py +++ b/tests/unit/queues/transport/wsgi/test_claims.py @@ -13,11 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +import datetime import json import uuid import ddt import falcon +import mock from testtools import matchers from . import base # noqa @@ -137,12 +139,12 @@ class ClaimsBaseTest(base.TestBase): self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(len(listed['messages']), len(claimed)) - # Check the claim's metadata - ## NOTE(cpp-cabrera): advance time to force claim aging - timeutils.set_time_override(timeutils.utcnow()) - timeutils.advance_time_seconds(10) - body = self.simulate_get(claim_href, self.project_id) - timeutils.clear_time_override() + now = timeutils.utcnow() + datetime.timedelta(seconds=10) + timeutils_utcnow = 'marconi.openstack.common.timeutils.utcnow' + with mock.patch(timeutils_utcnow) as mock_utcnow: + mock_utcnow.return_value = now + body = self.simulate_get(claim_href, self.project_id) + claim = json.loads(body[0]) self.assertEqual(self.srmock.status, falcon.HTTP_200) diff --git a/tests/unit/queues/transport/wsgi/test_messages.py b/tests/unit/queues/transport/wsgi/test_messages.py index 8b2ecf476..b668a3cd4 100644 --- a/tests/unit/queues/transport/wsgi/test_messages.py +++ b/tests/unit/queues/transport/wsgi/test_messages.py @@ -13,11 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +import datetime import json import uuid import ddt import falcon +import mock import six from testtools import matchers @@ -83,31 +85,32 @@ class MessagesBaseTest(base.TestBase): # Test GET on the message resource directly # NOTE(cpp-cabrera): force the passing of time to age a message - timeutils.set_time_override(timeutils.utcnow()) - timeutils.advance_time_seconds(10) - for msg_id in msg_ids: - message_uri = self.messages_path + '/' + msg_id + timeutils_utcnow = 'marconi.openstack.common.timeutils.utcnow' + now = timeutils.utcnow() + datetime.timedelta(seconds=10) + with mock.patch(timeutils_utcnow) as mock_utcnow: + mock_utcnow.return_value = now + for msg_id in msg_ids: + message_uri = self.messages_path + '/' + msg_id - # Wrong project ID - self.simulate_get(message_uri, '777777') - self.assertEqual(self.srmock.status, falcon.HTTP_404) + # Wrong project ID + self.simulate_get(message_uri, '777777') + self.assertEqual(self.srmock.status, falcon.HTTP_404) - # Correct project ID - result = self.simulate_get(message_uri, self.project_id) - self.assertEqual(self.srmock.status, falcon.HTTP_200) - self.assertEqual(self.srmock.headers_dict['Content-Location'], - message_uri) + # Correct project ID + result = self.simulate_get(message_uri, self.project_id) + self.assertEqual(self.srmock.status, falcon.HTTP_200) + self.assertEqual(self.srmock.headers_dict['Content-Location'], + message_uri) - # Check message properties - message = json.loads(result[0]) - self.assertEqual(message['href'], message_uri) - self.assertEqual(message['body'], lookup[message['ttl']]) + # Check message properties + message = json.loads(result[0]) + self.assertEqual(message['href'], message_uri) + self.assertEqual(message['body'], lookup[message['ttl']]) - # no negative age - # NOTE(cpp-cabrera): testtools lacks GreaterThanEqual on py26 - self.assertThat(message['age'], - matchers.GreaterThan(-1)) - timeutils.clear_time_override() + # no negative age + # NOTE(cpp-cabrera): testtools lacks GreaterThanEqual on py26 + self.assertThat(message['age'], + matchers.GreaterThan(-1)) # Test bulk GET query_string = 'ids=' + ','.join(msg_ids) @@ -275,7 +278,7 @@ class MessagesBaseTest(base.TestBase): def test_bulk_delete(self): path = self.queue_path + '/messages' self._post_messages(path, repeat=5) - [target, params] = self.srmock.headers_dict['Location'].split('?') + [target, params] = self.srmock.headers_dict['location'].split('?') # Deleting the whole collection is denied self.simulate_delete(path, self.project_id) @@ -378,7 +381,7 @@ class MessagesBaseTest(base.TestBase): self.simulate_post(path + '/claims', self.project_id, body='{"ttl": 100, "grace": 100}') self.assertEqual(self.srmock.status, falcon.HTTP_201) - location = self.srmock.headers_dict['Location'] + location = self.srmock.headers_dict['location'] # release claim self.simulate_delete(location, self.project_id) @@ -446,7 +449,7 @@ class MessagesBaseTest(base.TestBase): return self._get_msg_ids(headers)[0] def _get_msg_ids(self, headers): - return headers['Location'].rsplit('=', 1)[-1].split(',') + return headers['location'].rsplit('=', 1)[-1].split(',') class MessagesSQLiteTests(MessagesBaseTest):