]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Update safe_while's suggested usage pattern
authorZack Cerza <zack@cerza.org>
Sat, 8 Mar 2014 21:19:31 +0000 (15:19 -0600)
committerZack Cerza <zack@cerza.org>
Sat, 8 Mar 2014 21:19:31 +0000 (15:19 -0600)
I didn't love the way safe_while was encouraged to be used and it didn't
fit right with the new no-raising behavior. Now it's encouraged to be
used like this:

with safe_while() as proceed:
    while proceed():
        do_things()

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

index 4e46c9ef73c4ef23b53ee196a18fb53a54c756ed..29958cebd56cc79980e3a6df85d7eeeae9e4aa33 100644 (file)
@@ -58,10 +58,10 @@ class safe_while(object):
     The most simple example possible will try 10 times sleeping for 6 seconds:
 
         >>> from teuthology.contexutil import safe_while
-        >>> with safe_while() as bomb:
-        ...    while 1:
-        ...        bomb()
+        >>> with safe_while() as proceed:
+        ...    while proceed():
         ...        # repetitive code here
+        ...        print "hello world"
         ...
         Traceback (most recent call last):
         ...
@@ -125,8 +125,10 @@ class safe_while(object):
                 raise MaxWhileTries(error_msg)
             else:
                 log.warning(error_msg)
+                return False
         self.sleeper(self.sleep_current)
         self.sleep_current += self.increment
+        return True
 
     def __enter__(self):
         return self
index 5cf520fe83bad5f4182bded6c8308417acb33e40..c075e56d6dd51b858fbd84d3a5689496383d6a33 100644 (file)
@@ -821,9 +821,8 @@ def wait_until_healthy(ctx, remote):
     Wait until a Ceph cluster is healthy. Give up after 15min.
     """
     testdir = get_testdir(ctx)
-    with safe_while(tries=(900 / 6), action="wait_until_healthy") as timeout:
-        while True:  # finite
-            timeout()
+    with safe_while(tries=(900 / 6), action="wait_until_healthy") as proceed:
+        while proceed():
             r = remote.run(
                 args=[
                     'adjust-ulimits',
index ccef3dcc31a3096c561715d272d4b486ad06a4ff..2465459cd0fcbe6dca2a725c66695f5bc9447bc3 100644 (file)
@@ -1,27 +1,29 @@
 from pytest import raises
 from teuthology import contextutil
+from logging import ERROR
 
 
 class TestSafeWhile(object):
 
     def setup(self):
+        contextutil.log.setLevel(ERROR)
         self.fake_sleep = lambda s: True
         self.s_while = contextutil.safe_while
 
     def test_6_5_10_deal(self):
         with raises(contextutil.MaxWhileTries):
-            with self.s_while(_sleeper=self.fake_sleep) as bomb:
-                while 1:
-                    bomb()
+            with self.s_while(_sleeper=self.fake_sleep) as proceed:
+                while proceed():
+                    pass
 
     def test_6_0_1_deal(self):
         with raises(contextutil.MaxWhileTries) as error:
             with self.s_while(
                 tries=1,
                 _sleeper=self.fake_sleep
-            ) as bomb:
-                while 1:
-                    bomb()
+            ) as proceed:
+                while proceed():
+                    pass
 
         msg = error.value[0]
         assert 'waiting for 6 seconds' in msg
@@ -31,9 +33,9 @@ class TestSafeWhile(object):
             with self.s_while(
                 sleep=1,
                 _sleeper=self.fake_sleep
-            ) as bomb:
-                while 1:
-                    bomb()
+            ) as proceed:
+                while proceed():
+                    pass
 
         msg = error.value[0]
         assert 'waiting for 10 seconds' in msg
@@ -43,9 +45,9 @@ class TestSafeWhile(object):
             with self.s_while(
                 increment=1,
                 _sleeper=self.fake_sleep
-            ) as bomb:
-                while 1:
-                    bomb()
+            ) as proceed:
+                while proceed():
+                    pass
 
         msg = error.value[0]
         assert 'waiting for 105 seconds' in msg
@@ -55,9 +57,16 @@ class TestSafeWhile(object):
             with self.s_while(
                 action='doing the thing',
                 _sleeper=self.fake_sleep
-            ) as bomb:
-                while 1:
-                    bomb()
+            ) as proceed:
+                while proceed():
+                    pass
 
         msg = error.value[0]
         assert "'doing the thing'" in msg
+
+    def test_no_raise(self):
+        with self.s_while(_raise=False, _sleeper=self.fake_sleep) as proceed:
+            while proceed():
+                pass
+
+        assert True