Skip to content

Commit

Permalink
Cleanup tests (#965)
Browse files Browse the repository at this point in the history
* Change game fixtures to be async

* Ensure sockets are closed in ServerContext tests

* Refactor usage of caplog fixture

* Add flaky pytest mark
  • Loading branch information
Askaholic authored Jul 9, 2023
1 parent 158047e commit 33c5e7b
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 28 deletions.
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def transport():


@pytest.fixture
def game(database, players):
async def game(database, players):
return make_game(database, 1, players)


Expand All @@ -214,15 +214,15 @@ def game(database, players):


@pytest.fixture
def ugame(database, players):
async def ugame(database, players):
global GAME_UID
game = make_game(database, GAME_UID, players)
GAME_UID += 1
return game


@pytest.fixture
def coop_game(database, players):
async def coop_game(database, players):
global COOP_GAME_UID
game = make_game(database, COOP_GAME_UID, players, game_type=CoopGame)
game.validity = ValidityState.COOP_NOT_RANKED
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ async def test_galactic_war_1v1_game_ended_broadcasts_army_results(
await asyncio.wait_for(mq_proto_all.read_message(), timeout=10)


@pytest.mark.flaky
@pytest.mark.rabbitmq
@fast_forward(30)
async def test_galactic_war_2v1_game_ended_broadcasts_army_results(lobby_server, channel):
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/test_matchmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ async def test_game_matchmaking_disconnect(lobby_server):
assert msg == {"command": "match_cancelled", "game_id": 41956}


@pytest.mark.flaky
@fast_forward(130)
async def test_game_matchmaking_close_fa_and_requeue(lobby_server):
_, proto1, _, proto2 = await queue_players_for_matchmaking(lobby_server)
Expand Down
10 changes: 5 additions & 5 deletions tests/integration_tests/test_message_queue_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ async def test_incorrect_credentials(mocker, caplog):

await service.initialize()
expected_warning = "Unable to connect to RabbitMQ. Incorrect credentials?"
assert expected_warning in [rec.message for rec in caplog.records]
assert expected_warning in caplog.messages
assert service._is_ready is False
caplog.clear()

await service.declare_exchange("test_exchange")
expected_warning = "Not connected to RabbitMQ, unable to declare exchange."
assert expected_warning in [rec.message for rec in caplog.records]
assert expected_warning in caplog.messages
caplog.clear()

payload = {"msg": "test message"}
Expand All @@ -126,7 +126,7 @@ async def test_incorrect_credentials(mocker, caplog):
delivery_mode = aio_pika.DeliveryMode.NOT_PERSISTENT
await service.publish(exchange_name, routing_key, payload, delivery_mode)
expected_warning = "Not connected to RabbitMQ, unable to publish message."
assert expected_warning in [rec.message for rec in caplog.records]
assert expected_warning in caplog.messages

await service.shutdown()

Expand All @@ -138,7 +138,7 @@ async def test_incorrect_username(mocker, caplog):
await service.initialize()

expected_warning = "Unable to connect to RabbitMQ. Incorrect credentials?"
assert expected_warning in [rec.message for rec in caplog.records]
assert expected_warning in caplog.messages


async def test_incorrect_vhost(mocker, caplog):
Expand All @@ -147,7 +147,7 @@ async def test_incorrect_vhost(mocker, caplog):

await service.initialize()

assert any("Incorrect vhost?" in rec.message for rec in caplog.records)
assert any("Incorrect vhost?" in msg for msg in caplog.messages)


async def test_initialize_declare_exchange_race_condition(mq_uninit_service):
Expand Down
18 changes: 9 additions & 9 deletions tests/integration_tests/test_servercontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,22 @@ async def test_connection_broken_external(context):
assert len(ctx.connections) == 0


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

def make_protocol(_1, _2):
proto = mock.create_autospec(QDataStreamProtocol)
proto.read_message = mock.AsyncMock(
mocker.patch.object(
ctx.protocol_class,
"read_message",
mock.AsyncMock(
side_effect=RuntimeError("test")
)
return proto

ctx.protocol_class = make_protocol
)

with caplog.at_level("TRACE"):
_, _ = await asyncio.open_connection(*srv.sockets[0].getsockname())
_, writer = await asyncio.open_connection(*srv.sockets[0].getsockname())

assert "Exception in protocol" in caplog.text
with closing(writer):
assert "Exception in protocol" in caplog.text


async def test_unexpected_exception_in_connection_lost(context, caplog):
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/test_teammatchmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ async def test_game_matchmaking_with_parties(lobby_server):
assert msg["faction"] == i + 1


@pytest.mark.flaky
@fast_forward(30)
async def test_newbie_matchmaking_with_parties(lobby_server):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_custom_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@pytest.fixture
def custom_game(event_loop, database, game_service, game_stats_service):
async def custom_game(event_loop, database, game_service, game_stats_service):
return CustomGame(42, database, game_service, game_stats_service)


Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ async def game(database, game_service, game_stats_service):


@pytest.fixture
def coop_game(database, game_service, game_stats_service):
async def coop_game(database, game_service, game_stats_service):
return CoopGame(42, database, game_service, game_stats_service)


@pytest.fixture
def custom_game(database, game_service, game_stats_service):
async def custom_game(database, game_service, game_stats_service):
return CustomGame(42, database, game_service, game_stats_service)


Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_game_rating.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ async def game(event_loop, database, game_service, game_stats_service):


@pytest.fixture
def custom_game(event_loop, database, game_service, game_stats_service):
async def custom_game(event_loop, database, game_service, game_stats_service):
return CustomGame(42, database, game_service, game_stats_service)


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


Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_game_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_conflicting_simple_metadata(game_results, caplog):

with caplog.at_level(logging.INFO):
assert game_results.metadata(1) == ["recall"]
assert "Conflicting metadata" in caplog.records[0].message
assert "Conflicting metadata" in caplog.messages[0]


def test_conflicting_complex_metadata(game_results, caplog):
Expand All @@ -157,4 +157,4 @@ def test_conflicting_complex_metadata(game_results, caplog):

with caplog.at_level(logging.INFO):
assert game_results.metadata(1) == []
assert "unable to resolve" in caplog.records[0].message
assert "unable to resolve" in caplog.messages[0]
4 changes: 2 additions & 2 deletions tests/unit_tests/test_gameconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


@pytest.fixture
def real_game(event_loop, database, game_service, game_stats_service):
async def real_game(event_loop, database, game_service, game_stats_service):
return Game(42, database, game_service, game_stats_service)


Expand Down Expand Up @@ -399,7 +399,7 @@ async def test_cannot_parse_game_results(
with caplog.at_level(logging.WARNING):
await game_connection.handle_action("GameResult", [0, ""])
game.add_result.assert_not_called()
assert "Invalid result" in caplog.records[0].message
assert "Invalid result" in caplog.messages[0]


async def test_handle_action_GameOption(
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_laddergame.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@pytest.fixture()
def laddergame(database, game_service, game_stats_service):
async def laddergame(database, game_service, game_stats_service):
return LadderGame(
id_=465312,
database=database,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_message_queue_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def test_incorrect_port(mocker, caplog):
await service.initialize()

expected_warning = "Unable to connect to RabbitMQ. Is it running?"
assert expected_warning in [rec.message for rec in caplog.records]
assert expected_warning in caplog.messages


async def test_several_initializations_connect_only_once():
Expand Down

0 comments on commit 33c5e7b

Please sign in to comment.