summaryrefslogtreecommitdiff
path: root/nova
diff options
context:
space:
mode:
authorStephen Finucane <sfinucan@redhat.com>2019-12-16 15:43:02 +0000
committerStephen Finucane <stephenfin@redhat.com>2020-02-06 15:49:07 +0000
commitc8918e0c0eaf49e7e63338b249acaac6b2ef2f29 (patch)
treeb894ba1fcb1044725f5a882d88e5abad34faae11 /nova
parent291d45065a940c94a2f5866beb6e9915086d249f (diff)
downloadnova-c8918e0c0eaf49e7e63338b249acaac6b2ef2f29.tar.gz
trivial: Remove 'run_once' helper
This should have been removed when we removed the placement code in change I4181f39dea7eb10b84e6f5057938767b3e422aff. Change-Id: If5808075d853341bf274f35b7fcf0e0712f8f77a Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
Diffstat (limited to 'nova')
-rw-r--r--nova/tests/unit/test_utils.py98
-rw-r--r--nova/utils.py43
2 files changed, 0 insertions, 141 deletions
diff --git a/nova/tests/unit/test_utils.py b/nova/tests/unit/test_utils.py
index b3e10e856a..23a080f6a2 100644
--- a/nova/tests/unit/test_utils.py
+++ b/nova/tests/unit/test_utils.py
@@ -1045,104 +1045,6 @@ class GetEndpointTestCase(test.NoDBTestCase):
self.adap.get_endpoint.assert_called_once_with()
-class RunOnceTests(test.NoDBTestCase):
-
- fake_logger = mock.MagicMock()
-
- @utils.run_once("already ran once", fake_logger)
- def dummy_test_func(self, fail=False):
- if fail:
- raise ValueError()
- return True
-
- def setUp(self):
- super(RunOnceTests, self).setUp()
- self.dummy_test_func.reset()
- RunOnceTests.fake_logger.reset_mock()
-
- def test_wrapped_funtions_called_once(self):
- self.assertFalse(self.dummy_test_func.called)
- result = self.dummy_test_func()
- self.assertTrue(result)
- self.assertTrue(self.dummy_test_func.called)
-
- # assert that on second invocation no result
- # is returned and that the logger is invoked.
- result = self.dummy_test_func()
- RunOnceTests.fake_logger.assert_called_once()
- self.assertIsNone(result)
-
- def test_wrapped_funtions_called_once_raises(self):
- self.assertFalse(self.dummy_test_func.called)
- self.assertRaises(ValueError, self.dummy_test_func, fail=True)
- self.assertTrue(self.dummy_test_func.called)
-
- # assert that on second invocation no result
- # is returned and that the logger is invoked.
- result = self.dummy_test_func()
- RunOnceTests.fake_logger.assert_called_once()
- self.assertIsNone(result)
-
- def test_wrapped_funtions_can_be_reset(self):
- # assert we start with a clean state
- self.assertFalse(self.dummy_test_func.called)
- result = self.dummy_test_func()
- self.assertTrue(result)
-
- self.dummy_test_func.reset()
- # assert we restored a clean state
- self.assertFalse(self.dummy_test_func.called)
- result = self.dummy_test_func()
- self.assertTrue(result)
-
- # assert that we never called the logger
- RunOnceTests.fake_logger.assert_not_called()
-
- def test_reset_calls_cleanup(self):
- mock_clean = mock.Mock()
-
- @utils.run_once("already ran once", self.fake_logger,
- cleanup=mock_clean)
- def f():
- pass
-
- f()
- self.assertTrue(f.called)
-
- f.reset()
- self.assertFalse(f.called)
- mock_clean.assert_called_once_with()
-
- def test_clean_is_not_called_at_reset_if_wrapped_not_called(self):
- mock_clean = mock.Mock()
-
- @utils.run_once("already ran once", self.fake_logger,
- cleanup=mock_clean)
- def f():
- pass
-
- self.assertFalse(f.called)
-
- f.reset()
- self.assertFalse(f.called)
- self.assertFalse(mock_clean.called)
-
- def test_reset_works_even_if_cleanup_raises(self):
- mock_clean = mock.Mock(side_effect=ValueError())
-
- @utils.run_once("already ran once", self.fake_logger,
- cleanup=mock_clean)
- def f():
- pass
-
- f()
- self.assertTrue(f.called)
-
- self.assertRaises(ValueError, f.reset)
- self.assertFalse(f.called)
- mock_clean.assert_called_once_with()
-
-
class TestResourceClassNormalize(test.NoDBTestCase):
def test_normalize_name(self):
diff --git a/nova/utils.py b/nova/utils.py
index 914d5516ff..50222092d2 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -1079,49 +1079,6 @@ else:
yield [stack.enter_context(c) for c in contexts]
-def run_once(message, logger, cleanup=None):
- """This is a utility function decorator to ensure a function
- is run once and only once in an interpreter instance.
- The decorated function object can be reset by calling its
- reset function. All exceptions raised by the wrapped function,
- logger and cleanup function will be propagated to the caller.
- """
- def outer_wrapper(func):
- @functools.wraps(func)
- def wrapper(*args, **kwargs):
- if not wrapper.called:
- # Note(sean-k-mooney): the called state is always
- # updated even if the wrapped function completes
- # by raising an exception. If the caller catches
- # the exception it is their responsibility to call
- # reset if they want to re-execute the wrapped function.
- try:
- return func(*args, **kwargs)
- finally:
- wrapper.called = True
- else:
- logger(message)
-
- wrapper.called = False
-
- def reset(wrapper, *args, **kwargs):
- # Note(sean-k-mooney): we conditionally call the
- # cleanup function if one is provided only when the
- # wrapped function has been called previously. We catch
- # and reraise any exception that may be raised and update
- # the called state in a finally block to ensure its
- # always updated if reset is called.
- try:
- if cleanup and wrapper.called:
- return cleanup(*args, **kwargs)
- finally:
- wrapper.called = False
-
- wrapper.reset = functools.partial(reset, wrapper)
- return wrapper
- return outer_wrapper
-
-
def normalize_rc_name(rc_name):
"""Normalize a resource class name to standard form."""
if rc_name is None: