Пример #1
0
async def test_merger_no_connection_wait_active_connection(
        outside_source_stream, mock_merge_strategy, mock_reader_builder,
        mock_delayed_stream_builder, mock_readers, mock_connections,
        event_loop):
    connection = mock_connections()
    reader = mock_readers()
    mock_reader_builder.side_effect = [reader]
    canonical_stream = outside_source_stream

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

    reader.read.side_effect = long_read

    merger = Merger(mock_reader_builder, mock_delayed_stream_builder,
                    mock_merge_strategy, canonical_stream)
    f = asyncio.ensure_future(merger.no_connections_for(0.01))
    h = asyncio.ensure_future(merger.handle_connection(connection))

    await asyncio.sleep(0.02)
    exhaust_callbacks(event_loop)
    assert not f.done()
    await h
    exhaust_callbacks(event_loop)
    assert not f.done()
    await asyncio.sleep(0.02)
    exhaust_callbacks(event_loop)
    assert f.done()
    await f

    merger.stop_accepting_connections()
    await merger.wait_for_ended()
Пример #2
0
async def test_merger_ends_when_refusing_conns_and_no_connections(
        outside_source_stream, mock_merge_strategy, mock_reader_builder,
        mock_delayed_stream_builder):
    canonical_stream = outside_source_stream
    merger = Merger(mock_reader_builder, mock_delayed_stream_builder,
                    mock_merge_strategy, canonical_stream)
    merger.stop_accepting_connections()
    await merger.wait_for_ended()
    mock_merge_strategy.finalize.assert_called()
    assert canonical_stream.ended()
Пример #3
0
async def test_merger_rejects_writers_when_asked(outside_source_stream,
                                                 mock_merge_strategy,
                                                 mock_reader_builder,
                                                 mock_delayed_stream_builder,
                                                 mock_connections):
    connection = mock_connections()
    canonical_stream = outside_source_stream

    merger = Merger(mock_reader_builder, mock_delayed_stream_builder,
                    mock_merge_strategy, canonical_stream)
    merger.stop_accepting_connections()
    with pytest.raises(CannotAcceptConnectionError):
        await merger.handle_connection(connection)
    mock_reader_builder.assert_not_called()
    await merger.wait_for_ended()
Пример #4
0
async def test_merger_no_connections_wait_empty(outside_source_stream,
                                                mock_merge_strategy,
                                                mock_reader_builder,
                                                mock_delayed_stream_builder,
                                                event_loop):
    canonical_stream = outside_source_stream
    merger = Merger(mock_reader_builder, mock_delayed_stream_builder,
                    mock_merge_strategy, canonical_stream)
    f = asyncio.ensure_future(merger.no_connections_for(0.01))
    exhaust_callbacks(event_loop)
    assert not f.done()
    await asyncio.sleep(0.02)
    exhaust_callbacks(event_loop)
    assert f.done()
    await f

    merger.stop_accepting_connections()
    await merger.wait_for_ended()
Пример #5
0
async def test_merger_read_exception(outside_source_stream,
                                     mock_merge_strategy, mock_reader_builder,
                                     mock_delayed_stream_builder, mock_readers,
                                     mock_connections):
    connection = mock_connections()
    canonical_stream = outside_source_stream
    reader = mock_readers()
    mock_reader_builder.side_effect = [reader]
    reader.read.side_effect = BadConnectionError

    merger = Merger(mock_reader_builder, mock_delayed_stream_builder,
                    mock_merge_strategy, canonical_stream)
    with pytest.raises(BadConnectionError):
        await merger.handle_connection(connection)

    mock_merge_strategy.track_stream.assert_awaited_with(reader.stream)
    merger.stop_accepting_connections()
    await merger.wait_for_ended()
Пример #6
0
async def test_merger_one_connection_lifetime(outside_source_stream,
                                              mock_merge_strategy,
                                              mock_reader_builder,
                                              mock_delayed_stream_builder,
                                              mock_readers, mock_connections):
    connection = mock_connections()
    reader = mock_readers()
    mock_reader_builder.side_effect = [reader]
    canonical_stream = outside_source_stream

    merger = Merger(mock_reader_builder, mock_delayed_stream_builder,
                    mock_merge_strategy, canonical_stream)
    await merger.handle_connection(connection)

    reader.read.assert_awaited()
    mock_merge_strategy.track_stream.assert_awaited_with(reader.stream)

    merger.stop_accepting_connections()
    await merger.wait_for_ended()