def _check1connect2(results, cf1): self.assertEquals(cf1.protocol.made, 1) d1 = defer.Deferred() d2 = defer.Deferred() port = cf1.protocol.transport.getHost().port cf2 = MyClientFactory() cf2.clientConnectionFailed = fireWhenDoneFunc(d1, cf2.clientConnectionFailed) cf2.stopFactory = fireWhenDoneFunc(d2, cf2.stopFactory) reactor.connectTCP("127.0.0.1", p.getHost().port, cf2, bindAddress=("127.0.0.1", port)) d1.addCallback(_check2failed, cf1, cf2) d2.addCallback(_check2stopped, cf1, cf2) dl = defer.DeferredList([d1, d2]) dl.addCallback(_stop, cf1, cf2) return dl
def _connect1(results): d = defer.Deferred() cf1 = MyClientFactory() cf1.buildProtocol = fireWhenDoneFunc(d, cf1.buildProtocol) reactor.connectTCP("127.0.0.1", p.getHost().port, cf1, bindAddress=("127.0.0.1", 0)) d.addCallback(_conmade, cf1) return d
def _check1connect2(results, cf1): self.assertEquals(cf1.protocol.made, 1) d1 = defer.Deferred() d2 = defer.Deferred() port = cf1.protocol.transport.getHost().port cf2 = MyClientFactory() cf2.clientConnectionFailed = fireWhenDoneFunc( d1, cf2.clientConnectionFailed) cf2.stopFactory = fireWhenDoneFunc(d2, cf2.stopFactory) reactor.connectTCP("127.0.0.1", p.getHost().port, cf2, bindAddress=("127.0.0.1", port)) d1.addCallback(_check2failed, cf1, cf2) d2.addCallback(_check2stopped, cf1, cf2) dl = defer.DeferredList([d1, d2]) dl.addCallback(_stop, cf1, cf2) return dl
def testLimit(self): server = Server() c1, c2, c3, c4 = [SimpleProtocol() for i in range(4)] tServer = policies.ThrottlingFactory(server, 2) theDeferred = defer.Deferred() tServer.startFactory = fireWhenDoneFunc(theDeferred, tServer.startFactory) # Start listening p = reactor.listenTCP(0, tServer, interface="127.0.0.1") n = p.getHost().port def _connect123(results): for c in c1, c2, c3: p = reactor.connectTCP("127.0.0.1", n, SillyFactory(c)) deferreds = [c.dConnected for c in c1, c2, c3] deferreds.append(c3.dDisconnected) return defer.DeferredList(deferreds) def _check123(results): self.assertEquals([c.connected for c in c1, c2, c3], [1, 1, 1]) self.assertEquals([c.disconnected for c in c1, c2, c3], [0, 0, 1]) self.assertEquals(len(tServer.protocols.keys()), 2) return results def _lose1(results): # disconnect one protocol and now another should be able to connect c1.transport.loseConnection() return c1.dDisconnected def _connect4(results): reactor.connectTCP("127.0.0.1", n, SillyFactory(c4)) return c4.dConnected def _check4(results): self.assertEquals(c4.connected, 1) self.assertEquals(c4.disconnected, 0) return results def _cleanup(results): for c in c2, c4: c.transport.loseConnection() return defer.DeferredList([ defer.maybeDeferred(p.stopListening), c2.dDisconnected, c4.dDisconnected ]) theDeferred.addCallback(_connect123) theDeferred.addCallback(_check123) theDeferred.addCallback(_lose1) theDeferred.addCallback(_connect4) theDeferred.addCallback(_check4) theDeferred.addCallback(_cleanup) return theDeferred
def testLimit(self): server = Server() c1, c2, c3, c4 = [SimpleProtocol() for i in range(4)] tServer = policies.ThrottlingFactory(server, 2) theDeferred = defer.Deferred() tServer.startFactory = fireWhenDoneFunc(theDeferred, tServer.startFactory) # Start listening p = reactor.listenTCP(0, tServer, interface="127.0.0.1") n = p.getHost().port def _connect123(results): for c in c1, c2, c3: p = reactor.connectTCP("127.0.0.1", n, SillyFactory(c)) deferreds = [c.dConnected for c in c1, c2, c3] deferreds.append(c3.dDisconnected) return defer.DeferredList(deferreds) def _check123(results): self.assertEquals([c.connected for c in c1, c2, c3], [1, 1, 1]) self.assertEquals([c.disconnected for c in c1, c2, c3], [0, 0, 1]) self.assertEquals(len(tServer.protocols.keys()), 2) return results def _lose1(results): # disconnect one protocol and now another should be able to connect c1.transport.loseConnection() return c1.dDisconnected def _connect4(results): reactor.connectTCP("127.0.0.1", n, SillyFactory(c4)) return c4.dConnected def _check4(results): self.assertEquals(c4.connected, 1) self.assertEquals(c4.disconnected, 0) return results def _cleanup(results): for c in c2, c4: c.transport.loseConnection() return defer.DeferredList([ defer.maybeDeferred(p.stopListening), c2.dDisconnected, c4.dDisconnected]) theDeferred.addCallback(_connect123) theDeferred.addCallback(_check123) theDeferred.addCallback(_lose1) theDeferred.addCallback(_connect4) theDeferred.addCallback(_check4) theDeferred.addCallback(_cleanup) return theDeferred
def testClientBind(self): theDeferred = defer.Deferred() sf = MyServerFactory() sf.startFactory = fireWhenDoneFunc(theDeferred, sf.startFactory) p = reactor.listenTCP(0, sf, interface="127.0.0.1") self.ports.append(p) def _connect1(results): d = defer.Deferred() cf1 = MyClientFactory() cf1.buildProtocol = fireWhenDoneFunc(d, cf1.buildProtocol) reactor.connectTCP("127.0.0.1", p.getHost().port, cf1, bindAddress=("127.0.0.1", 0)) d.addCallback(_conmade, cf1) return d def _conmade(results, cf1): d = defer.Deferred() cf1.protocol.connectionMade = fireWhenDoneFunc(d, cf1.protocol.connectionMade) d.addCallback(_check1connect2, cf1) return d def _check1connect2(results, cf1): self.assertEquals(cf1.protocol.made, 1) d1 = defer.Deferred() d2 = defer.Deferred() port = cf1.protocol.transport.getHost().port cf2 = MyClientFactory() cf2.clientConnectionFailed = fireWhenDoneFunc(d1, cf2.clientConnectionFailed) cf2.stopFactory = fireWhenDoneFunc(d2, cf2.stopFactory) reactor.connectTCP("127.0.0.1", p.getHost().port, cf2, bindAddress=("127.0.0.1", port)) d1.addCallback(_check2failed, cf1, cf2) d2.addCallback(_check2stopped, cf1, cf2) dl = defer.DeferredList([d1, d2]) dl.addCallback(_stop, cf1, cf2) return dl def _check2failed(results, cf1, cf2): self.assertEquals(cf2.failed, 1) cf2.reason.trap(error.ConnectBindError) self.assert_(cf2.reason.check(error.ConnectBindError)) return results def _check2stopped(results, cf1, cf2): self.assertEquals(cf2.stopped, 1) return results def _stop(results, cf1, cf2): d = defer.Deferred() d.addCallback(_check1cleanup, cf1) cf1.stopFactory = fireWhenDoneFunc(d, cf1.stopFactory) cf1.protocol.transport.loseConnection() return d def _check1cleanup(results, cf1): self.assertEquals(cf1.stopped, 1) return self.cleanPorts(*self.ports) theDeferred.addCallback(_connect1) return theDeferred
def _stop(results, cf1, cf2): d = defer.Deferred() d.addCallback(_check1cleanup, cf1) cf1.stopFactory = fireWhenDoneFunc(d, cf1.stopFactory) cf1.protocol.transport.loseConnection() return d
def _conmade(results, cf1): d = defer.Deferred() cf1.protocol.connectionMade = fireWhenDoneFunc(d, cf1.protocol.connectionMade) d.addCallback(_check1connect2, cf1) return d
def _conmade(results, cf1): d = defer.Deferred() cf1.protocol.connectionMade = fireWhenDoneFunc( d, cf1.protocol.connectionMade) d.addCallback(_check1connect2, cf1) return d
def testClientBind(self): theDeferred = defer.Deferred() sf = MyServerFactory() sf.startFactory = fireWhenDoneFunc(theDeferred, sf.startFactory) p = reactor.listenTCP(0, sf, interface="127.0.0.1") self.ports.append(p) def _connect1(results): d = defer.Deferred() cf1 = MyClientFactory() cf1.buildProtocol = fireWhenDoneFunc(d, cf1.buildProtocol) reactor.connectTCP("127.0.0.1", p.getHost().port, cf1, bindAddress=("127.0.0.1", 0)) d.addCallback(_conmade, cf1) return d def _conmade(results, cf1): d = defer.Deferred() cf1.protocol.connectionMade = fireWhenDoneFunc( d, cf1.protocol.connectionMade) d.addCallback(_check1connect2, cf1) return d def _check1connect2(results, cf1): self.assertEquals(cf1.protocol.made, 1) d1 = defer.Deferred() d2 = defer.Deferred() port = cf1.protocol.transport.getHost().port cf2 = MyClientFactory() cf2.clientConnectionFailed = fireWhenDoneFunc( d1, cf2.clientConnectionFailed) cf2.stopFactory = fireWhenDoneFunc(d2, cf2.stopFactory) reactor.connectTCP("127.0.0.1", p.getHost().port, cf2, bindAddress=("127.0.0.1", port)) d1.addCallback(_check2failed, cf1, cf2) d2.addCallback(_check2stopped, cf1, cf2) dl = defer.DeferredList([d1, d2]) dl.addCallback(_stop, cf1, cf2) return dl def _check2failed(results, cf1, cf2): self.assertEquals(cf2.failed, 1) cf2.reason.trap(error.ConnectBindError) self.assert_(cf2.reason.check(error.ConnectBindError)) return results def _check2stopped(results, cf1, cf2): self.assertEquals(cf2.stopped, 1) return results def _stop(results, cf1, cf2): d = defer.Deferred() d.addCallback(_check1cleanup, cf1) cf1.stopFactory = fireWhenDoneFunc(d, cf1.stopFactory) cf1.protocol.transport.loseConnection() return d def _check1cleanup(results, cf1): self.assertEquals(cf1.stopped, 1) return self.cleanPorts(*self.ports) theDeferred.addCallback(_connect1) return theDeferred