Skip to content

Commit

Permalink
reduce code dup in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OliLay committed Oct 25, 2023
1 parent d351cb9 commit 4905bc7
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions tests/server/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,83 +42,75 @@ def test_simple(self):

assert "other_hash" not in cache

@staticmethod
def assert_hash_in_cache(cache: Cache, hash_value: str):
assert hash_value in cache
assert (cache.cache_folder / hash_value).exists()

@staticmethod
def assert_hash_not_in_cache(cache: Cache, hash_value: str):
assert hash_value not in cache
assert not (cache.cache_folder / hash_value).exists()

def test_eviction_size_limit(self):
with TemporaryDirectory() as tmp_dir:
root_dir = Path(tmp_dir)
cache = Cache(root_dir, max_size_bytes=10)
cache_dir = root_dir / "cache"
cache = Cache(Path(tmp_dir), max_size_bytes=10)

cache.put("hash1", bytearray([0x1, 0x2, 0x3, 0x9]))
cache.put("hash2", bytearray([0x1, 0x2, 0x3, 0xA]))
cache.put("hash3", bytearray([0xFF, 0xFF]))
assert len(cache) == 3
assert (cache_dir / "hash1").exists()
assert (cache_dir / "hash2").exists()
assert (cache_dir / "hash3").exists()
self.assert_hash_in_cache(cache, "hash1")
self.assert_hash_in_cache(cache, "hash2")
self.assert_hash_in_cache(cache, "hash3")

cache.put("hash4", bytearray([0x1]))
assert len(cache) == 3
assert "hash2" in cache
assert "hash3" in cache
assert "hash4" in cache
assert not (cache_dir / "hash1").exists()
assert (cache_dir / "hash2").exists()
assert (cache_dir / "hash3").exists()
assert (cache_dir / "hash4").exists()
self.assert_hash_not_in_cache(cache, "hash1")
self.assert_hash_in_cache(cache, "hash2")
self.assert_hash_in_cache(cache, "hash3")
self.assert_hash_in_cache(cache, "hash4")

cache.put("hash5", bytearray([0x1]))
assert len(cache) == 4
assert "hash2" in cache
assert "hash3" in cache
assert "hash4" in cache
assert "hash5" in cache
assert (cache_dir / "hash2").exists()
assert (cache_dir / "hash3").exists()
assert (cache_dir / "hash4").exists()
assert (cache_dir / "hash5").exists()
self.assert_hash_in_cache(cache, "hash2")
self.assert_hash_in_cache(cache, "hash3")
self.assert_hash_in_cache(cache, "hash4")
self.assert_hash_in_cache(cache, "hash5")

cache.put("hash6", bytearray([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]))
assert len(cache) == 2
assert not (cache_dir / "hash2").exists()
assert not (cache_dir / "hash3").exists()
assert not (cache_dir / "hash4").exists()
assert "hash5" in cache
assert "hash6" in cache
self.assert_hash_not_in_cache(cache, "hash2")
self.assert_hash_not_in_cache(cache, "hash3")
self.assert_hash_not_in_cache(cache, "hash4")
self.assert_hash_in_cache(cache, "hash5")
self.assert_hash_in_cache(cache, "hash6")

def test_eviction_order_lru(self):
with TemporaryDirectory() as tmp_dir:
root_dir = Path(tmp_dir)
cache = Cache(root_dir, max_size_bytes=10)
cache_dir = root_dir / "cache"
cache = Cache(Path(tmp_dir), max_size_bytes=10)

cache.put("hash1", bytearray([0x1, 0x2, 0x3, 0x9]))
cache.put("hash2", bytearray([0x1, 0x2, 0x3, 0xA]))
cache.put("hash3", bytearray([0xFF, 0xFF]))
assert len(cache) == 3
assert (cache_dir / "hash1").exists()
assert (cache_dir / "hash2").exists()
assert (cache_dir / "hash3").exists()
self.assert_hash_in_cache(cache, "hash1")
self.assert_hash_in_cache(cache, "hash2")
self.assert_hash_in_cache(cache, "hash3")

cache.get("hash1") # make "hash1" the latest used element
cache.put("hash4", bytearray([0xFF, 0xFF, 0x0, 0x0]))
assert len(cache) == 3
assert "hash2" not in cache
assert "hash1" in cache
assert "hash3" in cache
assert "hash4" in cache
# TODO: method for asserts combining IO exists and cache exists to reduce boilerplate
assert not (cache_dir / "hash2").exists()
assert (cache_dir / "hash1").exists()
assert (cache_dir / "hash3").exists()
assert (cache_dir / "hash4").exists()
self.assert_hash_not_in_cache(cache, "hash2")
self.assert_hash_in_cache(cache, "hash1")
self.assert_hash_in_cache(cache, "hash3")
self.assert_hash_in_cache(cache, "hash4")

assert "hash3" in cache # make "hash3" the latest used element
cache.put("hash5", bytearray([0xFF, 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0x0]))
assert len(cache) == 2
assert "hash3" in cache
assert "hash5" in cache
assert not (cache_dir / "hash1").exists()
assert not (cache_dir / "hash2").exists()
assert (cache_dir / "hash3").exists()
assert not (cache_dir / "hash4").exists()
assert (cache_dir / "hash5").exists()
self.assert_hash_in_cache(cache, "hash3")
self.assert_hash_in_cache(cache, "hash5")
self.assert_hash_not_in_cache(cache, "hash1")
self.assert_hash_not_in_cache(cache, "hash2")
self.assert_hash_not_in_cache(cache, "hash4")

0 comments on commit 4905bc7

Please sign in to comment.