Example #1
0
async def test_merger_active_connection_prevents_ending(
        outside_source_stream, mock_merge_strategy, mock_stream_builder,
        mock_connection_streams, mock_connections, event_loop):
    connection = mock_connections()
    connection_stream = mock_connection_streams()
    mock_stream_builder.side_effect = [connection_stream]
    canonical_stream = outside_source_stream

    async def long_read():
        await asyncio.sleep(0.05)
        return b""

    connection_stream.read.side_effect = long_read
    connection_stream.ended.side_effect = lambda: connection_stream.read.called

    merger = Merger(mock_stream_builder, 0.01, mock_merge_strategy,
                    canonical_stream)
    f = asyncio.ensure_future(merger.wait_for_ended())
    h = asyncio.ensure_future(merger.handle_connection(connection))
    await asyncio.sleep(0.02)
    exhaust_callbacks(event_loop)
    assert not f.done()
    await h
    await asyncio.sleep(0.02)
    exhaust_callbacks(event_loop)
    assert f.done()
Example #2
0
async def test_merger_connection_extends_grace_period(
        outside_source_stream, mock_merge_strategy, mock_stream_builder,
        mock_connection_streams, mock_connections, event_loop):
    connection = mock_connections()
    connection_stream = mock_connection_streams()
    mock_stream_builder.side_effect = [connection_stream]
    canonical_stream = outside_source_stream

    connection_stream.ended.return_value = True

    merger = Merger(mock_stream_builder, 0.03, mock_merge_strategy,
                    canonical_stream)
    f = asyncio.ensure_future(merger.wait_for_ended())
    await asyncio.sleep(0.02)
    await merger.handle_connection(connection)
    await asyncio.sleep(0.02)
    exhaust_callbacks(event_loop)
    assert not f.done()
    await f