1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.cache; 18 19 import com.google.common.cache.AbstractCache.SimpleStatsCounter; 20 import com.google.common.cache.AbstractCache.StatsCounter; 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.Lists; 23 24 import junit.framework.TestCase; 25 26 import java.util.List; 27 import java.util.concurrent.atomic.AtomicReference; 28 29 /** 30 * Unit test for {@link AbstractCache}. 31 * 32 * @author Charles Fry 33 */ 34 public class AbstractCacheTest extends TestCase { 35 testGetAllPresent()36 public void testGetAllPresent() { 37 final AtomicReference<Object> valueRef = new AtomicReference<Object>(); 38 Cache<Object, Object> cache = new AbstractCache<Object, Object>() { 39 @Override 40 public Object getIfPresent(Object key) { 41 return valueRef.get(); 42 } 43 }; 44 45 assertNull(cache.getIfPresent(new Object())); 46 47 Object newValue = new Object(); 48 valueRef.set(newValue); 49 assertSame(newValue, cache.getIfPresent(new Object())); 50 } 51 testInvalidateAll()52 public void testInvalidateAll() { 53 final List<Object> invalidated = Lists.newArrayList(); 54 Cache<Integer, Integer> cache = new AbstractCache<Integer, Integer>() { 55 @Override 56 public Integer getIfPresent(Object key) { 57 throw new UnsupportedOperationException(); 58 } 59 60 @Override 61 public void invalidate(Object key) { 62 invalidated.add(key); 63 } 64 }; 65 66 List<Integer> toInvalidate = ImmutableList.of(1, 2, 3, 4); 67 cache.invalidateAll(toInvalidate); 68 assertEquals(toInvalidate, invalidated); 69 } 70 testEmptySimpleStats()71 public void testEmptySimpleStats() { 72 StatsCounter counter = new SimpleStatsCounter(); 73 CacheStats stats = counter.snapshot(); 74 assertEquals(0, stats.requestCount()); 75 assertEquals(0, stats.hitCount()); 76 assertEquals(1.0, stats.hitRate()); 77 assertEquals(0, stats.missCount()); 78 assertEquals(0.0, stats.missRate()); 79 assertEquals(0, stats.loadSuccessCount()); 80 assertEquals(0, stats.loadExceptionCount()); 81 assertEquals(0, stats.loadCount()); 82 assertEquals(0, stats.totalLoadTime()); 83 assertEquals(0.0, stats.averageLoadPenalty()); 84 assertEquals(0, stats.evictionCount()); 85 } 86 testSingleSimpleStats()87 public void testSingleSimpleStats() { 88 StatsCounter counter = new SimpleStatsCounter(); 89 for (int i = 0; i < 11; i++) { 90 counter.recordHits(1); 91 } 92 for (int i = 0; i < 13; i++) { 93 counter.recordLoadSuccess(i); 94 } 95 for (int i = 0; i < 17; i++) { 96 counter.recordLoadException(i); 97 } 98 for (int i = 0; i < 23; i++) { 99 counter.recordMisses(1); 100 } 101 for (int i = 0; i < 27; i++) { 102 counter.recordEviction(); 103 } 104 CacheStats stats = counter.snapshot(); 105 int requestCount = 11 + 23; 106 assertEquals(requestCount, stats.requestCount()); 107 assertEquals(11, stats.hitCount()); 108 assertEquals(11.0 / requestCount, stats.hitRate()); 109 int missCount = 23; 110 assertEquals(missCount, stats.missCount()); 111 assertEquals(((double) missCount) / requestCount, stats.missRate()); 112 assertEquals(13, stats.loadSuccessCount()); 113 assertEquals(17, stats.loadExceptionCount()); 114 assertEquals(13 + 17, stats.loadCount()); 115 assertEquals(214, stats.totalLoadTime()); 116 assertEquals(214.0 / (13 + 17), stats.averageLoadPenalty()); 117 assertEquals(27, stats.evictionCount()); 118 } 119 testSimpleStatsIncrementBy()120 public void testSimpleStatsIncrementBy() { 121 long totalLoadTime = 0; 122 123 SimpleStatsCounter counter1 = new SimpleStatsCounter(); 124 for (int i = 0; i < 11; i++) { 125 counter1.recordHits(1); 126 } 127 for (int i = 0; i < 13; i++) { 128 counter1.recordLoadSuccess(i); 129 totalLoadTime += i; 130 } 131 for (int i = 0; i < 17; i++) { 132 counter1.recordLoadException(i); 133 totalLoadTime += i; 134 } 135 for (int i = 0; i < 19; i++) { 136 counter1.recordMisses(1); 137 } 138 for (int i = 0; i < 23; i++) { 139 counter1.recordEviction(); 140 } 141 142 SimpleStatsCounter counter2 = new SimpleStatsCounter(); 143 for (int i = 0; i < 27; i++) { 144 counter2.recordHits(1); 145 } 146 for (int i = 0; i < 31; i++) { 147 counter2.recordLoadSuccess(i); 148 totalLoadTime += i; 149 } 150 for (int i = 0; i < 37; i++) { 151 counter2.recordLoadException(i); 152 totalLoadTime += i; 153 } 154 for (int i = 0; i < 41; i++) { 155 counter2.recordMisses(1); 156 } 157 for (int i = 0; i < 43; i++) { 158 counter2.recordEviction(); 159 } 160 161 counter1.incrementBy(counter2); 162 assertEquals(new CacheStats(38, 60, 44, 54, totalLoadTime, 66), 163 counter1.snapshot()); 164 } 165 166 } 167