Skip to content

Instantly share code, notes, and snippets.

@enesakar
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save enesakar/fd217222d054b23fbe98 to your computer and use it in GitHub Desktop.

Select an option

Save enesakar/fd217222d054b23fbe98 to your computer and use it in GitHub Desktop.
@Test
public void testSplitBrain2() throws InterruptedException {
Config config = new Config();
config.getGroupConfig().setName("split");
config.setProperty(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS, "5");
config.setProperty(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS, "5");
MapConfig mapConfig = new MapConfig();
mapConfig.setName("ClusterMap");
mapConfig.setBackupCount(2);
config.addMapConfig(mapConfig);
HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
HazelcastInstance h3 = Hazelcast.newHazelcastInstance(config);
final CountDownLatch latch = new CountDownLatch(1);
h3.getLifecycleService().addLifecycleListener(new LifecycleListener() {
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleEvent.LifecycleState.MERGED) {
latch.countDown();
}
}
});
assertEquals(3, h1.getCluster().getMembers().size());
assertEquals(3, h2.getCluster().getMembers().size());
assertEquals(3, h3.getCluster().getMembers().size());
IMap<String, String> map = h1.getMap("ClusterMap");
map.put("OneKey", "OneValue");
map.put("TwoKey", "TwoValue");
map.put("ThreeKey", "ThreeValue");
assertEquals(3, map.size());
System.out.println("Put three values in map. Now waiting for 5 secs...");
// Thread.sleep(5000);
System.out.println();
System.out.println("1 -> " + map.get("OneKey"));
System.out.println("2 -> " + map.get("TwoKey"));
System.out.println("3 -> " + map.get("ThreeKey"));
System.out.println("All values present. Backup complete. Simulating drop connections now....");
closeConnectionBetween(h1, h3);
closeConnectionBetween(h2, h3);
closeConnectionBetween(h2, h1);
// Thread.sleep(1000);
// assertEquals(1, h1.getCluster().getMembers().size());
// assertEquals(1, h2.getCluster().getMembers().size());
// assertEquals(1, h3.getCluster().getMembers().size());
HazelcastInstance instances[] = new HazelcastInstance[]{h1, h2, h3};
System.out.println("Printing results of get calls now...");
for(int i=0; i<instances.length; i++) {
HazelcastInstance hz = instances[i];
System.out.println("Hazelcast Instance: "+ hz.getName());
IMap m = hz.getMap("ClusterMap");
assertEquals("OneValue", m.get("OneKey"));
assertEquals("TwoValue", m.get("TwoKey"));
assertEquals("ThreeValue", m.get("ThreeKey"));
System.out.println("1 -> "+m.get("OneKey"));
System.out.println("2 -> "+m.get("TwoKey"));
System.out.println("3 -> "+m.get("ThreeKey"));
}
// Thread.sleep(10000);
// assertTrue(latch.await(30, TimeUnit.SECONDS));
// assertEquals(3, h1.getCluster().getMembers().size());
// assertEquals(3, h2.getCluster().getMembers().size());
// assertEquals(3, h3.getCluster().getMembers().size());
}
private void closeConnectionBetween(HazelcastInstance h1, HazelcastInstance h2) {
if (h1 == null || h2 == null) return;
final Node n1 = TestUtil.getNode(h1);
final Node n2 = TestUtil.getNode(h2);
n1.clusterService.removeAddress(n2.address);
n2.clusterService.removeAddress(n1.address);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment