• 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"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.cache;
16 
17 import static com.google.common.cache.TestingCacheLoaders.incrementingLoader;
18 import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 
20 import com.google.common.cache.TestingCacheLoaders.IncrementingLoader;
21 import com.google.common.testing.FakeTicker;
22 import junit.framework.TestCase;
23 
24 /**
25  * Tests relating to automatic cache refreshing.
26  *
27  * @author Charles Fry
28  */
29 public class CacheRefreshTest extends TestCase {
testAutoRefresh()30   public void testAutoRefresh() {
31     FakeTicker ticker = new FakeTicker();
32     IncrementingLoader loader = incrementingLoader();
33     LoadingCache<Integer, Integer> cache =
34         CacheBuilder.newBuilder()
35             .refreshAfterWrite(3, MILLISECONDS)
36             .expireAfterWrite(6, MILLISECONDS)
37             .lenientParsing()
38             .ticker(ticker)
39             .build(loader);
40     int expectedLoads = 0;
41     int expectedReloads = 0;
42     for (int i = 0; i < 3; i++) {
43       assertEquals(Integer.valueOf(i), cache.getUnchecked(i));
44       expectedLoads++;
45       assertEquals(expectedLoads, loader.getLoadCount());
46       assertEquals(expectedReloads, loader.getReloadCount());
47       ticker.advance(1, MILLISECONDS);
48     }
49 
50     assertEquals(Integer.valueOf(0), cache.getUnchecked(0));
51     assertEquals(Integer.valueOf(1), cache.getUnchecked(1));
52     assertEquals(Integer.valueOf(2), cache.getUnchecked(2));
53     assertEquals(expectedLoads, loader.getLoadCount());
54     assertEquals(expectedReloads, loader.getReloadCount());
55 
56     // refresh 0
57     ticker.advance(1, MILLISECONDS);
58     assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
59     expectedReloads++;
60     assertEquals(Integer.valueOf(1), cache.getUnchecked(1));
61     assertEquals(Integer.valueOf(2), cache.getUnchecked(2));
62     assertEquals(expectedLoads, loader.getLoadCount());
63     assertEquals(expectedReloads, loader.getReloadCount());
64 
65     // write to 1 to delay its refresh
66     cache.asMap().put(1, -1);
67     ticker.advance(1, MILLISECONDS);
68     assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
69     assertEquals(Integer.valueOf(-1), cache.getUnchecked(1));
70     assertEquals(Integer.valueOf(2), cache.getUnchecked(2));
71     assertEquals(expectedLoads, loader.getLoadCount());
72     assertEquals(expectedReloads, loader.getReloadCount());
73 
74     // refresh 2
75     ticker.advance(1, MILLISECONDS);
76     assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
77     assertEquals(Integer.valueOf(-1), cache.getUnchecked(1));
78     assertEquals(Integer.valueOf(3), cache.getUnchecked(2));
79     expectedReloads++;
80     assertEquals(expectedLoads, loader.getLoadCount());
81     assertEquals(expectedReloads, loader.getReloadCount());
82 
83     ticker.advance(1, MILLISECONDS);
84     assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
85     assertEquals(Integer.valueOf(-1), cache.getUnchecked(1));
86     assertEquals(Integer.valueOf(3), cache.getUnchecked(2));
87     assertEquals(expectedLoads, loader.getLoadCount());
88     assertEquals(expectedReloads, loader.getReloadCount());
89 
90     // refresh 0 and 1
91     ticker.advance(1, MILLISECONDS);
92     assertEquals(Integer.valueOf(2), cache.getUnchecked(0));
93     expectedReloads++;
94     assertEquals(Integer.valueOf(0), cache.getUnchecked(1));
95     expectedReloads++;
96     assertEquals(Integer.valueOf(3), cache.getUnchecked(2));
97     assertEquals(expectedLoads, loader.getLoadCount());
98     assertEquals(expectedReloads, loader.getReloadCount());
99   }
100 }
101