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.ImmutableMap; 23 import com.google.common.collect.Lists; 24 import java.util.List; 25 import java.util.concurrent.atomic.AtomicReference; 26 import junit.framework.TestCase; 27 28 /** 29 * Unit test for {@link AbstractCache}. 30 * 31 * @author Charles Fry 32 */ 33 public class AbstractCacheTest extends TestCase { 34 testGetIfPresent()35 public void testGetIfPresent() { 36 final AtomicReference<Object> valueRef = new AtomicReference<>(); 37 Cache<Object, Object> cache = 38 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 testGetAllPresent_empty()52 public void testGetAllPresent_empty() { 53 Cache<Object, Object> cache = 54 new AbstractCache<Object, Object>() { 55 @Override 56 public Object getIfPresent(Object key) { 57 return null; 58 } 59 }; 60 61 assertEquals(ImmutableMap.of(), cache.getAllPresent(ImmutableList.of(new Object()))); 62 } 63 testGetAllPresent_cached()64 public void testGetAllPresent_cached() { 65 final Object cachedKey = new Object(); 66 final Object cachedValue = new Object(); 67 Cache<Object, Object> cache = 68 new AbstractCache<Object, Object>() { 69 @Override 70 public Object getIfPresent(Object key) { 71 return cachedKey.equals(key) ? cachedValue : null; 72 } 73 }; 74 75 assertEquals( 76 ImmutableMap.of(cachedKey, cachedValue), 77 cache.getAllPresent(ImmutableList.of(cachedKey, new Object()))); 78 } 79 testInvalidateAll()80 public void testInvalidateAll() { 81 final List<Object> invalidated = Lists.newArrayList(); 82 Cache<Integer, Integer> cache = 83 new AbstractCache<Integer, Integer>() { 84 @Override 85 public Integer getIfPresent(Object key) { 86 throw new UnsupportedOperationException(); 87 } 88 89 @Override 90 public void invalidate(Object key) { 91 invalidated.add(key); 92 } 93 }; 94 95 List<Integer> toInvalidate = ImmutableList.of(1, 2, 3, 4); 96 cache.invalidateAll(toInvalidate); 97 assertEquals(toInvalidate, invalidated); 98 } 99 testEmptySimpleStats()100 public void testEmptySimpleStats() { 101 StatsCounter counter = new SimpleStatsCounter(); 102 CacheStats stats = counter.snapshot(); 103 assertEquals(0, stats.requestCount()); 104 assertEquals(0, stats.hitCount()); 105 assertEquals(1.0, stats.hitRate()); 106 assertEquals(0, stats.missCount()); 107 assertEquals(0.0, stats.missRate()); 108 assertEquals(0, stats.loadSuccessCount()); 109 assertEquals(0, stats.loadExceptionCount()); 110 assertEquals(0, stats.loadCount()); 111 assertEquals(0, stats.totalLoadTime()); 112 assertEquals(0.0, stats.averageLoadPenalty()); 113 assertEquals(0, stats.evictionCount()); 114 } 115 testSingleSimpleStats()116 public void testSingleSimpleStats() { 117 StatsCounter counter = new SimpleStatsCounter(); 118 for (int i = 0; i < 11; i++) { 119 counter.recordHits(1); 120 } 121 for (int i = 0; i < 13; i++) { 122 counter.recordLoadSuccess(i); 123 } 124 for (int i = 0; i < 17; i++) { 125 counter.recordLoadException(i); 126 } 127 for (int i = 0; i < 23; i++) { 128 counter.recordMisses(1); 129 } 130 for (int i = 0; i < 27; i++) { 131 counter.recordEviction(); 132 } 133 CacheStats stats = counter.snapshot(); 134 int requestCount = 11 + 23; 135 assertEquals(requestCount, stats.requestCount()); 136 assertEquals(11, stats.hitCount()); 137 assertEquals(11.0 / requestCount, stats.hitRate()); 138 int missCount = 23; 139 assertEquals(missCount, stats.missCount()); 140 assertEquals(((double) missCount) / requestCount, stats.missRate()); 141 assertEquals(13, stats.loadSuccessCount()); 142 assertEquals(17, stats.loadExceptionCount()); 143 assertEquals(13 + 17, stats.loadCount()); 144 assertEquals(214, stats.totalLoadTime()); 145 assertEquals(214.0 / (13 + 17), stats.averageLoadPenalty()); 146 assertEquals(27, stats.evictionCount()); 147 } 148 testSimpleStatsOverflow()149 public void testSimpleStatsOverflow() { 150 StatsCounter counter = new SimpleStatsCounter(); 151 counter.recordLoadSuccess(Long.MAX_VALUE); 152 counter.recordLoadSuccess(1); 153 CacheStats stats = counter.snapshot(); 154 assertEquals(Long.MAX_VALUE, stats.totalLoadTime()); 155 } 156 testSimpleStatsIncrementBy()157 public void testSimpleStatsIncrementBy() { 158 long totalLoadTime = 0; 159 160 SimpleStatsCounter counter1 = new SimpleStatsCounter(); 161 for (int i = 0; i < 11; i++) { 162 counter1.recordHits(1); 163 } 164 for (int i = 0; i < 13; i++) { 165 counter1.recordLoadSuccess(i); 166 totalLoadTime += i; 167 } 168 for (int i = 0; i < 17; i++) { 169 counter1.recordLoadException(i); 170 totalLoadTime += i; 171 } 172 for (int i = 0; i < 19; i++) { 173 counter1.recordMisses(1); 174 } 175 for (int i = 0; i < 23; i++) { 176 counter1.recordEviction(); 177 } 178 179 SimpleStatsCounter counter2 = new SimpleStatsCounter(); 180 for (int i = 0; i < 27; i++) { 181 counter2.recordHits(1); 182 } 183 for (int i = 0; i < 31; i++) { 184 counter2.recordLoadSuccess(i); 185 totalLoadTime += i; 186 } 187 for (int i = 0; i < 37; i++) { 188 counter2.recordLoadException(i); 189 totalLoadTime += i; 190 } 191 for (int i = 0; i < 41; i++) { 192 counter2.recordMisses(1); 193 } 194 for (int i = 0; i < 43; i++) { 195 counter2.recordEviction(); 196 } 197 198 counter1.incrementBy(counter2); 199 assertEquals(new CacheStats(38, 60, 44, 54, totalLoadTime, 66), counter1.snapshot()); 200 } 201 } 202