View Javadoc
1   package com.atlassian.cache.hazelcast;
2   
3   import java.util.concurrent.TimeUnit;
4   
5   import com.atlassian.cache.CacheSettings;
6   import com.atlassian.cache.CacheSettingsBuilder;
7   import com.atlassian.hazelcast.test.HazelcastCluster;
8   
9   import com.hazelcast.config.MapConfig;
10  import com.hazelcast.core.HazelcastInstance;
11  import com.hazelcast.map.impl.MapService;
12  import com.hazelcast.spi.AbstractDistributedObject;
13  
14  import org.junit.After;
15  import org.junit.Before;
16  import org.junit.Rule;
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  
22  /**
23   * Tests whether a cache configured on one node is configured identically across the cluster
24   */
25  public class CacheConfigReplicationTest
26  {
27      @Rule
28      public final HazelcastCluster cluster = new HazelcastCluster.Builder()
29              .size(2, 3)
30              .build();
31  
32      private HazelcastCacheManager factory1;
33      private HazelcastCacheManager factory2;
34  
35      @Before
36      public void setup()
37      {
38          factory1 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
39          factory2 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(1));
40      }
41  
42      @After
43      public void tearDown()
44      {
45          factory1.destroy();
46          factory2.destroy();
47      }
48  
49      @Test
50      public void testMapConfigReplication() throws InterruptedException
51      {
52          CacheSettings settings = new CacheSettingsBuilder().remote().expireAfterWrite(10, TimeUnit.SECONDS).build();
53  
54          // create the a cache on node1
55          factory1.getCache("test-cache", null, settings);
56  
57          String mapName = HazelcastCacheManager.PREFIX_CACHE + "test-cache";
58  
59          // verify that the settings are applied to the IMap on node 1
60          MapConfig config1 = getConfigForMap(cluster.getNode(0), mapName);
61  
62          assertNotNull(config1);
63          assertEquals(mapName, config1.getName());
64          assertEquals(10, config1.getTimeToLiveSeconds());
65  
66          Thread.sleep(500L); // give the join some time to succeed and potentially replicate the config
67  
68          // verify that the same MapConfig that is actually being used in the MapContainer has been applied on node 2
69          MapConfig config2 = getConfigForMap(cluster.getNode(1), mapName);
70  
71          assertNotNull(config2);
72          assertEquals(mapName, config2.getName());
73          assertEquals(10, config2.getTimeToLiveSeconds());
74      }
75  
76      @Test
77      public void testMapConfigReplicationOnJoin() throws InterruptedException
78      {
79          CacheSettings settings = new CacheSettingsBuilder().remote().expireAfterWrite(20, TimeUnit.SECONDS).build();
80  
81          // create a Cache on node1
82          factory1.getCache("test-cache", null, settings);
83  
84          // add a new node and verify that the IMap is created with the correct settings on the node
85          HazelcastInstance node3 = cluster.addNode();
86  
87          // create the cacheFactory on node 3 as well
88          HazelcastCacheManager factory3 = HazelcastTestSupport.createDistributedFactory(node3);
89  
90          // verify that the same MapConfig that is actually being used in the MapContainer has been applied on node 2
91          String mapName = HazelcastCacheManager.PREFIX_CACHE + "test-cache";
92          MapConfig config3 = getConfigForMap(node3, mapName);
93          assertNotNull(config3);
94          assertEquals(mapName, config3.getName());
95          assertEquals(20, config3.getTimeToLiveSeconds());
96  
97          factory3.destroy();
98      }
99  
100     private MapConfig getConfigForMap(HazelcastInstance hazelcast, String mapName)
101     {
102         AbstractDistributedObject map = hazelcast.getDistributedObject(MapService.SERVICE_NAME, mapName);
103         MapService mapService = (MapService) map.getService();
104         return mapService.getMapServiceContext().getMapContainer(mapName).getMapConfig();
105     }
106 }