def test_load_properties(self): m = Message() m.properties = { 'content_type': 'application/json', 'content_encoding': 'utf-8', 'application_headers': { 'foo': 1, 'id': 'id#1', }, 'delivery_mode': 1, 'priority': 255, 'correlation_id': 'df31-142f-34fd-g42d', 'reply_to': 'cosmo', 'expiration': '2015-12-23', 'message_id': '3312', 'timestamp': 3912491234, 'type': 'generic', 'user_id': 'george', 'app_id': 'vandelay', 'cluster_id': 'NYC', } s = m._serialize_properties() m2 = Message() m2._load_properties(m2.CLASS_ID, s) self.assertDictEqual(m2.properties, m.properties)
def test_inbound_header__empty_body(self): m = Message() m.properties = {} buf = pack('>HxxQ', m.CLASS_ID, 0) buf += m._serialize_properties() assert m.inbound_header(buf, offset=0) == 12 assert m.ready
def test_inbound_header__empty_body(self): m = Message() m.properties = {} buf = pack(b'>HxxQ', m.CLASS_ID, 0) buf += m._serialize_properties() self.assertEqual(m.inbound_header(buf, offset=0), 12) self.assertTrue(m.ready)
def publish(self, event, demographic, reply_event): """Publish an event onto the queue.""" routing_key = 'event.' + event['event'] # If the demographic is not part of "EventDemographic", # we should assume EventDemographic is a routing key. if demographic not in EventDemographic: routing_key = demographic # Create the message. message = Message( body=msgpack.dumps(event, use_bin_type=True), message_id=event['data']['_uuid'], correlation_id=reply_event.uuid if reply_event else None, reply_to=self.node_identifier) # Determine exchange name exchange = self.config['exchange_%s' % ('fanout' if demographic is EventDemographic.GLOBAL_ALL else 'topic')] # Publish the message self.channel.basic_publish(message, exchange=exchange, routing_key=routing_key, mandatory=False, immediate=False)
def test_frame_max_update(self): msg = Message(body='t' * (self.connection.frame_max + 10)) frame = 2, 1, spec.Basic.Publish, b'x' * 10, msg self.connection.frame_max += 100 self.g(*frame) self.write.assert_called() memory = self.write.call_args[0][0] assert isinstance(memory, memoryview)
def test_load_properties__some_missing(self): m = Message() m.properties = { 'content_type': 'application/json', 'content_encoding': 'utf-8', 'delivery_mode': 1, 'correlation_id': 'df31-142f-34fd-g42d', 'reply_to': 'cosmo', 'expiration': '2015-12-23', 'message_id': '3312', 'type': None, 'app_id': None, 'cluster_id': None, } s = m._serialize_properties() m2 = Message() m2._load_properties(m2.CLASS_ID, s)
def test_write_slow_unicode(self): msg = Message(body='y' * 2048 + '\N{CHECK MARK}') frame = 2, 1, spec.Basic.Publish, b'x' * 10, msg self.g(*frame) self.write.assert_called() memory = self.write.call_args[0][0] assert isinstance(memory, bytes) assert '\N{CHECK MARK}'.encode('utf-8') in memory assert msg.properties['content_encoding'] == 'utf-8'
def test_write_non_utf8(self): msg = Message(body='body', content_encoding='utf-16') frame = 2, 1, spec.Basic.Publish, b'x' * 10, msg self.g(*frame) self.write.assert_called() memory = self.write.call_args[0][0] assert isinstance(memory, memoryview) assert 'body'.encode('utf-16') in memory.tobytes() assert msg.properties['content_encoding'] == 'utf-16'
def test_inbound_body(self): m = Message() m.body_size = 16 m.body_received = 8 m._pending_chunks = [b'the', b'quick'] m.inbound_body(b'brown') assert not m.ready m.inbound_body(b'fox') assert m.ready assert m.body == b'thequickbrownfox'
def test_message(self): m = Message( 'foo', channel=Mock(name='channel'), application_headers={'h': 'v'}, ) m.delivery_info = {'delivery_tag': '1234'}, self.assertEqual(m.body, 'foo') self.assertTrue(m.channel) self.assertDictEqual(m.headers, {'h': 'v'})
def test_inbound_body(self): m = Message() m.body_size = 16 m.body_received = 8 m._pending_chunks = [b'the', b'quick'] m.inbound_body(b'brown') self.assertFalse(m.ready) m.inbound_body(b'fox') self.assertTrue(m.ready) self.assertEqual(m.body, b'thequickbrownfox')
def test_message(self): m = Message( 'foo', channel=Mock(name='channel'), application_headers={'h': 'v'}, ) m.delivery_info = {'delivery_tag': '1234'}, assert m.body == 'foo' assert m.channel assert m.headers == {'h': 'v'}
def check_proplist(self, msg): """ Check roundtrip processing of a single object """ raw_properties = msg._serialize_properties() new_msg = Message() new_msg._load_properties(raw_properties) new_msg.body = msg.body self.assertEqual(msg, new_msg)
def test_inbound_header(self): m = Message() m.properties = { 'content_type': 'application/json', 'content_encoding': 'utf-8', } body = 'the quick brown fox' buf = b'\0' * 30 + pack(b'>HxxQ', m.CLASS_ID, len(body)) buf += m._serialize_properties() self.assertEqual(m.inbound_header(buf, offset=30), 42) self.assertEqual(m.body_size, len(body)) self.assertEqual(m.properties['content_type'], 'application/json') self.assertFalse(m.ready)
def test_inbound_header(self): m = Message() m.properties = { 'content_type': 'application/json', 'content_encoding': 'utf-8', } body = 'the quick brown fox' buf = b'\0' * 30 + pack('>HxxQ', m.CLASS_ID, len(body)) buf += m._serialize_properties() assert m.inbound_header(buf, offset=30) == 42 assert m.body_size == len(body) assert m.properties['content_type'] == 'application/json' assert not m.ready
def publish_message(self, exchange_name, routing_key, payload, props=None): """ Publish message to the queue. *Args:*\n _exchange_name_ - exchange name;\n _routing_key_ - routing key (quoted with requests.utils.quote);\n _payload_ - payload message;\n _props_ - additional arguments in dictionary format;\n Includes such keys as:\n - _content-type_ - message content type (shortstr); - _content_encoding_ - message encoding type (shortstr); - _application_headers_ - message headers table, a dictionary with keys of type string and values of types string | int | Decimal | datetime | dict values (table); - _delivery_mode_ - Non-persistent (1) or persistent (2) (octet); - _priority_ - message priority from 0 to 9 (octet); - _correlation_id_ - message identifier to which current message responds (shortstr); - _reply_to_ - commonly used to name a reply queue (shortstr); - _expiration_ - expiration date of message (shortstr); - _message_id_ - message identifier (shortstr); - _timestamp_ - timestamp of sending message (shortstr); - _type_ - message type (shortstr); - _user_id_ - user-sender identifier (shortstr); - _app_id_ - application identifier (shortstr); - _cluster_id_ - cluster identifier (shortstr);\n *Attention:*\n When using library in robot-files parameters (props) must be cast to the correct type.\n Example:\n | ${delivery_mode}= | Convert To Integer | 2 | This is due to the feature of RabbitMq library.\n *Example:*\n | ${list_headers}= | Create List | head_value | 2 | ${TRUE} | | ${headers_dict}= | Create Dictionary | head1=val1 | head2=${list_headers} | | ${prop_dict}= | Create Dictionary | application_headers=${headers_dict} | content-type=text/plain | priority=1 | expiration=1410966000 | message_id=101 | user_id=guest | | Publish Message | exchange_name=testExchange | routing_key=testQueue | payload=message body | props=${prop_dict} | """ if props is None: props = {} exchange_name = str(exchange_name) routing_key = str(routing_key) logger.debug('Publish message to {exc} with routing {r}'.format( exc=exchange_name, r=routing_key)) msg = Message(payload, **props) self._get_channel().basic_publish(msg, exchange_name, routing_key)
def test_on_basic_deliver(self): msg = Message() self.c._on_basic_deliver(123, '321', False, 'ex', 'rkey', msg) callback = self.c.callbacks[123] = Mock(name='cb') self.c._on_basic_deliver(123, '321', False, 'ex', 'rkey', msg) callback.assert_called_with(msg) assert msg.channel == self.c assert msg.delivery_info == { 'consumer_tag': 123, 'delivery_tag': '321', 'redelivered': False, 'exchange': 'ex', 'routing_key': 'rkey', }
def test_eq(self): msg = Message('hello', content_type='text/plain') self.assertNotEqual(msg, None) # # Make sure that something that looks vaguely # like a Message doesn't raise an Attribute # error when compared to a Message, and instead # returns False # class FakeMsg(object): pass fake_msg = FakeMsg() fake_msg.properties = {'content_type': 'text/plain'} self.assertNotEqual(msg, fake_msg)
def test_pickle(self): msg = Message('some body' * 200000, content_type='text/plain', content_encoding='utf-8', application_headers={ 'foo': 7, 'bar': 'baz', 'd2': { 'foo2': 'xxx', 'foo3': -1 } }, delivery_mode=1, priority=7) msg2 = pickle.loads(pickle.dumps(msg)) self.assertEqual(msg, msg2)
def test_header_message_empty_body(self): assert not self.g((1, 1, pack('>HH', *spec.Basic.Deliver))) self.callback.assert_not_called() with pytest.raises(UnexpectedFrame): self.g((1, 1, pack('>HH', *spec.Basic.Deliver))) m = Message() m.properties = {} buf = pack('>HxxQ', m.CLASS_ID, 0) buf += m._serialize_properties() assert self.g((2, 1, buf)) self.callback.assert_called() msg = self.callback.call_args[0][3] self.callback.assert_called_with( 1, msg.frame_method, msg.frame_args, msg, )
def test_roundtrip(self): """ Check round-trip processing of content-properties. """ self.check_proplist(Message()) self.check_proplist(Message(content_type='text/plain')) self.check_proplist( Message(content_type='text/plain', content_encoding='utf-8', application_headers={ 'foo': 7, 'bar': 'baz', 'd2': { 'foo2': 'xxx', 'foo3': -1 } }, delivery_mode=1, priority=7)) self.check_proplist( Message( application_headers={ 'regular': datetime(2007, 11, 12, 12, 34, 56), 'dst': datetime(2007, 7, 12, 12, 34, 56), })) n = datetime.now() n = n.replace( microsecond=0) # AMQP only does timestamps to 1-second resolution self.check_proplist(Message(application_headers={'foo': n})) self.check_proplist( Message(application_headers={'foo': Decimal('10.1')})) self.check_proplist( Message(application_headers={'foo': Decimal('-1987654.193')})) self.check_proplist(Message(timestamp=datetime(1980, 1, 2, 3, 4, 6)))
def test_on_get_ok(self): msg = Message() m = self.c._on_get_ok( 'dtag', 'redelivered', 'ex', 'rkey', 'mcount', msg, ) assert m is msg assert m.channel == self.c assert m.delivery_info == { 'delivery_tag': 'dtag', 'redelivered': 'redelivered', 'exchange': 'ex', 'routing_key': 'rkey', 'message_count': 'mcount', }
def test_header_message_content(self): assert not self.g((1, 1, pack('>HH', *spec.Basic.Deliver))) self.callback.assert_not_called() m = Message() m.properties = {} buf = pack('>HxxQ', m.CLASS_ID, 16) buf += m._serialize_properties() assert not self.g((2, 1, buf)) self.callback.assert_not_called() assert not self.g((3, 1, b'thequick')) self.callback.assert_not_called() assert self.g((3, 1, b'brownfox')) self.callback.assert_called() msg = self.callback.call_args[0][3] self.callback.assert_called_with( 1, msg.frame_method, msg.frame_args, msg, ) assert msg.body == b'thequickbrownfox'
def test_inbound_body__no_chunks(self): m = Message() m.body_size = 16 m.inbound_body('thequickbrownfox') self.assertTrue(m.ready)
def _parsed_message(body='{"_table": "artist", "id": "42"}', channel="search.index"): msg = Message(body=body) parsed_message = PMessage.from_amqp_message(channel, msg) return parsed_message
def test_write_fast_content(self): msg = Message(body=b'y' * 10, content_type='utf-8') frame = 2, 1, spec.Basic.Publish, b'x' * 10, msg self.g(*frame) self.write.assert_called()
def test_write_slow_content(self): msg = Message(body=b'y' * 2048, content_type='utf-8') frame = 2, 1, spec.Basic.Publish, b'x' * 10, msg self.g.send(frame) self.assertTrue(self.write.called)
def to_amqp_message(self): json_message = ujson.dumps(self.message) return Message(json_message, delivery_mode=AMQP_DELIVERY_MODE)
def test_write_zero_len_body(self): msg = Message(body=b'', content_type='application/octet-stream') frame = 2, 1, spec.Basic.Publish, b'x' * 10, msg self.g(*frame) self.write.assert_called() assert 'content_encoding' not in msg.properties
def test_write_slow_content(self): msg = Message(body=b'y' * 2048, content_type='utf-8') frame = 2, 1, spec.Basic.Publish, b'x' * 10, msg self.g(*frame) self.write.assert_called() assert 'content_encoding' not in msg.properties