Skip to content

Instantly share code, notes, and snippets.

@mathuin
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathuin/9536839 to your computer and use it in GitHub Desktop.
Save mathuin/9536839 to your computer and use it in GitHub Desktop.
Problem with StringTransport() and StringTransportWithDisconnection() not handling loseConnection as expected
from twisted.internet.protocol import Factory, Protocol
class SmallFactory(Factory):
def buildProtocol(self, addr):
return Small(self)
class Small(Protocol):
def __init__(self, factory):
self.factory = factory
self.connectionmade = False
self.connectionlost = False
def connectionMade(self):
self.connectionmade = True
def dataReceived(self, data):
self.output = data
self.transport.write(self.output)
self.transport.loseConnection()
def connectionLost(self):
self.connectionlost = True
(twistedauthproxy)jmt@cyan:~/crazy/twistedauthproxy$ trial test.test_small
test.test_small
SmallFactoryTestCase
test_small ... [FAIL]
test_small_with_disconnection ... [ERROR]
===============================================================================
[FAIL]
Traceback (most recent call last):
File "/home/jmt/crazy/twistedauthproxy/test/test_small.py", line 20, in test_small
self.assertTrue(self.proto.connectionlost)
File "/home/jmt/.virtualenvs/twistedauthproxy/local/lib/python2.7/site-packages/twisted/trial/_synctest.py", line 308, in assertTrue
raise self.failureException(msg)
twisted.trial.unittest.FailTest: None
test.test_small.SmallFactoryTestCase.test_small
===============================================================================
[ERROR]
Traceback (most recent call last):
File "/home/jmt/crazy/twistedauthproxy/test/test_small.py", line 27, in test_small_with_disconnection
self.proto.dataReceived(self.instring)
File "/home/jmt/crazy/twistedauthproxy/small_server.py", line 21, in dataReceived
self.transport.loseConnection()
File "/home/jmt/.virtualenvs/twistedauthproxy/local/lib/python2.7/site-packages/twisted/test/proto_helpers.py", line 268, in loseConnection
self.protocol.connectionLost(
exceptions.AttributeError: StringTransportWithDisconnection instance has no attribute 'protocol'
test.test_small.SmallFactoryTestCase.test_small_with_disconnection
-------------------------------------------------------------------------------
Ran 2 tests in 0.039s
FAILED (failures=1, errors=1)
from small_server import SmallFactory
from twisted.trial import unittest
from twisted.test import proto_helpers
class SmallFactoryTestCase(unittest.TestCase):
def setUp(self):
self.instring = "hi there"
self.factory = SmallFactory()
self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
def test_small(self):
self.tr = proto_helpers.StringTransport()
self.assertFalse(self.proto.connectionmade)
self.proto.makeConnection(self.tr)
self.assertTrue(self.proto.connectionmade)
self.proto.dataReceived(self.instring)
outstring = self.tr.value()
self.assertEqual(outstring, self.instring)
self.assertTrue(self.proto.connectionlost)
def test_small_with_disconnection(self):
self.tr = proto_helpers.StringTransportWithDisconnection()
self.assertFalse(self.proto.connectionmade)
self.proto.makeConnection(self.tr)
self.assertTrue(self.proto.connectionmade)
self.proto.dataReceived(self.instring)
outstring = self.tr.value()
self.assertEqual(outstring, self.instring)
self.assertTrue(self.proto.connectionlost)
@mathuin
Copy link
Author

mathuin commented Mar 13, 2014

test_small fails because Small.connectionLost is never run.

test_small_with_disconnection errors out because self.tr.protocol is never set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment