public void testCacheSimple() throws Exception {
long maxSize = 1000000;
long blockSize = calculateBlockSizeDefault(maxSize, 101);
LruBlockCache cache = new LruBlockCache(maxSize, blockSize);
Block[] blocks = generateRandomBlocks(100, blockSize);
long expectedCacheSize = cache.heapSize();
// Confirm empty
for (Block block : blocks) {
assertTrue(cache.getBlock(block.blockName) == null);
}
// Add blocks
for (Block block : blocks) {
cache.cacheBlock(block.blockName, block.buf);
expectedCacheSize += block.heapSize();
}
// Verify correctly calculated cache heap size
assertEquals(expectedCacheSize, cache.heapSize());
// Check if all blocks are properly cached and retrieved
for (Block block : blocks) {
byte buf1[] = cache.getBlock(block.blockName);
assertTrue(buf1 != null);
assertEquals(buf1.length, block.buf.length);
}
// Verify correctly calculated cache heap size
assertEquals(expectedCacheSize, cache.heapSize());
// Check if all blocks are properly cached and retrieved
for (Block block : blocks) {
byte buf1[] = cache.getBlock(block.blockName);
assertTrue(buf1 != null);
assertEquals(buf1.length, block.buf.length);
}
// Expect no evictions
assertEquals(0, cache.getEvictionCount());
// Thread t = new LruBlockCache.StatisticsThread(cache);
// t.start();
// t.join();
}