Ejemplo n.º 1
0
 def test_can_hydrate_extra_fields(self):
     from nautilus.conventions.actions import serialize_action, hydrate_action
     # the target
     target = dict(action_type='foo', payload='bar', foo='bar')
     # the serialized form of the object
     serialized = serialize_action(**target)
     # make sure we can hydrate the serialized form into the target
     assert hydrate_action(serialized) == target, (
         "Could not hydrate action with extra fields.")
Ejemplo n.º 2
0
    def test_can_serialize_and_deserialize_action(self):
        from nautilus.conventions.actions import serialize_action, hydrate_action
        # the target
        target = dict(action_type='hello', payload='world')
        # the hydrated form of the object
        serialized = serialize_action(**target)

        # make sure we can hydrate the hydrated form into the target
        assert hydrate_action(serialized) == target, (
            "Could not serialize/deserialize action.")
Ejemplo n.º 3
0
 def test_can_hydrate_extra_fields(self):
     from nautilus.conventions.actions import serialize_action, hydrate_action
     # the target
     target = dict(action_type='foo', payload='bar', foo='bar')
     # the serialized form of the object
     serialized = serialize_action(**target)
     # make sure we can hydrate the serialized form into the target
     assert hydrate_action(serialized) == target, (
         "Could not hydrate action with extra fields."
     )
Ejemplo n.º 4
0
    def test_can_serialize_and_deserialize_action(self):
        from nautilus.conventions.actions import serialize_action, hydrate_action
        # the target
        target = dict(
            action_type='hello',
            payload='world'
        )
        # the hydrated form of the object
        serialized = serialize_action(**target)

        # make sure we can hydrate the hydrated form into the target
        assert hydrate_action(serialized) == target, (
            "Could not serialize/deserialize action."
        )
Ejemplo n.º 5
0
    async def _consume_event_callback(self):
        # continuously loop
        while True:

            # grab the next message
            msg = await self._consumer.getone()
            # parse the message as json
            message = hydrate_action(msg.value.decode())
            # the correlation_id associated with this message
            correlation_id = message.get('correlation_id')
            # the action type of the message
            action_type = message['action_type']
            # if there is a consumer pattern
            if self.consumer_pattern:
                # if the action_type does not satisfy the pattern
                if not re.match(self.consumer_pattern, message['action_type']):
                    # don't do anything
                    continue

            # if we know how to respond to this message
            if correlation_id and correlation_id in self._request_handlers \
                and action_type != self._pending_outbound[correlation_id]:

                # pass the message to the handler
                self._request_handlers[correlation_id](message['payload'])
                # remove the entry in the handler dict
                del self._request_handlers[correlation_id]
                del self._pending_outbound[correlation_id]

            # otherwise there was no correlation id, pass it along to the general handlers
            else:
                # build the dictionary of message properties
                message_props = {
                    'correlation_id': correlation_id
                }

                # pass it to the handler
                await self.handle_message(
                    props=message_props,
                    **message
                )
Ejemplo n.º 6
0
    async def _consume_event_callback(self):
        # continuously loop
        while True:

            # grab the next message
            msg = await self._consumer.getone()
            # parse the message as json
            message = hydrate_action(msg.value.decode())
            # the correlation_id associated with this message
            correlation_id = message.get('correlation_id')
            # the action type of the message
            action_type = message['action_type']
            # if there is a consumer pattern
            if self.consumer_pattern:
                # if the action_type does not satisfy the pattern
                if not re.match(self.consumer_pattern, message['action_type']):
                    # don't do anything
                    continue

            # if we know how to respond to this message
            if correlation_id and correlation_id in self._request_handlers \
                and action_type != self._pending_outbound[correlation_id]:

                # pass the message to the handler
                self._request_handlers[correlation_id](message['payload'])
                # remove the entry in the handler dict
                del self._request_handlers[correlation_id]
                del self._pending_outbound[correlation_id]

            # otherwise there was no correlation id, pass it along to the general handlers
            else:
                # build the dictionary of message properties
                message_props = {'correlation_id': correlation_id}

                # pass it to the handler
                await self.handle_message(props=message_props, **message)