Here are the examples of the python api twisted.test.proto_helpers.StringTransportWithDisconnection taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
25 Examples
3
Example 1
Project: mamba-framework Source File: test_websocket.py
def setUp(self):
self.server = test_policies.Server()
tServer = TestableWebSocketFactory(task.Clock(), self.server)
self.port = tServer.buildProtocol(
address.IPv4Address('TCP', '127.0..0.1', 0))
self.tr = proto_helpers.StringTransportWithDisconnection()
self.tr.protocol = self.port
self.port.makeConnection(self.tr)
self.port.producer = self.port.wrappedProtocol
3
Example 2
Project: andcat Source File: test_netcat.py
def setUp(self):
self.buff = StringIO()
self.msg = StringIO()
def dataWriter(data):
self.buff.write(data)
def transferFinished(msg):
self.msg.write(msg)
self.factory = ReceiveFactory(dataWriter, transferFinished)
self.proto = self.factory.buildProtocol(('localhost', 0,))
self.transport = proto_helpers.StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
3
Example 3
Project: python-driver Source File: test_twistedreactor.py
def setUp(self):
if twistedreactor is None:
raise unittest.SkipTest("Twisted libraries not available")
twistedreactor.TwistedConnection.initialize_reactor()
self.tr = proto_helpers.StringTransportWithDisconnection()
self.tr.connector = Mock()
self.mock_connection = Mock()
self.tr.connector.factory = twistedreactor.TwistedConnectionClientFactory(
self.mock_connection)
self.obj_ut = twistedreactor.TwistedConnectionProtocol()
self.tr.protocol = self.obj_ut
3
Example 4
def setUp(self):
self.transport = StringTransportWithDisconnection()
self.protocol = httpclient.Protocol(self, owner=None)
self.protocol.factory = MockFactory()
self.protocol.makeConnection(self.transport)
self.transport.protocol = self.protocol
self.addCleanup(self._disconnect_protocol)
3
Example 5
def setUp(self):
factory = socks5.SOCKSv5Factory()
self.proto = factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransportWithDisconnection()
self.tr.protocol = self.proto
self.proto.makeConnection(self.tr)
3
Example 6
def setUp(self):
factory = socks5.SOCKSv5Factory()
self.proto = factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransportWithDisconnection()
self.tr.protocol = self.proto
self.proto.makeConnection(self.tr)
# Get things to where the next step is the client sends the auth message
self._sendMsg("050102")
self._recvMsg("0502")
3
Example 7
def setUp(self):
factory = socks5.SOCKSv5Factory()
self.proto = factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransportWithDisconnection()
self.tr.protocol = self.proto
self.proto.makeConnection(self.tr)
self.connectDeferred = None
# Get things to where the next step is the client sends the auth message
self._sendMsg("050100")
self._recvMsg("0500")
3
Example 8
Project: mythbox Source File: test_memcache.py
def setUp(self):
"""
Create a memcache client, connect it to a string protocol, and make it
use a deterministic clock.
"""
self.proto = MemCacheProtocol()
self.clock = Clock()
self.proto.callLater = self.clock.callLater
self.transport = StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
3
Example 9
Project: mythbox Source File: test_memcache.py
def setUp(self):
"""
Create a disconnected memcache client, using a deterministic clock.
"""
self.proto = MemCacheProtocol()
self.clock = Clock()
self.proto.callLater = self.clock.callLater
self.transport = StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
self.transport.loseConnection()
3
Example 10
Project: Comet Source File: test_voeventbroadcaster.py
def setUp(self):
self.factory = VOEventBroadcasterFactory(
DUMMY_SERVICE_IVORN.decode(), BCAST_TEST_INTERVAL
)
self.factory.alive_loop.clock = task.Clock()
self.factory.test_loop.clock = task.Clock()
self.connector = reactor.listenTCP(0, self.factory)
self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransportWithDisconnection()
self.proto.makeConnection(self.tr)
self.tr.protocol = self.proto
3
Example 11
Project: Comet Source File: test_voeventreceiver.py
def setUp(self):
self.factory = VOEventReceiverFactory(DUMMY_SERVICE_IVORN)
self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
self.clock = task.Clock()
self.proto.callLater = self.clock.callLater
self.tr = proto_helpers.StringTransportWithDisconnection()
self.proto.makeConnection(self.tr)
self.tr.protocol = self.proto
3
Example 12
Project: Comet Source File: test_voeventsender.py
def setUp(self):
self.event = DummyEvent()
self.factory = VOEventSenderFactory(self.event)
self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransportWithDisconnection()
self.proto.makeConnection(self.tr)
self.tr.protocol = self.proto
3
Example 13
Project: Comet Source File: test_voeventsubscriber.py
def setUp(self):
self.clock = task.Clock()
self.factory = VOEventSubscriberFactory(DUMMY_EVENT_IVORN)
self.factory.callLater = self.clock.callLater
self.transport = proto_helpers.StringTransportWithDisconnection()
self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
3
Example 14
Project: Comet Source File: test_voeventsubscriber.py
def setUp(self):
factory = VOEventSubscriberFactory(DUMMY_EVENT_IVORN)
self.clock = task.Clock()
factory.callLater = self.clock.callLater
self.proto = factory.buildProtocol(('127.0.0.1', 0))
self.proto.callLater = self.clock.callLater # Original in TimeoutMixin
self.tr = proto_helpers.StringTransportWithDisconnection()
self.proto.makeConnection(self.tr)
self.tr.protocol = self.proto
3
Example 15
Project: ccs-calendarserver Source File: test_memcachelock.py
def setUp(self):
"""
Create a memcache client, connect it to a string protocol, and make it
use a deterministic clock.
"""
TestCase.setUp(self)
self.proto = MemCacheProtocol()
self.clock = Clock()
self.proto.callLater = self.clock.callLater
self.transport = StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
3
Example 16
Project: txtorcon Source File: test_torcontrolprotocol.py
def setUp(self):
self.protocol = TorControlProtocol()
self.protocol.connectionMade = lambda: None
self.transport = proto_helpers.StringTransportWithDisconnection()
self.protocol.makeConnection(self.transport)
# why doesn't makeConnection do this?
self.transport.protocol = self.protocol
3
Example 17
def connectProxy(self, proxyClient):
"""
Connect a proxy client to a L{StringTransportWithDisconnection}.
@param proxyClient: A L{ProxyClient}.
@return: The L{StringTransportWithDisconnection}.
"""
clientTransport = StringTransportWithDisconnection()
clientTransport.protocol = proxyClient
proxyClient.makeConnection(clientTransport)
return clientTransport
3
Example 18
Project: SubliminalCollaborator Source File: test_proxy.py
def test_processWithPort(self):
"""
Check that L{ProxyRequest.process} correctly parse port in the incoming
URL, and create a outgoing connection with this port.
"""
transport = StringTransportWithDisconnection()
channel = DummyChannel(transport)
reactor = MemoryReactor()
request = ProxyRequest(channel, False, reactor)
request.gotLength(0)
request.requestReceived('GET', 'http://example.com:1234/foo/bar',
'HTTP/1.0')
# That should create one connection, with the port parsed from the URL
self.assertEqual(len(reactor.tcpClients), 1)
self.assertEqual(reactor.tcpClients[0][0], "example.com")
self.assertEqual(reactor.tcpClients[0][1], 1234)
3
Example 19
def setUp(self):
"""
Set up XmlStream and several observers.
"""
self.gotStreamStart = False
self.gotStreamEnd = False
self.gotStreamError = False
xs = xmlstream.XmlStream(xmlstream.Authenticator())
xs.addObserver('//event/stream/start', self.onStreamStart)
xs.addObserver('//event/stream/end', self.onStreamEnd)
xs.addObserver('//event/stream/error', self.onStreamError)
xs.makeConnection(proto_helpers.StringTransportWithDisconnection())
xs.transport.protocol = xs
xs.namespace = 'testns'
xs.version = (1, 0)
self.xmlstream = xs
3
Example 20
def __init__(self, user, realm, factory, address=address.IPv4Address('TCP', '127.0.0.1', 54321)):
self.user = user
self.transport = proto_helpers.StringTransportWithDisconnection()
self.protocol = factory.buildProtocol(address)
self.transport.protocol = self.protocol
self.user.mind = self.protocol
self.protocol.makeConnection(self.transport)
0
Example 21
Project: mythbox Source File: test_policies.py
def test_limit(self):
"""
Full test using a custom server limiting number of connections.
"""
server = Server()
c1, c2, c3, c4 = [SimpleProtocol() for i in range(4)]
tServer = policies.ThrottlingFactory(server, 2)
wrapTServer = WrappingFactory(tServer)
wrapTServer.deferred = defer.Deferred()
# Start listening
p = reactor.listenTCP(0, wrapTServer, interface="127.0.0.1")
n = p.getHost().port
def _connect123(results):
reactor.connectTCP("127.0.0.1", n, SillyFactory(c1))
c1.dConnected.addCallback(
lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c2)))
c2.dConnected.addCallback(
lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c3)))
return c3.dDisconnected
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])
wrapTServer.deferred.addCallback(_connect123)
wrapTServer.deferred.addCallback(_check123)
wrapTServer.deferred.addCallback(_lose1)
wrapTServer.deferred.addCallback(_connect4)
wrapTServer.deferred.addCallback(_check4)
wrapTServer.deferred.addCallback(_cleanup)
return wrapTServer.deferred
def test_writeLimit(self):
"""
Check the writeLimit parameter: write data, and check for the pause
status.
"""
server = Server()
tServer = TestableThrottlingFactory(task.Clock(), server, writeLimit=10)
port = tServer.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 0))
tr = StringTransportWithDisconnection()
tr.protocol = port
port.makeConnection(tr)
port.producer = port.wrappedProtocol
port.dataReceived("0123456789")
port.dataReceived("abcdefghij")
self.assertEquals(tr.value(), "0123456789abcdefghij")
self.assertEquals(tServer.writtenThisSecond, 20)
self.assertFalse(port.wrappedProtocol.paused)
# at this point server should've written 20 bytes, 10 bytes
# above the limit so writing should be paused around 1 second
# from 'now', and resumed a second after that
tServer.clock.advance(1.05)
self.assertEquals(tServer.writtenThisSecond, 0)
self.assertTrue(port.wrappedProtocol.paused)
tServer.clock.advance(1.05)
self.assertEquals(tServer.writtenThisSecond, 0)
self.assertFalse(port.wrappedProtocol.paused)
def test_readLimit(self):
"""
Check the readLimit parameter: read data and check for the pause
status.
"""
server = Server()
tServer = TestableThrottlingFactory(task.Clock(), server, readLimit=10)
port = tServer.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 0))
tr = StringTransportWithDisconnection()
tr.protocol = port
port.makeConnection(tr)
port.dataReceived("0123456789")
port.dataReceived("abcdefghij")
self.assertEquals(tr.value(), "0123456789abcdefghij")
self.assertEquals(tServer.readThisSecond, 20)
tServer.clock.advance(1.05)
self.assertEquals(tServer.readThisSecond, 0)
self.assertEquals(tr.producerState, 'paused')
tServer.clock.advance(1.05)
self.assertEquals(tServer.readThisSecond, 0)
self.assertEquals(tr.producerState, 'producing')
tr.clear()
port.dataReceived("0123456789")
port.dataReceived("abcdefghij")
self.assertEquals(tr.value(), "0123456789abcdefghij")
self.assertEquals(tServer.readThisSecond, 20)
tServer.clock.advance(1.05)
self.assertEquals(tServer.readThisSecond, 0)
self.assertEquals(tr.producerState, 'paused')
tServer.clock.advance(1.05)
self.assertEquals(tServer.readThisSecond, 0)
self.assertEquals(tr.producerState, 'producing')
class TimeoutTestCase(unittest.TestCase):
"""
Tests for L{policies.TimeoutFactory}.
"""
def setUp(self):
"""
Create a testable, deterministic clock, and a set of
server factory/protocol/transport.
"""
self.clock = task.Clock()
wrappedFactory = protocol.ServerFactory()
wrappedFactory.protocol = SimpleProtocol
self.factory = TestableTimeoutFactory(self.clock, wrappedFactory, 3)
self.proto = self.factory.buildProtocol(
address.IPv4Address('TCP', '127.0.0.1', 12345))
self.transport = StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
def test_timeout(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.
"""
# Let almost 3 time units pass
self.clock.pump([0.0, 0.5, 1.0, 1.0, 0.4])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Now let the timer elapse
self.clock.pump([0.0, 0.2])
self.failUnless(self.proto.wrappedProtocol.disconnected)
def test_sendAvoidsTimeout(self):
"""
Make sure that writing data to a transport from a protocol
constructed by a TimeoutFactory resets the timeout countdown.
"""
# Let half the countdown period elapse
self.clock.pump([0.0, 0.5, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Send some data (self.proto is the /real/ proto's transport, so this
# is the write that gets called)
self.proto.write('bytes bytes bytes')
# More time passes, putting us past the original timeout
self.clock.pump([0.0, 1.0, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Make sure writeSequence delays timeout as well
self.proto.writeSequence(['bytes'] * 3)
# Tick tock
self.clock.pump([0.0, 1.0, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Don't write anything more, just let the timeout expire
self.clock.pump([0.0, 2.0])
self.failUnless(self.proto.wrappedProtocol.disconnected)
def test_receiveAvoidsTimeout(self):
"""
Make sure that receiving data also resets the timeout countdown.
"""
# Let half the countdown period elapse
self.clock.pump([0.0, 1.0, 0.5])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Some bytes arrive, they should reset the counter
self.proto.dataReceived('bytes bytes bytes')
# We pass the original timeout
self.clock.pump([0.0, 1.0, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Nothing more arrives though, the new timeout deadline is passed,
# the connection should be dropped.
self.clock.pump([0.0, 1.0, 1.0])
self.failUnless(self.proto.wrappedProtocol.disconnected)
class TimeoutTester(protocol.Protocol, policies.TimeoutMixin):
"""
A testable protocol with timeout facility.
@ivar timedOut: set to C{True} if a timeout has been detected.
@type timedOut: C{bool}
"""
timeOut = 3
timedOut = False
def __init__(self, clock):
"""
Initialize the protocol with a C{task.Clock} object.
"""
self.clock = clock
def connectionMade(self):
"""
Upon connection, set the timeout.
"""
self.setTimeout(self.timeOut)
def dataReceived(self, data):
"""
Reset the timeout on data.
"""
self.resetTimeout()
protocol.Protocol.dataReceived(self, data)
def connectionLost(self, reason=None):
"""
On connection lost, cancel all timeout operations.
"""
self.setTimeout(None)
def timeoutConnection(self):
"""
Flags the timedOut variable to indicate the timeout of the connection.
"""
self.timedOut = True
def callLater(self, timeout, func, *args, **kwargs):
"""
Override callLater to use the deterministic clock.
"""
return self.clock.callLater(timeout, func, *args, **kwargs)
class TestTimeout(unittest.TestCase):
"""
Tests for L{policies.TimeoutMixin}.
"""
def setUp(self):
"""
Create a testable, deterministic clock and a C{TimeoutTester} instance.
"""
self.clock = task.Clock()
self.proto = TimeoutTester(self.clock)
def test_overriddenCallLater(self):
"""
Test that the callLater of the clock is used instead of
C{reactor.callLater}.
"""
self.proto.setTimeout(10)
self.assertEquals(len(self.clock.calls), 1)
def test_timeout(self):
"""
Check that the protocol does timeout at the time specified by its
C{timeOut} attribute.
"""
self.proto.makeConnection(StringTransport())
# timeOut value is 3
self.clock.pump([0, 0.5, 1.0, 1.0])
self.failIf(self.proto.timedOut)
self.clock.pump([0, 1.0])
self.failUnless(self.proto.timedOut)
def test_noTimeout(self):
"""
Check that receiving data is delaying the timeout of the connection.
"""
self.proto.makeConnection(StringTransport())
self.clock.pump([0, 0.5, 1.0, 1.0])
self.failIf(self.proto.timedOut)
self.proto.dataReceived('hello there')
self.clock.pump([0, 1.0, 1.0, 0.5])
self.failIf(self.proto.timedOut)
self.clock.pump([0, 1.0])
self.failUnless(self.proto.timedOut)
def test_resetTimeout(self):
"""
Check that setting a new value for timeout cancel the previous value
and install a new timeout.
"""
self.proto.timeOut = None
self.proto.makeConnection(StringTransport())
self.proto.setTimeout(1)
self.assertEquals(self.proto.timeOut, 1)
self.clock.pump([0, 0.9])
self.failIf(self.proto.timedOut)
self.clock.pump([0, 0.2])
self.failUnless(self.proto.timedOut)
def test_cancelTimeout(self):
"""
Setting the timeout to C{None} cancel any timeout operations.
"""
self.proto.timeOut = 5
self.proto.makeConnection(StringTransport())
self.proto.setTimeout(None)
self.assertEquals(self.proto.timeOut, None)
self.clock.pump([0, 5, 5, 5])
self.failIf(self.proto.timedOut)
def test_return(self):
"""
setTimeout should return the value of the previous timeout.
"""
self.proto.timeOut = 5
self.assertEquals(self.proto.setTimeout(10), 5)
self.assertEquals(self.proto.setTimeout(None), 10)
self.assertEquals(self.proto.setTimeout(1), None)
self.assertEquals(self.proto.timeOut, 1)
# Clean up the DelayedCall
self.proto.setTimeout(None)
class LimitTotalConnectionsFactoryTestCase(unittest.TestCase):
"""Tests for policies.LimitTotalConnectionsFactory"""
def testConnectionCounting(self):
# Make a basic factory
factory = policies.LimitTotalConnectionsFactory()
factory.protocol = protocol.Protocol
# connectionCount starts at zero
self.assertEqual(0, factory.connectionCount)
# connectionCount increments as connections are made
p1 = factory.buildProtocol(None)
self.assertEqual(1, factory.connectionCount)
p2 = factory.buildProtocol(None)
self.assertEqual(2, factory.connectionCount)
# and decrements as they are lost
p1.connectionLost(None)
self.assertEqual(1, factory.connectionCount)
p2.connectionLost(None)
self.assertEqual(0, factory.connectionCount)
def testConnectionLimiting(self):
# Make a basic factory with a connection limit of 1
factory = policies.LimitTotalConnectionsFactory()
factory.protocol = protocol.Protocol
factory.connectionLimit = 1
# Make a connection
p = factory.buildProtocol(None)
self.assertNotEqual(None, p)
self.assertEqual(1, factory.connectionCount)
# Try to make a second connection, which will exceed the connection
# limit. This should return None, because overflowProtocol is None.
self.assertEqual(None, factory.buildProtocol(None))
self.assertEqual(1, factory.connectionCount)
# Define an overflow protocol
class OverflowProtocol(protocol.Protocol):
def connectionMade(self):
factory.overflowed = True
factory.overflowProtocol = OverflowProtocol
factory.overflowed = False
# Try to make a second connection again, now that we have an overflow
# protocol. Note that overflow connections count towards the connection
# count.
op = factory.buildProtocol(None)
op.makeConnection(None) # to trigger connectionMade
self.assertEqual(True, factory.overflowed)
self.assertEqual(2, factory.connectionCount)
# Close the connections.
p.connectionLost(None)
self.assertEqual(1, factory.connectionCount)
op.connectionLost(None)
self.assertEqual(0, factory.connectionCount)
class WriteSequenceEchoProtocol(EchoProtocol):
def dataReceived(self, bytes):
if bytes.find('vector!') != -1:
self.transport.writeSequence([bytes])
else:
EchoProtocol.dataReceived(self, bytes)
class TestLoggingFactory(policies.TrafficLoggingFactory):
openFile = None
def open(self, name):
assert self.openFile is None, "open() called too many times"
self.openFile = StringIO()
return self.openFile
class LoggingFactoryTestCase(unittest.TestCase):
"""
Tests for L{policies.TrafficLoggingFactory}.
"""
def test_thingsGetLogged(self):
"""
Check the output produced by L{policies.TrafficLoggingFactory}.
"""
wrappedFactory = Server()
wrappedFactory.protocol = WriteSequenceEchoProtocol
t = StringTransportWithDisconnection()
f = TestLoggingFactory(wrappedFactory, 'test')
p = f.buildProtocol(('1.2.3.4', 5678))
t.protocol = p
p.makeConnection(t)
v = f.openFile.getvalue()
self.failUnless('*' in v, "* not found in %r" % (v,))
self.failIf(t.value())
p.dataReceived('here are some bytes')
v = f.openFile.getvalue()
self.assertIn("C 1: 'here are some bytes'", v)
self.assertIn("S 1: 'here are some bytes'", v)
self.assertEquals(t.value(), 'here are some bytes')
t.clear()
p.dataReceived('prepare for vector! to the extreme')
v = f.openFile.getvalue()
self.assertIn("SV 1: ['prepare for vector! to the extreme']", v)
self.assertEquals(t.value(), 'prepare for vector! to the extreme')
p.loseConnection()
v = f.openFile.getvalue()
self.assertIn('ConnectionDone', v)
def test_counter(self):
"""
Test counter management with the resetCounter method.
"""
wrappedFactory = Server()
f = TestLoggingFactory(wrappedFactory, 'test')
self.assertEqual(f._counter, 0)
f.buildProtocol(('1.2.3.4', 5678))
self.assertEqual(f._counter, 1)
# Reset log file
f.openFile = None
f.buildProtocol(('1.2.3.4', 5679))
self.assertEqual(f._counter, 2)
f.resetCounter()
self.assertEqual(f._counter, 0)
0
Example 22
Project: SubliminalCollaborator Source File: test_policies.py
def test_limit(self):
"""
Full test using a custom server limiting number of connections.
"""
server = Server()
c1, c2, c3, c4 = [SimpleProtocol() for i in range(4)]
tServer = policies.ThrottlingFactory(server, 2)
wrapTServer = WrappingFactory(tServer)
wrapTServer.deferred = defer.Deferred()
# Start listening
p = reactor.listenTCP(0, wrapTServer, interface="127.0.0.1")
n = p.getHost().port
def _connect123(results):
reactor.connectTCP("127.0.0.1", n, SillyFactory(c1))
c1.dConnected.addCallback(
lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c2)))
c2.dConnected.addCallback(
lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c3)))
return c3.dDisconnected
def _check123(results):
self.assertEqual([c.connected for c in c1, c2, c3], [1, 1, 1])
self.assertEqual([c.disconnected for c in c1, c2, c3], [0, 0, 1])
self.assertEqual(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.assertEqual(c4.connected, 1)
self.assertEqual(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])
wrapTServer.deferred.addCallback(_connect123)
wrapTServer.deferred.addCallback(_check123)
wrapTServer.deferred.addCallback(_lose1)
wrapTServer.deferred.addCallback(_connect4)
wrapTServer.deferred.addCallback(_check4)
wrapTServer.deferred.addCallback(_cleanup)
return wrapTServer.deferred
def test_writeLimit(self):
"""
Check the writeLimit parameter: write data, and check for the pause
status.
"""
server = Server()
tServer = TestableThrottlingFactory(task.Clock(), server, writeLimit=10)
port = tServer.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 0))
tr = StringTransportWithDisconnection()
tr.protocol = port
port.makeConnection(tr)
port.producer = port.wrappedProtocol
port.dataReceived("0123456789")
port.dataReceived("abcdefghij")
self.assertEqual(tr.value(), "0123456789abcdefghij")
self.assertEqual(tServer.writtenThisSecond, 20)
self.assertFalse(port.wrappedProtocol.paused)
# at this point server should've written 20 bytes, 10 bytes
# above the limit so writing should be paused around 1 second
# from 'now', and resumed a second after that
tServer.clock.advance(1.05)
self.assertEqual(tServer.writtenThisSecond, 0)
self.assertTrue(port.wrappedProtocol.paused)
tServer.clock.advance(1.05)
self.assertEqual(tServer.writtenThisSecond, 0)
self.assertFalse(port.wrappedProtocol.paused)
def test_readLimit(self):
"""
Check the readLimit parameter: read data and check for the pause
status.
"""
server = Server()
tServer = TestableThrottlingFactory(task.Clock(), server, readLimit=10)
port = tServer.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 0))
tr = StringTransportWithDisconnection()
tr.protocol = port
port.makeConnection(tr)
port.dataReceived("0123456789")
port.dataReceived("abcdefghij")
self.assertEqual(tr.value(), "0123456789abcdefghij")
self.assertEqual(tServer.readThisSecond, 20)
tServer.clock.advance(1.05)
self.assertEqual(tServer.readThisSecond, 0)
self.assertEqual(tr.producerState, 'paused')
tServer.clock.advance(1.05)
self.assertEqual(tServer.readThisSecond, 0)
self.assertEqual(tr.producerState, 'producing')
tr.clear()
port.dataReceived("0123456789")
port.dataReceived("abcdefghij")
self.assertEqual(tr.value(), "0123456789abcdefghij")
self.assertEqual(tServer.readThisSecond, 20)
tServer.clock.advance(1.05)
self.assertEqual(tServer.readThisSecond, 0)
self.assertEqual(tr.producerState, 'paused')
tServer.clock.advance(1.05)
self.assertEqual(tServer.readThisSecond, 0)
self.assertEqual(tr.producerState, 'producing')
class TimeoutTestCase(unittest.TestCase):
"""
Tests for L{policies.TimeoutFactory}.
"""
def setUp(self):
"""
Create a testable, deterministic clock, and a set of
server factory/protocol/transport.
"""
self.clock = task.Clock()
wrappedFactory = protocol.ServerFactory()
wrappedFactory.protocol = SimpleProtocol
self.factory = TestableTimeoutFactory(self.clock, wrappedFactory, 3)
self.proto = self.factory.buildProtocol(
address.IPv4Address('TCP', '127.0.0.1', 12345))
self.transport = StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
def test_timeout(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.
"""
# Let almost 3 time units pass
self.clock.pump([0.0, 0.5, 1.0, 1.0, 0.4])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Now let the timer elapse
self.clock.pump([0.0, 0.2])
self.failUnless(self.proto.wrappedProtocol.disconnected)
def test_sendAvoidsTimeout(self):
"""
Make sure that writing data to a transport from a protocol
constructed by a TimeoutFactory resets the timeout countdown.
"""
# Let half the countdown period elapse
self.clock.pump([0.0, 0.5, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Send some data (self.proto is the /real/ proto's transport, so this
# is the write that gets called)
self.proto.write('bytes bytes bytes')
# More time passes, putting us past the original timeout
self.clock.pump([0.0, 1.0, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Make sure writeSequence delays timeout as well
self.proto.writeSequence(['bytes'] * 3)
# Tick tock
self.clock.pump([0.0, 1.0, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Don't write anything more, just let the timeout expire
self.clock.pump([0.0, 2.0])
self.failUnless(self.proto.wrappedProtocol.disconnected)
def test_receiveAvoidsTimeout(self):
"""
Make sure that receiving data also resets the timeout countdown.
"""
# Let half the countdown period elapse
self.clock.pump([0.0, 1.0, 0.5])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Some bytes arrive, they should reset the counter
self.proto.dataReceived('bytes bytes bytes')
# We pass the original timeout
self.clock.pump([0.0, 1.0, 1.0])
self.failIf(self.proto.wrappedProtocol.disconnected)
# Nothing more arrives though, the new timeout deadline is passed,
# the connection should be dropped.
self.clock.pump([0.0, 1.0, 1.0])
self.failUnless(self.proto.wrappedProtocol.disconnected)
class TimeoutTester(protocol.Protocol, policies.TimeoutMixin):
"""
A testable protocol with timeout facility.
@ivar timedOut: set to C{True} if a timeout has been detected.
@type timedOut: C{bool}
"""
timeOut = 3
timedOut = False
def __init__(self, clock):
"""
Initialize the protocol with a C{task.Clock} object.
"""
self.clock = clock
def connectionMade(self):
"""
Upon connection, set the timeout.
"""
self.setTimeout(self.timeOut)
def dataReceived(self, data):
"""
Reset the timeout on data.
"""
self.resetTimeout()
protocol.Protocol.dataReceived(self, data)
def connectionLost(self, reason=None):
"""
On connection lost, cancel all timeout operations.
"""
self.setTimeout(None)
def timeoutConnection(self):
"""
Flags the timedOut variable to indicate the timeout of the connection.
"""
self.timedOut = True
def callLater(self, timeout, func, *args, **kwargs):
"""
Override callLater to use the deterministic clock.
"""
return self.clock.callLater(timeout, func, *args, **kwargs)
class TestTimeout(unittest.TestCase):
"""
Tests for L{policies.TimeoutMixin}.
"""
def setUp(self):
"""
Create a testable, deterministic clock and a C{TimeoutTester} instance.
"""
self.clock = task.Clock()
self.proto = TimeoutTester(self.clock)
def test_overriddenCallLater(self):
"""
Test that the callLater of the clock is used instead of
C{reactor.callLater}.
"""
self.proto.setTimeout(10)
self.assertEqual(len(self.clock.calls), 1)
def test_timeout(self):
"""
Check that the protocol does timeout at the time specified by its
C{timeOut} attribute.
"""
self.proto.makeConnection(StringTransport())
# timeOut value is 3
self.clock.pump([0, 0.5, 1.0, 1.0])
self.failIf(self.proto.timedOut)
self.clock.pump([0, 1.0])
self.failUnless(self.proto.timedOut)
def test_noTimeout(self):
"""
Check that receiving data is delaying the timeout of the connection.
"""
self.proto.makeConnection(StringTransport())
self.clock.pump([0, 0.5, 1.0, 1.0])
self.failIf(self.proto.timedOut)
self.proto.dataReceived('hello there')
self.clock.pump([0, 1.0, 1.0, 0.5])
self.failIf(self.proto.timedOut)
self.clock.pump([0, 1.0])
self.failUnless(self.proto.timedOut)
def test_resetTimeout(self):
"""
Check that setting a new value for timeout cancel the previous value
and install a new timeout.
"""
self.proto.timeOut = None
self.proto.makeConnection(StringTransport())
self.proto.setTimeout(1)
self.assertEqual(self.proto.timeOut, 1)
self.clock.pump([0, 0.9])
self.failIf(self.proto.timedOut)
self.clock.pump([0, 0.2])
self.failUnless(self.proto.timedOut)
def test_cancelTimeout(self):
"""
Setting the timeout to C{None} cancel any timeout operations.
"""
self.proto.timeOut = 5
self.proto.makeConnection(StringTransport())
self.proto.setTimeout(None)
self.assertEqual(self.proto.timeOut, None)
self.clock.pump([0, 5, 5, 5])
self.failIf(self.proto.timedOut)
def test_return(self):
"""
setTimeout should return the value of the previous timeout.
"""
self.proto.timeOut = 5
self.assertEqual(self.proto.setTimeout(10), 5)
self.assertEqual(self.proto.setTimeout(None), 10)
self.assertEqual(self.proto.setTimeout(1), None)
self.assertEqual(self.proto.timeOut, 1)
# Clean up the DelayedCall
self.proto.setTimeout(None)
class LimitTotalConnectionsFactoryTestCase(unittest.TestCase):
"""Tests for policies.LimitTotalConnectionsFactory"""
def testConnectionCounting(self):
# Make a basic factory
factory = policies.LimitTotalConnectionsFactory()
factory.protocol = protocol.Protocol
# connectionCount starts at zero
self.assertEqual(0, factory.connectionCount)
# connectionCount increments as connections are made
p1 = factory.buildProtocol(None)
self.assertEqual(1, factory.connectionCount)
p2 = factory.buildProtocol(None)
self.assertEqual(2, factory.connectionCount)
# and decrements as they are lost
p1.connectionLost(None)
self.assertEqual(1, factory.connectionCount)
p2.connectionLost(None)
self.assertEqual(0, factory.connectionCount)
def testConnectionLimiting(self):
# Make a basic factory with a connection limit of 1
factory = policies.LimitTotalConnectionsFactory()
factory.protocol = protocol.Protocol
factory.connectionLimit = 1
# Make a connection
p = factory.buildProtocol(None)
self.assertNotEqual(None, p)
self.assertEqual(1, factory.connectionCount)
# Try to make a second connection, which will exceed the connection
# limit. This should return None, because overflowProtocol is None.
self.assertEqual(None, factory.buildProtocol(None))
self.assertEqual(1, factory.connectionCount)
# Define an overflow protocol
class OverflowProtocol(protocol.Protocol):
def connectionMade(self):
factory.overflowed = True
factory.overflowProtocol = OverflowProtocol
factory.overflowed = False
# Try to make a second connection again, now that we have an overflow
# protocol. Note that overflow connections count towards the connection
# count.
op = factory.buildProtocol(None)
op.makeConnection(None) # to trigger connectionMade
self.assertEqual(True, factory.overflowed)
self.assertEqual(2, factory.connectionCount)
# Close the connections.
p.connectionLost(None)
self.assertEqual(1, factory.connectionCount)
op.connectionLost(None)
self.assertEqual(0, factory.connectionCount)
class WriteSequenceEchoProtocol(EchoProtocol):
def dataReceived(self, bytes):
if bytes.find('vector!') != -1:
self.transport.writeSequence([bytes])
else:
EchoProtocol.dataReceived(self, bytes)
class TestLoggingFactory(policies.TrafficLoggingFactory):
openFile = None
def open(self, name):
assert self.openFile is None, "open() called too many times"
self.openFile = StringIO()
return self.openFile
class LoggingFactoryTestCase(unittest.TestCase):
"""
Tests for L{policies.TrafficLoggingFactory}.
"""
def test_thingsGetLogged(self):
"""
Check the output produced by L{policies.TrafficLoggingFactory}.
"""
wrappedFactory = Server()
wrappedFactory.protocol = WriteSequenceEchoProtocol
t = StringTransportWithDisconnection()
f = TestLoggingFactory(wrappedFactory, 'test')
p = f.buildProtocol(('1.2.3.4', 5678))
t.protocol = p
p.makeConnection(t)
v = f.openFile.getvalue()
self.failUnless('*' in v, "* not found in %r" % (v,))
self.failIf(t.value())
p.dataReceived('here are some bytes')
v = f.openFile.getvalue()
self.assertIn("C 1: 'here are some bytes'", v)
self.assertIn("S 1: 'here are some bytes'", v)
self.assertEqual(t.value(), 'here are some bytes')
t.clear()
p.dataReceived('prepare for vector! to the extreme')
v = f.openFile.getvalue()
self.assertIn("SV 1: ['prepare for vector! to the extreme']", v)
self.assertEqual(t.value(), 'prepare for vector! to the extreme')
p.loseConnection()
v = f.openFile.getvalue()
self.assertIn('ConnectionDone', v)
def test_counter(self):
"""
Test counter management with the resetCounter method.
"""
wrappedFactory = Server()
f = TestLoggingFactory(wrappedFactory, 'test')
self.assertEqual(f._counter, 0)
f.buildProtocol(('1.2.3.4', 5678))
self.assertEqual(f._counter, 1)
# Reset log file
f.openFile = None
f.buildProtocol(('1.2.3.4', 5679))
self.assertEqual(f._counter, 2)
f.resetCounter()
self.assertEqual(f._counter, 0)
0
Example 23
Project: SubliminalCollaborator Source File: test_proxy.py
def _testRender(self, uri, expectedURI):
"""
Check that a request pointing at C{uri} produce a new proxy connection,
with the path of this request pointing at C{expectedURI}.
"""
root = Resource()
reactor = MemoryReactor()
resource = ReverseProxyResource("127.0.0.1", 1234, "/path", reactor)
root.putChild('index', resource)
site = Site(root)
transport = StringTransportWithDisconnection()
channel = site.buildProtocol(None)
channel.makeConnection(transport)
# Clear the timeout if the tests failed
self.addCleanup(channel.connectionLost, None)
channel.dataReceived("GET %s HTTP/1.1\r\nAccept: text/html\r\n\r\n" %
(uri,))
# Check that one connection has been created, to the good host/port
self.assertEqual(len(reactor.tcpClients), 1)
self.assertEqual(reactor.tcpClients[0][0], "127.0.0.1")
self.assertEqual(reactor.tcpClients[0][1], 1234)
# Check the factory passed to the connect, and its given path
factory = reactor.tcpClients[0][2]
self.assertIsInstance(factory, ProxyClientFactory)
self.assertEqual(factory.rest, expectedURI)
self.assertEqual(factory.headers["host"], "127.0.0.1:1234")
0
Example 24
def _testProcess(self, uri, expectedURI, method="GET", data=""):
"""
Build a request pointing at C{uri}, and check that a proxied request
is created, pointing a C{expectedURI}.
"""
transport = StringTransportWithDisconnection()
channel = DummyChannel(transport)
reactor = MemoryReactor()
request = ProxyRequest(channel, False, reactor)
request.gotLength(len(data))
request.handleContentChunk(data)
request.requestReceived(method, 'http://example.com%s' % (uri,),
'HTTP/1.0')
self.assertEqual(len(reactor.tcpClients), 1)
self.assertEqual(reactor.tcpClients[0][0], "example.com")
self.assertEqual(reactor.tcpClients[0][1], 80)
factory = reactor.tcpClients[0][2]
self.assertIsInstance(factory, ProxyClientFactory)
self.assertEqual(factory.command, method)
self.assertEqual(factory.version, 'HTTP/1.0')
self.assertEqual(factory.headers, {'host': 'example.com'})
self.assertEqual(factory.data, data)
self.assertEqual(factory.rest, expectedURI)
self.assertEqual(factory.father, request)
0
Example 25
def test_process(self):
"""
L{ReverseProxyRequest.process} should create a connection to its
factory host/port, using a L{ProxyClientFactory} instantiated with the
correct parameters, and particulary set the B{host} header to the
factory host.
"""
transport = StringTransportWithDisconnection()
channel = DummyChannel(transport)
reactor = MemoryReactor()
request = ReverseProxyRequest(channel, False, reactor)
request.factory = DummyFactory("example.com", 1234)
request.gotLength(0)
request.requestReceived('GET', '/foo/bar', 'HTTP/1.0')
# Check that one connection has been created, to the good host/port
self.assertEqual(len(reactor.tcpClients), 1)
self.assertEqual(reactor.tcpClients[0][0], "example.com")
self.assertEqual(reactor.tcpClients[0][1], 1234)
# Check the factory passed to the connect, and its headers
factory = reactor.tcpClients[0][2]
self.assertIsInstance(factory, ProxyClientFactory)
self.assertEqual(factory.headers, {'host': 'example.com'})