diff options
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/cache/test_memory.py | 41 | ||||
-rw-r--r-- | tests/unit/test_loopingcall.py | 23 | ||||
-rw-r--r-- | tests/unit/test_memorycache.py | 17 |
3 files changed, 38 insertions, 43 deletions
diff --git a/tests/unit/cache/test_memory.py b/tests/unit/cache/test_memory.py index c3b72789..52975c37 100644 --- a/tests/unit/cache/test_memory.py +++ b/tests/unit/cache/test_memory.py @@ -12,9 +12,10 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime +import time + +import mock -from openstack.common import timeutils from tests.unit.cache import base @@ -28,55 +29,51 @@ class MemorycacheTest(base.CacheBaseTest): cache_url = 'memory://' def test_timeout(self): - try: - now = datetime.datetime.utcnow() - timeutils.set_time_override(now) + now = time.time() + with mock.patch('time.time') as time_mock: + time_mock.return_value = now self.client.set('foo', 'bar', ttl=3) - timeutils.set_time_override(now + datetime.timedelta(seconds=1)) + time_mock.return_value = now + 1 self.assertEqual(self.client.get('foo'), 'bar') - timeutils.set_time_override(now + datetime.timedelta(seconds=3)) + time_mock.return_value = now + 3 self.assertIsNone(self.client.get('foo')) - finally: - timeutils.clear_time_override() def test_timeout_unset(self): - try: - now = datetime.datetime.utcnow() - timeutils.set_time_override(now) + now = time.time() + with mock.patch('time.time') as time_mock: + time_mock.return_value = now self.client.set('foo', 'bar', ttl=3) self.client.set('fooo', 'bar', ttl=4) self.client.set('foooo', 'bar', ttl=5) self.client.set('fooooo', 'bar', ttl=6) - timeutils.set_time_override(now + datetime.timedelta(seconds=1)) + time_mock.return_value = now + 1 self.assertEqual(self.client.get('foo'), 'bar') self.assertEqual(self.client.get('fooo'), 'bar') self.assertEqual(self.client.get('foooo'), 'bar') self.assertEqual(self.client.get('fooooo'), 'bar') - timeutils.set_time_override(now + datetime.timedelta(seconds=5)) + time_mock.return_value = now + 5 del self.client['foo'] self.assertIsNone(self.client.get('foo')) self.assertIsNone(self.client.get('fooo')) self.assertIsNone(self.client.get('foooo')) self.assertEqual(self.client.get('fooooo'), 'bar') - finally: - timeutils.clear_time_override() def test_timeout_unset_pop(self): - try: - now = datetime.datetime.utcnow() - timeutils.set_time_override(now) + now = time.time() + with mock.patch('time.time') as time_mock: + time_mock.return_value = now self.client.set('foo', 'bar', ttl=3) self.client.set('fooo', 'bar', ttl=4) self.client.set('foooo', 'bar', ttl=5) self.client.set('fooooo', 'bar', ttl=6) - timeutils.set_time_override(now + datetime.timedelta(seconds=1)) + time_mock.return_value = now + 1 self.assertEqual(self.client.get('foo'), 'bar') self.assertEqual(self.client.get('fooo'), 'bar') self.assertEqual(self.client.get('foooo'), 'bar') self.assertEqual(self.client.get('fooooo'), 'bar') - timeutils.set_time_override(now + datetime.timedelta(seconds=4)) + time_mock.return_value = now + 4 # NOTE(flaper87): Let unset delete foooo and timeout # expire foo and fooo. @@ -85,5 +82,3 @@ class MemorycacheTest(base.CacheBaseTest): self.assertIsNone(self.client.get('fooo')) self.assertIsNone(self.client.get('foooo')) self.assertEqual(self.client.get('fooooo'), 'bar') - finally: - timeutils.clear_time_override() diff --git a/tests/unit/test_loopingcall.py b/tests/unit/test_loopingcall.py index 2895bbba..41cdc7d0 100644 --- a/tests/unit/test_loopingcall.py +++ b/tests/unit/test_loopingcall.py @@ -62,22 +62,23 @@ class LoopingCallTestCase(test.BaseTestCase): now = datetime.datetime.utcnow() second = datetime.timedelta(seconds=1) smidgen = datetime.timedelta(microseconds=10000) - timeoverrides = [now, now + second - smidgen, - now, now + second + second, - now, now + second + smidgen] m = mox.Mox() m.StubOutWithMock(greenthread, 'sleep') greenthread.sleep(mox.IsAlmost(0.02)) greenthread.sleep(mox.IsAlmost(0.0)) greenthread.sleep(mox.IsAlmost(0.0)) + m.StubOutWithMock(timeutils, 'utcnow') + timeutils.utcnow().AndReturn(now) + timeutils.utcnow().AndReturn(now + second - smidgen) + timeutils.utcnow().AndReturn(now) + timeutils.utcnow().AndReturn(now + second + second) + timeutils.utcnow().AndReturn(now) + timeutils.utcnow().AndReturn(now + second + smidgen) + timeutils.utcnow().AndReturn(now) m.ReplayAll() - try: - timeutils.set_time_override(timeoverrides) - timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero) - timer.start(interval=1.01).wait() - finally: - timeutils.clear_time_override() - m.UnsetStubs() - m.VerifyAll() + timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero) + timer.start(interval=1.01).wait() + m.UnsetStubs() + m.VerifyAll() diff --git a/tests/unit/test_memorycache.py b/tests/unit/test_memorycache.py index a216d42a..054853e1 100644 --- a/tests/unit/test_memorycache.py +++ b/tests/unit/test_memorycache.py @@ -13,11 +13,12 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime +import time + +import mock from openstack.common import memorycache from openstack.common import test -from openstack.common import timeutils class MemorycacheTest(test.BaseTestCase): @@ -44,13 +45,11 @@ class MemorycacheTest(test.BaseTestCase): self.assertIsNone(self.client.get('foo')) def test_timeout(self): - try: - now = datetime.datetime.utcnow() - timeutils.set_time_override(now) + now = time.time() + with mock.patch('time.time') as time_mock: + time_mock.return_value = now self.client.set('foo', 'bar', time=3) - timeutils.set_time_override(now + datetime.timedelta(seconds=1)) + time_mock.return_value = now + 1 self.assertEqual(self.client.get('foo'), 'bar') - timeutils.set_time_override(now + datetime.timedelta(seconds=3)) + time_mock.return_value = now + 3 self.assertIsNone(self.client.get('foo')) - finally: - timeutils.clear_time_override() |