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