Browse Source

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
Ben Nemec 5 months ago
parent
commit
4c0d4490e8

+ 1
- 1
lower-constraints.txt View File

@@ -38,7 +38,7 @@ oslo.context==2.19.2
38 38
 oslo.i18n==3.15.3
39 39
 oslo.log==3.36.0
40 40
 oslo.serialization==2.18.0
41
-oslo.utils==3.33.0
41
+oslo.utils==3.38.0
42 42
 oslotest==3.2.0
43 43
 Paste==2.0.2
44 44
 PasteDeploy==1.5.0

+ 1
- 1
oslo_service/fixture.py View File

@@ -49,4 +49,4 @@ class SleepFixture(fixtures.Fixture):
49 49
     def _setUp(self):
50 50
         # Provide access to the mock so that calls to it can be asserted
51 51
         self.mock_wait = self.useFixture(fixtures.MockPatch(
52
-            'oslo_service.loopingcall._Event.wait')).mock
52
+            'oslo_utils.eventletutils.EventletEvent.wait')).mock

+ 8
- 29
oslo_service/loopingcall.py View File

@@ -21,8 +21,8 @@ import time
21 21
 
22 22
 from eventlet import event
23 23
 from eventlet import greenthread
24
-from eventlet import timeout as eventlettimeout
25 24
 from oslo_log import log as logging
25
+from oslo_utils import eventletutils
26 26
 from oslo_utils import excutils
27 27
 from oslo_utils import reflection
28 28
 from oslo_utils import timeutils
@@ -76,32 +76,6 @@ def _safe_wrapper(f, kind, func_name):
76 76
     return func
77 77
 
78 78
 
79
-class _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
-
105 79
 class LoopingCallBase(object):
106 80
     _KIND = _("Unknown looping call")
107 81
 
@@ -114,7 +88,7 @@ class LoopingCallBase(object):
114 88
         self.f = f
115 89
         self._thread = None
116 90
         self.done = None
117
-        self._abort = _Event()
91
+        self._abort = eventletutils.EventletEvent()
118 92
 
119 93
     @property
120 94
     def _running(self):
@@ -156,6 +130,11 @@ class LoopingCallBase(object):
156 130
         self._thread.link(self._on_done)
157 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 138
     def _run_loop(self, idle_for_func,
160 139
                   initial_delay=None, stop_on_exception=True):
161 140
         kind = self._KIND
@@ -172,7 +151,7 @@ class LoopingCallBase(object):
172 151
                 watch.stop()
173 152
                 if not self._running:
174 153
                     break
175
-                idle = idle_for_func(result, watch.elapsed())
154
+                idle = idle_for_func(result, self._elapsed(watch))
176 155
                 LOG.trace('%(kind)s %(func_name)r sleeping '
177 156
                           'for %(idle).02f seconds',
178 157
                           {'func_name': func_name, 'idle': idle,

+ 7
- 11
oslo_service/tests/test_loopingcall.py View File

@@ -122,23 +122,19 @@ class LoopingCallTestCase(test_base.BaseTestCase):
122 122
     def assertAlmostEqual(self, expected, actual, precision=7, message=None):
123 123
         self.assertEqual(0, round(actual - expected, precision), message)
124 124
 
125
-    @mock.patch('eventlet.greenthread.sleep')
126
-    @mock.patch('oslo_utils.timeutils.now')
127
-    def test_interval_adjustment(self, time_mock, sleep_mock):
125
+    @mock.patch('oslo_service.loopingcall.LoopingCallBase._sleep')
126
+    @mock.patch('oslo_service.loopingcall.LoopingCallBase._elapsed')
127
+    def test_interval_adjustment(self, elapsed_mock, sleep_mock):
128 128
         """Ensure the interval is adjusted to account for task duration."""
129 129
         self.num_runs = 3
130 130
 
131
-        now = 1234567890
132 131
         second = 1
133 132
         smidgen = 0.01
134 133
 
135
-        time_mock.side_effect = [now,  # restart
136
-                                 now + second - smidgen,  # end
137
-                                 now,  # restart
138
-                                 now + second + second,  # end
139
-                                 now,  # restart
140
-                                 now + second + smidgen,  # end
141
-                                 now]  # restart
134
+        elapsed_mock.side_effect = [second - smidgen,
135
+                                    second + second,
136
+                                    second + smidgen,
137
+                                    ]
142 138
         timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero)
143 139
         timer.start(interval=1.01).wait()
144 140
 

+ 1
- 1
requirements.txt View File

@@ -8,7 +8,7 @@ eventlet!=0.18.3,!=0.20.1,>=0.18.2 # MIT
8 8
 fixtures>=3.0.0 # Apache-2.0/BSD
9 9
 greenlet>=0.4.10 # MIT
10 10
 monotonic>=0.6;python_version<'3.3'  # Apache-2.0
11
-oslo.utils>=3.33.0 # Apache-2.0
11
+oslo.utils>=3.38.0 # Apache-2.0
12 12
 oslo.concurrency>=3.25.0 # Apache-2.0
13 13
 oslo.config>=5.1.0 # Apache-2.0
14 14
 oslo.log>=3.36.0 # Apache-2.0

Loading…
Cancel
Save