Example #1
0
    def test_unmarshal_message(self):
        json = '{"error": null, "value": "foobar"}'
        expected = {'value': 'foobar', 'error': None}
        marshal = Marshaler({})

        result = marshal.unmarshal_message(json)

        self.assertEquals(result, expected)
Example #2
0
    def test_unmarshal_message(self):
        json = '{"error": null, "value": "foobar"}'
        expected = {'value': 'foobar', 'error': None}
        marshal = Marshaler({})

        result = marshal.unmarshal_message(json)

        self.assertEquals(result, expected)
Example #3
0
    def test_unmarshal_response(self):
        json = '{"error": null, "value": "foobar"}'

        marshal = Marshaler({})
        result = marshal.unmarshal_response(json)

        self.assertTrue(isinstance(result, CommandResponse))
        self.assertEquals(result.value, 'foobar')
        self.assertEquals(result.error, None)
Example #4
0
    def test_unmarshal_response(self):
        json = '{"error": null, "value": "foobar"}'

        marshal = Marshaler({})
        result = marshal.unmarshal_response(json)

        self.assertTrue(isinstance(result, CommandResponse))
        self.assertEquals(result.value, 'foobar')
        self.assertEquals(result.error, None)
Example #5
0
    def test_marshal_response(self):
        response = Mock()
        response.marshal.return_value = {'value': 'success', 'error': None}

        marshal = Marshaler({})

        result = marshal.marshal_response(response)

        response.marshal.assert_called_once_with()
        self.assertEquals(result, '{"value": "success", "error": null}')
Example #6
0
    def test_unmarshal_command(self):
        json = '{"name": "foobar", "data": {"a":1}}'

        command = Mock()
        registry = {'foobar': command}

        marshal = Marshaler(registry)
        marshal.unmarshal_command(json)

        command.unmarshal.assert_called_once_with({'a': 1})
Example #7
0
    def test_marshal_response(self):
        response = Mock()
        response.marshal.return_value = {'value': 'success', 'error': None}

        marshal = Marshaler({})

        result = marshal.marshal_response(response)

        response.marshal.assert_called_once_with()
        self.assertEquals(result, '{"value": "success", "error": null}')
Example #8
0
    def test_unmarshal_command(self):
        json = '{"name": "foobar", "data": {"a":1}}'

        command = Mock()
        registry = {'foobar': command}

        marshal = Marshaler(registry)
        marshal.unmarshal_command(json)

        command.unmarshal.assert_called_once_with({'a': 1})
Example #9
0
    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"}'))
Example #10
0
    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"}'))
Example #11
0
class BusConsumer(object):

    def __init__(self):
        self._marshaler = Marshaler()

    def connect(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
        self.channel = self.connection.channel()

    def add_binding(self, callback, queue_name, exchange, key):
        self.callback = callback
        self.queue_name = queue_name
        self.channel.queue_declare(queue=queue_name, exclusive=False, durable=True)
        self.channel.queue_bind(queue=queue_name, exchange=exchange, routing_key=key)

    def on_message(self, channel, method, header, body):
        body = self._marshaler.unmarshal_message(body)
        logger.debug('Received new event : %s', body)
        self.callback(body)
        self.channel.basic_ack(delivery_tag=method.delivery_tag)

    def run(self):
        logger.info('Running...')
        try:
            self.channel.basic_consume(self.on_message, self.queue_name)
            self.channel.start_consuming()
        except AMQPConnectionError:
            raise BusConnectionError()

    def stop(self):
        if self.connection.is_open:
            self.channel.stop_consuming()
            self.connection.close()
Example #12
0
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)
Example #13
0
class BusConsumer(object):
    def __init__(self):
        self._marshaler = Marshaler()

    def connect(self):
        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters('localhost'))
        self.channel = self.connection.channel()

    def add_binding(self, callback, queue_name, exchange, key):
        self.callback = callback
        self.queue_name = queue_name
        self.channel.queue_declare(queue=queue_name,
                                   exclusive=False,
                                   durable=True)
        self.channel.queue_bind(queue=queue_name,
                                exchange=exchange,
                                routing_key=key)

    def on_message(self, channel, method, header, body):
        body = self._marshaler.unmarshal_message(body)
        logger.debug('Received new event : %s', body)
        self.callback(body)
        self.channel.basic_ack(delivery_tag=method.delivery_tag)

    def run(self):
        logger.info('Running...')
        try:
            self.channel.basic_consume(self.on_message, self.queue_name)
            self.channel.start_consuming()
        except AMQPConnectionError:
            raise BusConnectionError()

    def stop(self):
        if self.connection.is_open:
            self.channel.stop_consuming()
            self.connection.close()
Example #14
0
 def __init__(self):
     self._transport = None
     self._marshaler = Marshaler()
Example #15
0
 def __init__(self):
     self._transport = None
     self._marshaler = Marshaler()
Example #16
0
 def __init__(self):
     self._marshaler = Marshaler()
Example #17
0
 def run(self):
     self._marshaler = Marshaler(self._commands_registry)
     self._transport.run()
Example #18
0
 def __init__(self):
     self._marshaler = Marshaler()