]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/web/operations.py
Change OphandleTable to use a deterministic clock, so we can test it
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / operations.py
index 7fbf8d2701f9e7a540da1140720e64c8d1af4d98..8ee9f202768681212d68363426bca8084043c0d6 100644 (file)
@@ -24,10 +24,15 @@ class OphandleTable(rend.Page, service.Service):
     UNCOLLECTED_HANDLE_LIFETIME = 4*DAY
     COLLECTED_HANDLE_LIFETIME = 1*DAY
 
-    def __init__(self):
+    def __init__(self, clock=None):
         # both of these are indexed by ophandle
         self.handles = {} # tuple of (monitor, renderer, when_added)
         self.timers = {}
+        # The tests will provide a deterministic clock
+        # (twisted.internet.task.Clock) that they can control so that
+        # they can test ophandle expiration. If this is provided, I'll
+        # use it schedule the expiration of ophandles.
+        self.clock = clock
 
     def stopService(self):
         for t in self.timers.values():
@@ -103,7 +108,10 @@ class OphandleTable(rend.Page, service.Service):
     def _set_timer(self, ophandle, when):
         if ophandle in self.timers and self.timers[ophandle].active():
             self.timers[ophandle].cancel()
-        t = reactor.callLater(when, self._release_ophandle, ophandle)
+        if self.clock:
+            t = self.clock.callLater(when, self._release_ophandle, ophandle)
+        else:
+            t = reactor.callLater(when, self._release_ophandle, ophandle)
         self.timers[ophandle] = t
 
     def _release_ophandle(self, ophandle):