Skip to content

Commit

Permalink
Try again. Merge trial-deferred-logic
Browse files Browse the repository at this point in the history
git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@15259 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
jml committed Nov 30, 2005
1 parent 9b7155e commit 1c63a3e
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 190 deletions.
5 changes: 0 additions & 5 deletions twisted/mail/test/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,11 +1105,6 @@ def setUpClass(self):
def tearDownClass(self):
smtp.DNSNAME = self.DNSNAME

def tearDown(self):
reactor.iterate()
reactor.iterate()
reactor.iterate()

def testProcessAlias(self):
path = util.sibpath(__file__, 'process.alias.sh')
a = mail.alias.ProcessAlias(path, None, None)
Expand Down
8 changes: 4 additions & 4 deletions twisted/test/test_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ def _rememberProtocolInstance(addr):
def tearDown(self):
# Clean up sockets
self.client.transport.loseConnection()
d = self.port.stopListening()
if d is not None:
wait(d)
d = defer.maybeDeferred(self.port.stopListening)
d.addCallback(self.ebTearDown)
return d

def ebTearDown(self, ignore):
del self.serverProtocol

# Clean up temporary directory
shutil.rmtree(self.directory)

Expand Down
3 changes: 2 additions & 1 deletion twisted/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def __init__(self, *args, **kw):

