def test_failedBuildProtocol(self): """ An exception raised in C{buildProtocol} of our wrappedFactory results in our C{onConnection} errback being fired. """ class BogusFactory(ClientFactory): """ A one off factory whose C{buildProtocol} raises an C{Exception}. """ def buildProtocol(self, addr): raise ValueError("My protocol is poorly defined.") wf = endpoints._WrappingFactory(BogusFactory()) wf.buildProtocol(None) d = self.assertFailure(wf._onConnection, ValueError) d.addCallback(lambda e: self.assertEqual( e.args, ("My protocol is poorly defined.",))) return d
def test_logPrefixPassthrough(self): """ If the wrapped protocol provides L{ILoggingContext}, whatever is returned from the wrapped C{logPrefix} method is returned from L{_WrappingProtocol.logPrefix}. """ wf = endpoints._WrappingFactory(TestFactory()) wp = wf.buildProtocol(None) self.assertEqual(wp.logPrefix(), "A Test Protocol")
def test_doStart(self): """ L{_WrappingFactory.doStart} passes through to the wrapped factory's C{doStart} method, allowing application-specific setup and logging. """ factory = ClientFactory() wf = endpoints._WrappingFactory(factory) wf.doStart() self.assertEqual(1, factory.numPorts)
def connect(self, protocolFactory): """ Implement L{IStreamClientEndpoint.connect} to connect via a UNIX Socket """ try: wf = _WrappingFactory(protocolFactory) self._reactor.connectUNIX(self._path, wf, timeout=self._timeout, checkPID=self._checkPID) return wf._onConnection except: return defer.fail()
def test_logPrefixDefault(self): """ If the wrapped protocol does not provide L{ILoggingContext}, the wrapped protocol's class name is returned from L{_WrappingProtocol.logPrefix}. """ class NoProtocol(object): pass factory = TestFactory() factory.protocol = NoProtocol wf = endpoints._WrappingFactory(factory) wp = wf.buildProtocol(None) self.assertEqual(wp.logPrefix(), "NoProtocol")
def test_wrappedProtocolConnectionLost(self): """ Our wrappedProtocol's connectionLost method is called when L{_WrappingProtocol.connectionLost} is called. """ tf = TestFactory() wf = endpoints._WrappingFactory(tf) p = wf.buildProtocol(None) p.connectionLost("fail") self.assertEqual(p._wrappedProtocol.connectionsLost, ["fail"])
def test_wrappedProtocolDataReceived(self): """ The wrapped C{Protocol}'s C{dataReceived} will get called when our C{_WrappingProtocol}'s C{dataReceived} gets called. """ wf = endpoints._WrappingFactory(TestFactory()) p = wf.buildProtocol(None) p.makeConnection(None) p.dataReceived(b'foo') self.assertEqual(p._wrappedProtocol.data, [b'foo']) p.dataReceived(b'bar') self.assertEqual(p._wrappedProtocol.data, [b'foo', b'bar'])
def connect(self, protocolFactory): """ Implement L{IStreamClientEndpoint.connect} to connect via a UNIX Socket """ try: wf = _WrappingFactory(protocolFactory) self._reactor.connectUNIX( self._path, wf, timeout=self._timeout, checkPID=self._checkPID) return wf._onConnection except: return defer.fail()
def test_wrappedProtocolTransport(self): """ Our transport is properly hooked up to the wrappedProtocol when a connection is made. """ wf = endpoints._WrappingFactory(TestFactory()) p = wf.buildProtocol(None) dummyTransport = object() p.makeConnection(dummyTransport) self.assertEqual(p.transport, dummyTransport) self.assertEqual(p._wrappedProtocol.transport, dummyTransport)
def connect(self, protocolFactory): """ Implement L{IStreamClientEndpoint.connect} to connect with SSL over TCP. """ try: wf = _WrappingFactory(protocolFactory) self._reactor.connectSSL( self._host, self._port, wf, self._sslContextFactory, timeout=self._timeout, bindAddress=self._bindAddress, ) return wf._onConnection except: return defer.fail()
def test_clientConnectionFailed(self): """ Calls to L{_WrappingFactory.clientConnectionLost} should errback the L{_WrappingFactory._onConnection} L{Deferred} """ wf = endpoints._WrappingFactory(TestFactory()) expectedFailure = Failure(error.ConnectError(string="fail")) wf.clientConnectionFailed(None, expectedFailure) errors = [] def gotError(f): errors.append(f) wf._onConnection.addErrback(gotError) self.assertEqual(errors, [expectedFailure])
def test_clientConnectionFailed(self): """ Calls to L{_WrappingFactory.clientConnectionLost} should errback the L{_WrappingFactory._onConnection} L{Deferred} """ wf = endpoints._WrappingFactory(TestFactory()) expectedFailure = Failure(error.ConnectError(string="fail")) wf.clientConnectionFailed( None, expectedFailure) errors = [] def gotError(f): errors.append(f) wf._onConnection.addErrback(gotError) self.assertEqual(errors, [expectedFailure])
def test_failedBuildProtocol(self): """ An exception raised in C{buildProtocol} of our wrappedFactory results in our C{onConnection} errback being fired. """ class BogusFactory(ClientFactory): """ A one off factory whose C{buildProtocol} raises an C{Exception}. """ def buildProtocol(self, addr): raise ValueError("My protocol is poorly defined.") wf = endpoints._WrappingFactory(BogusFactory()) wf.buildProtocol(None) d = self.assertFailure(wf._onConnection, ValueError) d.addCallback(lambda e: self.assertEqual(e.args, ( "My protocol is poorly defined.", ))) return d