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>
This commit is contained in:
Stephen Finucane 2019-12-16 15:43:02 +00:00 committed by Stephen Finucane
parent 291d45065a
commit c8918e0c0e
2 changed files with 0 additions and 141 deletions

View File

@ -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):

View File

@ -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: