testutil.PollMixin: use a custom exception (and convert it) to avoid the ugly 'stash...
authorBrian Warner <warner@allmydata.com>
Wed, 3 Sep 2008 03:32:51 +0000 (20:32 -0700)
committerBrian Warner <warner@allmydata.com>
Wed, 3 Sep 2008 03:32:51 +0000 (20:32 -0700)
src/allmydata/util/testutil.py

index ec6afdc18134bc45ed4ad62361187104a6ee869b..c43b1219dfef6b25677f627e80cbeb7ec36b7099 100644 (file)
@@ -41,6 +41,9 @@ class SignalMixin:
 class TimeoutError(Exception):
     pass
 
+class PollComplete(Exception):
+    pass
+
 class PollMixin:
 
     def poll(self, check_f, pollinterval=0.01, timeout=None):
@@ -48,22 +51,24 @@ class PollMixin:
         # True, at which point the Deferred will fire.. If check_f raises an
         # exception, the Deferred will errback. If the check_f does not
         # indicate success within timeout= seconds, the Deferred will
-        # errback. If timeout=None, no timeout will be enforced.
+        # errback. If timeout=None, no timeout will be enforced, and the loop
+        # will poll forever (or really until Trial times out).
         cutoff = None
         if timeout is not None:
             cutoff = time.time() + timeout
-        stash = [] # ick. We have to pass the LoopingCall into itself
-        lc = task.LoopingCall(self._poll, check_f, stash, cutoff)
-        stash.append(lc)
+        lc = task.LoopingCall(self._poll, check_f, cutoff)
         d = lc.start(pollinterval)
+        def _convert_done(f):
+            f.trap(PollComplete)
+            return None
+        d.addErrback(_convert_done)
         return d
 
-    def _poll(self, check_f, stash, cutoff):
+    def _poll(self, check_f, cutoff):
         if cutoff is not None and time.time() > cutoff:
             raise TimeoutError()
-        lc = stash[0]
         if check_f():
-            lc.stop()
+            raise PollComplete()
 
 class StallMixin:
     def stall(self, res=None, delay=1):