Пример #1
0
def handle_message():
    """
    Example that shows how to handle messages
    """
    print("handle message")

    msg1 = Message(body="hello world!".encode("utf-8"), header={"h1": "val1"})

    msg2 = Message(body="hello world!".encode("utf-8"), header={"h1": "val1"})
    assert(msg1 == msg2)
    msg2 = msg1.serialize()
    msg2 = deserialize(msg2)
    assert(msg1 == msg2)

    msg3 = deserialize('{"body": "hello world!", "header": {"h1": "val1"}}')
    assert(msg1 == msg3)

    tmp = msg1.stringify()
    msg4 = destringify(tmp)
    assert(msg1 == msg4)

    msg5 = msg1.jsonify()
    assert(isinstance(msg5, dict))
    msg5 = dejsonify(msg5)
    assert(msg1 == msg5)

    print("...handle message OK!")
Пример #2
0
 def test_message_compression(self):
     """ Test message compression. """
     print("checking message compression")
     length = 10000
     body = 'a' * length
     ok = list()
     for module in COMPRESSORS:
         msg = Message(body=body, header={'l': 'ff'})
         jsonified = msg.jsonify({'compression': module})
         self.assert_(len(jsonified['body']) < length * 0.9,
                      "message should have been compressed with %s" %
                      module)
         ok.append(module)
     print("...message compression ok for %s" % ",".join(ok))