Example #1
0
 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)
Example #3
0
 def test_inbound_header__empty_body(self):
     m = Message()
     m.properties = {}
     buf = pack(b'>HxxQ', m.CLASS_ID, 0)
     buf += m._serialize_properties()
     assert m.inbound_header(buf, offset=0) == 12
     assert m.ready
Example #4
0
 def test_inbound_header__empty_body(self):
     m = Message()
     m.properties = {}
     buf = pack(">HxxQ", m.CLASS_ID, 0)
     buf += m._serialize_properties()
     self.assertEqual(m.inbound_header(buf, offset=0), 12)
     self.assertTrue(m.ready)
Example #5
0
 def enqueue(self, tag, data, schedule = None, error_times=0, channel=None):
     with self.open(channel):
         self.setup_tag(tag)
         msg = Message()
         msg.properties['delivery_mode'] = 2
         body = {'i': self.gensym(), 'c': data, 'e': error_times}
         id = None
         if schedule:
             ttl = round_by_accuracy(get_millis_to_the_date(schedule), self.SCHEDULING_ACCURACY)
             ttl1 = min(ttl, self.TTL_MAX)
             stag = ('%s.%s' % (self.get_tag(tag, 'SCHEDULE'), str(ttl1), ))
             self.channel.queue_declare(
                 queue=stag, durable=True, auto_delete=False,
                 arguments = { 'x-message-ttl' : ttl1, "x-dead-letter-exchange" : self.get_tag(tag, 'EXCHANGE') }
             )
             body['s'] = schedule.strftime(self.TIME_FORMAT)
             msg.body = self.serializer(body)
             id = self.channel.basic_publish(msg, routing_key=stag)
             log(" [%s] Sent scheduled -> ttl: %s(%s) schedule: %s" % (body['i'], ttl1, ttl, body['s'], ))
         else:
             rtag = self.get_tag(tag, 'READY')
             msg.body = self.serializer(body)
             id = self.channel.basic_publish(msg, routing_key=rtag)
             log(" [%s] Sent immediate -> tag: %s" % (body['i'], rtag))
         return body['i']
Example #6
0
 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()
     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)
Example #7
0
 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'}
Example #8
0
 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'}
Example #9
0
 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'})
Example #10
0
 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'})
Example #11
0
    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 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)
Example #13
0
 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
Example #14
0
 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)
Example #15
0
 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)
Example #16
0
 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()
     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
Example #17
0
 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')
Example #18
0
 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'
Example #19
0
 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)
Example #20
0
    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)
Example #21
0
 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)
Example #22
0
 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'
Example #23
0
 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")
Example #24
0
 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')
Example #25
0
 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'
Example #26
0
 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'
Example #27
0
    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,
        )
Example #28
0
    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'
Example #29
0
 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)
     assert m2.properties == m.properties
Example #30
0
    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)
Example #31
0
    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',
        }
Example #32
0
 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)
Example #33
0
    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)
Example #34
0
    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)
Example #35
0
    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)))
Example #36
0
 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',
     }
Example #37
0
 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)
Example #38
0
 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)
Example #39
0
 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)
Example #40
0
 def test_inbound_body__no_chunks(self):
     m = Message()
     m.body_size = 16
     m.inbound_body('thequickbrownfox')
     self.assertTrue(m.ready)
Example #41
0
 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)
Example #42
0
 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()
Example #43
0
 def test_inbound_body__no_chunks(self):
     m = Message()
     m.body_size = 16
     m.inbound_body("thequickbrownfox")
     self.assertTrue(m.ready)
Example #44
0
 def test_inbound_body__no_chunks(self):
     m = Message()
     m.body_size = 16
     m.inbound_body('thequickbrownfox')
     assert m.ready
Example #45
0
 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
Example #46
0
 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
Example #47
0
 def to_amqp_message(self):
     json_message = ujson.dumps(self.message)
     return Message(json_message, delivery_mode=AMQP_DELIVERY_MODE)
Example #48
0
 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