Пример #1
0
def test_outside_source_stream_discard():
    stream = OutsideSourceReplayStream()
    stream.set_header("header")
    stream.feed_data(b"abcdefgh")
    stream.discard(2)

    with pytest.raises(IndexError):
        stream.data.bytes()

    assert stream.data[2:] == b"cdefgh"
    assert stream.future_data[2:] == b"cdefgh"
    assert stream.data[-2:-1] == b"g"
    assert stream.future_data[-2:-1] == b"g"
    assert stream.data[3] == 100
    assert stream.future_data[3] == 100

    for bad_range in [1, slice(1, 5, 1), slice(-7, 5, 1), slice(5, 1, 1)]:
        with pytest.raises(IndexError):
            stream.data[bad_range]
        with pytest.raises(IndexError):
            stream.future_data[bad_range]

    assert len(stream.data) == 8
    assert len(stream.future_data) == 8

    v = stream.data.view(2)
    assert v == b"cdefgh"
    v.release()

    with pytest.raises(IndexError):
        stream.data.view()
    with pytest.raises(IndexError):
        stream.data.view(1, 3)
Пример #2
0
async def test_outside_source_stream_read(event_loop):
    stream = OutsideSourceReplayStream()
    f = asyncio.ensure_future(stream.wait_for_data(0))
    stream.set_header("header")
    await exhaust_callbacks(event_loop)
    assert not f.done()
    stream.feed_data(b"Lorem")
    assert await f == 5
    assert stream.data.bytes() == b"Lorem"

    stream.finish()
    await stream.wait_for_ended()
Пример #3
0
 def build(cls, merge_config, delay_config):
     canonical_replay = OutsideSourceReplayStream()
     merge_strategy = QuorumMergeStrategy.build(canonical_replay,
                                                merge_config)
     return cls(ReplayStreamReader.build,
                lambda s: DelayedReplayStream.build(s, delay_config),
                merge_strategy, canonical_replay)
Пример #4
0
async def test_outside_source_stream_immediate_data():
    stream = OutsideSourceReplayStream()
    f1 = asyncio.ensure_future(stream.wait_for_header())
    f2 = asyncio.ensure_future(stream.wait_for_data(0))
    stream.set_header("header")
    stream.feed_data(b"Lorem")
    assert await f1 == "header"
    assert await f2 == 5
    assert stream.header == "header"
    assert stream.data.bytes() == b"Lorem"
Пример #5
0
async def test_outside_source_stream_immediate_end(event_loop):
    stream = OutsideSourceReplayStream()
    f1 = asyncio.ensure_future(stream.wait_for_header())
    f2 = asyncio.ensure_future(stream.wait_for_data(0))
    f3 = asyncio.ensure_future(stream.wait_for_ended())
    exhaust_callbacks(event_loop)
    assert not any(x.done() for x in [f1, f2, f3])
    stream.finish()

    assert await f1 is None
    assert await f2 == 0
    assert stream.header is None
    assert stream.data.bytes() == b""
    await f3
Пример #6
0
def outside_source_stream(event_loop):
    # We use event_loop fixture so that stream uses the same loop as all tests
    return OutsideSourceReplayStream()
Пример #7
0
 def build(cls, connection):
     header_reader = ReplayHeader.from_connection
     stream = OutsideSourceReplayStream()
     return cls(header_reader, stream, connection)
Пример #8
0
async def test_outside_source_stream_wait_until_position(event_loop):
    stream = OutsideSourceReplayStream()
    f = asyncio.ensure_future(stream.wait_for_data(3))
    stream.set_header("header")
    stream.feed_data(b"a")
    exhaust_callbacks(event_loop)
    assert not f.done()
    stream.feed_data(b"aa")
    exhaust_callbacks(event_loop)
    assert not f.done()
    stream.feed_data(b"ccc")
    assert await f == 3
    assert stream.data[3:6] == b"ccc"
Пример #9
0
async def test_outside_source_stream_finish():
    stream = OutsideSourceReplayStream()
    f = asyncio.ensure_future(stream.wait_for_data(0))
    stream.finish()
    await f
    assert stream.ended()