Exemplo n.º 1
0
def test_constant_w(other, v1, v2):
    # constant(f, x).write(s, *) == f.write(x, *)
    assert other.write(
        v1, BytesIO()
    ).getvalue() == rw.constant(other, v1).write(
        v2, BytesIO()
    ).getvalue()
Exemplo n.º 2
0
def test_checksum(checksum_type):
    message = CallRequestMessage()
    message.checksum = (checksum_type, None)
    generate_checksum(message)
    payload = messages.RW[message.message_type].write(message,
                                                      BytesIO()).getvalue()

    msg = messages.RW[message.message_type].read(BytesIO(payload))
    assert verify_checksum(msg)
Exemplo n.º 3
0
def test_tracing_round_trip():
    for i in range(100):
        t = Tracing(
            random.randint(0, 100000),
            random.randint(0, 100000),
            random.randint(0, 100000),
            random.randint(0, 1),
        )

        buff = tracing_rw.write(t, BytesIO()).getvalue()
        assert t == tracing_rw.read(BytesIO(buff))
Exemplo n.º 4
0
def test_dictionary(value, width, pairs, bs):
    assert rw.dictionary(*pairs).read(bio(bs)) == value
    assert rw.dictionary(*pairs).write(
        value, BytesIO()
    ).getvalue() == bytearray(bs)

    assert rw.dictionary(*pairs).width() == width
Exemplo n.º 5
0
def test_chain(values, width, links, bs):
    assert list(rw.chain(*links).read(bio(bs))) == list(values)
    assert rw.chain(*links).write(
        values, BytesIO()
    ).getvalue() == bytearray(bs)

    assert rw.chain(*links).width() == width
Exemplo n.º 6
0
def test_dictionary_read_error():
    some_rw = InstanceDouble('tchannel.rw.ReadWriter')
    allow(some_rw).read.and_raise(ReadError('great sadness'))

    dict_rw = rw.dictionary(('foo', some_rw))
    with pytest.raises(ReadError):
        dict_rw.read(BytesIO())
Exemplo n.º 7
0
    def test_read(self):
        some_rw = InstanceDouble('tchannel.rw.ReadWriter')
        delegated_rw = self._mk_rw(some_rw)

        stream = BytesIO()

        expect(some_rw).read.with_args(stream)
        delegated_rw.read(stream)
Exemplo n.º 8
0
    def test_write(self):
        some_rw = InstanceDouble('tchannel.rw.ReadWriter')
        delegated_rw = self._mk_rw(some_rw)

        stream = BytesIO()

        expect(some_rw).write.with_args("foo", stream)
        delegated_rw.write("foo", stream)
Exemplo n.º 9
0
def init_request_with_headers():
    header_name = b'test_header'
    header_value = b'something'
    header_buffer = (make_short_bytes(len(header_name)) + header_name +
                     make_short_bytes(len(header_value)) + header_value)
    return BytesIO(
        make_short_bytes(PROTOCOL_VERSION) + make_short_bytes(1) +
        header_buffer)
Exemplo n.º 10
0
def test_dictionary_ignore_fields():
    d_rw = rw.dictionary(
        ('x', rw.number(1)),
        (rw.skip, rw.constant(rw.number(2), 42)),
    )

    assert d_rw.read(bio([1, 0, 2])) == {'x': 1}
    assert d_rw.write(
        {'x': 1, rw.skip: 2, 'y': 3}, BytesIO()
    ).getvalue() == bytearray([1, 0, 42])

    assert d_rw.width() == 3
Exemplo n.º 11
0
def test_instance_ignore():
    c_rw = rw.instance(
        ClassWithArgs,
        ('x', rw.number(1)),
        (rw.skip, rw.constant(rw.number(2), 42)),
        ('y', rw.number(1)),
    )

    assert c_rw.read(bio([1, 2, 3, 4])) == ClassWithArgs(1, 4)

    assert c_rw.write(
        ClassWithArgs(1, 2), BytesIO()
    ).getvalue() == bytearray([1, 0, 42, 2])
Exemplo n.º 12
0
def test_call_req_parse(call_request_bytes):
    msg = messages.call_req_rw.read(BytesIO(call_request_bytes))

    assert msg.flags == 0
    assert msg.ttl == 1024

    assert msg.tracing == messages.Tracing(span_id=283686952306183,
                                           parent_id=579005069656919567,
                                           trace_id=1157726452361532951,
                                           traceflags=1)

    assert msg.service == 'apache'
    assert msg.headers == {'key': 'val'}
    assert msg.checksum == (messages.ChecksumType.none, None)

    assert msg.args == [b'on', b'to', b'te']
Exemplo n.º 13
0
def test_headers_with_dict():
    h_rw = rw.headers(
        rw.number(2),
        rw.len_prefixed_string(rw.number(2)),
        rw.len_prefixed_string(rw.number(1))
    )

    headers = {
        'hello': 'world',
        'this': 'is a test',
    }

    buff = h_rw.write(headers, BytesIO()).getvalue()
    assert sorted(h_rw.read(bio(buff)), key=lambda x: x[0]) == [
        ['hello', 'world'],
        ['this', 'is a test']
    ]