class StolenTCPTestCase(test_tcp.ProperlyCloseFilesTestCase, test_tcp.WriteDataTestCase):
def setUp(self):
self._setUp()
test_tcp.PortCleanerUpper.setUp(self)
self.serverConns = []
f = protocol.ServerFactory()
f.protocol = protocol.Protocol
self.listener = reactor.listenSSL(
Expand Down
72 changes: 36 additions & 36 deletions twisted/test/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ def setUp(self):
self.ports = []

def tearDown(self):
self.cleanPorts(*self.ports)
return self.cleanPorts(*self.ports)

def cleanPorts(self, *ports):
for p in ports:
if p.connected:
p.loseConnection()
util.spinWhile(lambda :p.connected)

ds = [ defer.maybeDeferred(p.loseConnection)
for p in ports if p.connected ]
return defer.gatherResults(ds)


class ListeningTestCase(PortCleanerUpper):
Expand Down Expand Up @@ -189,9 +187,7 @@ def testTcpNoDelay(self):
reactor.iterate()
self.assertEquals(transport.getTcpNoDelay(), 0)

self.cleanPorts(clientF.protocol.transport, port)

clientF.lostReason.trap(error.ConnectionDone)
return self.cleanPorts(clientF.protocol.transport, port)

def testTcpKeepAlive(self):
f = MyServerFactory()
Expand All @@ -213,8 +209,7 @@ def testTcpKeepAlive(self):

spinUntil(lambda :transport.getTcpKeepAlive() == 0, timeout=1.0)

self.cleanPorts(clientF.protocol.transport, port)
clientF.lostReason.trap(error.ConnectionDone)
return self.cleanPorts(clientF.protocol.transport, port)

def testFailing(self):
clientF = MyClientFactory()
Expand All @@ -237,6 +232,7 @@ def testConnectByServiceFail(self):

def testConnectByService(self):
serv = socket.getservbyname
d = defer.succeed(None)
try:
s = MyServerFactory()
port = reactor.listenTCP(0, s, interface="127.0.0.1")
Expand All @@ -252,11 +248,12 @@ def testConnectByService(self):
raise

spinUntil(lambda :getattr(s, 'protocol', None) is not None)
self.cleanPorts(port, c.transport, cf.protocol.transport)

d = self.cleanPorts(port, c.transport, cf.protocol.transport)
finally:
socket.getservbyname = serv
self.assert_(s.called, '%s was not called' % (s,))
d.addCallback(lambda x : self.assert_(s.called,
'%s was not called' % (s,)))
return d


class StartStopFactory(protocol.Factory):
Expand Down Expand Up @@ -333,7 +330,7 @@ def testServerStartStop(self):
spinWhile(lambda :(p3.connected == 1))

self.assertEquals((f.started, f.stopped), (1, 1))
self.cleanPorts(*self.ports)
return self.cleanPorts(*self.ports)


def testClientStartStop(self):
Expand All @@ -353,7 +350,7 @@ def testClientStartStop(self):

spinUntil(lambda :factory.stopped)

self.cleanPorts(*self.ports)
return self.cleanPorts(*self.ports)


class ConnectorTestCase(PortCleanerUpper):
Expand All @@ -380,10 +377,11 @@ def testConnectorIdentity(self):

spinUntil(lambda :factory.stopped)

self.cleanPorts(*self.ports)
d = self.cleanPorts(*self.ports)

m[0].trap(error.ConnectionDone)
self.assertEquals(l, [connector, connector])
return d

def testUserFail(self):
f = MyServerFactory()
Expand All @@ -403,7 +401,7 @@ def startedConnecting(connector):
self.assertEquals(factory.failed, 1)
factory.reason.trap(error.UserError)

self.cleanPorts(*self.ports)
return self.cleanPorts(*self.ports)


def testReconnect(self):
Expand All @@ -428,7 +426,7 @@ def clientConnectionLost(c, reason):
factory.reason.trap(error.ConnectionRefusedError)
self.assertEquals(factory.stopped, 1)

self.cleanPorts(*self.ports)
return self.cleanPorts(*self.ports)


class CannotBindTestCase(PortCleanerUpper):
Expand All @@ -450,7 +448,7 @@ def testCannotBind(self):
self.assertRaises(error.CannotListenError,
reactor.listenTCP, n, f, interface='127.0.0.1')

self.cleanPorts(*self.ports)
return self.cleanPorts(*self.ports)

def testClientBind(self):
f = MyServerFactory()
Expand Down Expand Up @@ -484,7 +482,7 @@ def testClientBind(self):
spinWhile(lambda :p.connected)

self.assertEquals(factory.stopped, 1)
self.cleanPorts(*self.ports)
return self.cleanPorts(*self.ports)

class MyOtherClientFactory(protocol.ClientFactory):
def buildProtocol(self, address):
Expand Down Expand Up @@ -602,13 +600,9 @@ class ProperlyCloseFilesTestCase(PortCleanerUpper):
numberRounds = 2048
timeLimit = 200

def _setUp(self):
# This method is used by test_ssl
def setUp(self):
PortCleanerUpper.setUp(self)
self.serverConns = []

def setUp(self):
self._setUp()
f = protocol.ServerFactory()
f.protocol = NoopProtocol
f.protocol.master = self
Expand All @@ -630,10 +624,9 @@ def connector():
def tearDown(self):
# Wait until all the protocols on the server-side of this test have
# been disconnected, to avoid leaving junk in the reactor.
for d in self.serverConns:
util.wait(d)

PortCleanerUpper.tearDown(self)
d = defer.gatherResults(self.serverConns)
d.addBoth(lambda x : PortCleanerUpper.tearDown(self))
return d

def testProperlyCloseFiles(self):
self.connector()
Expand Down Expand Up @@ -907,25 +900,32 @@ def tearDown(self):
self.client.transport.loseConnection()
return self.p.stopListening()

def _delayDeferred(self, time, arg=None):
from twisted.internet import reactor
d = defer.Deferred()
reactor.callLater(time, d.callback, arg)
return d

def testNoNotification(self):
client = self.client
f = self.f
client.transport.write("hello")
w = client.transport.write
client.transport.loseWriteConnection()
reactor.iterate()
reactor.iterate()
reactor.iterate()
self.assertEquals(f.protocol.data, "hello")
self.assertEquals(f.protocol.closed, True)
d = self._delayDeferred(0.2, f.protocol)
d.addCallback(lambda x : self.assertEqual(f.protocol.data, 'hello'))
d.addCallback(lambda x : self.assertEqual(f.protocol.closed, True))
return d

def testShutdownException(self):
client = self.client
f = self.f
f.protocol.transport.loseConnection()
client.transport.write("X")
client.transport.loseWriteConnection()
spinUntil(lambda :f.protocol.closed, True)
d = self._delayDeferred(0.2, f.protocol)
d.addCallback(lambda x : self.failUnlessEqual(x.closed, True))
return d

class HalfClose3TestCase(PortCleanerUpper):
"""Test half-closing connections where notification code has bugs."""
Expand Down
11 changes: 10 additions & 1 deletion twisted/trial/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,24 @@ def addSuccess(self, test):
# pyunit compat -- we don't use this
pass

def _trimFrame(self, fail):
if len(fail.frames) < 2:
return fail.frames
oldFrames = fail.frames[:]
del fail.frames[1]
return oldFrames

def _formatFailureTraceback(self, fail):
# Short term hack
if isinstance(fail, str):
return fail
return fail
oldFrames = self._trimFrame(fail)
detailLevel = self.tbformat
result = fail.getTraceback(detail=detailLevel, elideFrameworkCode=True)
if detailLevel == 'default':
# Apparently trial's tests doen't like the 'Traceback:' line.
result = '\n'.join(result.split('\n')[1:])
fail.frames = oldFrames
return result

def _formatImportError(self, name, error):
Expand Down
Loading

0 comments on commit 1c63a3e

Please sign in to comment.