Use the oslo_utils stop watch in decaying timer
The decaying timer can now just use the functionality of the oslo_utils stop watch to avoid having to maintain similar information itself; now it just becomes a thin layer ontop of that object that provides its functionality. Change-Id: I1b014b821a6b980590ca5b4d850a515d55c42208
This commit is contained in:
@@ -18,10 +18,10 @@
|
|||||||
import copy
|
import copy
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import time
|
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from oslo_serialization import jsonutils
|
from oslo_serialization import jsonutils
|
||||||
|
from oslo_utils import timeutils
|
||||||
import six
|
import six
|
||||||
|
|
||||||
import oslo_messaging
|
import oslo_messaging
|
||||||
@@ -334,24 +334,16 @@ def deserialize_msg(msg):
|
|||||||
|
|
||||||
class DecayingTimer(object):
|
class DecayingTimer(object):
|
||||||
def __init__(self, duration=None):
|
def __init__(self, duration=None):
|
||||||
self._duration = duration
|
self._watch = timeutils.StopWatch(duration=duration)
|
||||||
self._ends_at = None
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
if self._duration is not None:
|
self._watch.start()
|
||||||
self._ends_at = time.time() + max(0, self._duration)
|
|
||||||
|
|
||||||
def check_return(self, timeout_callback=None, *args, **kwargs):
|
def check_return(self, timeout_callback=None, *args, **kwargs):
|
||||||
maximum = kwargs.pop('maximum', None)
|
maximum = kwargs.pop('maximum', None)
|
||||||
|
left = self._watch.leftover(return_none=True)
|
||||||
if self._duration is None:
|
if left is None:
|
||||||
return maximum
|
return maximum
|
||||||
if self._ends_at is None:
|
|
||||||
raise RuntimeError(_("Can not check/return a timeout from a timer"
|
|
||||||
" that has not been started."))
|
|
||||||
|
|
||||||
left = self._ends_at - time.time()
|
|
||||||
if left <= 0 and timeout_callback is not None:
|
if left <= 0 and timeout_callback is not None:
|
||||||
timeout_callback(*args, **kwargs)
|
timeout_callback(*args, **kwargs)
|
||||||
|
|
||||||
return left if maximum is None else min(left, maximum)
|
return left if maximum is None else min(left, maximum)
|
||||||
|
|||||||
@@ -13,8 +13,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
from oslo_messaging._drivers import common
|
from oslo_messaging._drivers import common
|
||||||
@@ -67,16 +65,24 @@ class TimerTestCase(test_utils.BaseTestCase):
|
|||||||
remaining = t.check_return(maximum=2)
|
remaining = t.check_return(maximum=2)
|
||||||
self.assertEqual(2, remaining)
|
self.assertEqual(2, remaining)
|
||||||
|
|
||||||
def test_duration_expired_no_callback(self):
|
@mock.patch('oslo_utils.timeutils.now')
|
||||||
|
def test_duration_expired_no_callback(self, now):
|
||||||
|
now.return_value = 0
|
||||||
t = common.DecayingTimer(2)
|
t = common.DecayingTimer(2)
|
||||||
t._ends_at = time.time() - 10
|
t.start()
|
||||||
remaining = t.check_return()
|
|
||||||
self.assertAlmostEqual(-10, remaining, 0)
|
|
||||||
|
|
||||||
def test_duration_callback(self):
|
now.return_value = 3
|
||||||
|
remaining = t.check_return()
|
||||||
|
self.assertEqual(0, remaining)
|
||||||
|
|
||||||
|
@mock.patch('oslo_utils.timeutils.now')
|
||||||
|
def test_duration_callback(self, now):
|
||||||
|
now.return_value = 0
|
||||||
t = common.DecayingTimer(2)
|
t = common.DecayingTimer(2)
|
||||||
t._ends_at = time.time() - 10
|
t.start()
|
||||||
|
|
||||||
|
now.return_value = 3
|
||||||
callback = mock.Mock()
|
callback = mock.Mock()
|
||||||
remaining = t.check_return(callback)
|
remaining = t.check_return(callback)
|
||||||
self.assertAlmostEqual(-10, remaining, 0)
|
self.assertEqual(0, remaining)
|
||||||
callback.assert_called_once
|
callback.assert_called_once
|
||||||
|
|||||||
Reference in New Issue
Block a user