Friday 15 January 2010

python - High-level test of Protocol behavior in twisted -



python - High-level test of Protocol behavior in twisted -

how write high-level test of protocol in twisted?

i know can utilize stringtransport test low-level protocol details, e.g. received raw info , state alter happened, or responded in way:

factory = somefactory() protocol = factory.buildprotocol("ignored") trans = proto_helpers.stringtransport("foo", 1234) protocol.makeconnection(trans) # ... protocol.datareceived("raw protocol data\n") self.assertequals("hello world!\n", self.transport.value())

however, wire protocol not critical right now. it's in flux, , i'm using amp not have think much anyway. i'd test high-level behavior:

a connection made, ensure protocol notifies gui. a list of contacts requested, create sure sends contacts i've supplied.

and on. example:

serverapp = fakeapplication() # contains application state serverfactory = somefactory(serverapp) testendpoint = magic.testendpoint() # class doesn't exist testendpoint.listen(factory) clientapp = fakeapplication() clientfactory = somefactory(clientapp) testendpoint.otherside.connect(clientfactory) self.assertequals(serverapp.nconnections, 1) self.assertequals(clientapp.nconnections, 1) assert clientapp.onnewconnection.was_called() # pseudo-code

what's missing kind of false endpoint or bidirectional transport connect 2 protocol objects.

any tips how test protocol behavior (rather wire-protocol encoding) in twisted?

as said, you're not trying test low-level details of bytes on wire. don't want test harness lets move irrelevant bytes around.

instead, want test harness lets away bytes , test application-level behavior that's of import higher level.

you mentioned you're using amp. amp api in twisted works giving command objects can pass protocol's callremote api.

if clicking "foo" button in gui supposed result in "bar" command traversing network particular arguments, hook gui code amp-alike deals commands. implementation of callremote such test double might (untested):

from twisted.internet.defer import execute class localamp(object): def __init__(self, backend): self._backend = backend def callremote(self, command, **kwargs): try: method = getattr(self._backend, command.__name__) except attributeerror: homecoming fail(nosuchmethod()) homecoming execute(method, **kwargs)

now can invoke application-level behavior without worrying protocol.

in perfect world, class distributed twisted part of testing library developers using amp. perhaps after build can contribute upstream.

python twisted python-unittest

No comments:

Post a Comment