Capture timing information for failed atomic actions

If an atomic action throws an exception, it will still capture timing
information; the choice of whether or not to display that information
can be left to reports. Also adds unit tests to ensure this behavior.

Change-Id: I12e96b8ac6c40904f46022a6633714d5c9b1dea1
This commit is contained in:
Chris St. Pierre 2015-10-15 14:14:14 -05:00
parent 177f4f2ef2
commit 32c481ee5f
2 changed files with 35 additions and 2 deletions

View File

@ -67,8 +67,7 @@ class ActionTimer(utils.Timer):
def __exit__(self, type_, value, tb):
super(ActionTimer, self).__exit__(type_, value, tb)
if type_ is None:
self.instance._atomic_actions[self.name] = self.duration()
self.instance._atomic_actions[self.name] = self.duration()
def action_timer(name):

View File

@ -42,6 +42,23 @@ class AtomicActionTestCase(test.TestCase):
self.assertEqual(costilius.OrderedDict(expected),
inst.atomic_actions())
@mock.patch("time.time", side_effect=[1, 3])
def test_action_timer_context_with_exception(self, mock_time):
inst = atomic.ActionTimerMixin()
class TestException(Exception):
pass
try:
with atomic.ActionTimer(inst, "test"):
raise TestException("test")
except TestException:
pass
expected = [("test", 2)]
self.assertEqual(costilius.OrderedDict(expected),
inst.atomic_actions())
@mock.patch("time.time", side_effect=[1, 3])
def test_action_timer_decorator(self, mock_time):
@ -56,6 +73,23 @@ class AtomicActionTestCase(test.TestCase):
self.assertEqual(costilius.OrderedDict({"some": 2}),
inst.atomic_actions())
@mock.patch("time.time", side_effect=[1, 3])
def test_action_timer_decorator_with_exception(self, mock_time):
class TestException(Exception):
pass
class TestTimer(atomic.ActionTimerMixin):
@atomic.action_timer("test")
def some_func(self):
raise TestException("test")
inst = TestTimer()
self.assertRaises(TestException, inst.some_func)
self.assertEqual(costilius.OrderedDict({"test": 2}),
inst.atomic_actions())
@mock.patch("time.time", side_effect=[1, 3, 1, 3])
def test_optional_action_timer_decorator(self, mock_time):