async def test_connect_authenticate(client, protocol_client, backend_client, servers_cache, websocket): servers_cache.get.return_value = async_return_value(["wss://abc.com/websocket"]) backend_client.get_authentication_data.return_value = STEAM_ID, ACCOUNT_NAME, TOKEN protocol_client.authenticate.return_value = async_return_value(None) protocol_client.run.return_value = async_raise(websockets.ConnectionClosedOK(1000, ""), 10) await client.run() servers_cache.get.assert_called_once_with() protocol_client.authenticate.assert_called_once_with(STEAM_ID, ACCOUNT_NAME, TOKEN, ANY) protocol_client.run.assert_called_once_with()
async def _send_dict(self, msg_dict: Dict[str, Any]): """JSON encode a dictionary and send it.""" payload = json.dumps(msg_dict) try: #if there's no socket, assume the connection is closed if not self.websocket: raise websockets.ConnectionClosedOK(1001, 'Socket disconnected') await self.websocket.send(payload) except websockets.ConnectionClosed: _LOGGER.info( "Tried to send a message, but connection already closed.")
async def test_servers_cache_retry( client, protocol_client, backend_client, servers_cache, websocket, mocker, exception ): servers_cache.get.side_effect = [ async_raise(exception), async_return_value(["wss://abc.com/websocket"]) ] sleep = mocker.patch("protocol.websocket_client.asyncio.sleep", side_effect=lambda x: async_return_value(None)) backend_client.get_authentication_data.return_value = STEAM_ID, ACCOUNT_NAME, TOKEN protocol_client.authenticate.return_value = async_return_value(None) protocol_client.run.return_value = async_raise(websockets.ConnectionClosedOK(1000, ""), 10) await client.run() sleep.assert_any_call(RECONNECT_INTERVAL_SECONDS)
async def test_connect_error(client, backend_client, protocol_client, servers_cache, mocker, exception): servers_cache.get.side_effect = [ async_return_value(["wss://websocket_1", "wss://websocket_2"]), ] connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=[ async_raise(exception), async_return_value(MagicMock()) ] ) backend_client.get_authentication_data.return_value = STEAM_ID, ACCOUNT_NAME, TOKEN protocol_client.authenticate.return_value = async_return_value(None) protocol_client.run.return_value = async_raise(websockets.ConnectionClosedOK(1000, ""), 10) await client.run() connect.assert_has_calls([call("wss://websocket_1", ssl=ANY), call("wss://websocket_2", ssl=ANY)])
async def test_websocket_close_reconnect(client, protocol_client, backend_client, servers_cache, websocket): servers_cache.get.side_effect = [ async_return_value(["wss://abc.com/websocket"]), async_return_value(["wss://abc.com/websocket"]) ] backend_client.get_authentication_data.return_value = STEAM_ID, ACCOUNT_NAME, TOKEN protocol_client.authenticate.return_value = async_return_value(None) protocol_client.run.side_effect = [ async_raise(websockets.ConnectionClosedError(1002, ""), 10), async_raise(websockets.ConnectionClosedOK(1000, ""), 10) ] protocol_client.close.return_value = async_return_value(None) protocol_client.wait_closed.return_value = async_return_value(None) websocket.close.return_value = async_return_value(None) websocket.wait_closed.return_value = async_return_value(None) await client.run() assert servers_cache.get.call_count == 2 assert protocol_client.authenticate.call_count == 2 assert protocol_client.run.call_count == 2