[Twisted-Python] Avoiding network when testing Perspective Broker

Maxim Lacrima lacrima.maxim at gmail.com
Thu Oct 25 03:18:31 MDT 2012


Hi!

I am learning to develop TDD way. I want to create a server that
understands PB protocol. Initially I thought it would be a good idea to
avoid real network connections in my tests, so I tried to use
`proto_helpers.StringTransport`:

----------
import cStringIO
from twisted.spread import pb
from twisted.trial import unittest
from twisted.test import proto_helpers

class Document(pb.Root):

    def remote_convert(self, props):
        self.props = props


class DocTestCase(unittest.TestCase):

    def setUp(self):

        # set up server
        self.doc = Document()
        factory = pb.PBServerFactory(self.doc)
        self.broker = factory.buildProtocol(('127.0.0.1', 0))
        tr = proto_helpers.StringTransport()
        self.broker.makeConnection(tr)


        # this is what a client sends
        self.props = {'name': 'MyDoc',
                      'path': '/path/'}

        # prepare data
        serialized_props = self.broker.serialize(self.props)
        msg = ('message', 1, 'root', 'convert', 1,
               ['tuple', serialized_props], ['dictionary'])
        io = cStringIO.StringIO()
        self.broker._encode(msg, io.write)
        self.chunk = io.getvalue()

    def test_convert(self):
        # data arrived
        self.broker.dataReceived(self.chunk)

        self.assertEqual(self.props, self.doc.props)
----------

However, `Document.remote_convert` is never executed so the test above
fails. After debugging I discovered that `Broker._encode` produces
different results depending on whether `self.broker.makeConnection(tr)` is
called or not.

And I created a test case that shows a difference. Here `test_convert1`
succeeds while `test_convert2` fails:
----------
class DocTestCase(unittest.TestCase):

    def setUp(self):
        self.doc = Document()
        factory = pb.PBServerFactory(self.doc)
        self.broker = factory.buildProtocol(('127.0.0.1', 0))

        self.props = {'name': 'MyDoc',
                      'path': '/path/'}

        serialized_props = self.broker.serialize(self.props)
        self.msg = ('message', 1, 'root', 'convert', 1,
                    ['tuple', serialized_props], ['dictionary'])

    def test_convert1(self):
        self.broker.currentDialect = 'pb'
        self.broker.setPrefixLimit(64)
        self.broker.transport = proto_helpers.StringTransport()

        io = cStringIO.StringIO()
        self.broker._encode(self.msg, io.write)
        self.broker.dataReceived(io.getvalue())

        self.assertEqual(self.props, self.doc.props)

    def test_convert2(self):
        self.tr = proto_helpers.StringTransport()
        self.broker.makeConnection(self.tr)

        io = cStringIO.StringIO()
        self.broker._encode(self.msg, io.write)
        self.broker.dataReceived(io.getvalue())

        self.assertEqual(self.props, self.doc.props)
----------

I wonder what causes this behavior and, in general, if `StringTransport` is
suitable for testing PB protocol.

Thanks in advance. For your convenience I attached files with these test
cases.

-- 
with regards,
Maxim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20121025/65bb7b1b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_pb.py
Type: application/octet-stream
Size: 1344 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20121025/65bb7b1b/attachment-0004.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_pb1.py
Type: application/octet-stream
Size: 1099 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20121025/65bb7b1b/attachment-0005.obj>


More information about the Twisted-Python mailing list