Exemplo n.º 14
0
def test_call_res_parse():
    buff = bytearray([
        0x00,                    # flags:1
        0x00,                    # code:1

        # tracing:24
        0x00, 0x01, 0x02, 0x03,  # span_id:8
        0x04, 0x05, 0x06, 0x07,  #
        0x08, 0x09, 0x0a, 0x0b,  # parent_id:8
        0x0c, 0x0d, 0x0e, 0x0f,  #
        0x10, 0x11, 0x12, 0x13,  # trace_id:8
        0x14, 0x15, 0x16, 0x17,  #
        0x01,                    # traceflags:1

        0x01,                    # nh:1
        0x03, 0x6b, 0x65, 0x79,  # (hk~1 hv~1){nh}
        0x03, 0x76, 0x61, 0x6c,  # ...
        0x00,                    # csumtype:1 (csum:4){0,1}
        0x00, 0x02, 0x6f, 0x6e,  # arg1~2
        0x00, 0x02, 0x74, 0x6f,  # arg2~2
        0x00, 0x02, 0x74, 0x65   # arg3~2
    ])

    msg = messages.call_res_rw.read(BytesIO(buff))

    assert msg.flags == 0
    assert msg.code == 0

    assert msg.tracing == messages.Tracing(
        span_id=283686952306183,
        parent_id=579005069656919567,
        trace_id=1157726452361532951,
        traceflags=1
    )

    assert msg.headers == {'key': 'val'}
    assert msg.checksum == (messages.ChecksumType.none, None)

    assert msg.args == [b'on', b'to', b'te']
Exemplo n.º 15
0
def test_decode_empty_buffer():
    """Verify we can parse zero size frame."""
    assert frame_rw.read(BytesIO(b'\x00\x00\x00\x00')) is None
Exemplo n.º 16
0
def test_tracing_read(tracing, bs):
    assert tracing_rw.read(BytesIO(bytearray(bs))) == tracing
Exemplo n.º 17
0
def test_decode_with_message_length(dummy_frame):
    """Verify we can pre-flight a message size."""
    dummy_frame[2] = Types.PING_REQ
    f = frame_rw.read(BytesIO(dummy_frame[2:]), size=len(dummy_frame))
    message_rw = messages.RW[f.header.message_type]
    message_rw.read(BytesIO(f.payload)) == PingRequestMessage()
Exemplo n.º 18
0
def roundtrip(value, v_rw):
    return v_rw.read(bio(v_rw.write(value, BytesIO()).getvalue()))
Exemplo n.º 19
0
def test_none_w():
    stream = BytesIO()
    assert rw.none().write(42, stream) == stream
    assert stream.getvalue() == ''
Exemplo n.º 20
0
def test_valid_ping_request():
    """Verify we don't barf on 0-length bodies."""
    assert (messages.ping_req_rw.read(
        BytesIO()) == messages.PingRequestMessage())
Exemplo n.º 21
0
def bio(bs):
    return BytesIO(bytearray(bs))
Exemplo n.º 22
0
def test_switch(switch_rw, cases, width, value, bs):
    s_rw = rw.switch(switch_rw, cases)
    assert s_rw.read(bio(bs)) == value
    assert s_rw.write(value, BytesIO()).getvalue() == bytearray(bs)

    assert s_rw.width() == width
Exemplo n.º 23
0
def test_roundtrip_message(message_class, message_rw, attrs):
    """Verify all message types serialize and deserialize properly."""
    message = message_class(**attrs)
    buff = message_rw.write(message, BytesIO()).getvalue()
    assert message == message_rw.read(BytesIO(buff))
Exemplo n.º 24
0
def test_headers(l_rw, k_rw, v_rw, headers, bs):
    h_rw = rw.headers(l_rw, k_rw, v_rw)
    assert h_rw.read(bio(bs)) == headers
    assert h_rw.write(headers, BytesIO()).getvalue() == bytearray(bs)

    assert h_rw.width() == l_rw.width()
Exemplo n.º 25
0
def test_parse_message(message_rw, byte_stream):
    """Verify all messages parse properly."""
    error = message_rw.read(BytesIO(byte_stream))
    assert error.code == 1
    assert error.description == u'hi'
Exemplo n.º 26
0
def init_request_message():
    return BytesIO(
        make_short_bytes(0x02) +  # version 2
        make_short_bytes(0)  # 0 headers
    )
Exemplo n.º 27
0
def test_instance(obj, width, params, bs):
    i_rw = rw.instance(*params)
    assert i_rw.read(bio(bs)) == obj
    assert i_rw.write(obj, BytesIO()).getvalue() == bytearray(bs)

    assert i_rw.width() == width
Exemplo n.º 28
0
def test_number(num, width, bs):
    assert rw.number(width).read(bio(bs)) == num
    assert rw.number(width).write(num, BytesIO()).getvalue() == bytearray(bs)
    assert rw.number(width).width() == width
Exemplo n.º 29
0
def test_checksum_read(bs, typ, value):
    assert checksum_rw.read(BytesIO(bytearray(bs))) == (typ, value)
Exemplo n.º 30
0
def test_len_prefixed_string_binary(s, len_width, bs):
    s_rw = rw.len_prefixed_string(rw.number(len_width), is_binary=True)
    assert s_rw.read(bio(bs)) == s
    assert s_rw.write(s, BytesIO()).getvalue() == bytearray(bs)

    assert s_rw.width() == len_width