Last active
August 29, 2015 13:57
-
-
Save mathuin/9536839 to your computer and use it in GitHub Desktop.
Problem with StringTransport() and StringTransportWithDisconnection() not handling loseConnection as expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test_small fails because Small.connectionLost is never run.
test_small_with_disconnection errors out because self.tr.protocol is never set.