[Twisted-Python] Re: [Twisted-commits] r18220 - Make tests clean up after themselves

Jean-Paul Calderone exarkun at divmod.com
Fri Sep 22 06:58:36 MDT 2006


On Thu, 21 Sep 2006 21:32:47 -0600, Jonathan Lange <jml at wolfwood.twistedmatrix.com> wrote:
>Author: jml
>Date: Thu Sep 21 21:32:47 2006
>New Revision: 18220
>
>Modified:
>   trunk/twisted/web/test/test_distrib.py
>   trunk/twisted/web2/test/test_static.py
>   trunk/twisted/words/test/test_service.py
>Log:
>Make tests clean up after themselves
>
> * Fixes #1883
> * Author: jml
> * Reviewer: MFen
>
>Although not apparent to the naked eye, these tests previous did not
>correctly clean up after themselves. Instead, they relied on a couple
>of spins of the reactor to do their cleaning for them. This has been
>corrected.
>
>
>
>Modified: trunk/twisted/web/test/test_distrib.py
>==============================================================================
>--- trunk/twisted/web/test/test_distrib.py	(original)
>+++ trunk/twisted/web/test/test_distrib.py	Thu Sep 21 21:32:47 2006
>@@ -15,16 +15,26 @@
>                 self.logFile.close()
>             del self.logFile
>
>+
>+class PBServerFactory(pb.PBServerFactory):

Docstring?

>+    def buildProtocol(self, addr):
>+        self.proto = pb.PBServerFactory.buildProtocol(self, addr)
>+        return self.proto
>+
>+
> class DistribTest(unittest.TestCase):
>     port1 = None
>     port2 = None
>     sub = None
>
>     def tearDown(self):

Docstring?

