Skip to content

Commit

Permalink
Fixed some more places where coroutines were not awaited (#516)
Browse files Browse the repository at this point in the history
* Fixed some more places where coroutines were not awaited

* Actually run the shutdown command
  • Loading branch information
Askaholic authored Jan 18, 2020
1 parent e591a8c commit 5a7f8bc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def signal_handler(_sig, _frame):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

loop.run_until_complete(done)
players_online.broadcast_shutdown()
loop.run_until_complete(players_online.broadcast_shutdown())
ladder_service.shutdown_queues()

# Close DB connections
Expand Down
13 changes: 8 additions & 5 deletions server/player_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,20 @@ async def update_data(self):
if row is not None:
self.client_version_info = (row[0], row[1])

def broadcast_shutdown(self):
async def broadcast_shutdown(self):
tasks = []
for player in self:
try:
if player.lobby_connection:
tasks.append(
player.lobby_connection.send_warning(
"The server has been shut down for maintenance, "
"but should be back online soon. "
"If you experience any problems, please restart your client. "
"<br/><br/>We apologize for this interruption."
"but should be back online soon. If you experience any "
"problems, please restart your client. <br/><br/>"
"We apologize for this interruption."
)
)
except Exception as ex:
self._logger.debug(
"Could not send shutdown message to %s: %s", player, ex
)
await asyncio.gather(*tasks, return_exceptions=True)
2 changes: 1 addition & 1 deletion server/stats/game_stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async def process_game_stats(self, player: Player, game: Game, stats_json):

await self._event_service.execute_batch_update(player.id, e_queue)
if player.lobby_connection is not None:
player.lobby_connection.send_updated_achievements(updated_achievements)
await player.lobby_connection.send_updated_achievements(updated_achievements)

def _category_stats(self, unit_stats, survived, achievements_queue, events_queue):
built_air = unit_stats['air'].get('built', 0)
Expand Down
6 changes: 4 additions & 2 deletions tests/unit_tests/test_game_stats_service.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from unittest.mock import Mock

import asynctest
import pytest
from asynctest import CoroutineMock
from server.factions import Faction
from server.games import Game
from server.games.game_results import GameOutcome, GameResult, GameResults
from server.lobbyconnection import LobbyConnection
from server.stats import achievement_service as ach
from server.stats import event_service as ev
from server.stats.game_stats_service import GameStatsService
from asynctest import CoroutineMock

pytestmark = pytest.mark.asyncio

Expand Down Expand Up @@ -113,7 +115,7 @@ async def test_process_game_stats(
with open("tests/data/game_stats_full_example.json", "r") as stats_file:
stats = stats_file.read()

mock_lconn = Mock()
mock_lconn = asynctest.create_autospec(LobbyConnection)
player.lobby_connection = mock_lconn

await game_stats_service.process_game_stats(player, game, stats)
Expand Down
12 changes: 7 additions & 5 deletions tests/unit_tests/test_player_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from mock import Mock
import asynctest
import pytest
from mock import Mock
from server.lobbyconnection import LobbyConnection
from server.rating import RatingType

pytestmark = pytest.mark.asyncio
Expand Down Expand Up @@ -85,23 +87,23 @@ async def test_update_data(player_factory, player_service):

async def test_broadcast_shutdown(player_factory, player_service):
player = player_factory()
lconn = Mock()
lconn = asynctest.create_autospec(LobbyConnection)
player.lobby_connection = lconn
player_service[0] = player

player_service.broadcast_shutdown()
await player_service.broadcast_shutdown()

player.lobby_connection.send_warning.assert_called_once()


async def test_broadcast_shutdown_error(player_factory, player_service):
player = player_factory()
lconn = Mock()
lconn = asynctest.create_autospec(LobbyConnection)
lconn.send_warning.side_effect = ValueError
player.lobby_connection = lconn

player_service[0] = player

player_service.broadcast_shutdown()
await player_service.broadcast_shutdown()

player.lobby_connection.send_warning.assert_called_once()

0 comments on commit 5a7f8bc

Please sign in to comment.