]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Add optional 'action' parameter to safe_while
authorZack Cerza <zack@cerza.org>
Fri, 7 Mar 2014 20:02:33 +0000 (14:02 -0600)
committerZack Cerza <zack@cerza.org>
Fri, 7 Mar 2014 20:02:33 +0000 (14:02 -0600)
This is to make it easier to see what actually timed out when scanning
error logs

Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
teuthology/contextutil.py
teuthology/test/test_contextutil.py

index 39626dc840be9e42deef11ef3c721f0115c853d2..55fe02e996cf7e65e3d657f1d625644d30cd4eea 100644 (file)
@@ -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
index d9bb08f99df82057bc43279cb0aff850d2eb0eb7..ccef3dcc31a3096c561715d272d4b486ad06a4ff 100644 (file)
@@ -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