>+        dl = [defer.Deferred(), defer.Deferred()]
>+        self.f1.proto.notifyOnDisconnect(lambda: dl[0].callback(None))
>         if self.sub is not None:
>+            self.sub.publisher.broker.notifyOnDisconnect(
>+                lambda: dl[1].callback(None))
>             self.sub.publisher.broker.transport.loseConnection()
>         http._logDateTimeStop()
>-        dl = []
>         if self.port1 is not None:
>             dl.append(self.port1.stopListening())
>         if self.port2 is not None:
>@@ -36,8 +46,8 @@
>         r1 = resource.Resource()
>         r1.putChild("there", static.Data("root", "text/plain"))
>         site1 = server.Site(r1)
>-        f1 = pb.PBServerFactory(distrib.ResourcePublisher(site1))
>-        self.port1 = reactor.listenTCP(0, f1)
>+        self.f1 = PBServerFactory(distrib.ResourcePublisher(site1))
>+        self.port1 = reactor.listenTCP(0, self.f1)
>         self.sub = distrib.ResourceSubscription("127.0.0.1",
>                                                 self.port1.getHost().port)
>         r2 = resource.Resource()
>
>Modified: trunk/twisted/web2/test/test_static.py
>==============================================================================
>--- trunk/twisted/web2/test/test_static.py	(original)
>+++ trunk/twisted/web2/test/test_static.py	Thu Sep 21 21:32:47 2006
>@@ -11,7 +11,6 @@
>         self.text = "Hello, World\n"
>         self.data = static.Data(self.text, "text/plain")
>
>-
>     def test_dataState(self):
>         """
>         Test the internal state of the Data object
>@@ -42,7 +41,6 @@
>                          http_headers.MimeType("text", "plain"))
>         def checkStream(data):
>             self.assertEquals(str(data), self.text)
>-
>         return stream.readStream(iweb.IResponse(self.data.render(None)).stream,
>                                  checkStream)
>
>@@ -58,7 +56,8 @@
>                               maxBytes=16)
>         self.root.addSlash = True
>
>-    def uploadFile(self, fieldname, filename, mimetype, content, resrc=None, host='foo', path='/'):
>+    def uploadFile(self, fieldname, filename, mimetype, content, resrc=None,
>+                   host='foo', path='/'):
>         if not resrc:
>             resrc = self.root
>
>@@ -102,32 +101,33 @@
>         return d
>
>     def test_enforcesMaxBytes(self):

Docstring?

>-        return self.assertInResponse(self.uploadFile('FileNameOne', 'myfilename',
>-                                         'text/html', 'X'*32),
>-                              (200, {}, 'exceeds maximum length'))
>+        return self.assertInResponse(
>+            self.uploadFile('FileNameOne', 'myfilename', 'text/html', 'X'*32),
>+            (200, {}, 'exceeds maximum length'))
>
>     def test_enforcesMimeType(self):

Docstring?

>-        return self.assertInResponse(self.uploadFile('FileNameOne', 'myfilename',
>-                                              'application/x-python', 'X'),
>-                              (200, {}, 'type not allowed'))
>+        return self.assertInResponse(
>+            self.uploadFile('FileNameOne', 'myfilename',
>+                            'application/x-python', 'X'),
>+            (200, {}, 'type not allowed'))
>
>     def test_invalidField(self):

Docstring?

>-        return self.assertInResponse(self.uploadFile('NotARealField', 'myfilename',
>-                                              'text/html', 'X'),
>-                              (200, {}, 'not a valid field'))
>+        return self.assertInResponse(

Docstring?

>+            self.uploadFile('NotARealField', 'myfilename', 'text/html', 'X'),
>+            (200, {}, 'not a valid field'))
>
>     def test_reportFileSave(self):

Docstring?

>-        return self.assertInResponse(self.uploadFile('FileNameOne', 'myfilename',
>-                                              'text/plain',
>-                                              'X'),
>-                              (200, {}, 'Saved file'))
>+        return self.assertInResponse(
>+            self.uploadFile('FileNameOne', 'myfilename', 'text/plain', 'X'),
>+            (200, {}, 'Saved file'))
>
>     def test_compareFileContents(self):

Docstring?

>         def gotFname(fname):
>             contents = file(fname, 'r').read()
>             self.assertEquals(contents, 'Test contents')
>-
>-        return self.uploadFile('FileNameOne', 'myfilename', 'text/plain',
>-                               'Test contents').addCallback(
>-            self.fileNameFromResponse
>-            ).addCallback(gotFname)
>+
>+        d = self.uploadFile('FileNameOne', 'myfilename', 'text/plain',
>+                            'Test contents')
>+        d.addCallback(self.fileNameFromResponse)
>+        d.addCallback(gotFname)
>+        return d
>
>Modified: trunk/twisted/words/test/test_service.py
>==============================================================================
>--- trunk/twisted/words/test/test_service.py	(original)
>+++ trunk/twisted/words/test/test_service.py	Thu Sep 21 21:32:47 2006
>@@ -875,21 +875,26 @@
>         self.portal = portal.Portal(
>             self.realm, [self.checker])
>         self.serverFactory = pb.PBServerFactory(self.portal)
>+        self.serverFactory.protocol = self._protocolFactory
>         self.serverFactory.unsafeTracebacks = True
>         self.clientFactory = pb.PBClientFactory()
>         self.clientFactory.unsafeTracebacks = True
>-
>         self.serverPort = reactor.listenTCP(0, self.serverFactory)
>         self.clientConn = reactor.connectTCP(
>             '127.0.0.1',
>             self.serverPort.getHost().port,
>             self.clientFactory)
>
>+    def _protocolFactory(self, *args, **kw):

Docstring?

>+        self._serverProtocol = pb.Broker(0)
>+        return self._serverProtocol
>
>     def tearDown(self):

Docstring?

>+        d3 = Deferred()
>+        self._serverProtocol.notifyOnDisconnect(lambda: d3.callback(None))
>         return DeferredList([
>             maybeDeferred(self.serverPort.stopListening),
>-            maybeDeferred(self.clientConn.disconnect)])
>+            maybeDeferred(self.clientConn.disconnect), d3])
>
>     def _loggedInAvatar(self, name, password, mind):
>         creds = credentials.UsernamePassword(name, password)
>
>_______________________________________________
>Twisted-commits mailing list
>Twisted-commits at twistedmatrix.com
>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-commits
>




More information about the Twisted-Python mailing list