示例#1
0
    def testOffloadMessage(self, mocked_filename, mocked_unique_name):
        oc._MAX_CONTENTS_SIZE = 0

        self.assertEqual(oc.offload_message({}, converter), {})
        self.assertEqual(oc.offload_message({"any": "value"}, converter), {"any": "value"})

        mocked_writer = mock_open()
        with mock.patch('builtins.open', mocked_writer):
            self.assertEqual(oc.offload_message({"contents": "contents", "any": "value"}, converter),
                                                {"contents_ref": "unique_name", "any": "value"})
            mocked_writer.assert_called_once_with('filename', 'w')
            handle = mocked_writer()
            handle.write.assert_called_once_with('converted contents')

        '''
示例#2
0
    def publish(self, exchange, key, msg):
        """Publish a message on a queue

        The message will be converted to json before publishing it on the queue

        :param exchange: The exchange to publish to
        :param key: The routing key of the message
        :param msg: The message
        :return: None
        """
        # Check whether a connection has been established
        if self._channel is None:
            raise Exception("Connection with message broker not available")

        # Allow for offloaded contents
        msg = offload_message(msg, to_json)

        # Convert the message to json
        json_msg = to_json(msg)

        # Publish the message as a persistent message on the queue
        self._channel.basic_publish(
            exchange=exchange,
            routing_key=key,
            properties=pika.BasicProperties(
                delivery_mode=2  # Make messages persistent
            ),
            body=json_msg)
    def publish(self, queue, key, msg):
        """Publish a message on a queue

        The message will be converted to json before publishing it on the queue

        :param queue: The queue object
        :param msg: The message
        :return: None
        """
        def to_json(obj):
            return json.dumps(obj, cls=GobTypeJSONEncoder, allow_nan=False)

        # Check whether a connection has been established
        if self._channel is None:
            raise Exception("Connection with message broker not available")

        # Allow for offloaded contents
        msg = offload_message(msg, to_json)

        # Convert the message to json
        json_msg = to_json(msg)

        # Publish the message as a persistent message on the queue
        self._channel.basic_publish(
            exchange=queue["exchange"],
            routing_key=key,
            properties=pika.BasicProperties(
                delivery_mode=2  # Make messages persistent
            ),
            body=json_msg
        )
    def testOffloadMessageException(self, mocked_filename, mocked_unique_name):
        msg = {'contents': 'the contents'}
        converter = mock.MagicMock(side_effect=IOError)

        with mock.patch('builtins.open', mock_open()):
            self.assertEqual(msg, oc.offload_message(msg, converter))