Example #1
0
    def test_no_side_effects_from_message_marshal_error(self):
        # Verify that frame buffer is empty on entry
        self.assertEqual(b'', self.connection._frame_buffer)

        # Use Basic.Public with invalid body to trigger marshalling error
        method = spec.Basic.Publish()
        properties = spec.BasicProperties()
        # Verify that marshalling of method and header won't trigger error
        frame.Method(1, method).marshal()
        frame.Header(1, body_size=10, props=properties).marshal()
        # Create bogus body that should trigger an error during marshalling
        body = [1, 2, 3, 4]
        # Verify that frame body can be created using the bogus body, but
        # that marshalling will fail
        frame.Body(1, body)
        with self.assertRaises(TypeError):
            frame.Body(1, body).marshal()

        # Now, attempt to send the method with the bogus body
        with self.assertRaises(TypeError):
            self.connection._send_method(channel_number=1,
                                         method=method,
                                         content=(properties, body))

        # Now make sure that nothing is enqueued on frame buffer
        self.assertEqual(b'', self.connection._frame_buffer)
 def test_connection_blocked(self):
     self.assertEqual(self.on_unavailable.call_count, 0)
     blocked_frame = frame.Method(
         0, spec.Connection.Blocked('This is a reason'))
     self.client.on_connection_blocked(blocked_frame)
     self.assertEqual(self.on_unavailable.call_count, 1)
     self.assertTrue(self.client.blocked)
Example #3
0
    def _on_publish(self, exchange, routing_key, properties, body,
                    mandatory=True):
        LOGGER.debug('on_publish to %s using %s (pc: %s)',
                     exchange, routing_key, self.PUBLISHER_CONFIRMATIONS)
        msg = PublishedMessage(exchange, routing_key, properties, body)
        self.publish_calls.append(msg)
        error = None

        if self.publish_callable:
            try:
                self.publish_callable(
                    exchange, routing_key, properties, body, mandatory)
            except (UndeliveredMessage, UnroutableMessage) as err:
                error = err
                LOGGER.debug('Message side effect is %r', error)

        if self.PUBLISHER_CONFIRMATIONS:
            method = spec.Basic.Ack
            msg.delivered = True
            if isinstance(error, UndeliveredMessage):
                method = spec.Basic.Nack
                msg.delivered = False
            elif isinstance(error, UnroutableMessage):
                msg.delivered = False
                self.io_loop.add_callback(
                    self.process.connections['mock'].on_return, 1,
                    spec.Basic.Return(
                        312, 'NO ROUTE', exchange, routing_key),
                    properties,
                    body)
            self.io_loop.add_callback(
                self.process.connections['mock'].on_confirmation,
                frame.Method(1, method(len(self.publish_calls), False)))
 def test_connection_unblocked_no_unavailable(self):
     self.client.state = amqp.Client.STATE_IDLE
     self.assertTrue(self.client.idle)
     self.client.on_ready = None
     unblocked_frame = frame.Method(0, spec.Connection.Unblocked())
     self.client.on_connection_unblocked(unblocked_frame)
     self.assertTrue(self.client.ready)
 def test_connection_unblocked(self):
     self.client.state = amqp.Client.STATE_IDLE
     self.assertTrue(self.client.idle)
     unblocked_frame = frame.Method(0, spec.Connection.Unblocked())
     self.client.on_connection_unblocked(unblocked_frame)
     self.assertEqual(self.on_ready.call_count, 1)
     self.assertTrue(self.client.ready)
Example #6
0
 def test_ascii_body_instance(self):
     method_frame = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(method_frame)
     header_frame = frame.Header(1, 11, spec.BasicProperties)
     self.obj.process(header_frame)
     body_frame = frame.Body(1, 'foo-bar-baz')
     method_frame, header_frame, body_value = self.obj.process(body_frame)
     self.assertIsInstance(body_value, str)
Example #7
0
 def test_process_with_body_frame_partial(self):
     value = frame.Header(1, 100, spec.BasicProperties)
     self.obj.process(value)
     value = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(value)
     value = frame.Body(1, 'abc123')
     self.obj.process(value)
     self.assertEqual(self.obj._body_fragments, [value.fragment])
