async def test_server_single_connection(mock_database, tmpdir,
                                        unused_tcp_port_factory):
    s_port, p_port = [unused_tcp_port_factory() for i in range(2)]
    conf = copy.deepcopy(config_dict)
    conf["server"]["port"] = s_port
    conf["server"]["prometheus_port"] = p_port
    conf["storage"]["vault_path"] = str(tmpdir)

    await mock_database.add_mock_game((1, 1, 1), [(1, 1), (2, 2)])
    server = Server.build(dep_database=lambda _: mock_database,
                          config=server_config(conf))
    await server.start()
    r, w = await asyncio.open_connection('127.0.0.1', s_port)

    w.write(b"P/1/foo\0")
    w.write(example_replay.data)
    await w.drain()
    w.close()
    rep = await server._replays.wait_for_replay(1)
    await rep.wait_for_ended()

    await assert_connection_closed(r, w)
    rfile = list(tmpdir.visit('1.fafreplay'))
    replay_ticks = await mock_database.get_game_ticks(1)
    assert len(rfile) == 1
    assert replay_ticks == 1928
async def test_server_force_close_server(mock_database, tmpdir,
                                         unused_tcp_port):
    s_port = unused_tcp_port
    conf = copy.deepcopy(config_dict)
    conf["server"]["port"] = s_port
    conf["storage"]["vault_path"] = str(tmpdir)

    await mock_database.add_mock_game((1, 1, 1), [(1, 1), (2, 2)])
    server = Server.build(dep_database=lambda _: mock_database,
                          config=server_config(conf))
    await server.start()
    r, w = await asyncio.open_connection('127.0.0.1', s_port)

    async def write_forever():
        w.write(b"P/1/foo\0")
        w.write(example_replay.header_data)
        while True:
            w.write(b"foo")
            await asyncio.sleep(0.05)

    writing = asyncio.ensure_future(write_forever())
    await asyncio.sleep(0.5)
    await server.stop()
    writing.cancel()
    with pytest.raises(ConnectionRefusedError):
        await asyncio.open_connection('127.0.0.1', s_port)

    await assert_connection_closed(r, w)
    rfile = list(tmpdir.visit('1.fafreplay'))
    replay_ticks = await mock_database.get_game_ticks(1)
    assert len(rfile) == 1
    assert replay_ticks is None
Beispiel #3
0
async def test_server_replay_force_end(mock_database, tmpdir):
    conf = dict(config)
    conf["config_server_port"] = 15002
    conf["config_replay_store_path"] = str(tmpdir)
    conf["config_replay_forced_end_time"] = 1
    conf["config_replay_delay"] = 0.5

    await mock_database.add_mock_game((1, 1, 1), [(1, 1), (2, 2)])
    server = Server.build(dep_database=lambda **kwargs: mock_database,
                          **conf)
    await server.start()
    r, w = await asyncio.open_connection('127.0.0.1', 15002)

    async def write_forever():
        w.write(b"P/1/foo\0")
        w.write(example_replay.header_data)
        while True:
            w.write(b"foo")
            await asyncio.sleep(0.05)

    writing = asyncio.ensure_future(write_forever())
    rep = await server._replays.wait_for_replay(1)
    await rep.wait_for_ended()
    writing.cancel()

    await assert_connection_closed(r, w)
    rfile = list(tmpdir.visit('1.fafreplay'))
    assert len(rfile) == 1
Beispiel #4
0
async def test_server_force_close_server(mock_database, tmpdir):
    conf = dict(config)
    conf["config_server_port"] = 15003
    conf["config_replay_store_path"] = str(tmpdir)

    await mock_database.add_mock_game((1, 1, 1), [(1, 1), (2, 2)])
    server = Server.build(dep_database=lambda **kwargs: mock_database,
                          **conf)
    await server.start()
    r, w = await asyncio.open_connection('127.0.0.1', 15003)

    async def write_forever():
        w.write(b"P/1/foo\0")
        w.write(example_replay.header_data)
        while True:
            w.write(b"foo")
            await asyncio.sleep(0.05)

    writing = asyncio.ensure_future(write_forever())
    await asyncio.sleep(0.5)
    await server.stop()
    writing.cancel()
    with pytest.raises(ConnectionRefusedError):
        await asyncio.open_connection('127.0.0.1', 15003)

    await assert_connection_closed(r, w)
    rfile = list(tmpdir.visit('1.fafreplay'))
    assert len(rfile) == 1
async def test_server_reader_is_delayed(mock_database, tmpdir,
                                        unused_tcp_port):
    s_port = unused_tcp_port
    conf = copy.deepcopy(config_dict)
    conf["replay"]["delay"]["replay_delay"] = 0.5
    conf["server"]["port"] = s_port
    conf["storage"]["vault_path"] = str(tmpdir)

    await mock_database.add_mock_game((1, 1, 1), [(1, 1), (2, 2)])
    server = Server.build(dep_database=lambda _: mock_database,
                          config=server_config(conf))
    await server.start()
    r, w = await asyncio.open_connection('127.0.0.1', s_port)
    r2, w2 = await asyncio.open_connection('127.0.0.1', s_port)
    read_data = bytearray()
    written_data = bytearray()
    # Use large chunk so buffering doesn't affect read data length
    CHUNK = 4096

    async def write_forever():
        nonlocal written_data
        w.write(b"P/1/foo\0")
        w.write(example_replay.header_data)
        written_data += example_replay.header_data
        while True:
            w.write(b"f" * CHUNK)
            written_data += b"f" * CHUNK
            await asyncio.sleep(0.05)

    async def read_forever():
        nonlocal read_data
        w2.write(b"G/1/foo\0")
        while True:
            b = await r2.read(CHUNK)
            if not b:
                break
            read_data += b

    reading = asyncio.ensure_future(write_forever())
    writing = asyncio.ensure_future(read_forever())

    for i in range(20):
        await asyncio.sleep(0.05)
        assert len(read_data) <= max(
            len(written_data) - 5 * CHUNK, len(example_replay.header_data))
        assert len(read_data) >= len(written_data) - 15 * CHUNK

    reading.cancel()
    writing.cancel()
    await server.stop()
