Home | History | Annotate | Download | only in cache
      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 
     25 import junit.framework.TestCase;
     26 
     27 import java.util.List;
     28 import java.util.concurrent.atomic.AtomicReference;
     29 
     30 /**
     31  * Unit test for {@link AbstractCache}.
     32  *
     33  * @author Charles Fry
     34  */
     35 public class AbstractCacheTest extends TestCase {
     36 
     37   public void testGetIfPresent() {
     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 
     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 
     53   public void testGetAllPresent_empty() {
     54     Cache<Object, Object> cache = new AbstractCache<Object, Object>() {
     55       @Override
     56       public Object getIfPresent(Object key) {
     57         return null;
     58       }
     59     };
     60 
     61     assertEquals(
     62         ImmutableMap.of(),
     63         cache.getAllPresent(ImmutableList.of(new Object())));
     64   }
     65 
     66   public void testGetAllPresent_cached() {
     67     final Object cachedKey = new Object();
     68     final Object cachedValue = new Object();
     69     Cache<Object, Object> cache = new AbstractCache<Object, Object>() {
     70       @Override
     71       public 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 
     81   public void testInvalidateAll() {
     82     final List<Object> invalidated = Lists.newArrayList();
     83     Cache<Integer, Integer> cache = 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 
    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 
    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 
    149   public void testSimpleStatsIncrementBy() {
    150     long totalLoadTime = 0;
    151 
    152     SimpleStatsCounter counter1 = new SimpleStatsCounter();
    153     for (int i = 0; i < 11; i++) {
    154       counter1.recordHits(1);
    155     }
    156     for (int i = 0; i < 13; i++) {
    157       counter1.recordLoadSuccess(i);
    158       totalLoadTime += i;
    159     }
    160     for (int i = 0; i < 17; i++) {
    161       counter1.recordLoadException(i);
    162       totalLoadTime += i;
    163     }
    164     for (int i = 0; i < 19; i++) {
    165       counter1.recordMisses(1);
    166     }
    167     for (int i = 0; i < 23; i++) {
    168       counter1.recordEviction();
    169     }
    170 
    171     SimpleStatsCounter counter2 = new SimpleStatsCounter();
    172     for (int i = 0; i < 27; i++) {
    173       counter2.recordHits(1);
    174     }
    175     for (int i = 0; i < 31; i++) {
    176       counter2.recordLoadSuccess(i);
    177       totalLoadTime += i;
    178     }
    179     for (int i = 0; i < 37; i++) {
    180       counter2.recordLoadException(i);
    181       totalLoadTime += i;
    182     }
    183     for (int i = 0; i < 41; i++) {
    184       counter2.recordMisses(1);
    185     }
    186     for (int i = 0; i < 43; i++) {
    187       counter2.recordEviction();
    188     }
    189 
    190     counter1.incrementBy(counter2);
    191     assertEquals(new CacheStats(38, 60, 44, 54, totalLoadTime, 66),
    192         counter1.snapshot());
    193   }
    194 
    195 }
    196