From 44132d4344902f98007e6e58ea3bee56c701b400 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Wed, 7 Jan 2015 15:49:54 +0100 Subject: [PATCH] rabbit: fix timeout timer when duration is None When the duration of the timeout timer used in the rabbit driver.is None and we want that the timer return a maximum of N secs it return None (infinite) instead of N. This change fixes that. Closes-bug: #1408370 Change-Id: I7f4cb3075f776c63aa7dc497173677f92b68c16d --- oslo/messaging/_drivers/common.py | 4 ++-- tests/test_utils.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/oslo/messaging/_drivers/common.py b/oslo/messaging/_drivers/common.py index 0923fe30d..9a4f69617 100644 --- a/oslo/messaging/_drivers/common.py +++ b/oslo/messaging/_drivers/common.py @@ -343,13 +343,13 @@ class DecayingTimer(object): return self def check_return(self, timeout_callback, *args, **kwargs): + maximum = kwargs.pop('maximum', None) if self._duration is None: - return None + return None if maximum is None else maximum if self._ends_at is None: raise RuntimeError("Can not check/return a timeout from a timer" " that has not been started") - maximum = kwargs.pop('maximum', None) left = self._ends_at - time.time() if left <= 0: timeout_callback(*args, **kwargs) diff --git a/tests/test_utils.py b/tests/test_utils.py index 13210097d..38ee1d0ca 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +from oslo.messaging._drivers import common from oslo.messaging import _utils as utils from tests import utils as test_utils @@ -47,3 +48,17 @@ class VersionIsCompatibleTestCase(test_utils.BaseTestCase): def test_version_is_compatible_no_rev_is_zero(self): self.assertTrue(utils.version_is_compatible('1.23.0', '1.23')) + + +class TimerTestCase(test_utils.BaseTestCase): + def test_duration_is_none(self): + t = common.DecayingTimer() + t.start() + remaining = t.check_return(None) + self.assertEqual(None, remaining) + + def test_duration_is_none_and_maximun_set(self): + t = common.DecayingTimer() + t.start() + remaining = t.check_return(None, maximum=2) + self.assertEqual(2, remaining)