Beispiel #6
0
def main():
    try:
        config = get_program_config()
    except ConfigurationError:
        logger.exception("Invalid configuration was provided!")
        return 1

    try:
        logger.setLevel(config.log_level)
        server = Server.build(config=config)
        loop = asyncio.get_event_loop()
        setup_signal_handler(server, loop)
        loop.run_until_complete(server.run())
        loop.close()
        return 0
    except Exception:
        logger.exception("Critical server error!")
        return 1
def main():
    # FIXME - report errors regarding this as well
    logger.setLevel(int(eget("LOG_LEVEL", logging.INFO)))
    try:
        config = get_config_from_env()
    except ValueError as e:
        logger.critical(e)
        return 1

    server = Server.build(**config)
    loop = asyncio.get_event_loop()
    setup_signal_handler(server, loop)
    try:
        loop.run_until_complete(server.run())
        return 0
    except Exception as e:
        logger.critical(f"Critical server error!")
        logger.exception(e)
        return 1
async def server_stress_test(mock_database, tmpdir, unused_tcp_port_factory,
                             chunk_size):
    s_port, p_port = [unused_tcp_port_factory() for i in range(2)]
    conf = copy.deepcopy(config_dict)
    conf["server"]["port"] = s_port
    conf["server"]["prometheus_port"] = p_port
    conf["storage"]["vault_path"] = str(tmpdir)
    conf["replay"]["delay"]["replay_delay"] = 0.5

    for i in range(1, 50):
        await mock_database.add_mock_game((i, 1, 1), [(1, 1), (2, 2)])

    server = Server.build(dep_database=lambda _: mock_database,
                          config=server_config(conf))
    await server.start()

    async def do_write(r, w, i):
        w.write(f"P/{i}/foo\0".encode())
        for pos in range(0, len(example_replay.data), chunk_size):
            w.write(example_replay.data[pos:pos + chunk_size])
            await w.drain()
            await asyncio.sleep(0.01)
        w.close()

    async def do_read(r, w, i):
        w.write(f"G/{i}/foo\0".encode())
        while True:
            b = await r.read(chunk_size)
            if not b:
                break

    for i in range(1, 5):
        for _ in range(5):
            r, w = await asyncio.open_connection('127.0.0.1', s_port)
            asyncio.ensure_future(do_write(r, w, i))
        for _ in range(5):
            r, w = await asyncio.open_connection('127.0.0.1', s_port)
            asyncio.ensure_future(do_read(r, w, i))

    await asyncio.sleep(0.5)
    await server._connections.wait_until_empty()
    await server.stop()
Beispiel #9
0
async def test_server_single_connection(mock_database, tmpdir):
    conf = dict(config)
    conf["config_server_port"] = 15001
    conf["config_prometheus_port"] = 16001
    conf["config_replay_store_path"] = str(tmpdir)

    await mock_database.add_mock_game((1, 1, 1), [(1, 1), (2, 2)])
    server = Server.build(dep_database=lambda **kwargs: mock_database,
                          **conf)
    await server.start()
    r, w = await asyncio.open_connection('127.0.0.1', 15001)

    w.write(b"P/1/foo\0")
    w.write(example_replay.data)
    await w.drain()
    w.close()
    rep = await server._replays.wait_for_replay(1)
    await rep.wait_for_ended()

    await assert_connection_closed(r, w)
    rfile = list(tmpdir.visit('1.fafreplay'))
    assert len(rfile) == 1
Beispiel #10
0
async def test_server_stress_test(mock_database, tmpdir):
    conf = dict(config)
    conf["config_server_port"] = 15005
    conf["config_prometheus_port"] = 16005
    conf["config_replay_store_path"] = str(tmpdir)
    conf["config_sent_replay_delay"] = 0.5

    for i in range(1, 50):
        await mock_database.add_mock_game((i, 1, 1), [(1, 1), (2, 2)])

    server = Server.build(dep_database=lambda **kwargs: mock_database,
                          **conf)
    await server.start()

    async def do_write(r, w, i):
        w.write(f"P/{i}/foo\0".encode())
        for pos in range(0, len(example_replay.data), 4000):
            w.write(example_replay.data[pos:pos+4000])
            await w.drain()
            await asyncio.sleep(0.05)
        w.close()

    async def do_read(r, w, i):
        w.write(f"G/{i}/foo\0".encode())
        while True:
            b = await r.read(4000)
            if not b:
                break

    for i in range(1, 50):
        for j in range(5):
            r, w = await asyncio.open_connection('127.0.0.1', 15005)
            asyncio.ensure_future(do_write(r, w, i))
        for j in range(5):
            r, w = await asyncio.open_connection('127.0.0.1', 15005)
            asyncio.ensure_future(do_read(r, w, i))

    await asyncio.sleep(0.5)
    await server._connections.wait_until_empty()
def test_server_init(tmpdir):
    conf = copy.deepcopy(config_dict)
    conf["storage"]["vault_path"] = str(tmpdir)
    Server.build(config=server_config(conf))
Beispiel #12
0
def test_server_init():
    Server.build(**config)