async def test_no_cache_fist_connect_failure(backend_client, mocker, exception): addresses = [ "address_1", "address_2" ] persistent_cache = {} persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) backend_client.get_servers.return_value = addresses websocket = MagicMock() websocket.close = AsyncMock() websocket.close.return_value = None connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=[ async_raise(exception), async_return_value(websocket) ] ) used_server_cell_id = "0" assert await cache.get(used_server_cell_id) == [wrap_address(addresses[1])] backend_client.get_servers.assert_called_once_with(used_server_cell_id) assert 'servers_cache' in persistent_cache assert persistent_cache_state.modified connect.assert_has_calls([call(wrap_address(address), ssl=ANY) for address in addresses])
async def test_timeouted_cache(backend_client, mocker): addresses = [ "echo.websocket.org" ] used_server_cell_id = "0" persistent_cache = { 'servers_cache': json.dumps({ used_server_cell_id:{ 'timeout': time.time() - 10, 'servers': [(wrap_address(address), 3.206969738006592) for address in addresses] } }) } persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) backend_client.get_servers.return_value = addresses websocket = MagicMock() websocket.close = AsyncMock() websocket.close.return_value = None connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=lambda *args, **kwargs: async_return_value(websocket) ) assert await cache.get(used_server_cell_id) == [wrap_address(address) for address in addresses] backend_client.get_servers.assert_called_once_with(used_server_cell_id) assert json.loads(persistent_cache['servers_cache'])[used_server_cell_id]['timeout'] > time.time() assert persistent_cache_state.modified connect.assert_has_calls([call(wrap_address(address), ssl=ANY) for address in addresses])
def __init__(self, reader, writer, token): super().__init__(Platform.Steam, __version__, reader, writer, token) self._steam_id = None self._miniprofile_id = None self._level_db_parser = None self._regmon = get_steam_registry_monitor() self._local_games_cache: Optional[List[LocalGame]] = None self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self._ssl_context.load_verify_locations(certifi.where()) self._http_client = AuthenticatedHttpClient() self._client = SteamHttpClient(self._http_client) self._persistent_storage_state = PersistentCacheState() self._servers_cache = ServersCache(self._client, self._ssl_context, self.persistent_cache, self._persistent_storage_state) self._friends_cache = FriendsCache() self._steam_client = WebSocketClient(self._client, self._ssl_context, self._servers_cache, self._friends_cache) self._achievements_cache = Cache() self._achievements_cache_updated = False self._achievements_semaphore = asyncio.Semaphore(20) self._tags_semaphore = asyncio.Semaphore(5) self._library_settings_import_iterator = 0 self._game_tags_cache = {} self._update_task = None def user_presence_update_handler(user_id: str, user_info: UserInfo): self.update_user_presence(user_id, from_user_info(user_info)) self._friends_cache.updated_handler = user_presence_update_handler
async def test_valid_cache(backend_client): addresses = [ "address_1" ] used_server_cell_id = "0" persistent_cache = {'servers_cache': json.dumps({used_server_cell_id: {'timeout': time.time() + 10, 'servers': [(addresses[0], 3.206969738006592)]}})} persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) assert await cache.get(used_server_cell_id) == addresses backend_client.get_servers.assert_not_called() assert not persistent_cache_state.modified
def __init__(self, reader, writer, token): super().__init__(Platform.Steam, __version__, reader, writer, token) self._regmon = get_steam_registry_monitor() self._local_games_cache: Optional[List[LocalGame]] = None self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self._ssl_context.load_verify_locations(certifi.where()) self._http_client = AuthenticatedHttpClient() self._client = SteamHttpClient(self._http_client) self._persistent_storage_state = PersistentCacheState() self._servers_cache = ServersCache(self._client, self._ssl_context, self.persistent_cache, self._persistent_storage_state) self._friends_cache = FriendsCache() self._games_cache = GamesCache() self._translations_cache = dict() self._stats_cache = StatsCache() self._user_info_cache = UserInfoCache() self._times_cache = TimesCache() self._steam_client = WebSocketClient( self._client, self._ssl_context, self._servers_cache, self._friends_cache, self._games_cache, self._translations_cache, self._stats_cache, self._times_cache, self._user_info_cache, self.store_credentials) self._steam_client_run_task = None self._tags_semaphore = asyncio.Semaphore(5) self._library_settings_import_iterator = 0 self._last_launch: Timestamp = 0 self._update_local_games_task = asyncio.create_task(asyncio.sleep(0)) self._update_owned_games_task = asyncio.create_task(asyncio.sleep(0)) self._owned_games_parsed = None self._auth_data = None self._cooldown_timer = time.time() async def user_presence_update_handler(user_id: str, proto_user_info: ProtoUserInfo): self.update_user_presence( user_id, await presence_from_user_info(proto_user_info, self._translations_cache)) self._friends_cache.updated_handler = user_presence_update_handler
async def test_no_cache_all_connect_failure(backend_client, mocker, exception): addresses = ["address_1"] persistent_cache = {} persistent_cache_state = PersistentCacheState() cache = ServersCache(backend_client, MagicMock(), persistent_cache, persistent_cache_state) backend_client.get_servers.return_value = addresses connect = mocker.patch("protocol.websocket_client.websockets.connect", return_value=async_raise(exception)) assert await cache.get() == [] backend_client.get_servers.assert_called_once_with() assert 'servers_cache' not in persistent_cache assert not persistent_cache_state.modified connect.assert_has_calls( [call(wrap_address(address), ssl=ANY) for address in addresses])