diff --git a/requirements-dev.txt b/requirements-dev.txt index 49b80d4d4..0e52e50e9 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,7 +2,7 @@ asynctest black flake8 pre-commit -pyre-check==0.0.43 +pyre-check==0.0.41 pytest pytest-asyncio pytest-cov diff --git a/src/poke_env/player/player_network_interface.py b/src/poke_env/player/player_network_interface.py index 9b72140c7..9e260f0a0 100644 --- a/src/poke_env/player/player_network_interface.py +++ b/src/poke_env/player/player_network_interface.py @@ -13,6 +13,8 @@ from asyncio import ensure_future from asyncio import Event from asyncio import Lock +from asyncio import sleep +from time import perf_counter from typing import List from typing import Optional @@ -82,7 +84,7 @@ async def _change_avatar(self, avatar_id: Optional[int]) -> None: :param avatar_id: The new avatar id. If None, nothing happens. :type avatar_id: int """ - assert self.logged_in.is_set() + self._wait_for_login() if avatar_id is not None: await self._send_message(f"/avatar {avatar_id}") @@ -159,6 +161,11 @@ async def _handle_message(self, message: str) -> None: raise NotImplementedError("Unhandled message: %s" % message) except CancelledError as e: self.logger.critical("CancelledError intercepted. %s", e) + except Exception as exception: + self.logger.exception( + "Unhandled exception raised while handling message:\n%s", message + ) + raise exception async def _log_in(self, split_message: List[str]) -> None: """Log the player with specified username and password. @@ -211,6 +218,16 @@ async def _send_message( await self._websocket.send(to_send) self.logger.info(">>> %s", to_send) + def _wait_for_login( + self, checking_interval: float = 0.001, wait_for: int = 5 + ) -> None: + start = perf_counter() + while perf_counter() - start < wait_for: + sleep(checking_interval) + if self.logged_in: + return + assert self.logged_in + async def listen(self) -> None: """Listen to a showdown websocket and dispatch messages to be handled.""" self.logger.info("Starting listening to showdown websocket")