def test_data_cancels_timeout(self): """ When data is received, the timeout is canceled """ clock = Clock() protocol = HangCheckProtocol(Protocol(), reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.dataReceived('some-data') assert_clock_idle(self, clock)
def test_transport(self): """ The transport passed to the underlying protocol is the underlying transport. """ clock = Clock() wrapped_protocol = Protocol() protocol = HangCheckProtocol(wrapped_protocol, reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) self.assertIdentical(wrapped_protocol.transport, transport)
def test_disconnects(self): """ When connecting to a server that doesn't send any data, the protocol disconnects after the timeout. """ clock = Clock() protocol = HangCheckProtocol(Protocol(), reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) clock.advance(120) self.assertTrue(transport.disconnecting) assert_clock_idle(self, clock)
def connect(self, factory): """ Connect the given L{IProtocolFactory} to a L{StringTransport} and return a fired L{Deferred}. @param factory: see L{IStreamClientEndpoint} @return: see L{IStreamClientEndpoint} """ protocol = factory.buildProtocol(None) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) self.transports.append(transport) return succeed(protocol)
def test_forwards_data(self): """ Data received by the protocol gets passed to the wrapped protocol. """ clock = Clock() wrapped_protocol = AccumulatingProtocol() protocol = HangCheckProtocol(wrapped_protocol, reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.dataReceived('some-data') self.assertEqual(wrapped_protocol.data, "some-data")
def test_disconnect_cancels_timeout(self): """ If the connection is closed, the hang check is cancelled. """ clock = Clock() protocol = HangCheckProtocol( Protocol(), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.connectionLost(Failure(ConnectionDone("Bye."))) assert_clock_idle(self, clock)
def test_data_and_disconnect(self): """ If the connection receives data and then is closed, no error results. """ clock = Clock() protocol = HangCheckProtocol( Protocol(), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.dataReceived("some-data") protocol.connectionLost(Failure(ConnectionDone("Bye."))) assert_clock_idle(self, clock)
def test_disconnect_cancels_timeout(self): """ If the connection is closed, the hang check is cancelled. """ clock = Clock() protocol = HangCheckProtocol( Protocol(), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.connectionLost( Failure(ConnectionDone("Bye.")) ) assert_clock_idle(self, clock)
def test_disconnect_forwarded(self): """ If the connection is closed, the underlying protocol is informed. """ clock = Clock() wrapped_protocol = AccumulatingProtocol() protocol = HangCheckProtocol(wrapped_protocol, reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) reason = ConnectionDone("Bye.") protocol.connectionLost(Failure(reason)) self.assertTrue(wrapped_protocol.closed) self.assertEqual( wrapped_protocol.closedReason.value, reason, )
def test_data_and_disconnect(self): """ If the connection receives data and then is closed, no error results. """ clock = Clock() protocol = HangCheckProtocol( Protocol(), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.dataReceived("some-data") protocol.connectionLost( Failure(ConnectionDone("Bye.")) ) assert_clock_idle(self, clock)
def test_calls_callback(self): """ When connecting to a server that doesn't send any data, the protocol calls the hung callback. """ results = [] clock = Clock() protocol = HangCheckProtocol( Protocol(), hung_callback=lambda: results.append(True), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) clock.advance(120) self.assertEqual(results, [True]) assert_clock_idle(self, clock)
def _timeoutTest(self, onDone, clientFactory): """ Connect the clientFactory, and check the timeout on the request. """ clock = task.Clock() client = clientFactory.buildProtocol( address.IPv4Address('TCP', 'example.net', 25)) client.callLater = clock.callLater t = StringTransport() client.makeConnection(t) t.protocol = client def check(ign): self.assertEquals(clock.seconds(), 0.5) d = self.assertFailure(onDone, smtp.SMTPTimeoutError ).addCallback(check) # The first call should not trigger the timeout clock.advance(0.1) # But this one should clock.advance(0.4) return d
def test_disconnect_forwarded(self): """ If the connection is closed, the underlying protocol is informed. """ clock = Clock() wrapped_protocol = AccumulatingProtocol() protocol = HangCheckProtocol(wrapped_protocol, reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) reason = ConnectionDone("Bye.") protocol.connectionLost( Failure(reason) ) self.assertTrue(wrapped_protocol.closed) self.assertEqual( wrapped_protocol.closedReason.value, reason, )