Example #8
0
 def test_process_with_full_message(self):
     method_frame = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(method_frame)
     header_frame = frame.Header(1, 6, spec.BasicProperties)
     self.obj.process(header_frame)
     body_frame = frame.Body(1, 'abc123')
     response = self.obj.process(body_frame)
     self.assertEqual(response, (method_frame, header_frame, 'abc123'))
Example #9
0
 def test_process_with_body_frame_six_bytes(self):
     method_frame = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(method_frame)
     header_frame = frame.Header(1, 10, spec.BasicProperties)
     self.obj.process(header_frame)
     body_frame = frame.Body(1, 'abc123')
     self.obj.process(body_frame)
     self.assertEqual(self.obj._seen_so_far, 6)
Example #10
0
 def test_process_with_body_frame_too_big(self):
     method_frame = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(method_frame)
     header_frame = frame.Header(1, 6, spec.BasicProperties)
     self.obj.process(header_frame)
     body_frame = frame.Body(1, 'abcd1234')
     self.assertRaises(exceptions.BodyTooLongError, self.obj.process,
                       body_frame)
 def test_process_with_body_frame_too_big(self):
     obj = channel.ContentFrameDispatcher()
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 6, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, 'abcd1234')
     self.assertRaises(exceptions.BodyTooLongError, obj.process, body_frame)
 def test_process_with_body_frame_six_bytes(self):
     obj = channel.ContentFrameDispatcher(False)
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 10, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, 'abc123')
     obj.process(body_frame)
     self.assertEqual(obj._seen_so_far, 6)
 def test_process_with_full_message(self):
     obj = channel.ContentFrameDispatcher(False)
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 6, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, 'abc123')
     response = obj.process(body_frame)
     self.assertEqual(response, (method_frame, header_frame, 'abc123'))
 def test_process_with_body_frame_partial(self):
     obj = channel.ContentFrameDispatcher(False)
     value = frame.Header(1, 100, spec.BasicProperties)
     obj.process(value)
     value = frame.Method(1, spec.Basic.Deliver())
     obj.process(value)
     value = frame.Body(1, 'abc123')
     obj.process(value)
     self.assertEqual(obj._body_fragments, [value.fragment])
 def test_ascii_body_instance(self):
     obj = channel.ContentFrameDispatcher(False)
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 11, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, 'foo-bar-baz')
     method_frame, header_frame, body_value = obj.process(body_frame)
     self.assertIsInstance(body_value, str)
 def test_utf8_body_instance(self):
     obj = channel.ContentFrameDispatcher(False)
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 14, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, 'utf8_value=\xe2\x9c\x93')
     method_frame, header_frame, body_value = obj.process(body_frame)
     self.assertIsInstance(body_value, unicode)
Example #17
0
 def test_reset_header_frame(self):
     method_frame = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(method_frame)
     header_frame = frame.Header(1, 10, spec.BasicProperties)
     self.obj.process(header_frame)
     body_frame = frame.Body(1, 'abc123')
     self.obj.process(body_frame)
     self.obj._reset()
     self.assertEqual(self.obj._header_frame, None)
 def test_binary_non_unicode_value(self):
     expectation =('a', 0.8)
     obj = channel.ContentFrameDispatcher(False)
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 20, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, marshal.dumps(expectation))
     method_frame, header_frame, body_value = obj.process(body_frame)
     self.assertEqual(marshal.loads(body_value), expectation)
 def test_utf8_body_value(self):
     expectation = u'utf8_value=✓'
     obj = channel.ContentFrameDispatcher(False)
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 14, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, 'utf8_value=\xe2\x9c\x93')
     method_frame, header_frame, body_value = obj.process(body_frame)
     self.assertEqual(body_value, expectation)
 def test_reset_method_frame(self):
     obj = channel.ContentFrameDispatcher(False)
     method_frame = frame.Method(1, spec.Basic.Deliver())
     obj.process(method_frame)
     header_frame = frame.Header(1, 10, spec.BasicProperties)
     obj.process(header_frame)
     body_frame = frame.Body(1, 'abc123')
     obj.process(body_frame)
     obj._reset()
     self.assertEqual(obj._method_frame, None)
