def test_equality(self): a = spec.BasicProperties(content_type='text/plain') self.assertEqual(a, a) self.assertNotEqual(a, None) b = spec.BasicProperties() self.assertNotEqual(a, b) b.content_type = 'text/plain' self.assertEqual(a, b) a.correlation_id = 'abc123' self.assertNotEqual(a, b) b.correlation_id = 'abc123' self.assertEqual(a, b)
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 basic_publish(self, exchange, routing_key, body, properties=None, mandatory=False, immediate=False): """Publish to the channel with the given exchange, routing key and body. For more information on basic_publish and what the parameters do, see: http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish :param exchange: The exchange to publish to :type exchange: str or unicode :param routing_key: The routing key to bind on :type routing_key: str or unicode :param body: The message body :type body: str or unicode :param pika.spec.BasicProperties properties: Basic.properties :param bool mandatory: The mandatory flag :param bool immediate: The immediate flag """ if not self.is_open: raise exceptions.ChannelClosed() if immediate: LOGGER.warning('The immediate flag is deprecated in RabbitMQ') if isinstance(body, unicode): body = body.encode('utf-8') properties = properties or spec.BasicProperties() self._send_method(spec.Basic.Publish(exchange=exchange, routing_key=routing_key, mandatory=mandatory, immediate=immediate), (properties, body))
def test_message_id_only(self): message_id = str(uuid.uuid4()) properties = spec.BasicProperties('application/json', message_id=message_id) self.assertEqual(utils.message_info('', '', properties), message_id)
def _generate_amqp_data(self): return { 'exchange': self.output_id.amqp_exchange, 'routing_key': self.output_id.amqp_routing_key, 'body': self.output_id.amqp_payload, 'properties': spec.BasicProperties(), 'mandatory': False }
def prepare_message(self, body, priority=None, content_type=None, content_encoding=None, headers=None, properties=None): properties = spec.BasicProperties(priority=priority, content_type=content_type, content_encoding=content_encoding, headers=headers) return body, properties
def send_message(self, exchange, routing_key, msg): """Send a single message.""" log.msg('%s (%s): %s' % (exchange, routing_key, repr(msg)), system='Pika:=>') yield self.channel.exchange_declare(exchange=exchange, type='topic', durable=True, auto_delete=False) prop = spec.BasicProperties(delivery_mode=2) try: yield self.channel.basic_publish(exchange=exchange, routing_key=routing_key, body=msg, properties=prop) except Exception as error: log.msg('Error while sending message: %s' % error, system=self.name)
def send_message(_result): d = self.channel.basic_publish("testexch", "testrk", "testbody") # Send the Basic.Return frame method = spec.Basic.Return(exchange="testexch", routing_key="testrk") return_cb(self.channel, method, spec.BasicProperties(), "testbody") # Send the Basic.Nack frame frame = Method(1, spec.Basic.Nack(delivery_tag=1)) self.channel._on_delivery_confirmation(frame) return d
def consume(self, queue, no_ack=False, exclusive=False, arguments=None, inactivity_timeout=None): # No messages to consume if not published_message: yield (None, None, None) yield (spec.Basic.Deliver(delivery_tag=1, exchange="exchange", routing_key="key"), spec.BasicProperties(), published_message)
def test_publishing_passthrough(self, get_properties): properties = spec.BasicProperties() get_properties.return_value = properties self.consumer.publish_message('foo', 'bar', None, 'test_value') expectation = { 'body': 'test_value', 'exchange': 'foo', 'properties': properties, 'routing_key': 'bar' } self.channel.basic_publish.assert_called_once_with(**expectation)
def send_message(self, exchange, routing_key, msg): """Send a single message.""" LOG.debug('%s (%s): %s', exchange, routing_key, repr(msg)) # yield self.channel.exchange_declare(exchange=exchange, type='topic', durable=True, auto_delete=False) prop = spec.BasicProperties(delivery_mode=2) try: yield self.channel.basic_publish(exchange=exchange, routing_key=routing_key, body=msg, properties=prop) except Exception as error: LOG.error('Error while sending message: %s', error)
def test_message_info_output_no_correlation_id(self): message_id = str(uuid.uuid4()) exchange = str(uuid.uuid4()) routing_key = str(uuid.uuid4()) expectation = '{} published to "{}" using "{}"'.format( message_id, exchange, routing_key) properties = spec.BasicProperties('application/json', message_id=message_id) self.assertEqual(utils.message_info(exchange, routing_key, properties), expectation)
def __init__(self, host): # try: # if not self.local.queue : # self.local.queue = puka.Client("amqp://"+ host + "/") # self.connect() # self.local.properties = spec.BasicProperties() # except AttributeError: # print 'init' self.local.queue = PukaQueue("amqp://"+ host + "/") # self.connect() self.local.properties = spec.BasicProperties()
def basic_publish(self, exchange, routing_key, body, properties=None, mandatory=False): """ Publish to the channel with the given exchange, routing key and body. If flow control is enabled and you publish a message while another is sending, a ContentTransmissionForbidden exception ill be generated. """ # If properties are not passed in, use the spec's default properties = properties or spec.BasicProperties() self.transport.send_method(spec.Basic.Publish(exchange=exchange, routing_key=routing_key, mandatory=mandatory), (properties, body), False)
def basic_publish(self, exchange, routing_key, body, properties=None, mandatory=False): """ Publish to the channel with the given exchange, routing key and body. For more information on basic_publish and what the parameters do, see: http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish """ # If properties are not passed in, use the spec's default properties = properties or spec.BasicProperties() self.transport.send_method(spec.Basic.Publish(exchange=exchange, routing_key=routing_key, mandatory=mandatory), (properties, body))
def test_publishing(self, get_properties): properties = spec.BasicProperties( content_type=avroconsumer.DATUM_MIME_TYPE, type='push.apns.v1') get_properties.return_value = properties self.consumer.publish_message('foo', 'bar', { 'content_type': avroconsumer.DATUM_MIME_TYPE, 'type': 'push.apns.v1' }, self.json_data) expectation = { 'body': self.avro_datum, 'exchange': 'foo', 'properties': properties, 'routing_key': 'bar' } self.channel.basic_publish.assert_called_once_with(**expectation)
def _prepare_message_to_send(self): """Combine user's message fields an system fields (_unique_id, context's data etc) """ msg = self.message.copy() if self.context: for key, value in six.iteritems(self.context): key = six.text_type(key) msg['_$_' + key] = value props = pika_spec.BasicProperties( content_type=self._content_type, headers={_VERSION_HEADER: _VERSION}, message_id=self.unique_id, ) return msg, props
def basic_publish(self, exchange, routing_key, body, properties=None, mandatory=False, immediate=False): self._validate_connection_and_channel() if immediate: LOGGER.warning('The immediate flag is deprecated in RabbitMQ') if isinstance(body, unicode_type): body = body.encode('utf-8') properties = properties or spec.BasicProperties() self.send_method( spec.Basic.Publish(exchange=exchange, routing_key=routing_key, mandatory=mandatory, immediate=immediate), (properties, body))
def test_send_message_updates_frames_sent_and_bytes_sent( self, _adapter_emit_data): self.connection._flush_outbound = mock.Mock() self.connection._body_max_length = 10000 method = spec.Basic.Publish(exchange='my-exchange', routing_key='my-route') props = spec.BasicProperties() body = b'b' * 1000000 self.connection._send_method(channel_number=1, method=method, content=(props, body)) frames_sent = _adapter_emit_data.call_count bytes_sent = sum( len(call[0][0]) for call in _adapter_emit_data.call_args_list) self.assertEqual(self.connection.frames_sent, frames_sent) self.assertEqual(self.connection.bytes_sent, bytes_sent)
def test_send_message_updates_frames_sent_and_bytes_sent(self): self.connection._flush_outbound = mock.Mock() self.connection._body_max_length = 10000 method = spec.Basic.Publish( exchange='my-exchange', routing_key='my-route') props = spec.BasicProperties() body = b'b' * 1000000 self.connection._send_method( channel_number=1, method=method, content=(props, body)) frames_sent = len(self.connection.outbound_buffer) bytes_sent = sum( len(frame) for frame in self.connection.outbound_buffer) self.assertEqual(self.connection.frames_sent, frames_sent) self.assertEqual(self.connection.bytes_sent, bytes_sent) # Make sure _detect_backpressure doesn't throw self.connection._detect_backpressure()
def create_message(self, message, properties=None, exchange='rejected', routing_key='test'): """Create a message instance for use with the consumer in testing. :param any message: the body of the message to create :param dict properties: AMQP message properties :param str exchange: The exchange the message should appear to be from :param str routing_key: The message's routing key :rtype: :class:`rejected.data.Message` """ if not properties: properties = {} if isinstance(message, dict) and \ properties.get('content_type') == 'application/json': message = json.dumps(message) return data.Message( connection='mock', channel=self.process.connections['mock'].channel, method=spec.Basic.Deliver( 'ctag0', 1, False, exchange, routing_key), properties=spec.BasicProperties( app_id=properties.get('app_id', 'rejected.testing'), content_encoding=properties.get('content_encoding'), content_type=properties.get('content_type'), correlation_id=properties.get( 'correlation_id', self.correlation_id), delivery_mode=properties.get('delivery_mode', 1), expiration=properties.get('expiration'), headers=properties.get('headers'), message_id=properties.get('message_id', str(uuid.uuid4())), priority=properties.get('priority'), reply_to=properties.get('reply_to'), timestamp=properties.get('timestamp', int(time.time())), type=properties.get('type'), user_id=properties.get('user_id') ), body=message)
def publish_message(self, exchange, routing_key, message, mimetype='text/plain', mandatory=False, immediate=False): """Publish a message to RabbitMQ. Auto-JSON encodes all non-string data types. :param exchange: RabbitMQ exchange to publish to. :type exchange: str :param routing_key: RabbitMQ publishing routing key :type routing_key: str :param mimetime: mimetype of the message :type mimetype: str :param mandatory: AMQP Basic.Publish mandatory flag :type mandatory: bool :param immediate: AMQP Basic.Publish immediate flag :type immediate: bool """ # Auto-JSON encode if it's not a string if not isinstance(message, basestring): message = json.dumps(message) mimetype = 'application/json' # Create the properties for the message props = spec.BasicProperties(content_type=mimetype, content_encoding=self.encoding, timestamp=time.time(), app_id=self.app_id, user_id=self.params.credentials.username) # Publish the message self._channel.basic_publish(exchange, routing_key, message, props, mandatory, immediate)
def basic_publish(self, exchange, routing_key, body, properties=None, mandatory=False, immediate=False): """Publish to the channel with the given exchange, routing key and body. Returns a boolean value indicating the success of the operation. For more information on basic_publish and what the parameters do, see: http://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish :param exchange: The exchange to publish to :type exchange: str or unicode :param routing_key: The routing key to bind on :type routing_key: str or unicode :param body: The message body :type body: str or unicode :param pika.spec.Properties properties: Basic.properties :param bool mandatory: The mandatory flag :param bool immediate: The immediate flag """ if not self.is_open: raise exceptions.ChannelClosed() if immediate: LOGGER.warning('The immediate flag is deprecated in RabbitMQ') properties = properties or spec.BasicProperties() if mandatory: self._response = None if isinstance(body, unicode): body = body.encode('utf-8') if self._confirmation: response = self._rpc( spec.Basic.Publish(exchange=exchange, routing_key=routing_key, mandatory=mandatory, immediate=immediate), None, [spec.Basic.Ack, spec.Basic.Nack], (properties, body)) if mandatory and self._response: response = self._response[0] LOGGER.warning('Message was returned (%s): %s', response.reply_code, response.reply_text) return False if isinstance(response.method, spec.Basic.Ack): return True elif isinstance(response.method, spec.Basic.Nack): return False else: raise ValueError('Unexpected frame type: %r', response) else: self._send_method( spec.Basic.Publish(exchange=exchange, routing_key=routing_key, mandatory=mandatory, immediate=immediate), (properties, body), False) if mandatory: if self._response: response = self._response[0] LOGGER.warning('Message was returned (%s): %s', response.reply_code, response.reply_text) return False return True
def test_headers_repr(self): hdr = 'timestamp_in_ms' v = long(912598613) h = { hdr : v } p = spec.BasicProperties(content_type='text/plain', headers=h) self.assertEqual(repr(p.headers[hdr]), '912598613L')
def test_no_identifiable_info(self): properties = spec.BasicProperties('application/json') self.assertEqual(utils.message_info('', '', properties), '')
import mock from pika import spec import time __version__ = '99.99.99' CHANNEL = mock.Mock('pika.channel.Channel') METHOD = spec.Basic.Deliver('ctag0', 1, False, 'exchange', 'routing_key') PROPERTIES = spec.BasicProperties(content_type='application/json', content_encoding='qux', headers={ 'foo': 'bar', 'baz': 1 }, delivery_mode=2, priority=5, correlation_id='c123', reply_to='rtrk', expiration='32768', message_id='mid123', timestamp=time.time(), type='test', user_id='foo', app_id='bar') BODY = '{"qux": true, "foo": "bar", "baz": 1}' class MockConsumer(object): def __init__(self, configuration, process): """Creates a new instance of a Mock Consumer class. To perform initialization tasks, extend Consumer._initialize :param dict configuration: The configuration from rejected
def headers_marshal_test(self): header = frame.Header(1, 100, spec.BasicProperties(delivery_mode=2)) self.assertEqual(header.marshal(), self.CONTENT_HEADER)