[Buildbot-commits] buildbot/buildbot/test test_maildir.py,1.5,1.6

Brian Warner warner at users.sourceforge.net
Tue Jan 23 21:04:35 UTC 2007


Update of /cvsroot/buildbot/buildbot/buildbot/test
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv15423/buildbot/test

Modified Files:
	test_maildir.py 
Log Message:
[project @ clean up maildir usage, remove a use of reactor.iterate from tests]

Original author: warner at lothar.com
Date: 2007-01-23 21:02:10

Index: test_maildir.py
===================================================================
RCS file: /cvsroot/buildbot/buildbot/buildbot/test/test_maildir.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- test_maildir.py	13 Mar 2006 05:48:35 -0000	1.5
+++ test_maildir.py	23 Jan 2007 21:04:32 -0000	1.6
@@ -3,43 +3,48 @@
 from twisted.trial import unittest
 import os, shutil
 from buildbot.changes.mail import FCMaildirSource
-from twisted.internet import reactor
-from twisted.python import util
+from twisted.internet import defer, reactor, task
+from twisted.python import util, log
+
+class TimeOutError(Exception):
+    """The message were not received in a timely fashion"""
 
 class MaildirTest(unittest.TestCase):
+    SECONDS_PER_MESSAGE = 1.0
+
     def setUp(self):
-        print "creating empty maildir"
+        log.msg("creating empty maildir")
         self.maildir = "test-maildir"
         if os.path.isdir(self.maildir):
             shutil.rmtree(self.maildir)
-            print "removing stale maildir"
+            log.msg("removing stale maildir")
         os.mkdir(self.maildir)
         os.mkdir(os.path.join(self.maildir, "cur"))
         os.mkdir(os.path.join(self.maildir, "new"))
         os.mkdir(os.path.join(self.maildir, "tmp"))
         self.source = None
-        self.done = 0
 
     def tearDown(self):
-        print "removing old maildir"
+        log.msg("removing old maildir")
         shutil.rmtree(self.maildir)
         if self.source:
-            self.source.stopService()
+            return self.source.stopService()
 
     def addChange(self, c):
         # NOTE: this assumes every message results in a Change, which isn't
         # true for msg8-prefix
-        print "got change"
+        log.msg("got change")
         self.changes.append(c)
 
     def deliverMail(self, msg):
-        print "delivering", msg
+        log.msg("delivering", msg)
         newdir = os.path.join(self.maildir, "new")
         # to do this right, use safecat
         shutil.copy(msg, newdir)
 
-    def do_timeout(self):
-        self.done = 1
+    def poll(self, changes, count, d):
+        if len(changes) == count:
+            d.callback("passed")
 
     def testMaildir(self):
         self.changes = []
@@ -51,29 +56,35 @@
                      if msg.startswith("msg")]
         testfiles.sort()
         count = len(testfiles)
+        d = defer.Deferred()
+
         for i in range(count):
             msg = testfiles[i]
-            reactor.callLater(2*i, self.deliverMail,
+            reactor.callLater(self.SECONDS_PER_MESSAGE*i, self.deliverMail,
                               os.path.join(testfiles_dir, msg))
-        t = reactor.callLater(2*i + 15, self.do_timeout)
-        while not (self.done or len(self.changes) == count):
-            reactor.iterate(0.1)
-        s.stopService()
-        if self.done:
-            return self.fail("timeout: messages weren't received on time")
-        t.cancel()
+        self.loop = task.LoopingCall(self.poll, self.changes, count, d)
+        self.loop.start(0.1)
+        t = reactor.callLater(self.SECONDS_PER_MESSAGE*i + 15,
+                              d.errback, TimeOutError)
         # TODO: verify the messages, should use code from test_mailparse but
         # I'm not sure how to factor the verification routines out in a
         # useful fashion
+
         #for i in range(count):
         #    msg, check = test_messages[i]
         #    check(self, self.changes[i])
-        
 
-if __name__ == '__main__':
-    suite = unittest.TestSuite()
-    suite.addTestClass(MaildirTest)
-    import sys
-    reporter = unittest.TextReporter(sys.stdout)
-    suite.run(reporter)
-    
+        def _shutdown(res):
+            if t.active():
+                t.cancel()
+            self.loop.stop()
+            return res
+        d.addBoth(_shutdown)
+
+        return d
+
+    # TODO: it would be nice to set this timeout after counting the number of
+    # messages in buildbot/test/mail/msg*, but I suspect trial wants to have
+    # this number before the method starts, and maybe even before setUp()
+    testMaildir.timeout = SECONDS_PER_MESSAGE*9 + 15
+





More information about the Commits mailing list