Example #21
0
 def test_ascii_body_value(self):
     expectation = 'foo-bar-baz'
     self.obj = channel.ContentFrameDispatcher()
     method_frame = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(method_frame)
     header_frame = frame.Header(1, 11, spec.BasicProperties)
     self.obj.process(header_frame)
     body_frame = frame.Body(1, 'foo-bar-baz')
     method_frame, header_frame, body_value = self.obj.process(body_frame)
     self.assertEqual(body_value, expectation)
     self.assertIsInstance(body_value, str)
Example #22
0
    def test_deliver_frame_to_channel_with_frame_for_unknown_channel(
            self, critical_mock):
        unknown_channel_num = 99
        self.assertNotIn(unknown_channel_num, self.connection._channels)

        unexpected_frame = frame.Method(unknown_channel_num, mock.Mock())
        self.connection._deliver_frame_to_channel(unexpected_frame)

        critical_mock.assert_called_once_with(
            'Received %s frame for unregistered channel %i on %s',
            unexpected_frame.NAME, unknown_channel_num, self.connection)
Example #23
0
 def test_binary_non_unicode_value(self):
     expectation = ('a', 0.8)
     self.obj = channel.ContentFrameAssembler()
     method_frame = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(method_frame)
     marshalled_body = marshal.dumps(expectation)
     header_frame = frame.Header(1, len(marshalled_body),
                                 spec.BasicProperties)
     self.obj.process(header_frame)
     body_frame = frame.Body(1, marshalled_body)
     method_frame, header_frame, body_value = self.obj.process(body_frame)
     self.assertEqual(marshal.loads(body_value), expectation)
Example #24
0
 def _send_message(self, channel_number, method, content=None):
     length = len(content[1])
     self.connection.send_frame(
         frame.Method(channel_number, method).marshal())
     self.connection.send_frame(
         frame.Header(channel_number, length, content[0]))
     if content[1]:
         body_max_length = (self.connection.frame_max -
                            spec.FRAME_HEADER_SIZE - spec.FRAME_END_SIZE)
         chunks = int(math.ceil(float(length) / body_max_length))
         for chunk in xrange(0, chunks):
             start = chunk * body_max_length
             end = start + body_max_length
             if end > length:
                 end = length
             self.connection.send_frame(
                 frame.Body(channel_number, content[1][start:end]))
Example #25
0
    def _send_method(self, channel_number, method_frame, content=None):
        """Constructs a RPC method frame and then sends it to the broker.

        :param int channel_number: The channel number for the frame
        :param pika.object.Method method_frame: The method frame to send
        :param tuple content: If set, is a content frame, is tuple of
                              properties and body.

        """
        LOGGER.debug('Sending on channel %i: %r', channel_number, method_frame)
        self._send_frame(frame.Method(channel_number, method_frame))
        if isinstance(content, tuple):
            self._send_frame(frame.Header(channel_number,
                                          len(content[1]),
                                           content[0]))
            if content[1]:
                body_buf = simplebuffer.SimpleBuffer(content[1])
                while body_buf:
                    piece_len = min(len(body_buf), self._body_max_length)
                    piece = body_buf.read_and_consume(piece_len)
                    self._send_frame(frame.Body(channel_number, piece))
Example #26
0
 def test_process_with_unexpected_frame_type(self):
     value = frame.Method(1, spec.Basic.Qos())
     self.assertRaises(exceptions.UnexpectedFrameError, self.obj.process,
                       value)
Example #27
0
 def frame_underscore_marshal_test(self):
     basic_ack = frame.Method(1, spec.Basic.Ack(100))
     self.assertEqual(basic_ack.marshal(), self.BASIC_ACK)
Example #28
0
 def test_process_with_basic_deliver(self):
     value = frame.Method(1, spec.Basic.Deliver())
     self.obj.process(value)
     self.assertEqual(self.obj._method_frame, value)
 def test_connection_blocked_no_unavailable(self):
     self.client.on_unavailable = None
     blocked_frame = frame.Method(
         0, spec.Connection.Blocked('This is a reason'))
     self.client.on_connection_blocked(blocked_frame)
     self.assertTrue(self.client.blocked)
Example #30
0
 def test_name_or_value_method_frame(self):
     value = frame.Method(1, self.PREFIX_CLASS())
     self.assertEqual(callback._name_or_value(value), self.PREFIX)