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