summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Nemec <bnemec@redhat.com>2018-11-01 16:37:23 +0000
committerBen Nemec <bnemec@redhat.com>2019-01-10 22:06:11 +0000
commit4c0d4490e8dcac1f02d8b94c6937b3e258b5d594 (patch)
tree7d47eae7c46d98db714491217e6ada4c52ab3667
parentd987a4a84c40ac905a1295341c0e05a73c7b6b1c (diff)
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
Notes
Notes (review): Code-Review+2: Zane Bitter <zbitter@redhat.com> Code-Review+2: Moisés Guimarães <moguimar@redhat.com> Workflow+1: Moisés Guimarães <moguimar@redhat.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Tue, 15 Jan 2019 09:18:29 +0000 Reviewed-on: https://review.openstack.org/614809 Project: openstack/oslo.service Branch: refs/heads/master
-rw-r--r--lower-constraints.txt2
-rw-r--r--oslo_service/fixture.py2
-rw-r--r--oslo_service/loopingcall.py37
-rw-r--r--oslo_service/tests/test_loopingcall.py18
-rw-r--r--requirements.txt2
5 files changed, 18 insertions, 43 deletions
diff --git a/lower-constraints.txt b/lower-constraints.txt
index 9f72e90..4757020 100644
--- a/lower-constraints.txt
+++ b/lower-constraints.txt
@@ -38,7 +38,7 @@ oslo.context==2.19.2
38oslo.i18n==3.15.3 38oslo.i18n==3.15.3
39oslo.log==3.36.0 39oslo.log==3.36.0
40oslo.serialization==2.18.0 40oslo.serialization==2.18.0
41oslo.utils==3.33.0 41oslo.utils==3.38.0
42oslotest==3.2.0 42oslotest==3.2.0
43Paste==2.0.2 43Paste==2.0.2
44PasteDeploy==1.5.0 44PasteDeploy==1.5.0
diff --git a/oslo_service/fixture.py b/oslo_service/fixture.py
index 056eef6..87d559c 100644
--- a/oslo_service/fixture.py
+++ b/oslo_service/fixture.py
@@ -49,4 +49,4 @@ class SleepFixture(fixtures.Fixture):
49 def _setUp(self): 49 def _setUp(self):
50 # Provide access to the mock so that calls to it can be asserted 50 # Provide access to the mock so that calls to it can be asserted
51 self.mock_wait = self.useFixture(fixtures.MockPatch( 51 self.mock_wait = self.useFixture(fixtures.MockPatch(
52 'oslo_service.loopingcall._Event.wait')).mock 52 'oslo_utils.eventletutils.EventletEvent.wait')).mock
diff --git a/oslo_service/loopingcall.py b/oslo_service/loopingcall.py
index dc95804..4ea8f64 100644
--- a/oslo_service/loopingcall.py
+++ b/oslo_service/loopingcall.py
@@ -21,8 +21,8 @@ import time
21 21
22from eventlet import event 22from eventlet import event
23from eventlet import greenthread 23from eventlet import greenthread
24from eventlet import timeout as eventlettimeout
25from oslo_log import log as logging 24from oslo_log import log as logging
25from oslo_utils import eventletutils
26from oslo_utils import excutils 26from oslo_utils import excutils
27from oslo_utils import reflection 27from oslo_utils import reflection
28from oslo_utils import timeutils 28from oslo_utils import timeutils
@@ -76,32 +76,6 @@ def _safe_wrapper(f, kind, func_name):
76 return func 76 return func
77 77
78 78
79class _Event(object):
80 """A class that provides consistent eventlet/threading Event API.
81
82 It's a copy from the oslo_utils repository, as we need the private version
83 because we don't want to use the threading.Event version.
84 """
85 def __init__(self, *args, **kwargs):
86 self.clear()
87
88 def clear(self):
89 self._set = False
90 self._event = event.Event()
91
92 def is_set(self):
93 return self._set
94
95 def set(self):
96 self._set = True
97 self._event.send(True)
98
99 def wait(self, timeout=None):
100 with eventlettimeout.Timeout(timeout, False):
101 self._event.wait()
102 return self.is_set()
103
104
105class LoopingCallBase(object): 79class LoopingCallBase(object):
106 _KIND = _("Unknown looping call") 80 _KIND = _("Unknown looping call")
107 81
@@ -114,7 +88,7 @@ class LoopingCallBase(object):
114 self.f = f 88 self.f = f
115 self._thread = None 89 self._thread = None
116 self.done = None 90 self.done = None
117 self._abort = _Event() 91 self._abort = eventletutils.EventletEvent()
118 92
119 @property 93 @property
120 def _running(self): 94 def _running(self):
@@ -156,6 +130,11 @@ class LoopingCallBase(object):
156 self._thread.link(self._on_done) 130 self._thread.link(self._on_done)
157 return self.done 131 return self.done
158 132
133 # NOTE(bnemec): This is just a wrapper function we can mock so we aren't
134 # affected by other users of the StopWatch class.
135 def _elapsed(self, watch):
136 return watch.elapsed()
137
159 def _run_loop(self, idle_for_func, 138 def _run_loop(self, idle_for_func,
160 initial_delay=None, stop_on_exception=True): 139 initial_delay=None, stop_on_exception=True):
161 kind = self._KIND 140 kind = self._KIND
@@ -172,7 +151,7 @@ class LoopingCallBase(object):
172 watch.stop() 151 watch.stop()
173 if not self._running: 152 if not self._running:
174 break 153 break
175 idle = idle_for_func(result, watch.elapsed()) 154 idle = idle_for_func(result, self._elapsed(watch))
176 LOG.trace('%(kind)s %(func_name)r sleeping ' 155 LOG.trace('%(kind)s %(func_name)r sleeping '
177 'for %(idle).02f seconds', 156 'for %(idle).02f seconds',
178 {'func_name': func_name, 'idle': idle, 157 {'func_name': func_name, 'idle': idle,
diff --git a/oslo_service/tests/test_loopingcall.py b/oslo_service/tests/test_loopingcall.py
index bf55a16..4c1642f 100644
--- a/oslo_service/tests/test_loopingcall.py
+++ b/oslo_service/tests/test_loopingcall.py
@@ -122,23 +122,19 @@ class LoopingCallTestCase(test_base.BaseTestCase):
122 def assertAlmostEqual(self, expected, actual, precision=7, message=None): 122 def assertAlmostEqual(self, expected, actual, precision=7, message=None):
123 self.assertEqual(0, round(actual - expected, precision), message) 123 self.assertEqual(0, round(actual - expected, precision), message)
124 124
125 @mock.patch('eventlet.greenthread.sleep') 125 @mock.patch('oslo_service.loopingcall.LoopingCallBase._sleep')
126 @mock.patch('oslo_utils.timeutils.now') 126 @mock.patch('oslo_service.loopingcall.LoopingCallBase._elapsed')
127 def test_interval_adjustment(self, time_mock, sleep_mock): 127 def test_interval_adjustment(self, elapsed_mock, sleep_mock):
128 """Ensure the interval is adjusted to account for task duration.""" 128 """Ensure the interval is adjusted to account for task duration."""
129 self.num_runs = 3 129 self.num_runs = 3
130 130
131 now = 1234567890
132 second = 1 131 second = 1
133 smidgen = 0.01 132 smidgen = 0.01
134 133
135 time_mock.side_effect = [now, # restart 134 elapsed_mock.side_effect = [second - smidgen,
136 now + second - smidgen, # end 135 second + second,
137 now, # restart 136 second + smidgen,
138 now + second + second, # end 137 ]
139 now, # restart
140 now + second + smidgen, # end
141 now] # restart
142 timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero) 138 timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero)
143 timer.start(interval=1.01).wait() 139 timer.start(interval=1.01).wait()
144 140
diff --git a/requirements.txt b/requirements.txt
index ecca359..450a853 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -8,7 +8,7 @@ eventlet!=0.18.3,!=0.20.1,>=0.18.2 # MIT
8fixtures>=3.0.0 # Apache-2.0/BSD 8fixtures>=3.0.0 # Apache-2.0/BSD
9greenlet>=0.4.10 # MIT 9greenlet>=0.4.10 # MIT
10monotonic>=0.6;python_version<'3.3' # Apache-2.0 10monotonic>=0.6;python_version<'3.3' # Apache-2.0
11oslo.utils>=3.33.0 # Apache-2.0 11oslo.utils>=3.38.0 # Apache-2.0
12oslo.concurrency>=3.25.0 # Apache-2.0 12oslo.concurrency>=3.25.0 # Apache-2.0
13oslo.config>=5.1.0 # Apache-2.0 13oslo.config>=5.1.0 # Apache-2.0
14oslo.log>=3.36.0 # Apache-2.0 14oslo.log>=3.36.0 # Apache-2.0