View Javadoc
1   package com.atlassian.cache.hazelcast;
2   
3   import java.util.concurrent.TimeUnit;
4   
5   import com.atlassian.cache.Cache;
6   import com.atlassian.cache.CacheSettings;
7   import com.atlassian.cache.CacheSettingsBuilder;
8   import com.atlassian.cache.ManagedCache;
9   
10  import com.hazelcast.config.InMemoryFormat;
11  import com.hazelcast.config.MapConfig;
12  import com.hazelcast.config.NearCacheConfig;
13  import com.hazelcast.core.HazelcastInstance;
14  
15  import org.junit.Before;
16  import org.junit.ClassRule;
17  import org.junit.Test;
18  
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.contains;
21  import static org.hamcrest.Matchers.equalTo;
22  import static org.hamcrest.Matchers.hasSize;
23  import static org.hamcrest.Matchers.is;
24  import static org.hamcrest.Matchers.not;
25  import static org.hamcrest.Matchers.notNullValue;
26  import static org.hamcrest.Matchers.nullValue;
27  
28  public class HazelcastCacheManagerTest
29  {
30      @ClassRule
31      public static InitOnceHazelcastCluster cluster = InitOnceHazelcastCluster.getInstance();
32  
33      @Before
34      public void setup()
35      {
36          cluster.reset();
37      }
38  
39      @Test
40      public void testDefaultCacheConfigIsUsedForNewCaches()
41      {
42          HazelcastCacheManager factory = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0), new CacheSettingsBuilder()
43                  .maxEntries(10000)
44                  .expireAfterAccess(6, TimeUnit.MINUTES)
45                  .build());
46  
47          String cacheName = "defaults-test";
48          Cache<String, String> cache = factory.getCache(cacheName);
49  
50          assertThat(cache, not(nullValue()));
51          assertThat(cache.getName(), is(cacheName));
52          assertThat(cache.getKeys(), hasSize(0));
53  
54          String mapName = HazelcastCacheManager.PREFIX_CACHE + cacheName;
55  
56          CacheSettings config = factory.getCacheSettings(mapName);
57          assertThat(config.getMaxEntries(), is(10000));
58          assertThat((int) Math.ceil(config.getExpireAfterAccess() / 1000d), is(360));
59  
60          factory.destroy();
61      }
62  
63      @Test
64      public void testNonFlushableCacheIsNotFlushed()
65      {
66          HazelcastCacheManager factory = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
67  
68          // Create the "broken-toilet" cache
69          Cache<String, String> nonFlushableCache = factory.getCache("broken-toilet", null, new CacheSettingsBuilder().unflushable().build());
70          nonFlushableCache.put("key1", "value1");
71  
72          Cache<String, String> normalCache = factory.getCache("normal-cache");
73          normalCache.put("key2", "value2");
74  
75          factory.flushCaches();
76  
77          nonFlushableCache = factory.getCache("broken-toilet");
78          assertThat(nonFlushableCache.get("key1"), is("value1"));
79  
80          normalCache = factory.getCache("normal-cache");
81          assertThat(normalCache.getKeys(), hasSize(0));
82  
83          factory.destroy();
84      }
85  
86      @Test
87      public void testTheSameCacheInstanceIsReturnedForTheSameName()
88      {
89          HazelcastCacheManager factory = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
90  
91          final String cacheName = "same-instance-test";
92          Cache<String, String> cache = factory.getCache(cacheName);
93  
94          cache.put("fav", "dog");
95          assertThat(cache.getKeys(), contains("fav"));
96          assertThat(cache.get("fav"), is("dog"));
97  
98          cache = factory.getCache(cacheName);
99          assertThat(cache.getKeys(), contains("fav"));
100         assertThat(cache.get("fav"), is("dog"));
101 
102         factory.destroy();
103     }
104 
105     @Test
106     public void testWildcardConfigIsUsed()
107     {
108         HazelcastInstance node = cluster.getNode(0);
109 
110         // set up a wildcard config on each of the nodes
111         MapConfig wildcardConfig = node.getConfig().getMapConfig(HazelcastCacheManager.PREFIX + "*").setAsyncBackupCount(4);
112         wildcardConfig.setNearCacheConfig(new NearCacheConfig()
113                 .setCacheLocalEntries(true)
114                 .setInMemoryFormat(InMemoryFormat.OBJECT));
115         
116         for (HazelcastInstance hazelcast : cluster.getNodes()) {
117             hazelcast.getConfig().addMapConfig(wildcardConfig);
118         }
119 
120         HazelcastCacheManager factory = HazelcastTestSupport.createDistributedFactory(node);
121         factory.getCache("testing", null, new CacheSettingsBuilder().remote().replicateViaCopy().build());
122 
123         // verify that a specific MapConfig has been created for this cache _and_ that the wildcard settings have been incorporated
124         MapConfig cacheConfig = node.getConfig().findMapConfig(HazelcastCacheManager.PREFIX_CACHE + "testing");
125 
126         assertThat(cacheConfig, notNullValue());
127         assertThat(cacheConfig.getName(), equalTo(HazelcastCacheManager.PREFIX_CACHE + "testing"));
128         assertThat(cacheConfig.getAsyncBackupCount(), equalTo(4));
129         
130         // verify that the nearCacheConfig settings from the wildcard also apply
131         NearCacheConfig nearCacheConfig = cacheConfig.getNearCacheConfig();
132         assertThat(nearCacheConfig, notNullValue());
133         assertThat(nearCacheConfig.getInMemoryFormat(), equalTo(InMemoryFormat.OBJECT));
134         assertThat(nearCacheConfig.isCacheLocalEntries(), equalTo(true));
135     }
136 
137     @Test
138     public void testUpdateCacheSettings()
139     {
140         HazelcastInstance node = cluster.getNode(0);
141         final String cacheName = "update-cache.myCache";
142 
143         HazelcastCacheManager factory = HazelcastTestSupport.createDistributedFactory(node);
144 
145         CacheSettings cacheSettings = new CacheSettingsBuilder()
146                 .maxEntries(3000)
147                 .remote()
148                 .flushable()
149                 .expireAfterAccess(3000, TimeUnit.MILLISECONDS)
150                 .expireAfterWrite(6000, TimeUnit.MILLISECONDS)
151                 .replicateSynchronously()
152                 .replicateViaCopy()
153                 .build();
154 
155         //Initialize the cache
156         factory.getCache(cacheName, null, cacheSettings);
157 
158         ManagedCache managedCache = factory.getManagedCache(cacheName);
159 
160         assert managedCache != null;
161         assertThat(3000, equalTo(managedCache.currentMaxEntries()));
162         assertThat(3000L, equalTo(managedCache.currentExpireAfterAccessMillis()));
163         assertThat(6000L, equalTo(managedCache.currentExpireAfterWriteMillis()));
164 
165         managedCache.updateMaxEntries(5000);
166         assertThat(5000, equalTo(managedCache.currentMaxEntries()));
167 
168         managedCache.updateExpireAfterAccess(2000L, TimeUnit.MILLISECONDS);
169         assertThat(2000L, equalTo(managedCache.currentExpireAfterAccessMillis()));
170 
171         managedCache.updateExpireAfterWrite(8000L, TimeUnit.MILLISECONDS);
172         assertThat(8000L, equalTo(managedCache.currentExpireAfterWriteMillis()));
173     }
174 
175     @Test
176     public void testNearCachesExpireEntriesBeforeIMap() throws InterruptedException
177     {
178         HazelcastInstance node = cluster.getNode(0);
179         final String cacheName = "near-cache-expiry.testMap";
180 
181 
182         MapConfig mapConfig = node.getConfig().getMapConfig(HazelcastCacheManager.PREFIX + cacheName);
183         mapConfig.setNearCacheConfig(new NearCacheConfig()
184                 .setCacheLocalEntries(true)
185                 .setInMemoryFormat(InMemoryFormat.OBJECT));
186 
187         for (HazelcastInstance hazelcast : cluster.getNodes()) {
188             hazelcast.getConfig().addMapConfig(mapConfig);
189         }
190 
191         HazelcastCacheManager factory = HazelcastTestSupport.createDistributedFactory(node);
192 
193         CacheSettings cacheSettings = new CacheSettingsBuilder()
194                 .expireAfterAccess(3000L, TimeUnit.MILLISECONDS)
195                 .remote()
196                 .flushable()
197                 .maxEntries(50)
198                 .replicateAsynchronously()
199                 .replicateViaCopy()
200                 .build();
201 
202         Cache<String, String> cache = factory.getCache(cacheName, null, cacheSettings);
203 
204         String key = cluster.generateKeyOwnedByNode(0);
205         String value = "value";
206 
207         //Put a value, this should start the maxIdle expiry clock ticking
208         cache.put(key, value);
209         assertThat(cache.get(key), equalTo(value));
210 
211         //Continuously hitting the near cache doesn't 'tickle' the underlying cache, so our near caches should
212         //Invalidate themselves infrequently but enough so that the IMap doesn't expire entries.
213         for (int count=0; count<40; count++)
214         {
215             Thread.sleep(100L);
216             assertThat("cache entry expired even after accessing the near cache!", cache.get(key), equalTo(value));
217         }
218     }
219 }