Skip to content

Commit

Permalink
Update dependencies (#996)
Browse files Browse the repository at this point in the history
* Update dependencies

Pytest-asyncio is once again making breaking changes that cause a bunch 
of warnings to be raised, some from the plugin itself, so we just pin it 
to the working version for now.

* Replace uses of the event loop fixture with asyncio.get_running_loop
  • Loading branch information
Askaholic authored Jan 5, 2024
1 parent 8dcdbc2 commit 00cf9ff
Show file tree
Hide file tree
Showing 14 changed files with 486 additions and 474 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ flaky = "*"
hypothesis = "*" # Versions between 6.47.1 and 6.56.4 added a prerelease dependency. See https://github.com/pypa/pipenv/issues/1760
pdoc3 = "*"
pytest = "*"
pytest-asyncio = "*"
pytest-asyncio = "~=0.21.0"
pytest-cov = "*"
pytest-mock = "*"
vulture = "*"
Expand Down
885 changes: 451 additions & 434 deletions Pipfile.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def jwk_kid():

@pytest.fixture
async def lobby_server_factory(
event_loop,
database,
broadcast_service,
player_service,
Expand All @@ -133,15 +132,15 @@ async def lobby_server_factory(
oauth_service,
violation_service,
policy_server,
jwks_server
jwks_server,
):
all_contexts = []

async def make_lobby_server(config):
instance = ServerInstance(
"UnitTestServer",
database,
loop=event_loop,
loop=asyncio.get_running_loop(),
_override_services={
"broadcast_service": broadcast_service,
"geo_ip_service": geoip_service,
Expand Down Expand Up @@ -186,7 +185,7 @@ async def make_lobby_server(config):
# https://github.com/FAForever/server/issues/717
for proto in context.__connected_client_protos:
proto.abort()
await exhaust_callbacks(event_loop)
await exhaust_callbacks()


@pytest.fixture
Expand Down Expand Up @@ -364,17 +363,18 @@ async def get(request):


@pytest.fixture
async def proxy_server(lobby_server_proxy, event_loop):
async def proxy_server(lobby_server_proxy):
buf_len = 262144
dnsbl = proxyprotocol.dnsbl.NoopDnsbl()

host, port = lobby_server_proxy.sockets[0].getsockname()
dest = proxyprotocol.server.Address(f"{host}:{port}")

server = await event_loop.create_server(
loop = asyncio.get_running_loop()
server = await loop.create_server(
lambda: proxyprotocol.server.protocol.DownstreamProtocol(
proxyprotocol.server.protocol.UpstreamProtocol,
event_loop,
loop,
buf_len,
dnsbl,
dest
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def host(proto):
@pytest.mark.slow
@fast_forward(300)
async def test_game_info_broadcast_on_connection_error(
event_loop, lobby_server, tmp_user, ladder_service, game_service, caplog
lobby_server, tmp_user, ladder_service, game_service, caplog
):
"""
Causes connection errors in `do_report_dirties` which in turn will cause
Expand Down
6 changes: 4 additions & 2 deletions tests/integration_tests/test_server_instance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from server import ServerInstance
from server.config import config
from server.protocol import QDataStreamProtocol, SimpleJsonProtocol
Expand Down Expand Up @@ -31,14 +33,14 @@ async def test_multiple_contexts(
party_service,
rating_service,
oauth_service,
event_loop
):
config.USE_POLICY_SERVER = False

loop = asyncio.get_running_loop()
instance = ServerInstance(
"TestMultiContext",
database,
loop=event_loop,
loop=loop,
_override_services={
"broadcast_service": broadcast_service,
"game_service": game_service,
Expand Down
5 changes: 2 additions & 3 deletions tests/integration_tests/test_servercontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def make_connection() -> LobbyConnection:


async def test_serverside_abort(
event_loop,
mock_context,
mock_connection,
mock_service
Expand All @@ -79,7 +78,7 @@ async def test_serverside_abort(
with closing(writer):
proto = QDataStreamProtocol(reader, writer)
await proto.send_message({"some_junk": True})
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

mock_connection.on_connection_lost.assert_any_call()
mock_service.on_connection_lost.assert_called_once()
Expand Down Expand Up @@ -108,7 +107,7 @@ async def test_connection_broken_external(context):
assert len(ctx.connections) == 0


async def test_unexpected_exception(event_loop, context, caplog, mocker):
async def test_unexpected_exception(context, caplog, mocker):
srv, ctx = context

mocker.patch.object(
Expand Down
5 changes: 3 additions & 2 deletions tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from contextlib import AbstractContextManager, asynccontextmanager
from time import perf_counter
from unittest import mock
Expand Down Expand Up @@ -81,7 +82,6 @@ async def game_connection(
players,
game_service,
player_service,
event_loop
):
conn = GameConnection(
database=database,
Expand All @@ -93,9 +93,10 @@ async def game_connection(
)

conn.finished_sim = False
loop = asyncio.get_running_loop()

def fin():
event_loop.run_until_complete(conn.abort())
loop.run_until_complete(conn.abort())

request.addfinalizer(fin)
return conn
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ async def test_game_results_commander_kills(


@fast_forward(5)
async def test_game_rated_only_once(event_loop, game, game_add_players):
async def test_game_rated_only_once(game, game_add_players):
game.state = GameState.LOBBY
game_add_players(game, 2)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_game_rating.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def custom_game(database, game_service, game_stats_service):


@pytest.fixture
async def ladder_game(event_loop, database, game_service, game_stats_service):
async def ladder_game(database, game_service, game_stats_service):
return LadderGame(42, database, game_service, game_stats_service, rating_type=RatingType.LADDER_1V1)


Expand Down
15 changes: 5 additions & 10 deletions tests/unit_tests/test_gameconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,20 @@ async def test_handle_action_GameState_idle_sets_player_state(
async def test_handle_action_GameState_lobby_sends_HostGame(
game: Game,
game_connection: GameConnection,
event_loop,
players
):
game_connection.player = players.hosting
game.map = Map(None, "some_map")

await game_connection.handle_action("GameState", ["Lobby"])
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

assert_message_sent(game_connection, "HostGame", [game.map.folder_name])


async def test_handle_action_GameState_lobby_calls_ConnectToHost(
game: Game,
game_connection: GameConnection,
event_loop,
players
):
game_connection.send = mock.AsyncMock()
Expand All @@ -162,15 +160,14 @@ async def test_handle_action_GameState_lobby_calls_ConnectToHost(
game.host = players.hosting

await game_connection.handle_action("GameState", ["Lobby"])
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

game_connection.connect_to_host.assert_called_with(players.hosting.game_connection)


async def test_handle_action_GameState_lobby_calls_ConnectToPeer(
game: Game,
game_connection: GameConnection,
event_loop,
players
):
game_connection.send = mock.AsyncMock()
Expand All @@ -187,15 +184,14 @@ async def test_handle_action_GameState_lobby_calls_ConnectToPeer(
game.connections = [peer_conn]

await game_connection.handle_action("GameState", ["Lobby"])
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

game_connection.connect_to_peer.assert_called_with(peer_conn)


async def test_handle_lobby_state_handles_GameError(
real_game: Game,
game_connection: GameConnection,
event_loop,
players
):
game_connection.abort = mock.AsyncMock()
Expand All @@ -209,15 +205,14 @@ async def test_handle_lobby_state_handles_GameError(
real_game.state = GameState.ENDED

await game_connection.handle_action("GameState", ["Lobby"])
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

game_connection.abort.assert_called_once()


async def test_handle_action_GameState_lobby_calls_abort(
game: Game,
game_connection: GameConnection,
event_loop,
players
):
game_connection.send = mock.AsyncMock()
Expand All @@ -229,7 +224,7 @@ async def test_handle_action_GameState_lobby_calls_abort(
game.map = Map(None, "some_map")

await game_connection.handle_action("GameState", ["Lobby"])
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

game_connection.abort.assert_called_once()

Expand Down
11 changes: 4 additions & 7 deletions tests/unit_tests/test_ladder_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ async def test_on_match_found_sets_player_state(
async def test_start_and_cancel_search(
ladder_service: LadderService,
player_factory,
event_loop,
):
p1 = player_factory(
"Dostya",
Expand All @@ -705,7 +704,7 @@ async def test_start_and_cancel_search(

ladder_service.start_search([p1], "ladder1v1")
search = ladder_service._searches[p1]["ladder1v1"]
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

assert p1.state == PlayerState.SEARCHING_LADDER
assert search in ladder_service.queues["ladder1v1"]._queue
Expand All @@ -720,7 +719,6 @@ async def test_start_and_cancel_search(
async def test_start_search_cancels_previous_search(
ladder_service: LadderService,
player_factory,
event_loop,
):
p1 = player_factory(
"Dostya",
Expand All @@ -731,14 +729,14 @@ async def test_start_search_cancels_previous_search(

ladder_service.start_search([p1], "ladder1v1")
search1 = ladder_service._searches[p1]["ladder1v1"]
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

assert p1.state == PlayerState.SEARCHING_LADDER
assert search1 in ladder_service.queues["ladder1v1"]._queue

ladder_service.start_search([p1], "ladder1v1")
search2 = ladder_service._searches[p1]["ladder1v1"]
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

assert p1.state == PlayerState.SEARCHING_LADDER
assert search1.is_cancelled
Expand All @@ -749,7 +747,6 @@ async def test_start_search_cancels_previous_search(
async def test_cancel_all_searches(
ladder_service: LadderService,
player_factory,
event_loop,
):
p1 = player_factory(
"Dostya",
Expand All @@ -760,7 +757,7 @@ async def test_cancel_all_searches(

ladder_service.start_search([p1], "ladder1v1")
search = ladder_service._searches[p1]["ladder1v1"]
await exhaust_callbacks(event_loop)
await exhaust_callbacks()

assert p1.state == PlayerState.SEARCHING_LADDER
assert search in ladder_service.queues["ladder1v1"]._queue
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/test_lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async def lobbyconnection(


@pytest.fixture
def policy_server(event_loop):
async def policy_server():
host = "localhost"
port = 6080

Expand All @@ -128,9 +128,9 @@ async def start_app():
site = web.TCPSite(runner, host, port)
await site.start()

event_loop.run_until_complete(start_app())
await start_app()
yield (host, port)
event_loop.run_until_complete(runner.cleanup())
await runner.cleanup()


async def test_unauthenticated_calls_abort(lobbyconnection, test_game_info):
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/event_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def awaiter(*args, **kwargs):
fut = asyncio.create_task(f(*args, **kwargs))

while not fut.done() and time < timeout:
await exhaust_callbacks(loop)
await exhaust_callbacks()
await advance_time(0.1)
time += 0.1

Expand Down
3 changes: 2 additions & 1 deletion tests/utils/exhaust_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
import asyncio


async def exhaust_callbacks(loop):
async def exhaust_callbacks():
"""
Run the loop until all ready callbacks are executed.
The coroutine doesn't wait for callbacks scheduled in the future with
:meth:`~asyncio.BaseEventLoop.call_at()` or
:meth:`~asyncio.BaseEventLoop.call_later()`.
:param loop: event loop
"""
loop = asyncio.get_running_loop()
while loop._ready:
await asyncio.sleep(0)

0 comments on commit 00cf9ff

Please sign in to comment.