Skip to content

Commit

Permalink
Add timeout to base game class (#798)
Browse files Browse the repository at this point in the history
* Add timeout to base game class

* Add unitTest
  • Loading branch information
Sheikah45 authored Jun 11, 2021
1 parent 23955b8 commit b81229c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 0 additions & 1 deletion server/games/coop.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, *args, **kwargs):
"Expansion": "true"
})
self.leaderboard_saved = False
asyncio.get_event_loop().create_task(self.timeout_game(60))

async def validate_game_mode_settings(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions server/games/custom_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class CustomGame(Game):

def __init__(self, id_, *args, **kwargs):
new_kwargs = {
"rating_type": RatingType.GLOBAL
"rating_type": RatingType.GLOBAL,
"setup_timeout": 30
}
new_kwargs.update(kwargs)
super().__init__(id_, *args, **new_kwargs)
asyncio.get_event_loop().create_task(self.timeout_game())

async def _run_pre_rate_validity_checks(self):
limit = len(self.players) * 60
Expand Down
4 changes: 3 additions & 1 deletion server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(
displayed_rating_range: Optional[InclusiveRange] = None,
enforce_rating_range: bool = False,
max_players: int = 12,
setup_timeout: int = 60,
):
self._db = database
self._results = GameResultReports(id_)
Expand Down Expand Up @@ -120,8 +121,9 @@ def __init__(
self._launch_fut = asyncio.Future()

self._logger.debug("%s created", self)
asyncio.get_event_loop().create_task(self.timeout_game(setup_timeout))

async def timeout_game(self, timeout: int = 30):
async def timeout_game(self, timeout: int = 60):
await asyncio.sleep(timeout)
if self.state is GameState.INITIALIZING:
self._is_hosted.set_exception(
Expand Down
8 changes: 7 additions & 1 deletion tests/unit_tests/test_ladder_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,23 @@ async def test_start_game_1v1(
LadderGame.wait_launched.assert_called_once()


@fast_forward(35)
@fast_forward(65)
async def test_start_game_timeout(
ladder_service: LadderService,
player_factory,
monkeypatch
):
queue = ladder_service.queues["ladder1v1"]
p1 = player_factory("Dostya", player_id=1, lobby_connection_spec="auto")
p2 = player_factory("Rhiza", player_id=2, lobby_connection_spec="auto")

monkeypatch.setattr(LadderGame, "timeout_game", CoroutineMock())
monkeypatch.setattr(LadderGame, "on_game_end", CoroutineMock())

await ladder_service.start_game([p1], [p2], queue)

LadderGame.timeout_game.assert_called_once()
LadderGame.on_game_end.assert_called()
p1.lobby_connection.write.assert_called_once_with({"command": "match_cancelled"})
p2.lobby_connection.write.assert_called_once_with({"command": "match_cancelled"})
assert p1.lobby_connection.launch_game.called
Expand Down

0 comments on commit b81229c

Please sign in to comment.