<s> """<STR_LIT>""" <EOL> import socket , random , errno <EOL> from zope . interface import implements <EOL> from twisted . trial import unittest <EOL> from twisted . python . log import msg <EOL> from twisted . internet import protocol , reactor , defer , interfaces <EOL> from twisted . internet import error <EOL> from twisted . internet . address import IPv4Address <EOL> from twisted . internet . interfaces import IHalfCloseableProtocol , IPullProducer <EOL> from twisted . protocols import policies <EOL> from twisted . test . proto_helpers import AccumulatingProtocol <EOL> def loopUntil ( predicate , interval = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> from twisted . internet import task <EOL> d = defer . Deferred ( ) <EOL> def check ( ) : <EOL> res = predicate ( ) <EOL> if res : <EOL> d . callback ( res ) <EOL> call = task . LoopingCall ( check ) <EOL> def stop ( result ) : <EOL> call . stop ( ) <EOL> return result <EOL> d . addCallback ( stop ) <EOL> d2 = call . start ( interval ) <EOL> d2 . addErrback ( d . errback ) <EOL> return d <EOL> class ClosingProtocol ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> msg ( "<STR_LIT>" ) <EOL> self . transport . loseConnection ( ) <EOL> def connectionLost ( self , reason ) : <EOL> msg ( "<STR_LIT>" ) <EOL> reason . trap ( error . ConnectionDone ) <EOL> class ClosingFactory ( protocol . ServerFactory ) : <EOL> """<STR_LIT>""" <EOL> _cleanerUpper = None <EOL> def buildProtocol ( self , conn ) : <EOL> self . _cleanerUpper = self . port . stopListening ( ) <EOL> return ClosingProtocol ( ) <EOL> def cleanUp ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _cleanerUpper is None : <EOL> return self . port . stopListening ( ) <EOL> return self . _cleanerUpper <EOL> class MyProtocolFactoryMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> protocolFactory = AccumulatingProtocol <EOL> protocolConnectionMade = None <EOL> protocolConnectionLost = None <EOL> protocol = None <EOL> called = <NUM_LIT:0> <EOL> def __init__ ( self ) : <EOL> self . peerAddresses = [ ] <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> self . peerAddresses . append ( addr ) <EOL> self . called += <NUM_LIT:1> <EOL> p = self . protocolFactory ( ) <EOL> p . factory = self <EOL> p . closedDeferred = self . protocolConnectionLost <EOL> self . protocolConnectionLost = None <EOL> self . protocol = p <EOL> return p <EOL> class MyServerFactory ( MyProtocolFactoryMixin , protocol . ServerFactory ) : <EOL> """<STR_LIT>""" <EOL> class MyClientFactory ( MyProtocolFactoryMixin , protocol . ClientFactory ) : <EOL> """<STR_LIT>""" <EOL> failed = <NUM_LIT:0> <EOL> stopped = <NUM_LIT:0> <EOL> def __init__ ( self ) : <EOL> MyProtocolFactoryMixin . __init__ ( self ) <EOL> self . deferred = defer . Deferred ( ) <EOL> self . failDeferred = defer . Deferred ( ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> self . failed = <NUM_LIT:1> <EOL> self . reason = reason <EOL> self . failDeferred . callback ( None ) <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> self . lostReason = reason <EOL> self . deferred . callback ( None ) <EOL> def stopFactory ( self ) : <EOL> self . stopped = <NUM_LIT:1> <EOL> class ListeningTestCase ( unittest . TestCase ) : <EOL> def test_listen ( self ) : <EOL> """<STR_LIT>""" <EOL> f = MyServerFactory ( ) <EOL> p1 = reactor . listenTCP ( <NUM_LIT:0> , f , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( p1 . stopListening ) <EOL> self . failUnless ( interfaces . IListeningPort . providedBy ( p1 ) ) <EOL> def testStopListening ( self ) : <EOL> """<STR_LIT>""" <EOL> f = MyServerFactory ( ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , f , interface = "<STR_LIT:127.0.0.1>" ) <EOL> n = port . getHost ( ) . port <EOL> def cbStopListening ( ignored ) : <EOL> port = reactor . listenTCP ( n , f , interface = "<STR_LIT:127.0.0.1>" ) <EOL> return port . stopListening ( ) <EOL> d = defer . maybeDeferred ( port . stopListening ) <EOL> d . addCallback ( cbStopListening ) <EOL> return d <EOL> def testNumberedInterface ( self ) : <EOL> f = MyServerFactory ( ) <EOL> p1 = reactor . listenTCP ( <NUM_LIT:0> , f , interface = '<STR_LIT:127.0.0.1>' ) <EOL> return p1 . stopListening ( ) <EOL> def testPortRepr ( self ) : <EOL> f = MyServerFactory ( ) <EOL> p = reactor . listenTCP ( <NUM_LIT:0> , f ) <EOL> portNo = str ( p . getHost ( ) . port ) <EOL> self . failIf ( repr ( p ) . find ( portNo ) == - <NUM_LIT:1> ) <EOL> def stoppedListening ( ign ) : <EOL> self . failIf ( repr ( p ) . find ( portNo ) != - <NUM_LIT:1> ) <EOL> d = defer . maybeDeferred ( p . stopListening ) <EOL> return d . addCallback ( stoppedListening ) <EOL> def test_serverRepr ( self ) : <EOL> """<STR_LIT>""" <EOL> server = MyServerFactory ( ) <EOL> serverConnMade = server . protocolConnectionMade = defer . Deferred ( ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , server ) <EOL> self . addCleanup ( port . stopListening ) <EOL> client = MyClientFactory ( ) <EOL> clientConnMade = client . protocolConnectionMade = defer . Deferred ( ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , <EOL> port . getHost ( ) . port , client ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> def check ( ( serverProto , clientProto ) ) : <EOL> portNumber = port . getHost ( ) . port <EOL> self . assertEquals ( <EOL> repr ( serverProto . transport ) , <EOL> "<STR_LIT>" % ( portNumber , ) ) <EOL> serverProto . transport . loseConnection ( ) <EOL> clientProto . transport . loseConnection ( ) <EOL> return defer . gatherResults ( [ serverConnMade , clientConnMade ] <EOL> ) . addCallback ( check ) <EOL> def test_restartListening ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( port . stopListening ) <EOL> def cbStopListening ( ignored ) : <EOL> port . startListening ( ) <EOL> client = MyClientFactory ( ) <EOL> serverFactory . protocolConnectionMade = defer . Deferred ( ) <EOL> client . protocolConnectionMade = defer . Deferred ( ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , <EOL> port . getHost ( ) . port , client ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return defer . gatherResults ( [ serverFactory . protocolConnectionMade , <EOL> client . protocolConnectionMade ] <EOL> ) . addCallback ( close ) <EOL> def close ( ( serverProto , clientProto ) ) : <EOL> clientProto . transport . loseConnection ( ) <EOL> serverProto . transport . loseConnection ( ) <EOL> d = defer . maybeDeferred ( port . stopListening ) <EOL> d . addCallback ( cbStopListening ) <EOL> return d <EOL> def test_exceptInStop ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> def raiseException ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> serverFactory . stopFactory = raiseException <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> return self . assertFailure ( port . stopListening ( ) , RuntimeError ) <EOL> def test_restartAfterExcept ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> def raiseException ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> serverFactory . stopFactory = raiseException <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( port . stopListening ) <EOL> def cbStopListening ( ignored ) : <EOL> del serverFactory . stopFactory <EOL> port . startListening ( ) <EOL> client = MyClientFactory ( ) <EOL> serverFactory . protocolConnectionMade = defer . Deferred ( ) <EOL> client . protocolConnectionMade = defer . Deferred ( ) <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , <EOL> port . getHost ( ) . port , client ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> return defer . gatherResults ( [ serverFactory . protocolConnectionMade , <EOL> client . protocolConnectionMade ] <EOL> ) . addCallback ( close ) <EOL> def close ( ( serverProto , clientProto ) ) : <EOL> clientProto . transport . loseConnection ( ) <EOL> serverProto . transport . loseConnection ( ) <EOL> return self . assertFailure ( port . stopListening ( ) , RuntimeError <EOL> ) . addCallback ( cbStopListening ) <EOL> def test_directConnectionLostCall ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> portNumber = port . getHost ( ) . port <EOL> port . connectionLost ( None ) <EOL> client = MyClientFactory ( ) <EOL> serverFactory . protocolConnectionMade = defer . Deferred ( ) <EOL> client . protocolConnectionMade = defer . Deferred ( ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , portNumber , client ) <EOL> def check ( ign ) : <EOL> client . reason . trap ( error . ConnectionRefusedError ) <EOL> return client . failDeferred . addCallback ( check ) <EOL> def test_exceptInConnectionLostCall ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> def raiseException ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> serverFactory . stopFactory = raiseException <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . assertRaises ( RuntimeError , port . connectionLost , None ) <EOL> def callWithSpew ( f ) : <EOL> from twisted . python . util import spewerWithLinenums as spewer <EOL> import sys <EOL> sys . settrace ( spewer ) <EOL> try : <EOL> f ( ) <EOL> finally : <EOL> sys . settrace ( None ) <EOL> class LoopbackTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_closePortInProtocolFactory ( self ) : <EOL> """<STR_LIT>""" <EOL> f = ClosingFactory ( ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , f , interface = "<STR_LIT:127.0.0.1>" ) <EOL> f . port = port <EOL> self . addCleanup ( f . cleanUp ) <EOL> portNumber = port . getHost ( ) . port <EOL> clientF = MyClientFactory ( ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , portNumber , clientF ) <EOL> def check ( x ) : <EOL> self . assertTrue ( clientF . protocol . made ) <EOL> self . assertTrue ( port . disconnected ) <EOL> clientF . lostReason . trap ( error . ConnectionDone ) <EOL> return clientF . deferred . addCallback ( check ) <EOL> def _trapCnxDone ( self , obj ) : <EOL> getattr ( obj , '<STR_LIT>' , lambda x : None ) ( error . ConnectionDone ) <EOL> def _connectedClientAndServerTest ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> serverConnMade = defer . Deferred ( ) <EOL> serverFactory . protocolConnectionMade = serverConnMade <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( port . stopListening ) <EOL> portNumber = port . getHost ( ) . port <EOL> clientF = MyClientFactory ( ) <EOL> clientConnMade = defer . Deferred ( ) <EOL> clientF . protocolConnectionMade = clientConnMade <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , portNumber , clientF ) <EOL> connsMade = defer . gatherResults ( [ serverConnMade , clientConnMade ] ) <EOL> def connected ( ( serverProtocol , clientProtocol ) ) : <EOL> callback ( serverProtocol , clientProtocol ) <EOL> serverProtocol . transport . loseConnection ( ) <EOL> clientProtocol . transport . loseConnection ( ) <EOL> connsMade . addCallback ( connected ) <EOL> return connsMade <EOL> def test_tcpNoDelay ( self ) : <EOL> """<STR_LIT>""" <EOL> def check ( serverProtocol , clientProtocol ) : <EOL> for p in [ serverProtocol , clientProtocol ] : <EOL> transport = p . transport <EOL> self . assertEquals ( transport . getTcpNoDelay ( ) , <NUM_LIT:0> ) <EOL> transport . setTcpNoDelay ( <NUM_LIT:1> ) <EOL> self . assertEquals ( transport . getTcpNoDelay ( ) , <NUM_LIT:1> ) <EOL> transport . setTcpNoDelay ( <NUM_LIT:0> ) <EOL> self . assertEquals ( transport . getTcpNoDelay ( ) , <NUM_LIT:0> ) <EOL> return self . _connectedClientAndServerTest ( check ) <EOL> def test_tcpKeepAlive ( self ) : <EOL> """<STR_LIT>""" <EOL> def check ( serverProtocol , clientProtocol ) : <EOL> for p in [ serverProtocol , clientProtocol ] : <EOL> transport = p . transport <EOL> self . assertEquals ( transport . getTcpKeepAlive ( ) , <NUM_LIT:0> ) <EOL> transport . setTcpKeepAlive ( <NUM_LIT:1> ) <EOL> self . assertEquals ( transport . getTcpKeepAlive ( ) , <NUM_LIT:1> ) <EOL> transport . setTcpKeepAlive ( <NUM_LIT:0> ) <EOL> self . assertEquals ( transport . getTcpKeepAlive ( ) , <NUM_LIT:0> ) <EOL> return self . _connectedClientAndServerTest ( check ) <EOL> def testFailing ( self ) : <EOL> clientF = MyClientFactory ( ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> , clientF , timeout = <NUM_LIT:5> ) <EOL> def check ( ignored ) : <EOL> clientF . reason . trap ( error . ConnectionRefusedError ) <EOL> return clientF . failDeferred . addCallback ( check ) <EOL> def test_connectionRefusedErrorNumber ( self ) : <EOL> """<STR_LIT>""" <EOL> serverSockets = [ ] <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> serverSocket = socket . socket ( ) <EOL> serverSocket . bind ( ( '<STR_LIT:127.0.0.1>' , <NUM_LIT:0> ) ) <EOL> serverSocket . listen ( <NUM_LIT:1> ) <EOL> serverSockets . append ( serverSocket ) <EOL> random . shuffle ( serverSockets ) <EOL> clientCreator = protocol . ClientCreator ( reactor , protocol . Protocol ) <EOL> def tryConnectFailure ( ) : <EOL> def connected ( proto ) : <EOL> """<STR_LIT>""" <EOL> proto . transport . loseConnection ( ) <EOL> if serverSockets : <EOL> return tryConnectFailure ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> serverSocket = serverSockets . pop ( ) <EOL> serverHost , serverPort = serverSocket . getsockname ( ) <EOL> serverSocket . close ( ) <EOL> connectDeferred = clientCreator . connectTCP ( serverHost , serverPort ) <EOL> connectDeferred . addCallback ( connected ) <EOL> return connectDeferred <EOL> refusedDeferred = tryConnectFailure ( ) <EOL> self . assertFailure ( refusedDeferred , error . ConnectionRefusedError ) <EOL> def connRefused ( exc ) : <EOL> self . assertEqual ( exc . osError , errno . ECONNREFUSED ) <EOL> refusedDeferred . addCallback ( connRefused ) <EOL> def cleanup ( passthrough ) : <EOL> while serverSockets : <EOL> serverSockets . pop ( ) . close ( ) <EOL> return passthrough <EOL> refusedDeferred . addBoth ( cleanup ) <EOL> return refusedDeferred <EOL> def test_connectByServiceFail ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( <EOL> error . ServiceNameUnknownError , <EOL> reactor . connectTCP , <EOL> "<STR_LIT:127.0.0.1>" , "<STR_LIT>" , MyClientFactory ( ) ) <EOL> def test_connectByService ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> serverConnMade = defer . Deferred ( ) <EOL> serverFactory . protocolConnectionMade = serverConnMade <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( port . stopListening ) <EOL> portNumber = port . getHost ( ) . port <EOL> clientFactory = MyClientFactory ( ) <EOL> clientConnMade = defer . Deferred ( ) <EOL> clientFactory . protocolConnectionMade = clientConnMade <EOL> def fakeGetServicePortByName ( serviceName , protocolName ) : <EOL> if serviceName == '<STR_LIT:http>' and protocolName == '<STR_LIT>' : <EOL> return portNumber <EOL> return <NUM_LIT:10> <EOL> self . patch ( socket , '<STR_LIT>' , fakeGetServicePortByName ) <EOL> reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , '<STR_LIT:http>' , clientFactory ) <EOL> connMade = defer . gatherResults ( [ serverConnMade , clientConnMade ] ) <EOL> def connected ( ( serverProtocol , clientProtocol ) ) : <EOL> self . assertTrue ( <EOL> serverFactory . called , <EOL> "<STR_LIT>" ) <EOL> serverProtocol . transport . loseConnection ( ) <EOL> clientProtocol . transport . loseConnection ( ) <EOL> connMade . addCallback ( connected ) <EOL> return connMade <EOL> class StartStopFactory ( protocol . Factory ) : <EOL> started = <NUM_LIT:0> <EOL> stopped = <NUM_LIT:0> <EOL> def startFactory ( self ) : <EOL> if self . started or self . stopped : <EOL> raise RuntimeError <EOL> self . started = <NUM_LIT:1> <EOL> def stopFactory ( self ) : <EOL> if not self . started or self . stopped : <EOL> raise RuntimeError <EOL> self . stopped = <NUM_LIT:1> <EOL> class ClientStartStopFactory ( MyClientFactory ) : <EOL> started = <NUM_LIT:0> <EOL> stopped = <NUM_LIT:0> <EOL> def startFactory ( self ) : <EOL> if self . started or self . stopped : <EOL> raise RuntimeError <EOL> self . started = <NUM_LIT:1> <EOL> def stopFactory ( self ) : <EOL> if not self . started or self . stopped : <EOL> raise RuntimeError <EOL> self . stopped = <NUM_LIT:1> <EOL> class FactoryTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_serverStartStop ( self ) : <EOL> """<STR_LIT>""" <EOL> f = StartStopFactory ( ) <EOL> p1 = reactor . listenTCP ( <NUM_LIT:0> , f , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . addCleanup ( p1 . stopListening ) <EOL> self . assertEqual ( ( f . started , f . stopped ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> p2 = reactor . listenTCP ( <NUM_LIT:0> , f , interface = '<STR_LIT:127.0.0.1>' ) <EOL> p3 = reactor . listenTCP ( <NUM_LIT:0> , f , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . assertEqual ( ( f . started , f . stopped ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> d1 = defer . maybeDeferred ( p1 . stopListening ) <EOL> d2 = defer . maybeDeferred ( p2 . stopListening ) <EOL> closedDeferred = defer . gatherResults ( [ d1 , d2 ] ) <EOL> def cbClosed ( ignored ) : <EOL> self . assertEqual ( ( f . started , f . stopped ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> return p3 . stopListening ( ) <EOL> closedDeferred . addCallback ( cbClosed ) <EOL> def cbClosedAll ( ignored ) : <EOL> self . assertEquals ( ( f . started , f . stopped ) , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> closedDeferred . addCallback ( cbClosedAll ) <EOL> return closedDeferred <EOL> def test_clientStartStop ( self ) : <EOL> """<STR_LIT>""" <EOL> f = ClosingFactory ( ) <EOL> p = reactor . listenTCP ( <NUM_LIT:0> , f , interface = "<STR_LIT:127.0.0.1>" ) <EOL> f . port = p <EOL> self . addCleanup ( f . cleanUp ) <EOL> portNumber = p . getHost ( ) . port <EOL> factory = ClientStartStopFactory ( ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , portNumber , factory ) <EOL> self . assertTrue ( factory . started ) <EOL> return loopUntil ( lambda : factory . stopped ) <EOL> class ConnectorTestCase ( unittest . TestCase ) : <EOL> def test_connectorIdentity ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = ClosingFactory ( ) <EOL> tcpPort = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> serverFactory . port = tcpPort <EOL> self . addCleanup ( serverFactory . cleanUp ) <EOL> portNumber = tcpPort . getHost ( ) . port <EOL> seenConnectors = [ ] <EOL> seenFailures = [ ] <EOL> clientFactory = ClientStartStopFactory ( ) <EOL> clientFactory . clientConnectionLost = ( <EOL> lambda connector , reason : ( seenConnectors . append ( connector ) , <EOL> seenFailures . append ( reason ) ) ) <EOL> clientFactory . startedConnecting = seenConnectors . append <EOL> connector = reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , portNumber , clientFactory ) <EOL> self . assertTrue ( interfaces . IConnector . providedBy ( connector ) ) <EOL> dest = connector . getDestination ( ) <EOL> self . assertEquals ( dest . type , "<STR_LIT>" ) <EOL> self . assertEquals ( dest . host , "<STR_LIT:127.0.0.1>" ) <EOL> self . assertEquals ( dest . port , portNumber ) <EOL> d = loopUntil ( lambda : clientFactory . stopped ) <EOL> def clientFactoryStopped ( ignored ) : <EOL> seenFailures [ <NUM_LIT:0> ] . trap ( error . ConnectionDone ) <EOL> self . assertEqual ( seenConnectors , [ connector , connector ] ) <EOL> d . addCallback ( clientFactoryStopped ) <EOL> return d <EOL> def test_userFail ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> tcpPort = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( tcpPort . stopListening ) <EOL> portNumber = tcpPort . getHost ( ) . port <EOL> def startedConnecting ( connector ) : <EOL> connector . stopConnecting ( ) <EOL> clientFactory = ClientStartStopFactory ( ) <EOL> clientFactory . startedConnecting = startedConnecting <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , portNumber , clientFactory ) <EOL> d = loopUntil ( lambda : clientFactory . stopped ) <EOL> def check ( ignored ) : <EOL> self . assertEquals ( clientFactory . failed , <NUM_LIT:1> ) <EOL> clientFactory . reason . trap ( error . UserError ) <EOL> return d . addCallback ( check ) <EOL> def test_reconnect ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = ClosingFactory ( ) <EOL> tcpPort = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> serverFactory . port = tcpPort <EOL> self . addCleanup ( serverFactory . cleanUp ) <EOL> portNumber = tcpPort . getHost ( ) . port <EOL> clientFactory = MyClientFactory ( ) <EOL> def clientConnectionLost ( connector , reason ) : <EOL> connector . connect ( ) <EOL> clientFactory . clientConnectionLost = clientConnectionLost <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , portNumber , clientFactory ) <EOL> d = loopUntil ( lambda : clientFactory . failed ) <EOL> def reconnectFailed ( ignored ) : <EOL> p = clientFactory . protocol <EOL> self . assertEqual ( ( p . made , p . closed ) , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> clientFactory . reason . trap ( error . ConnectionRefusedError ) <EOL> self . assertEqual ( clientFactory . stopped , <NUM_LIT:1> ) <EOL> return d . addCallback ( reconnectFailed ) <EOL> class CannotBindTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_cannotBind ( self ) : <EOL> """<STR_LIT>""" <EOL> f = MyServerFactory ( ) <EOL> p1 = reactor . listenTCP ( <NUM_LIT:0> , f , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . addCleanup ( p1 . stopListening ) <EOL> n = p1 . getHost ( ) . port <EOL> dest = p1 . getHost ( ) <EOL> self . assertEquals ( dest . type , "<STR_LIT>" ) <EOL> self . assertEquals ( dest . host , "<STR_LIT:127.0.0.1>" ) <EOL> self . assertEquals ( dest . port , n ) <EOL> self . assertRaises ( error . CannotListenError , <EOL> reactor . listenTCP , n , f , interface = '<STR_LIT:127.0.0.1>' ) <EOL> def _fireWhenDoneFunc ( self , d , f ) : <EOL> """<STR_LIT>""" <EOL> from twisted . python import util as tputil <EOL> def newf ( * args , ** kw ) : <EOL> rtn = f ( * args , ** kw ) <EOL> d . callback ( '<STR_LIT>' ) <EOL> return rtn <EOL> return tputil . mergeFunctionMetadata ( f , newf ) <EOL> def test_clientBind ( self ) : <EOL> """<STR_LIT>""" <EOL> theDeferred = defer . Deferred ( ) <EOL> sf = MyServerFactory ( ) <EOL> sf . startFactory = self . _fireWhenDoneFunc ( theDeferred , sf . startFactory ) <EOL> p = reactor . listenTCP ( <NUM_LIT:0> , sf , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( p . stopListening ) <EOL> def _connect1 ( results ) : <EOL> d = defer . Deferred ( ) <EOL> cf1 = MyClientFactory ( ) <EOL> cf1 . buildProtocol = self . _fireWhenDoneFunc ( d , cf1 . buildProtocol ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , p . getHost ( ) . port , cf1 , <EOL> bindAddress = ( "<STR_LIT:127.0.0.1>" , <NUM_LIT:0> ) ) <EOL> d . addCallback ( _conmade , cf1 ) <EOL> return d <EOL> def _conmade ( results , cf1 ) : <EOL> d = defer . Deferred ( ) <EOL> cf1 . protocol . connectionMade = self . _fireWhenDoneFunc ( <EOL> d , cf1 . protocol . connectionMade ) <EOL> d . addCallback ( _check1connect2 , cf1 ) <EOL> return d <EOL> def _check1connect2 ( results , cf1 ) : <EOL> self . assertEquals ( cf1 . protocol . made , <NUM_LIT:1> ) <EOL> d1 = defer . Deferred ( ) <EOL> d2 = defer . Deferred ( ) <EOL> port = cf1 . protocol . transport . getHost ( ) . port <EOL> cf2 = MyClientFactory ( ) <EOL> cf2 . clientConnectionFailed = self . _fireWhenDoneFunc ( <EOL> d1 , cf2 . clientConnectionFailed ) <EOL> cf2 . stopFactory = self . _fireWhenDoneFunc ( d2 , cf2 . stopFactory ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , p . getHost ( ) . port , cf2 , <EOL> bindAddress = ( "<STR_LIT:127.0.0.1>" , port ) ) <EOL> d1 . addCallback ( _check2failed , cf1 , cf2 ) <EOL> d2 . addCallback ( _check2stopped , cf1 , cf2 ) <EOL> dl = defer . DeferredList ( [ d1 , d2 ] ) <EOL> dl . addCallback ( _stop , cf1 , cf2 ) <EOL> return dl <EOL> def _check2failed ( results , cf1 , cf2 ) : <EOL> self . assertEquals ( cf2 . failed , <NUM_LIT:1> ) <EOL> cf2 . reason . trap ( error . ConnectBindError ) <EOL> self . assertTrue ( cf2 . reason . check ( error . ConnectBindError ) ) <EOL> return results <EOL> def _check2stopped ( results , cf1 , cf2 ) : <EOL> self . assertEquals ( cf2 . stopped , <NUM_LIT:1> ) <EOL> return results <EOL> def _stop ( results , cf1 , cf2 ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( _check1cleanup , cf1 ) <EOL> cf1 . stopFactory = self . _fireWhenDoneFunc ( d , cf1 . stopFactory ) <EOL> cf1 . protocol . transport . loseConnection ( ) <EOL> return d <EOL> def _check1cleanup ( results , cf1 ) : <EOL> self . assertEquals ( cf1 . stopped , <NUM_LIT:1> ) <EOL> theDeferred . addCallback ( _connect1 ) <EOL> return theDeferred <EOL> class MyOtherClientFactory ( protocol . ClientFactory ) : <EOL> def buildProtocol ( self , address ) : <EOL> self . address = address <EOL> self . protocol = AccumulatingProtocol ( ) <EOL> return self . protocol <EOL> class LocalRemoteAddressTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_hostAddress ( self ) : <EOL> """<STR_LIT>""" <EOL> serverFactory = MyServerFactory ( ) <EOL> serverFactory . protocolConnectionLost = defer . Deferred ( ) <EOL> serverConnectionLost = serverFactory . protocolConnectionLost <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , serverFactory , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . addCleanup ( port . stopListening ) <EOL> n = port . getHost ( ) . port <EOL> clientFactory = MyClientFactory ( ) <EOL> onConnection = clientFactory . protocolConnectionMade = defer . Deferred ( ) <EOL> connector = reactor . connectTCP ( '<STR_LIT:127.0.0.1>' , n , clientFactory ) <EOL> def check ( ignored ) : <EOL> self . assertEquals ( [ port . getHost ( ) ] , clientFactory . peerAddresses ) <EOL> self . assertEquals ( <EOL> port . getHost ( ) , clientFactory . protocol . transport . getPeer ( ) ) <EOL> onConnection . addCallback ( check ) <EOL> def cleanup ( ignored ) : <EOL> connector . disconnect ( ) <EOL> return serverConnectionLost <EOL> onConnection . addCallback ( cleanup ) <EOL> return onConnection <EOL> class WriterProtocol ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . transport . write ( "<STR_LIT>" ) <EOL> seq = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:\n>" ] <EOL> self . transport . writeSequence ( seq ) <EOL> peer = self . transport . getPeer ( ) <EOL> if peer . type != "<STR_LIT>" : <EOL> print "<STR_LIT>" , peer <EOL> self . factory . problem = <NUM_LIT:1> <EOL> us = self . transport . getHost ( ) <EOL> if us . type != "<STR_LIT>" : <EOL> print "<STR_LIT>" , us <EOL> self . factory . problem = <NUM_LIT:1> <EOL> self . factory . done = <NUM_LIT:1> <EOL> self . transport . loseConnection ( ) <EOL> class ReaderProtocol ( protocol . Protocol ) : <EOL> def dataReceived ( self , data ) : <EOL> self . factory . data += data <EOL> def connectionLost ( self , reason ) : <EOL> self . factory . done = <NUM_LIT:1> <EOL> class WriterClientFactory ( protocol . ClientFactory ) : <EOL> def __init__ ( self ) : <EOL> self . done = <NUM_LIT:0> <EOL> self . data = "<STR_LIT>" <EOL> def buildProtocol ( self , addr ) : <EOL> p = ReaderProtocol ( ) <EOL> p . factory = self <EOL> self . protocol = p <EOL> return p <EOL> class WriteDataTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_writer ( self ) : <EOL> """<STR_LIT>""" <EOL> f = protocol . Factory ( ) <EOL> f . protocol = WriterProtocol <EOL> f . done = <NUM_LIT:0> <EOL> f . problem = <NUM_LIT:0> <EOL> wrappedF = WiredFactory ( f ) <EOL> p = reactor . listenTCP ( <NUM_LIT:0> , wrappedF , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( p . stopListening ) <EOL> n = p . getHost ( ) . port <EOL> clientF = WriterClientFactory ( ) <EOL> wrappedClientF = WiredFactory ( clientF ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , n , wrappedClientF ) <EOL> def check ( ignored ) : <EOL> self . failUnless ( f . done , "<STR_LIT>" ) <EOL> self . failUnless ( f . problem == <NUM_LIT:0> , "<STR_LIT>" ) <EOL> self . failUnless ( clientF . done , <EOL> "<STR_LIT>" ) <EOL> expected = "<STR_LIT>" . join ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:\n>" ] ) <EOL> self . failUnless ( clientF . data == expected , <EOL> "<STR_LIT>" ) <EOL> d = defer . gatherResults ( [ wrappedF . onDisconnect , <EOL> wrappedClientF . onDisconnect ] ) <EOL> return d . addCallback ( check ) <EOL> def test_writeAfterShutdownWithoutReading ( self ) : <EOL> """<STR_LIT>""" <EOL> if reactor . __class__ . __name__ == '<STR_LIT>' : <EOL> raise unittest . SkipTest ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if reactor . __class__ . __name__ == '<STR_LIT>' : <EOL> raise unittest . SkipTest ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> clientPaused = defer . Deferred ( ) <EOL> serverLost = defer . Deferred ( ) <EOL> class Disconnecter ( protocol . Protocol ) : <EOL> """<STR_LIT>""" <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> msg ( '<STR_LIT>' ) <EOL> def disconnect ( ignored ) : <EOL> msg ( '<STR_LIT>' ) <EOL> self . transport . loseConnection ( ) <EOL> msg ( '<STR_LIT>' ) <EOL> clientPaused . addCallback ( disconnect ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> msg ( '<STR_LIT>' ) <EOL> serverLost . callback ( None ) <EOL> msg ( '<STR_LIT>' ) <EOL> server = protocol . ServerFactory ( ) <EOL> server . protocol = Disconnecter <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , server , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . addCleanup ( port . stopListening ) <EOL> addr = port . getHost ( ) <EOL> class Infinite ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( IPullProducer ) <EOL> def __init__ ( self , consumer ) : <EOL> self . consumer = consumer <EOL> def resumeProducing ( self ) : <EOL> msg ( '<STR_LIT>' ) <EOL> self . consumer . write ( '<STR_LIT:x>' ) <EOL> msg ( '<STR_LIT>' ) <EOL> def stopProducing ( self ) : <EOL> msg ( '<STR_LIT>' ) <EOL> class UnreadingWriter ( protocol . Protocol ) : <EOL> """<STR_LIT>""" <EOL> def connectionMade ( self ) : <EOL> msg ( '<STR_LIT>' ) <EOL> self . transport . pauseProducing ( ) <EOL> clientPaused . callback ( None ) <EOL> msg ( '<STR_LIT>' ) <EOL> def write ( ignored ) : <EOL> msg ( '<STR_LIT>' ) <EOL> producer = Infinite ( self . transport ) <EOL> msg ( '<STR_LIT>' ) <EOL> self . transport . registerProducer ( producer , False ) <EOL> msg ( '<STR_LIT>' ) <EOL> serverLost . addCallback ( write ) <EOL> client = MyClientFactory ( ) <EOL> client . protocolFactory = UnreadingWriter <EOL> clientConnectionLost = client . deferred <EOL> def cbClientLost ( ignored ) : <EOL> msg ( '<STR_LIT>' ) <EOL> return client . lostReason <EOL> clientConnectionLost . addCallback ( cbClientLost ) <EOL> msg ( '<STR_LIT>' % ( addr . host , addr . port ) ) <EOL> reactor . connectTCP ( addr . host , addr . port , client ) <EOL> msg ( '<STR_LIT>' ) <EOL> return self . assertFailure ( clientConnectionLost , error . ConnectionLost ) <EOL> class ConnectionLosingProtocol ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . transport . write ( "<STR_LIT:1>" ) <EOL> self . transport . loseConnection ( ) <EOL> self . master . _connectionMade ( ) <EOL> self . master . ports . append ( self . transport ) <EOL> class NoopProtocol ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . d = defer . Deferred ( ) <EOL> self . master . serverConns . append ( self . d ) <EOL> def connectionLost ( self , reason ) : <EOL> self . d . callback ( True ) <EOL> class ConnectionLostNotifyingProtocol ( protocol . Protocol ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , onConnectionLost ) : <EOL> self . lostConnectionReason = None <EOL> self . onConnectionLost = onConnectionLost <EOL> def connectionLost ( self , reason ) : <EOL> self . lostConnectionReason = reason <EOL> self . onConnectionLost . callback ( self ) <EOL> class HandleSavingProtocol ( ConnectionLostNotifyingProtocol ) : <EOL> """<STR_LIT>""" <EOL> def makeConnection ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> self . handle = transport . getHandle ( ) <EOL> return protocol . Protocol . makeConnection ( self , transport ) <EOL> class ProperlyCloseFilesMixin : <EOL> """<STR_LIT>""" <EOL> def createServer ( self , address , portNumber , factory ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def connectClient ( self , address , portNumber , clientCreator ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def getHandleExceptionType ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def getHandleErrorCode ( self ) : <EOL> """<STR_LIT>""" <EOL> return errno . EBADF <EOL> def test_properlyCloseFiles ( self ) : <EOL> """<STR_LIT>""" <EOL> onServerConnectionLost = defer . Deferred ( ) <EOL> serverFactory = protocol . ServerFactory ( ) <EOL> serverFactory . protocol = lambda : ConnectionLostNotifyingProtocol ( <EOL> onServerConnectionLost ) <EOL> serverPort = self . createServer ( '<STR_LIT:127.0.0.1>' , <NUM_LIT:0> , serverFactory ) <EOL> onClientConnectionLost = defer . Deferred ( ) <EOL> serverAddr = serverPort . getHost ( ) <EOL> clientCreator = protocol . ClientCreator ( <EOL> reactor , lambda : HandleSavingProtocol ( onClientConnectionLost ) ) <EOL> clientDeferred = self . connectClient ( <EOL> serverAddr . host , serverAddr . port , clientCreator ) <EOL> def clientConnected ( client ) : <EOL> """<STR_LIT>""" <EOL> client . transport . write ( <EOL> '<STR_LIT>' ) <EOL> client . transport . loseConnection ( ) <EOL> return defer . gatherResults ( [ <EOL> onClientConnectionLost , onServerConnectionLost ] ) <EOL> clientDeferred . addCallback ( clientConnected ) <EOL> def clientDisconnected ( ( client , server ) ) : <EOL> """<STR_LIT>""" <EOL> client . lostConnectionReason . trap ( error . ConnectionClosed ) <EOL> server . lostConnectionReason . trap ( error . ConnectionClosed ) <EOL> expectedErrorCode = self . getHandleErrorCode ( ) <EOL> err = self . assertRaises ( <EOL> self . getHandleExceptionType ( ) , client . handle . send , '<STR_LIT>' ) <EOL> self . assertEqual ( err . args [ <NUM_LIT:0> ] , expectedErrorCode ) <EOL> clientDeferred . addCallback ( clientDisconnected ) <EOL> def cleanup ( passthrough ) : <EOL> """<STR_LIT>""" <EOL> result = defer . maybeDeferred ( serverPort . stopListening ) <EOL> result . addCallback ( lambda ign : passthrough ) <EOL> return result <EOL> clientDeferred . addBoth ( cleanup ) <EOL> return clientDeferred <EOL> class ProperlyCloseFilesTestCase ( unittest . TestCase , ProperlyCloseFilesMixin ) : <EOL> """<STR_LIT>""" <EOL> def createServer ( self , address , portNumber , factory ) : <EOL> """<STR_LIT>""" <EOL> return reactor . listenTCP ( portNumber , factory , interface = address ) <EOL> def connectClient ( self , address , portNumber , clientCreator ) : <EOL> """<STR_LIT>""" <EOL> return clientCreator . connectTCP ( address , portNumber ) <EOL> def getHandleExceptionType ( self ) : <EOL> """<STR_LIT>""" <EOL> return socket . error <EOL> class WiredForDeferreds ( policies . ProtocolWrapper ) : <EOL> def __init__ ( self , factory , wrappedProtocol ) : <EOL> policies . ProtocolWrapper . __init__ ( self , factory , wrappedProtocol ) <EOL> def connectionMade ( self ) : <EOL> policies . ProtocolWrapper . connectionMade ( self ) <EOL> self . factory . onConnect . callback ( None ) <EOL> def connectionLost ( self , reason ) : <EOL> policies . ProtocolWrapper . connectionLost ( self , reason ) <EOL> self . factory . onDisconnect . callback ( None ) <EOL> class WiredFactory ( policies . WrappingFactory ) : <EOL> protocol = WiredForDeferreds <EOL> def __init__ ( self , wrappedFactory ) : <EOL> policies . WrappingFactory . __init__ ( self , wrappedFactory ) <EOL> self . onConnect = defer . Deferred ( ) <EOL> self . onDisconnect = defer . Deferred ( ) <EOL> class AddressTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> class RememberingWrapper ( protocol . ClientFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , factory ) : <EOL> self . addresses = [ ] <EOL> self . factory = factory <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> self . addresses . append ( addr ) <EOL> return self . factory . buildProtocol ( addr ) <EOL> self . server = MyServerFactory ( ) <EOL> self . serverConnMade = self . server . protocolConnectionMade = defer . Deferred ( ) <EOL> self . serverConnLost = self . server . protocolConnectionLost = defer . Deferred ( ) <EOL> self . serverWrapper = RememberingWrapper ( self . server ) <EOL> self . client = MyClientFactory ( ) <EOL> self . clientConnMade = self . client . protocolConnectionMade = defer . Deferred ( ) <EOL> self . clientConnLost = self . client . protocolConnectionLost = defer . Deferred ( ) <EOL> self . clientWrapper = RememberingWrapper ( self . client ) <EOL> self . port = reactor . listenTCP ( <NUM_LIT:0> , self . serverWrapper , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . connector = reactor . connectTCP ( <EOL> self . port . getHost ( ) . host , self . port . getHost ( ) . port , self . clientWrapper ) <EOL> return defer . gatherResults ( [ self . serverConnMade , self . clientConnMade ] ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . connector . disconnect ( ) <EOL> return defer . gatherResults ( [ <EOL> self . serverConnLost , self . clientConnLost , <EOL> defer . maybeDeferred ( self . port . stopListening ) ] ) <EOL> def test_buildProtocolClient ( self ) : <EOL> """<STR_LIT>""" <EOL> serverHost = self . server . protocol . transport . getHost ( ) <EOL> clientPeer = self . client . protocol . transport . getPeer ( ) <EOL> self . assertEqual ( <EOL> self . clientWrapper . addresses , <EOL> [ IPv4Address ( '<STR_LIT>' , serverHost . host , serverHost . port ) ] ) <EOL> self . assertEqual ( <EOL> self . clientWrapper . addresses , <EOL> [ IPv4Address ( '<STR_LIT>' , clientPeer . host , clientPeer . port ) ] ) <EOL> def test_buildProtocolServer ( self ) : <EOL> """<STR_LIT>""" <EOL> clientHost = self . client . protocol . transport . getHost ( ) <EOL> serverPeer = self . server . protocol . transport . getPeer ( ) <EOL> self . assertEqual ( <EOL> self . serverWrapper . addresses , <EOL> [ IPv4Address ( '<STR_LIT>' , serverPeer . host , serverPeer . port ) ] ) <EOL> self . assertEqual ( <EOL> self . serverWrapper . addresses , <EOL> [ IPv4Address ( '<STR_LIT>' , clientHost . host , clientHost . port ) ] ) <EOL> class LargeBufferWriterProtocol ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . transport . write ( '<STR_LIT:X>' * self . factory . len ) <EOL> self . factory . done = <NUM_LIT:1> <EOL> self . transport . loseConnection ( ) <EOL> class LargeBufferReaderProtocol ( protocol . Protocol ) : <EOL> def dataReceived ( self , data ) : <EOL> self . factory . len += len ( data ) <EOL> def connectionLost ( self , reason ) : <EOL> self . factory . done = <NUM_LIT:1> <EOL> class LargeBufferReaderClientFactory ( protocol . ClientFactory ) : <EOL> def __init__ ( self ) : <EOL> self . done = <NUM_LIT:0> <EOL> self . len = <NUM_LIT:0> <EOL> def buildProtocol ( self , addr ) : <EOL> p = LargeBufferReaderProtocol ( ) <EOL> p . factory = self <EOL> self . protocol = p <EOL> return p <EOL> class FireOnClose ( policies . ProtocolWrapper ) : <EOL> """<STR_LIT>""" <EOL> def connectionLost ( self , reason ) : <EOL> policies . ProtocolWrapper . connectionLost ( self , reason ) <EOL> self . factory . deferred . callback ( None ) <EOL> class FireOnCloseFactory ( policies . WrappingFactory ) : <EOL> protocol = FireOnClose <EOL> def __init__ ( self , wrappedFactory ) : <EOL> policies . WrappingFactory . __init__ ( self , wrappedFactory ) <EOL> self . deferred = defer . Deferred ( ) <EOL> class LargeBufferTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> datalen = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> def testWriter ( self ) : <EOL> f = protocol . Factory ( ) <EOL> f . protocol = LargeBufferWriterProtocol <EOL> f . done = <NUM_LIT:0> <EOL> f . problem = <NUM_LIT:0> <EOL> f . len = self . datalen <EOL> wrappedF = FireOnCloseFactory ( f ) <EOL> p = reactor . listenTCP ( <NUM_LIT:0> , wrappedF , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( p . stopListening ) <EOL> n = p . getHost ( ) . port <EOL> clientF = LargeBufferReaderClientFactory ( ) <EOL> wrappedClientF = FireOnCloseFactory ( clientF ) <EOL> reactor . connectTCP ( "<STR_LIT:127.0.0.1>" , n , wrappedClientF ) <EOL> d = defer . gatherResults ( [ wrappedF . deferred , wrappedClientF . deferred ] ) <EOL> def check ( ignored ) : <EOL> self . failUnless ( f . done , "<STR_LIT>" ) <EOL> self . failUnless ( clientF . len == self . datalen , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( clientF . len , self . datalen ) ) <EOL> self . failUnless ( clientF . done , <EOL> "<STR_LIT>" ) <EOL> return d . addCallback ( check ) <EOL> class MyHCProtocol ( AccumulatingProtocol ) : <EOL> implements ( IHalfCloseableProtocol ) <EOL> readHalfClosed = False <EOL> writeHalfClosed = False <EOL> def readConnectionLost ( self ) : <EOL> self . readHalfClosed = True <EOL> if self . writeHalfClosed : <EOL> self . connectionLost ( None ) <EOL> def writeConnectionLost ( self ) : <EOL> self . writeHalfClosed = True <EOL> if self . readHalfClosed : <EOL> self . connectionLost ( None ) <EOL> class MyHCFactory ( protocol . ServerFactory ) : <EOL> called = <NUM_LIT:0> <EOL> protocolConnectionMade = None <EOL> def buildProtocol ( self , addr ) : <EOL> self . called += <NUM_LIT:1> <EOL> p = MyHCProtocol ( ) <EOL> p . factory = self <EOL> self . protocol = p <EOL> return p <EOL> class HalfCloseTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . f = f = MyHCFactory ( ) <EOL> self . p = p = reactor . listenTCP ( <NUM_LIT:0> , f , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( p . stopListening ) <EOL> d = loopUntil ( lambda : p . connected ) <EOL> self . cf = protocol . ClientCreator ( reactor , MyHCProtocol ) <EOL> d . addCallback ( lambda _ : self . cf . connectTCP ( p . getHost ( ) . host , <EOL> p . getHost ( ) . port ) ) <EOL> d . addCallback ( self . _setUp ) <EOL> return d <EOL> def _setUp ( self , client ) : <EOL> self . client = client <EOL> self . clientProtoConnectionLost = self . client . closedDeferred = defer . Deferred ( ) <EOL> self . assertEquals ( self . client . transport . connected , <NUM_LIT:1> ) <EOL> return loopUntil ( lambda : getattr ( self . f , '<STR_LIT>' , None ) is not None ) <EOL> def tearDown ( self ) : <EOL> self . assertEquals ( self . client . closed , <NUM_LIT:0> ) <EOL> self . client . transport . loseConnection ( ) <EOL> d = defer . maybeDeferred ( self . p . stopListening ) <EOL> d . addCallback ( lambda ign : self . clientProtoConnectionLost ) <EOL> d . addCallback ( self . _tearDown ) <EOL> return d <EOL> def _tearDown ( self , ignored ) : <EOL> self . assertEquals ( self . client . closed , <NUM_LIT:1> ) <EOL> self . assertEquals ( self . f . protocol . closed , <NUM_LIT:0> ) <EOL> d = defer . Deferred ( ) <EOL> def _connectionLost ( reason ) : <EOL> self . f . protocol . closed = <NUM_LIT:1> <EOL> d . callback ( None ) <EOL> self . f . protocol . connectionLost = _connectionLost <EOL> self . f . protocol . transport . loseConnection ( ) <EOL> d . addCallback ( lambda x : self . assertEquals ( self . f . protocol . closed , <NUM_LIT:1> ) ) <EOL> return d <EOL> def testCloseWriteCloser ( self ) : <EOL> client = self . client <EOL> f = self . f <EOL> t = client . transport <EOL> t . write ( "<STR_LIT:hello>" ) <EOL> d = loopUntil ( lambda : len ( t . _tempDataBuffer ) == <NUM_LIT:0> ) <EOL> def loseWrite ( ignored ) : <EOL> t . loseWriteConnection ( ) <EOL> return loopUntil ( lambda : t . _writeDisconnected ) <EOL> def check ( ignored ) : <EOL> self . assertEquals ( client . closed , False ) <EOL> self . assertEquals ( client . writeHalfClosed , True ) <EOL> self . assertEquals ( client . readHalfClosed , False ) <EOL> return loopUntil ( lambda : f . protocol . readHalfClosed ) <EOL> def write ( ignored ) : <EOL> w = client . transport . write <EOL> w ( "<STR_LIT>" ) <EOL> w ( "<STR_LIT>" ) <EOL> self . assertEquals ( <NUM_LIT:0> , len ( client . transport . _tempDataBuffer ) ) <EOL> self . assertEquals ( f . protocol . data , "<STR_LIT:hello>" ) <EOL> self . assertEquals ( f . protocol . closed , False ) <EOL> self . assertEquals ( f . protocol . readHalfClosed , True ) <EOL> return d . addCallback ( loseWrite ) . addCallback ( check ) . addCallback ( write ) <EOL> def testWriteCloseNotification ( self ) : <EOL> f = self . f <EOL> f . protocol . transport . loseWriteConnection ( ) <EOL> d = defer . gatherResults ( [ <EOL> loopUntil ( lambda : f . protocol . writeHalfClosed ) , <EOL> loopUntil ( lambda : self . client . readHalfClosed ) ] ) <EOL> d . addCallback ( lambda _ : self . assertEquals ( <EOL> f . protocol . readHalfClosed , False ) ) <EOL> return d <EOL> class HalfClose2TestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . f = f = MyServerFactory ( ) <EOL> self . f . protocolConnectionMade = defer . Deferred ( ) <EOL> self . p = p = reactor . listenTCP ( <NUM_LIT:0> , f , interface = "<STR_LIT:127.0.0.1>" ) <EOL> d = protocol . ClientCreator ( reactor , AccumulatingProtocol ) . connectTCP ( <EOL> p . getHost ( ) . host , p . getHost ( ) . port ) <EOL> d . addCallback ( self . _gotClient ) <EOL> return d <EOL> def _gotClient ( self , client ) : <EOL> self . client = client <EOL> return self . f . protocolConnectionMade <EOL> def tearDown ( self ) : <EOL> self . client . transport . loseConnection ( ) <EOL> return self . p . stopListening ( ) <EOL> def testNoNotification ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . transport . write ( "<STR_LIT:hello>" ) <EOL> self . client . transport . loseWriteConnection ( ) <EOL> self . f . protocol . closedDeferred = d = defer . Deferred ( ) <EOL> self . client . closedDeferred = d2 = defer . Deferred ( ) <EOL> d . addCallback ( lambda x : <EOL> self . assertEqual ( self . f . protocol . data , '<STR_LIT:hello>' ) ) <EOL> d . addCallback ( lambda x : self . assertEqual ( self . f . protocol . closed , True ) ) <EOL> return defer . gatherResults ( [ d , d2 ] ) <EOL> def testShutdownException ( self ) : <EOL> """<STR_LIT>""" <EOL> self . f . protocol . transport . loseConnection ( ) <EOL> self . client . transport . write ( "<STR_LIT:X>" ) <EOL> self . client . transport . loseWriteConnection ( ) <EOL> self . f . protocol . closedDeferred = d = defer . Deferred ( ) <EOL> self . client . closedDeferred = d2 = defer . Deferred ( ) <EOL> d . addCallback ( lambda x : <EOL> self . failUnlessEqual ( self . f . protocol . closed , True ) ) <EOL> return defer . gatherResults ( [ d , d2 ] ) <EOL> class HalfCloseBuggyApplicationTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . serverFactory = MyHCFactory ( ) <EOL> self . serverFactory . protocolConnectionMade = defer . Deferred ( ) <EOL> self . port = reactor . listenTCP ( <EOL> <NUM_LIT:0> , self . serverFactory , interface = "<STR_LIT:127.0.0.1>" ) <EOL> self . addCleanup ( self . port . stopListening ) <EOL> addr = self . port . getHost ( ) <EOL> creator = protocol . ClientCreator ( reactor , MyHCProtocol ) <EOL> clientDeferred = creator . connectTCP ( addr . host , addr . port ) <EOL> def setClient ( clientProtocol ) : <EOL> self . clientProtocol = clientProtocol <EOL> clientDeferred . addCallback ( setClient ) <EOL> return defer . gatherResults ( [ <EOL> self . serverFactory . protocolConnectionMade , <EOL> clientDeferred ] ) <EOL> def aBug ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _notificationRaisesTest ( self ) : <EOL> """<STR_LIT>""" <EOL> closed = self . clientProtocol . closedDeferred = defer . Deferred ( ) <EOL> self . clientProtocol . transport . loseWriteConnection ( ) <EOL> def check ( ignored ) : <EOL> errors = self . flushLoggedErrors ( RuntimeError ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> closed . addCallback ( check ) <EOL> return closed <EOL> def test_readNotificationRaises ( self ) : <EOL> """<STR_LIT>""" <EOL> self . serverFactory . protocol . readConnectionLost = self . aBug <EOL> return self . _notificationRaisesTest ( ) <EOL> def test_writeNotificationRaises ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clientProtocol . writeConnectionLost = self . aBug <EOL> return self . _notificationRaisesTest ( ) <EOL> class LogTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_logstrClientSetup ( self ) : <EOL> """<STR_LIT>""" <EOL> server = MyServerFactory ( ) <EOL> client = MyClientFactory ( ) <EOL> client . protocolConnectionMade = defer . Deferred ( ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , server , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . addCleanup ( port . stopListening ) <EOL> connector = reactor . connectTCP ( <EOL> port . getHost ( ) . host , port . getHost ( ) . port , client ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> self . assertEquals ( connector . transport . logstr , <EOL> "<STR_LIT>" ) <EOL> def cb ( ign ) : <EOL> self . assertEquals ( connector . transport . logstr , <EOL> "<STR_LIT>" ) <EOL> client . protocolConnectionMade . addCallback ( cb ) <EOL> return client . protocolConnectionMade <EOL> class PauseProducingTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_pauseProducingInConnectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> server = MyServerFactory ( ) <EOL> client = MyClientFactory ( ) <EOL> client . protocolConnectionMade = defer . Deferred ( ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , server , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . addCleanup ( port . stopListening ) <EOL> connector = reactor . connectTCP ( <EOL> port . getHost ( ) . host , port . getHost ( ) . port , client ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> def checkInConnectionMade ( proto ) : <EOL> tr = proto . transport <EOL> self . assertIn ( tr , reactor . getReaders ( ) + <EOL> reactor . getWriters ( ) ) <EOL> proto . transport . pauseProducing ( ) <EOL> self . assertNotIn ( tr , reactor . getReaders ( ) + <EOL> reactor . getWriters ( ) ) <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( checkAfterConnectionMade ) <EOL> reactor . callLater ( <NUM_LIT:0> , d . callback , proto ) <EOL> return d <EOL> def checkAfterConnectionMade ( proto ) : <EOL> tr = proto . transport <EOL> self . assertNotIn ( tr , reactor . getReaders ( ) + <EOL> reactor . getWriters ( ) ) <EOL> client . protocolConnectionMade . addCallback ( checkInConnectionMade ) <EOL> return client . protocolConnectionMade <EOL> if not interfaces . IReactorFDSet . providedBy ( reactor ) : <EOL> test_pauseProducingInConnectionMade . skip = "<STR_LIT>" <EOL> class CallBackOrderTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_loseOrder ( self ) : <EOL> """<STR_LIT>""" <EOL> server = MyServerFactory ( ) <EOL> server . protocolConnectionMade = ( defer . Deferred ( ) <EOL> . addCallback ( lambda proto : self . addCleanup ( <EOL> proto . transport . loseConnection ) ) ) <EOL> client = MyClientFactory ( ) <EOL> client . protocolConnectionLost = defer . Deferred ( ) <EOL> client . protocolConnectionMade = defer . Deferred ( ) <EOL> def _cbCM ( res ) : <EOL> """<STR_LIT>""" <EOL> reactor . callLater ( <NUM_LIT:0> , client . protocol . transport . loseConnection ) <EOL> client . protocolConnectionMade . addCallback ( _cbCM ) <EOL> port = reactor . listenTCP ( <NUM_LIT:0> , server , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . addCleanup ( port . stopListening ) <EOL> connector = reactor . connectTCP ( <EOL> port . getHost ( ) . host , port . getHost ( ) . port , client ) <EOL> self . addCleanup ( connector . disconnect ) <EOL> def _cbCCL ( res ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def _cbCL ( res ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def _cbGather ( res ) : <EOL> self . assertEquals ( res , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> d = defer . gatherResults ( [ <EOL> client . protocolConnectionLost . addCallback ( _cbCL ) , <EOL> client . deferred . addCallback ( _cbCCL ) ] ) <EOL> return d . addCallback ( _cbGather ) <EOL> try : <EOL> import resource <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> numRounds = resource . getrlimit ( resource . RLIMIT_NOFILE ) [ <NUM_LIT:0> ] + <NUM_LIT:10> <EOL> ProperlyCloseFilesTestCase . numberRounds = numRounds </s>
