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