From 4c0d4490e8dcac1f02d8b94c6937b3e258b5d594 Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Thu, 1 Nov 2018 16:37:23 +0000 Subject: [PATCH] Use eventletutils Event class Instead of having a copy-pasted version in this project, let's just use the original directly. It is added to the public API of oslo.utils in the dependency. Depends-On: https://review.openstack.org/614806 Change-Id: If0dfac2505d097c117ef94c99399b1614f1e1f8f --- lower-constraints.txt | 2 +- oslo_service/fixture.py | 2 +- oslo_service/loopingcall.py | 37 ++++++-------------------- oslo_service/tests/test_loopingcall.py | 18 +++++-------- requirements.txt | 2 +- 5 files changed, 18 insertions(+), 43 deletions(-) diff --git a/lower-constraints.txt b/lower-constraints.txt index 9f72e902..47570201 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -38,7 +38,7 @@ oslo.context==2.19.2 oslo.i18n==3.15.3 oslo.log==3.36.0 oslo.serialization==2.18.0 -oslo.utils==3.33.0 +oslo.utils==3.38.0 oslotest==3.2.0 Paste==2.0.2 PasteDeploy==1.5.0 diff --git a/oslo_service/fixture.py b/oslo_service/fixture.py index 056eef60..87d559c0 100644 --- a/oslo_service/fixture.py +++ b/oslo_service/fixture.py @@ -49,4 +49,4 @@ class SleepFixture(fixtures.Fixture): def _setUp(self): # Provide access to the mock so that calls to it can be asserted self.mock_wait = self.useFixture(fixtures.MockPatch( - 'oslo_service.loopingcall._Event.wait')).mock + 'oslo_utils.eventletutils.EventletEvent.wait')).mock diff --git a/oslo_service/loopingcall.py b/oslo_service/loopingcall.py index dc958040..4ea8f644 100644 --- a/oslo_service/loopingcall.py +++ b/oslo_service/loopingcall.py @@ -21,8 +21,8 @@ import time from eventlet import event from eventlet import greenthread -from eventlet import timeout as eventlettimeout from oslo_log import log as logging +from oslo_utils import eventletutils from oslo_utils import excutils from oslo_utils import reflection from oslo_utils import timeutils @@ -76,32 +76,6 @@ def _safe_wrapper(f, kind, func_name): return func -class _Event(object): - """A class that provides consistent eventlet/threading Event API. - - It's a copy from the oslo_utils repository, as we need the private version - because we don't want to use the threading.Event version. - """ - def __init__(self, *args, **kwargs): - self.clear() - - def clear(self): - self._set = False - self._event = event.Event() - - def is_set(self): - return self._set - - def set(self): - self._set = True - self._event.send(True) - - def wait(self, timeout=None): - with eventlettimeout.Timeout(timeout, False): - self._event.wait() - return self.is_set() - - class LoopingCallBase(object): _KIND = _("Unknown looping call") @@ -114,7 +88,7 @@ class LoopingCallBase(object): self.f = f self._thread = None self.done = None - self._abort = _Event() + self._abort = eventletutils.EventletEvent() @property def _running(self): @@ -156,6 +130,11 @@ class LoopingCallBase(object): self._thread.link(self._on_done) return self.done + # NOTE(bnemec): This is just a wrapper function we can mock so we aren't + # affected by other users of the StopWatch class. + def _elapsed(self, watch): + return watch.elapsed() + def _run_loop(self, idle_for_func, initial_delay=None, stop_on_exception=True): kind = self._KIND @@ -172,7 +151,7 @@ class LoopingCallBase(object): watch.stop() if not self._running: break - idle = idle_for_func(result, watch.elapsed()) + idle = idle_for_func(result, self._elapsed(watch)) LOG.trace('%(kind)s %(func_name)r sleeping ' 'for %(idle).02f seconds', {'func_name': func_name, 'idle': idle, diff --git a/oslo_service/tests/test_loopingcall.py b/oslo_service/tests/test_loopingcall.py index bf55a16f..4c1642f5 100644 --- a/oslo_service/tests/test_loopingcall.py +++ b/oslo_service/tests/test_loopingcall.py @@ -122,23 +122,19 @@ class LoopingCallTestCase(test_base.BaseTestCase): def assertAlmostEqual(self, expected, actual, precision=7, message=None): self.assertEqual(0, round(actual - expected, precision), message) - @mock.patch('eventlet.greenthread.sleep') - @mock.patch('oslo_utils.timeutils.now') - def test_interval_adjustment(self, time_mock, sleep_mock): + @mock.patch('oslo_service.loopingcall.LoopingCallBase._sleep') + @mock.patch('oslo_service.loopingcall.LoopingCallBase._elapsed') + def test_interval_adjustment(self, elapsed_mock, sleep_mock): """Ensure the interval is adjusted to account for task duration.""" self.num_runs = 3 - now = 1234567890 second = 1 smidgen = 0.01 - time_mock.side_effect = [now, # restart - now + second - smidgen, # end - now, # restart - now + second + second, # end - now, # restart - now + second + smidgen, # end - now] # restart + elapsed_mock.side_effect = [second - smidgen, + second + second, + second + smidgen, + ] timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero) timer.start(interval=1.01).wait() diff --git a/requirements.txt b/requirements.txt index ecca3594..450a8532 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ eventlet!=0.18.3,!=0.20.1,>=0.18.2 # MIT fixtures>=3.0.0 # Apache-2.0/BSD greenlet>=0.4.10 # MIT monotonic>=0.6;python_version<'3.3' # Apache-2.0 -oslo.utils>=3.33.0 # Apache-2.0 +oslo.utils>=3.38.0 # Apache-2.0 oslo.concurrency>=3.25.0 # Apache-2.0 oslo.config>=5.1.0 # Apache-2.0 oslo.log>=3.36.0 # Apache-2.0