class BusCtlClient(object): def __init__(self): self._transport = None self._marshaler = Marshaler() def close(self): if not self.connected: return self._transport.close() self._transport = None def connect(self): if self.connected: raise Exception('already connected') self._transport = self._new_transport() @property def connected(self): return self._transport is not None def _new_transport(self): return AMQPTransportClient.create_and_connect() def declare_exchange(self, name, exchange_type, durable): self._transport.exchange_declare(name, exchange_type, durable) def publish_event(self, exchange, routing_key, event): body = self._marshaler.marshal_command(event) self._transport.send(exchange, routing_key, body)
def test_marshal_command(self): command = Mock() command.name = 'foobar' command.marshal.return_value = {'a': 1} marshal = Marshaler({}) result = marshal.marshal_command(command) command.marshal.assert_called_once_with() self.assertEquals(result, ('{"data": {"a": 1}, "name": "foobar"}'))