HazelcastInstance hz1 = newHazelcastInstance(config, "test-node1", new FirewallingNodeContext());
HazelcastInstance hz2 = newHazelcastInstance(config, "test-node2", new FirewallingNodeContext());
HazelcastInstance hz3 = newHazelcastInstance(config, "test-node3", new FirewallingNodeContext());
final Node n1 = TestUtil.getNode(hz1);
Node n2 = TestUtil.getNode(hz2);
Node n3 = TestUtil.getNode(hz3);
final CountDownLatch splitLatch = new CountDownLatch(2);
MembershipAdapter membershipAdapter = new MembershipAdapter() {
@Override
public void memberRemoved(MembershipEvent event) {
if (n1.getLocalMember().equals(event.getMember())) {
splitLatch.countDown();
}
}
};
hz2.getCluster().addMembershipListener(membershipAdapter);
hz3.getCluster().addMembershipListener(membershipAdapter);
final CountDownLatch mergeLatch = new CountDownLatch(2);
LifecycleListener lifecycleListener = new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleState.MERGED) {
mergeLatch.countDown();
}
}
};
hz2.getLifecycleService().addLifecycleListener(lifecycleListener);
hz3.getLifecycleService().addLifecycleListener(lifecycleListener);
FirewallingTcpIpConnectionManager cm1 = getConnectionManager(hz1);
FirewallingTcpIpConnectionManager cm2 = getConnectionManager(hz2);
FirewallingTcpIpConnectionManager cm3 = getConnectionManager(hz3);
// block n2 & n3 on n1
cm1.block(n2.address);
cm1.block(n3.address);
// remove and block n1 on n2 & n3
n2.clusterService.removeAddress(n1.address);
n3.clusterService.removeAddress(n1.address);
cm2.block(n1.address);
cm3.block(n1.address);
assertTrue(splitLatch.await(120, TimeUnit.SECONDS));
assertEquals(3, hz1.getCluster().getMembers().size());
assertEquals(2, hz2.getCluster().getMembers().size());
assertEquals(2, hz3.getCluster().getMembers().size());
// unblock n2 on n1 and n1 on n2 & n3
// n1 still blocks access to n3
cm1.unblock(n2.address);
cm2.unblock(n1.address);
cm3.unblock(n1.address);
assertTrue(mergeLatch.await(120, TimeUnit.SECONDS));
assertEquals(3, hz1.getCluster().getMembers().size());
assertEquals(3, hz2.getCluster().getMembers().size());
assertEquals(3, hz3.getCluster().getMembers().size());
assertEquals(n1.getThisAddress(), n1.getMasterAddress());
assertEquals(n1.getThisAddress(), n2.getMasterAddress());
assertEquals(n1.getThisAddress(), n3.getMasterAddress());
}