def test_zero_cache_size(self): uris_to_sizes = {"5": 5, "3": 3} def delete_fn(uri, logger): return uris_to_sizes[uri] cache = URICache(delete_fn, max_total_size_bytes=0, debug_mode=True) cache.add("5", 5) assert cache.get_total_size_bytes() == 5 cache.mark_unused("5") assert cache.get_total_size_bytes() == 0 cache.add("3", 3) cache.add("5", 5) assert cache.get_total_size_bytes() == 8 cache.mark_unused("3") cache.mark_unused("5") assert cache.get_total_size_bytes() == 0
def test_mark_used(self): uris_to_sizes = {"a": 3, "b": 3, "big": 300} def delete_fn(uri, logger): return uris_to_sizes[uri] cache = URICache(delete_fn, max_total_size_bytes=10, debug_mode=True) cache.add("a", 3) cache.add("b", 3) cache.mark_unused("a") cache.mark_unused("b") assert "a" in cache and "b" in cache assert cache.get_total_size_bytes() == 6 cache.mark_used("a") cache.add("big", 300) # We are over capacity and the only unused URI is "b", so we delete it assert "a" in cache and "big" in cache and "b" not in cache assert cache.get_total_size_bytes() == 303 cache.mark_unused("big") assert "big" not in cache assert cache.get_total_size_bytes() == 3
def test_nonzero_cache_size(self): uris_to_sizes = {"a": 4, "b": 4, "c": 4} def delete_fn(uri, logger): return uris_to_sizes[uri] cache = URICache(delete_fn, max_total_size_bytes=10, debug_mode=True) cache.add("a", 4) cache.add("b", 4) cache.mark_unused("a") assert "a" in cache cache.add("c", 4) # Now we have total size 12, which exceeds the max size 10. assert cache.get_total_size_bytes() == 8 # "a" was the only unused URI, so it must have been deleted. assert "b" and "c" in cache and "a" not in cache