public void testResizeBlockCache() throws Exception {
long maxSize = 300000;
long blockSize = calculateBlockSize(maxSize, 31);
LruBlockCache cache = new LruBlockCache(maxSize, blockSize, false, (int) Math.ceil(1.2 * maxSize / blockSize), LruBlockCache.DEFAULT_LOAD_FACTOR,
LruBlockCache.DEFAULT_CONCURRENCY_LEVEL, 0.98f, // min
0.99f, // acceptable
0.33f, // single
0.33f, // multi
0.34f);// memory
Block[] singleBlocks = generateFixedBlocks(10, blockSize, "single");
Block[] multiBlocks = generateFixedBlocks(10, blockSize, "multi");
Block[] memoryBlocks = generateFixedBlocks(10, blockSize, "memory");
// Add all blocks from all priorities
for (int i = 0; i < 10; i++) {
// Just add single blocks
cache.cacheBlock(singleBlocks[i].blockName, singleBlocks[i].buf);
// Add and get multi blocks
cache.cacheBlock(multiBlocks[i].blockName, multiBlocks[i].buf);
cache.getBlock(multiBlocks[i].blockName);
// Add memory blocks as such
cache.cacheBlock(memoryBlocks[i].blockName, memoryBlocks[i].buf, true);
}
// Do not expect any evictions yet
assertEquals(0, cache.getEvictionCount());
// Resize to half capacity plus an extra block (otherwise we evict an extra)
cache.setMaxSize((long) (maxSize * 0.5f));
// Should have run a single eviction
assertEquals(1, cache.getEvictionCount());
// And we expect 1/2 of the blocks to be evicted
assertEquals(15, cache.getEvictedCount());
// And the oldest 5 blocks from each category should be gone
for (int i = 0; i < 5; i++) {
assertEquals(null, cache.getBlock(singleBlocks[i].blockName));
assertEquals(null, cache.getBlock(multiBlocks[i].blockName));
assertEquals(null, cache.getBlock(memoryBlocks[i].blockName));
}
// And the newest 5 blocks should still be accessible
for (int i = 5; i < 10; i++) {
assertEquals(singleBlocks[i].buf, cache.getBlock(singleBlocks[i].blockName));
assertEquals(multiBlocks[i].buf, cache.getBlock(multiBlocks[i].blockName));
assertEquals(memoryBlocks[i].buf, cache.getBlock(memoryBlocks[i].blockName));
}
}