<div class="gmail_quote"><div>Hi!</div><div><br></div><div>Your replies are very encouraging. Thank you!</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><div><div><div></div></div><div>I'm curious, since we rarely get to see the positive impact of documentation, and only hear about it when it didn't exist - did you discover this testing style from <a href="http://twistedmatrix.com/documents/current/core/howto/trial.html#auto5" target="_blank">http://twistedmatrix.com/documents/current/core/howto/trial.html#auto5</a> ? :)</div>
<div><div><br></div></div></div></div></blockquote></div><br clear="all"><div>Yes, I started from that document. It describes well how to call certain parts of Twisted to check some behavior and this stimulated me to discover Twisted API.</div>
<div><br></div><div>I think I have finally discovered a good way for writing tests that use PB. I used `test.test_pb.IOPump` `test.test_pb.connectedServerAndClient` as a basis for my tests.</div><div>--------</div><div><div>
from twisted.spread import pb</div><div>from twisted.trial import unittest</div><div>from twisted.test import proto_helpers</div><div><br></div><div>class Document(pb.Root):</div><div><br></div><div> def remote_convert(self):</div>
<div> return 'I was called'</div><div><br></div><div><br></div><div>class IOPump:</div><div><br></div><div> def __init__(self, client, server, clientIO, serverIO):</div><div> self.client = client</div>
<div> self.server = server</div><div> self.clientIO = clientIO</div><div> self.serverIO = serverIO</div><div><br></div><div> def pump(self):</div><div> cData = self.clientIO.value()</div><div>
sData = self.serverIO.value()</div><div> self.clientIO.clear()</div><div> self.serverIO.clear()</div><div> self.server.dataReceived(cData)</div><div> self.client.dataReceived(sData)</div>
<div><br></div><div><br></div><div>def connect(root):</div><div> serverFactory = pb.PBServerFactory(root())</div><div> serverBroker = serverFactory.buildProtocol(())</div><div><br></div><div> clientFactory = pb.PBClientFactory()</div>
<div> clientBroker = clientFactory.buildProtocol(())</div><div><br></div><div> clientTransport = proto_helpers.StringTransport()</div><div> serverTransport = proto_helpers.StringTransport()</div><div> clientBroker.makeConnection(clientTransport)</div>
<div> serverBroker.makeConnection(serverTransport)</div><div><br></div><div> pump = IOPump(clientBroker, serverBroker, clientTransport, serverTransport)</div><div> # initial communication</div><div> pump.pump()</div>
<div><br></div><div> return clientFactory, serverFactory, pump</div><div><br></div><div><br></div><div>class DocTestCase(unittest.TestCase):</div><div><br></div><div> def test_convert(self):</div><div> def cb0(doc):</div>
<div> d = doc.callRemote('convert')</div><div> return d</div><div><br></div><div> def cb1(res):</div><div> self.assertEqual('I was called', res)</div><div> return res</div>
<div><br></div><div> client, server, pump = connect(Document)</div><div><br></div><div> d = client.getRootObject()</div><div> d.addCallback(cb0)</div><div> d.addCallback(cb1)</div><div><br></div>
<div> pump.pump()</div><div> pump.pump()</div><div><br></div><div> return d</div><div><br></div></div><div>--------</div><div><br></div><div>The only caveat here is that if I forget to call pump.pump() sufficient number of times, then the callback with an assertion may not be executed and this can lead to false positives</div>
<div><br></div>-- <br>with regards,<br>Maxim<br>