Exemplo n.º 1
0
def test_with_cache(blobs_digests):
    blobs, digests = blobs_digests
    blob1, blob2, blob3, *_ = blobs
    digest1, digest2, digest3, *_ = digests

    cache = LRUMemoryCache(256)
    fallback = LRUMemoryCache(256)
    with_cache_storage = WithCacheStorage(cache, fallback)

    assert not with_cache_storage.has_blob(digest1)
    write(with_cache_storage, digest1, blob1)
    assert cache.has_blob(digest1)
    assert fallback.has_blob(digest1)
    assert with_cache_storage.get_blob(digest1).read() == blob1

    # Even if a blob is in cache, we still need to check if the fallback
    # has it.
    write(cache, digest2, blob2)
    assert not with_cache_storage.has_blob(digest2)
    write(fallback, digest2, blob2)
    assert with_cache_storage.has_blob(digest2)

    # When a blob is in the fallback but not the cache, reading it should
    # put it into the cache.
    write(fallback, digest3, blob3)
    assert with_cache_storage.get_blob(digest3).read() == blob3
    assert cache.has_blob(digest3)
    assert cache.get_blob(digest3).read() == blob3
    assert cache.has_blob(digest3)
Exemplo n.º 2
0
def test_lru_eviction(blobs_digests):
    blobs, digests = blobs_digests
    blob1, blob2, blob3, *_ = blobs
    digest1, digest2, digest3, *_ = digests

    lru = LRUMemoryCache(8)
    write(lru, digest1, blob1)
    write(lru, digest2, blob2)
    assert lru.has_blob(digest1)
    assert lru.has_blob(digest2)

    write(lru, digest3, blob3)
    # Check that the LRU evicted blob1 (it was written first)
    assert not lru.has_blob(digest1)
    assert lru.has_blob(digest2)
    assert lru.has_blob(digest3)

    assert lru.get_blob(digest2).read() == blob2
    write(lru, digest1, blob1)
    # Check that the LRU evicted blob3 (since we just read blob2)
    assert lru.has_blob(digest1)
    assert lru.has_blob(digest2)
    assert not lru.has_blob(digest3)

    assert lru.has_blob(digest2)
    write(lru, digest3, blob1)
    # Check that the LRU evicted blob1 (since we just checked blob3)
    assert not lru.has_blob(digest1)
    assert lru.has_blob(digest2)
    assert lru.has_blob(digest3)