def setUpClass(self): self.clock = Clock() self.clock.install()
class TimeoutTestCase(unittest.TestCase): def setUpClass(self): self.clock = Clock() self.clock.install() def tearDownClass(self): self.clock.uninstall() def _serverSetup(self): # Create a server factory, get a protocol from it, connect it to a # transport, and return all three. wrappedFactory = protocol.ServerFactory() wrappedFactory.protocol = SimpleProtocol factory = policies.TimeoutFactory(wrappedFactory, 3) proto = factory.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 12345)) transport = StringTransportWithDisconnection() transport.protocol = proto proto.makeConnection(transport) return factory, proto, transport def testTimeout(self): # Make sure that when a TimeoutFactory accepts a connection, it will # time out that connection if no data is read or written within the # timeout period. # Make the server-side connection factory, proto, transport = self._serverSetup() # Let almost 3 time units pass self.clock.pump(reactor, [0.0, 0.5, 1.0, 1.0, 0.4]) self.failIf(proto.wrappedProtocol.disconnected) # Now let the timer elapse self.clock.pump(reactor, [0.0, 0.2]) self.failUnless(proto.wrappedProtocol.disconnected) def testSendAvoidsTimeout(self): # Make sure that writing data to a transport from a protocol # constructed by a TimeoutFactory resets the timeout countdown. # Make the server-side connection factory, proto, transport = self._serverSetup() # Let half the countdown period elapse self.clock.pump(reactor, [0.0, 0.5, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Send some data (proto is the /real/ proto's transport, so this is # the write that gets called) proto.write('bytes bytes bytes') # More time passes, putting us past the original timeout self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Make sure writeSequence delays timeout as well proto.writeSequence(['bytes'] * 3) # Tick tock self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Don't write anything more, just let the timeout expire self.clock.pump(reactor, [0.0, 2.0]) self.failUnless(proto.wrappedProtocol.disconnected) def testReceiveAvoidsTimeout(self): # Make sure that receiving data also resets the timeout countdown. # Make the server-side connection factory, proto, transport = self._serverSetup() # Let half the countdown period elapse self.clock.pump(reactor, [0.0, 1.0, 0.5]) self.failIf(proto.wrappedProtocol.disconnected) # Some bytes arrive, they should reset the counter proto.dataReceived('bytes bytes bytes') # We pass the original timeout self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Nothing more arrives though, the new timeout deadline is passed, # the connection should be dropped. self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failUnless(proto.wrappedProtocol.disconnected)
class TestTimeout(unittest.TestCase): def setUpClass(self): self.clock = Clock() self.clock.install() def tearDownClass(self): self.clock.uninstall() def testTimeout(self): p = TimeoutTester() s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) self.clock.pump(reactor, [0, 0.5, 1.0, 1.0]) self.failIf(p.timedOut) self.clock.pump(reactor, [0, 1.0]) self.failUnless(p.timedOut) def testNoTimeout(self): p = TimeoutTester() s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) self.clock.pump(reactor, [0, 0.5, 1.0, 1.0]) self.failIf(p.timedOut) p.dataReceived('hello there') self.clock.pump(reactor, [0, 1.0, 1.0, 0.5]) self.failIf(p.timedOut) self.clock.pump(reactor, [0, 1.0]) self.failUnless(p.timedOut) def testResetTimeout(self): p = TimeoutTester() p.timeOut = None s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) p.setTimeout(1) self.assertEquals(p.timeOut, 1) self.clock.pump(reactor, [0, 0.9]) self.failIf(p.timedOut) self.clock.pump(reactor, [0, 0.2]) self.failUnless(p.timedOut) def testCancelTimeout(self): p = TimeoutTester() p.timeOut = 5 s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) p.setTimeout(None) self.assertEquals(p.timeOut, None) self.clock.pump(reactor, [0, 5, 5, 5]) self.failIf(p.timedOut) def testReturn(self): p = TimeoutTester() p.timeOut = 5 self.assertEquals(p.setTimeout(10), 5) self.assertEquals(p.setTimeout(None), 10) self.assertEquals(p.setTimeout(1), None) self.assertEquals(p.timeOut, 1) # Clean up the DelayedCall p.setTimeout(None)
def testCallLaterDelayAndReset(self): clock = Clock() clock.install() try: callbackTimes = [None, None] def resetCallback(): callbackTimes[0] = clock() def delayCallback(): callbackTimes[1] = clock() ireset = reactor.callLater(2, resetCallback) idelay = reactor.callLater(3, delayCallback) clock.pump(reactor, [0, 1]) ireset.reset(2) # (now)1 + 2 = 3 idelay.delay(3) # (orig)3 + 3 = 6 clock.pump(reactor, [0, 1]) self.assertIdentical(callbackTimes[0], None) self.assertIdentical(callbackTimes[0], None) clock.pump(reactor, [0, 1]) self.assertEquals(callbackTimes[0], 3) self.assertEquals(callbackTimes[1], None) clock.pump(reactor, [0, 3]) self.assertEquals(callbackTimes[1], 6) finally: clock.uninstall()
def testCallLaterDelayAndReset(self): """ Test that the reactor handles DelayedCalls which have been reset or delayed. """ clock = Clock() clock.install() try: callbackTimes = [None, None] def resetCallback(): callbackTimes[0] = clock() def delayCallback(): callbackTimes[1] = clock() ireset = reactor.callLater(2, resetCallback) idelay = reactor.callLater(3, delayCallback) clock.pump(reactor, [0, 1]) self.assertIdentical(callbackTimes[0], None) self.assertIdentical(callbackTimes[1], None) ireset.reset(2) # (now)1 + 2 = 3 idelay.delay(3) # (orig)3 + 3 = 6 clock.pump(reactor, [0, 1]) self.assertIdentical(callbackTimes[0], None) self.assertIdentical(callbackTimes[1], None) clock.pump(reactor, [0, 1]) self.assertEquals(callbackTimes[0], 3) self.assertEquals(callbackTimes[1], None) clock.pump(reactor, [0, 3]) self.assertEquals(callbackTimes[1], 6) finally: clock.uninstall()
class TimeoutTestCase(unittest.TestCase): def setUpClass(self): self.clock = Clock() self.clock.install() def tearDownClass(self): self.clock.uninstall() def _serverSetup(self): # Create a server factory, get a protocol from it, connect it to a # transport, and return all three. wrappedFactory = protocol.ServerFactory() wrappedFactory.protocol = SimpleProtocol factory = policies.TimeoutFactory(wrappedFactory, 3) proto = factory.buildProtocol( address.IPv4Address('TCP', '127.0.0.1', 12345)) transport = StringTransportWithDisconnection() transport.protocol = proto proto.makeConnection(transport) return factory, proto, transport def testTimeout(self): # Make sure that when a TimeoutFactory accepts a connection, it will # time out that connection if no data is read or written within the # timeout period. # Make the server-side connection factory, proto, transport = self._serverSetup() # Let almost 3 time units pass self.clock.pump(reactor, [0.0, 0.5, 1.0, 1.0, 0.4]) self.failIf(proto.wrappedProtocol.disconnected) # Now let the timer elapse self.clock.pump(reactor, [0.0, 0.2]) self.failUnless(proto.wrappedProtocol.disconnected) def testSendAvoidsTimeout(self): # Make sure that writing data to a transport from a protocol # constructed by a TimeoutFactory resets the timeout countdown. # Make the server-side connection factory, proto, transport = self._serverSetup() # Let half the countdown period elapse self.clock.pump(reactor, [0.0, 0.5, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Send some data (proto is the /real/ proto's transport, so this is # the write that gets called) proto.write('bytes bytes bytes') # More time passes, putting us past the original timeout self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Make sure writeSequence delays timeout as well proto.writeSequence(['bytes'] * 3) # Tick tock self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Don't write anything more, just let the timeout expire self.clock.pump(reactor, [0.0, 2.0]) self.failUnless(proto.wrappedProtocol.disconnected) def testReceiveAvoidsTimeout(self): # Make sure that receiving data also resets the timeout countdown. # Make the server-side connection factory, proto, transport = self._serverSetup() # Let half the countdown period elapse self.clock.pump(reactor, [0.0, 1.0, 0.5]) self.failIf(proto.wrappedProtocol.disconnected) # Some bytes arrive, they should reset the counter proto.dataReceived('bytes bytes bytes') # We pass the original timeout self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) # Nothing more arrives though, the new timeout deadline is passed, # the connection should be dropped. self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failUnless(proto.wrappedProtocol.disconnected)
class TimeoutTestCase(unittest.TestCase): def setUpClass(self): self.clock = Clock() self.clock.install() def tearDownClass(self): self.clock.uninstall() def _serverSetup(self): wrappedFactory = protocol.ServerFactory() wrappedFactory.protocol = SimpleProtocol factory = policies.TimeoutFactory(wrappedFactory, 3) proto = factory.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 12345)) transport = StringTransportWithDisconnection() transport.protocol = proto proto.makeConnection(transport) return factory, proto, transport def testTimeout(self): factory, proto, transport = self._serverSetup() self.clock.pump(reactor, [0.0, 0.5, 1.0, 1.0, 0.4]) self.failIf(proto.wrappedProtocol.disconnected) self.clock.pump(reactor, [0.0, 0.2]) self.failUnless(proto.wrappedProtocol.disconnected) def testSendAvoidsTimeout(self): factory, proto, transport = self._serverSetup() self.clock.pump(reactor, [0.0, 0.5, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) proto.write('bytes bytes bytes') self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) proto.writeSequence(['bytes'] * 3) self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) self.clock.pump(reactor, [0.0, 2.0]) self.failUnless(proto.wrappedProtocol.disconnected) def testReceiveAvoidsTimeout(self): factory, proto, transport = self._serverSetup() self.clock.pump(reactor, [0.0, 1.0, 0.5]) self.failIf(proto.wrappedProtocol.disconnected) proto.dataReceived('bytes bytes bytes') self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failIf(proto.wrappedProtocol.disconnected) self.clock.pump(reactor, [0.0, 1.0, 1.0]) self.failUnless(proto.wrappedProtocol.disconnected)
class TestTimeout(unittest.TestCase): def setUpClass(self): self.clock = Clock() self.clock.install() def tearDownClass(self): self.clock.uninstall() def testOverriddenCallLater(self): """ Test that setting callLater on a subclass of TimeoutMixin causes the protocol to use that callable instead of C{reactor.callLater}. """ calls = [] p = TimeoutTester() p.callLater = lambda *a, **kw: calls.append((a, kw)) p.setTimeout(10) self.assertEquals(len(calls), 1) def testTimeout(self): p = TimeoutTester() s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) self.clock.pump(reactor, [0, 0.5, 1.0, 1.0]) self.failIf(p.timedOut) self.clock.pump(reactor, [0, 1.0]) self.failUnless(p.timedOut) def testNoTimeout(self): p = TimeoutTester() s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) self.clock.pump(reactor, [0, 0.5, 1.0, 1.0]) self.failIf(p.timedOut) p.dataReceived('hello there') self.clock.pump(reactor, [0, 1.0, 1.0, 0.5]) self.failIf(p.timedOut) self.clock.pump(reactor, [0, 1.0]) self.failUnless(p.timedOut) def testResetTimeout(self): p = TimeoutTester() p.timeOut = None s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) p.setTimeout(1) self.assertEquals(p.timeOut, 1) self.clock.pump(reactor, [0, 0.9]) self.failIf(p.timedOut) self.clock.pump(reactor, [0, 0.2]) self.failUnless(p.timedOut) def testCancelTimeout(self): p = TimeoutTester() p.timeOut = 5 s = StringIOWithoutClosing() p.makeConnection(protocol.FileWrapper(s)) p.setTimeout(None) self.assertEquals(p.timeOut, None) self.clock.pump(reactor, [0, 5, 5, 5]) self.failIf(p.timedOut) def testReturn(self): p = TimeoutTester() p.timeOut = 5 self.assertEquals(p.setTimeout(10), 5) self.assertEquals(p.setTimeout(None), 10) self.assertEquals(p.setTimeout(1), None) self.assertEquals(p.timeOut, 1) # Clean up the DelayedCall p.setTimeout(None)