From: Zack Cerza Date: Fri, 7 Mar 2014 20:02:33 +0000 (-0600) Subject: Add optional 'action' parameter to safe_while X-Git-Tag: 1.1.0~1617 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=73f5af2f6ab53327ab817ff2e514f1a2db666ee2;p=teuthology.git Add optional 'action' parameter to safe_while This is to make it easier to see what actually timed out when scanning error logs Signed-off-by: Zack Cerza --- diff --git a/teuthology/contextutil.py b/teuthology/contextutil.py index 39626dc840..55fe02e996 100644 --- a/teuthology/contextutil.py +++ b/teuthology/contextutil.py @@ -76,14 +76,18 @@ class safe_while(object): Setting the increment value will cause the sleep time to increase by that value at each step. + You may also optionally pass in an "action" string to be used in the raised + exception's error message to aid in log readability. """ - def __init__(self, sleep=6, increment=0, tries=10, _sleeper=None): + def __init__(self, sleep=6, increment=0, tries=10, action=None, + _sleeper=None): self.sleep = sleep self.increment = increment self.tries = tries self.counter = 0 self.sleep_current = sleep + self.action = action self.sleeper = _sleeper or time.sleep def _make_error_msg(self): @@ -97,9 +101,17 @@ class safe_while(object): self.tries ) ) - return 'reached maximum tries (%s) after waiting for %s seconds' % ( - self.tries, total_seconds_waiting + msg = 'reached maximum tries ({tries})' + \ + 'after waiting for {total} seconds' + if self.action: + msg = "'{action}'" + msg + + msg = msg.format( + action=self.action, + tries=self.tries, + total=total_seconds_waiting, ) + return msg def __call__(self): self.counter += 1 diff --git a/teuthology/test/test_contextutil.py b/teuthology/test/test_contextutil.py index d9bb08f99d..ccef3dcc31 100644 --- a/teuthology/test/test_contextutil.py +++ b/teuthology/test/test_contextutil.py @@ -49,3 +49,15 @@ class TestSafeWhile(object): msg = error.value[0] assert 'waiting for 105 seconds' in msg + + def test_action(self): + with raises(contextutil.MaxWhileTries) as error: + with self.s_while( + action='doing the thing', + _sleeper=self.fake_sleep + ) as bomb: + while 1: + bomb() + + msg = error.value[0] + assert "'doing the thing'" in msg