def test_read_on_disk_err(self): flag = True def onFire(x): global flag flag = True ce = CacheEntry('http://example.com/foo/bar/doesnotexist', 'test', 0.0, onFire) ce.datafile = None d = ce.read() def xxx(f): self.assert_(False) d.addCallback(xxx) def xxx(f): global flag flag = True d.addErrback(xxx) def yyy(f): print 'yyy' self.assert_(flag) d.addCallback(yyy) try: ce.move_from_file() except: pass return d
def test_read_on_disk_ok(self): cells = {'flag':False, 'x' : None, 'y': None} def my_asserts(): print 'my_asserts' self.assert_(cells['flag']) self.assertNotEquals(cells['x'], cells['y']) def onFire(ce): f = ce.datafile self.assertEquals(f.read(), 'deadbeaf') cells['y'] = id(f) ce = CacheEntry('http://example.com/foo/bar/data', 'test', 0.0, onFire) ce.datafile = None ce.fname = url2name(ce.key) d = ce.read() def xxx(f): cells['flag'] = True self.assertEquals(f.read(), 'deadbeaf') cells['x'] = id(f) my_asserts() d.addCallback(xxx) return d
def test_read_on_memory(self): ce = CacheEntry(None, None, 0.0, lambda x:x) ce.write(DataFile('deadbeaf', '')) d = ce.read() def xxx(f): self.assertEquals(f.read(), 'deadbeaf') d.addCallback(xxx) return d
def test_touch(self): import time ce = CacheEntry(None, None, 0.0, None) self.assert_(ce.last_touch == 0.0) before = time.time() ce.touch() after = time.time() self.assert_(before <= ce.last_touch) self.assert_(ce.last_touch <= after)
def test_read_on_miss(self): ce = CacheEntry('http://example.com/foo/bar/data', 'test', 0.0, lambda x:x) d = ce.read() def xxx(f): self.assertEquals(ce.datafile.read(), 'deadbeaf') d.addCallback(xxx) f = DataFile('deadbeaf', '', 'text/plain') ce.write(f) return d
def test_abort(self): flag = True ce = CacheEntry('http://example.com/foo/bar/data', 'test', 0.0, lambda x:x) d = ce.read() def xxx(f): self.assertFail() d.addCallback(xxx) def yyy(failure): print 'yyy', failure self.assert_(failure.check(Cancel)) self.assert_(flag) d.addErrback(yyy) self.assert_(ce.readRequests) ce.abort() self.assert_(len(ce.readRequests) == 0 )
def mock_trophy_title_info_cache(fixed_time): cache = Cache() entires = {} invalidation_time = fixed_time + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC for k, v in TITLE_TO_TROPHY_TITLE.items(): entires[k] = CacheEntry(value=v, timestamp=invalidation_time) cache._entries = entires return cache
} UNLOCKED_ACHIEVEMENTS = [ Achievement( achievement_id="NPWR11556_00_1", achievement_name="achievement 1", unlock_time=538304493.0, ), Achievement(achievement_id="NPWR11556_00_2", achievement_name="achievement 2", unlock_time=1318782798.0) ] TROPHIES_CACHE = Cache() TROPHIES_CACHE._entries = { "NPWR11556_00": CacheEntry(value=UNLOCKED_ACHIEVEMENTS, timestamp=1490374318.0) } BACKEND_USER_PROFILES = { "start": 0, "size": 7, "totalResults": 7, "profiles": [{ "accountId": 1, "onlineId": "veryopenperson", "avatarUrls": [{
async def test_cache_old_entry( authenticated_plugin, mock_client_get_owned_games, mock_get_game_trophy_title_info_map, ): async def get_owned_games_patched(now_time, games, title_ttis): def trophy_title_map_side_effect(titles): return {t: ttis for t, ttis in title_ttis.items() if t in titles} mock_client_get_owned_games.return_value = games mock_get_game_trophy_title_info_map.side_effect = trophy_title_map_side_effect with patch('time.time', Mock(return_value=now_time)): await authenticated_plugin.get_owned_games() def assert_trophy_title_info_cache(cache_entries): assert cache_entries == authenticated_plugin._trophy_title_info_cache._entries outdated_game, other_game = GAMES[0], GAMES[1] outdated_ttis, other_game_ttis = [], [ TrophyTitleInfo("NPWR33322_00", Mock(str)) ] updated_ttis = [ TrophyTitleInfo("NPWR11556_00", Mock(str)), TrophyTitleInfo("NPWR12507_00", Mock(str)) ] # user has 1 game item that has no comm_id at all first_time = 140_000_000 first_games = [outdated_game] first_title_cids = {outdated_game.game_id: outdated_ttis} await get_owned_games_patched(first_time, first_games, first_title_cids) assert_trophy_title_info_cache({ outdated_game.game_id: CacheEntry(outdated_ttis, first_time + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC) }) mock_get_game_trophy_title_info_map.assert_called_once_with( [outdated_game.game_id]) mock_get_game_trophy_title_info_map.reset_mock() # user buys other game; other_game title should be added to cache with second_time second_time = first_time + (TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC // 2) second_games = first_games + [other_game] second_title_cids = { **first_title_cids, other_game.game_id: other_game_ttis } await get_owned_games_patched(second_time, second_games, second_title_cids) assert_trophy_title_info_cache({ outdated_game.game_id: CacheEntry(outdated_ttis, first_time + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC), other_game.game_id: CacheEntry(other_game_ttis, second_time + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC) }) mock_get_game_trophy_title_info_map.assert_called_once_with( [other_game.game_id]) mock_get_game_trophy_title_info_map.reset_mock() # comm_ids cache for first title should be updated with third_time # other_game cache should not be updated as it is fresh enough third_time = first_time + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC + 5 third_games = second_games third_title_cids = {**second_title_cids, GAMES[0].game_id: updated_ttis} await get_owned_games_patched(third_time, third_games, third_title_cids) assert_trophy_title_info_cache({ outdated_game.game_id: CacheEntry(updated_ttis, third_time + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC), other_game.game_id: CacheEntry(other_game_ttis, second_time + TROPHY_TITLE_INFO_INVALIDATION_PERIOD_SEC) }) mock_get_game_trophy_title_info_map.assert_called_once_with( [outdated_game.game_id])