def test_aging_cache_set_get_status(self): # GIVEN cache = AgingCache(max_age_seconds=5) for i in range(10): cache[i] = f"value_{i}" # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})") util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})") self.assertGreaterEqual(len(cache), 10) # WHEN cache.set_item_status(5, "Some Status") for i in range(10): item_status = cache.get_item_status(i) util.logger.spam(f"in cache item({item_status})") # THEN self.assertEqual(len(cache), 10) self.assertEqual(cache.get_item_status(5), "Some Status")
def test_aging_cache_set_item_status_by_time(self): # GIVEN cache = AgingCache(max_age_seconds=5) for i in range(10): cache[i] = f"value_{i}" # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})") util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})") self.assertGreaterEqual(len(cache), 10) # WHEN time.sleep(1) cache.set_item_status_by_time(int(time.time()), "timeout") for i in range(10, 20): cache[i] = f"value_{i}" # THEN timeout_count = 0 for i in range(20): item_status = cache.get_item_status(i) if item_status == "timeout": timeout_count += 1 # util.logger.spam(f"in cache item({item_status})") self.assertEqual(timeout_count, 10)