async def test_connections_close_all(mock_replays, mock_header_read,
                                     mock_connections):
    read_header_called = asyncio.locks.Event()
    verified = asyncio.locks.Event()

    connection = mock_connections()

    async def at_header_read(conn):
        read_header_called.set()
        await verified.wait()
        return asynctest.Mock()

    mock_header_read.side_effect = at_header_read

    conns = Connections(mock_header_read, mock_replays)

    f = asyncio.ensure_future(conns.handle_connection(connection))
    await read_header_called.wait()
    await conns.close_all()

    connection.close.assert_called()
    connection.wait_closed.assert_awaited()
    verified.set()
    await f
    await conns.wait_until_empty()
async def test_connections_handle_connection(mock_replays, mock_header_read,
                                             mock_connections):
    connection = mock_connections()
    conns = Connections(mock_header_read, mock_replays)
    mock_header = asynctest.Mock()
    mock_header_read.return_value = mock_header

    await conns.handle_connection(connection)
    mock_header_read.assert_awaited()
    mock_replays.handle_connection.assert_awaited()
    connection.close.assert_called()
    await conns.wait_until_empty()
 def build(cls,
           *,
           dep_connection_producer=ConnectionProducer.build,
           dep_database=Database.build,
           config_prometheus_port,
           **kwargs):
     database = dep_database(**kwargs)
     bookkeeper = Bookkeeper.build(database, **kwargs)
     replays = Replays.build(bookkeeper, **kwargs)
     conns = Connections.build(replays, **kwargs)
     producer = dep_connection_producer(conns.handle_connection, **kwargs)
     return cls(producer, database, conns, replays, bookkeeper,
                config_prometheus_port)
Esempio n. 4
0
 def build(cls,
           *,
           dep_connection_producer=ConnectionProducer.build,
           dep_database=Database.build,
           config):
     database = dep_database(config.db)
     bookkeeper = Bookkeeper.build(database, config.storage)
     replays = Replays.build(bookkeeper, config.replay)
     conns = Connections.build(replays,
                               config.server.connection_header_read_timeout)
     producer = dep_connection_producer(conns.handle_connection,
                                        config.server.port)
     return cls(producer, database, conns, replays, bookkeeper,
                config.server.prometheus_port)
async def test_connections_error_closes_connection(mock_replays,
                                                   mock_header_read,
                                                   mock_connections):

    connection = mock_connections()

    async def at_header_read(conn):
        raise BadConnectionError

    mock_header_read.side_effect = at_header_read

    conns = Connections(mock_header_read, mock_replays)
    await conns.handle_connection(connection)
    connection.close.assert_called()
    await conns.wait_until_empty()
async def test_connections_empty_connection_is_handled(mock_replays,
                                                       mock_header_read,
                                                       mock_connections):

    # We don't care if empty connection is treated differently, but it should
    # still be properly closed and such
    connection = mock_connections()

    async def at_header_read(conn):
        raise EmptyConnectionError

    mock_header_read.side_effect = at_header_read

    conns = Connections(mock_header_read, mock_replays)
    await conns.handle_connection(connection)
    connection.close.assert_called()
    connection.wait_closed.assert_awaited()
    await conns.wait_until_empty()
def test_connections_init(mock_replays, mock_header_read):
    Connections(mock_header_read, mock_replays)