Пример #1
0
def test_pack_unpack():
    struct = Struct()
    struct.fields["key"].number_value = 0.1212121921839893438974837
    struct.fields["value"].number_value = 90.0
    struct.fields["string"].string_value = "0.1212121921839893438974837"

    message = Message()
    message.pack(struct)
    unpacked = message.unpack(Struct)

    assert message.content_type == ContentType.PROTOBUF
    assert str(struct) == str(unpacked)
    assert struct == unpacked

    message.content_type = ContentType.JSON
    message.pack(struct)
    unpacked = message.unpack(Struct)

    assert message.content_type == ContentType.JSON
    assert str(struct) == str(unpacked)
    assert struct == unpacked

    # test pack/unpack for dict object with default serializtion as JSON
    dictionary = {"key": 0.1233444444, "hodor": "hold the door", "default": 0}
    message = Message(content=dictionary)
    assert message.content_type == ContentType.JSON
    unpacked = message.unpack()
    assert dictionary == unpacked

    # test pack/unpack for dict object with protobuf content-type
    message = Message()
    message.content_type = ContentType.PROTOBUF
    message.pack(dictionary)
    unpacked = message.unpack()
    assert dictionary == unpacked
Пример #2
0
def test_create_reply():
    request = Message()
    request.topic = "topic"
    request.reply_to = "reply_to"
    request.content_type = ContentType.JSON

    reply = request.create_reply()

    assert reply.topic == request.reply_to
    assert reply.correlation_id == request.correlation_id
    assert reply.content_type == request.content_type
Пример #3
0
def test_amqp_conversion():
    sent = Message()
    sent.created_at = int(now() * 1000) / 1000.0
    sent.reply_to = "reply_to"
    sent.subscription_id = "subscription_id"
    sent.content_type = ContentType.JSON
    sent.body = '{"field":"value"}'.encode('latin1')
    sent.topic = "MyTopic"
    sent.status = Status(
        code=StatusCode.FAILED_PRECONDITION,
        why="Bad Args...",
    )
    sent.metadata = {
        'x-b3-sampled': '1',
        'x-b3-traceid': 'f047c6f208eb36ab',
        'x-b3-flags': '0',
        'x-b3-spanid': 'ef81a2f9c261473d',
        'x-b3-parentspanid': '0000000000000000'
    }

    body = sent.body

    amqp_message = amqp.Message(channel=None,
                                body=body,
                                **WireV1.to_amqp_properties(sent))

    amqp_message.delivery_info = {
        "routing_key": sent.topic,
        "consumer_tag": sent.subscription_id,
    }

    received = WireV1.from_amqp_message(amqp_message)
    print(sent.__str__(), received.__str__())
    assert str(sent) == str(received)
    assert sent.created_at == received.created_at
    assert sent.reply_to == received.reply_to
    assert sent.subscription_id == received.subscription_id
    assert sent.content_type == received.content_type
    assert sent.body == received.body
    assert sent.status == received.status
    assert sent.topic == received.topic
    assert sent.correlation_id == received.correlation_id
    assert sent.timeout == received.timeout
    assert sent.metadata == received.metadata