def test_posting_object_state(self):
        """Given that I have an RMQ producer
            when I deserialize an object via the producer
            then I should be able to re-hydrate it via the consumer
        """
        request = TestMessage()
        header = BrightsideMessageHeader(uuid4(), self.test_topic, BrightsideMessageType.command, uuid4())
        body = BrightsideMessageBody(JsonRequestSerializer(request=request).serialize_to_json(), BrightsideMessageBodyType.application_json)
        message = BrightsideMessage(header, body)

        self._consumer.purge()

        self._producer.send(message)

        read_message = self._consumer.receive(3)
        self._consumer.acknowledge(read_message)

        deserialized_request = JsonRequestSerializer(request=TestMessage(), serialized_request=read_message.body.value)\
            .deserialize_from_json()

        self.assertIsNotNone(deserialized_request)
        self.assertEqual(request.bool_value, deserialized_request.bool_value)
        self.assertEqual(request.float_value, deserialized_request.float_value)
        self.assertEqual(request.integer_value, deserialized_request.integer_value)
        self.assertEqual(request.id, deserialized_request.id)
Beispiel #2
0
    def receive(self, timeout: int) -> BrightsideMessage:

        self._message = BrightsideMessage(BrightsideMessageHeader(uuid4(), "", BrightsideMessageType.none), BrightsideMessageBody(""))

        def _consume(cnx: BrokerConnection, timesup: int) -> None:
            try:
                cnx.drain_events(timeout=timesup)
            except kombu_exceptions.TimeoutError as te:
                pass

        def _consume_errors(exc, interval: int)-> None:
            self._logger.error('Draining error: %s, will retry triggering in %s seconds', exc, interval, exc_info=True)

        def _read_message(body: str, msg: KombuMessage) -> None:
            self._logger.debug("Monitoring event received at: %s headers: %s payload: %s", datetime.utcnow().isoformat(), msg.headers, body)
            self._message = self._message_factory.create_message(msg)
            msg.ack()

        connection = BrokerConnection(hostname=self._amqp_uri)
        with connections[connection].acquire(block=True) as conn:
            self._logger.debug('Got connection: %s', conn.as_uri())
            with Consumer(conn, queues=[self._queue], callbacks=[_read_message]) as consumer:
                consumer.qos(prefetch_count=1)
                ensure_kwargs = self.RETRY_OPTIONS.copy()
                ensure_kwargs['errback'] = _consume_errors
                safe_drain = conn.ensure(consumer, _consume, **ensure_kwargs)
                safe_drain(conn, timeout)

        return self._message
Beispiel #3
0
def run_server():
    # setup
    topic = "cup of tea"
    connection = BrightsideKombuConnection("amqp://*****:*****@localhost:5672//", "teasmaid.exchange")
    producer = BrightsideKombuProducer(connection)

    # tea request
    request = TeaRequest(BeverageType.tea, has_milk=True, no_of_sugars=2)

    header = BrightsideMessageHeader(uuid4(), topic, BrightsideMessageType.command, uuid4())
    body = BrightsideMessageBody(json.dumps(request.__dict__))
    message = BrightsideMessage(header, body)

    producer.send(message)
    def test_posting_a_message(self):
        """Given that I have an RMQ message producer
            when I send that message via the producer
            then I should be able to read that message via the consumer
        """
        header = BrightsideMessageHeader(uuid4(), self.test_topic, BrightsideMessageType.command, uuid4())
        body = BrightsideMessageBody("test content")
        message = BrightsideMessage(header, body)

        self._consumer.purge()

        self._producer.send(message)

        read_message = self._consumer.receive(3)
        self._consumer.acknowledge(read_message)

        self.assertEqual(message.id, read_message.id)
        self.assertEqual(message.body.value, read_message.body.value)
        self.assertTrue(self._consumer.has_acknowledged(read_message))
Beispiel #5
0
    def create_message(self, message: Message) -> BrightsideMessage:

        self._has_read_errors = False

        def _get_correlation_id() -> UUID:
            header, err = self._read_header(message_correlation_id_header, message)
            if err is None:
                return UUID(header)
            else:
                self._has_read_errors = True
                return ""

        def _get_message_id() -> UUID:
            header, err = self._read_header(message_id_header, message)
            if err is None:
                return UUID(header)
            else:
                self._has_read_errors = True
                return uuid4()

        def _get_message_type() -> BrightsideMessageType:
            header, err = self._read_header(message_type_header, message)
            if err is None:
                return BrightsideMessageType(header)
            else:
                self._has_read_errors = True
                return BrightsideMessageType.unacceptable

        def _get_payload() -> str:
            body, err = self._read_payload(message)
            if err is None:
                return body
            else:
                self._has_read_errors = True
                return ""

        def _get_payload_type() -> str:
            payload_type, err = self._read_payload_type(message)
            if err is None:
                return payload_type
            else:
                self._has_read_errors = True
                return ""

        def _get_topic() -> str:
            header, err = self._read_header(message_topic_name_header, message)
            if err is None:
                return header
            else:
                self._has_read_errors = True
                return ""

        message_id = _get_message_id()
        topic = _get_topic()
        message_type = _get_message_type() if not message.errors or self._has_read_errors else BrightsideMessageType.unacceptable
        correlation_id = _get_correlation_id()
        payload = _get_payload()
        payload_type = _get_payload_type()

        message_header = BrightsideMessageHeader(identity=message_id, topic=topic, message_type=message_type,
                                                 correlation_id=correlation_id, content_type="json")

        message_body = BrightsideMessageBody(body=payload, body_type=payload_type)

        return BrightsideMessage(message_header, message_body)
Beispiel #6
0
def map_to_message(request: Request) -> BrightsideMessage:
    message_body = BrightsideMessageBody(json.dumps(request.__dict__))
    message = BrightsideMessage(BrightsideMessageHeader(request.id),
                                message_body)
    return message