rehashListener = new DataRehashedListener();
c1.addListener(rehashListener);
}
public void testJoinAndLeave() {
ConsistentHash ch1Node = advancedCache(0).getDistributionManager().getReadConsistentHash();
assertEquals(rehashListener.removeEvents().size(), 0);
// start a second node and wait for the rebalance to end
addClusterEnabledCacheManager(defaultConfig);
cache(1);
TestingUtil.waitForRehashToComplete(cache(0), cache(1));
ConsistentHash ch2Nodes = advancedCache(0).getDistributionManager().getReadConsistentHash();
List<DataRehashedEvent<Object, Object>> events = rehashListener.removeEvents();
assertEquals(events.size(), 2);
DataRehashedEvent<Object, Object> pre = events.get(0);
DataRehashedEvent<Object, Object> post = events.get(1);
assertTrue(pre.isPre());
assertEquals(pre.getConsistentHashAtStart(), ch1Node);
// we could get this "intermediate" CH with TopologyChanged events, but this should be enough
assertNotNull(pre.getConsistentHashAtEnd());
assertEquals(pre.getMembersAtEnd(), ch2Nodes.getMembers());
assertFalse(post.isPre());
assertEquals(post.getConsistentHashAtStart(), ch1Node);
assertEquals(post.getConsistentHashAtEnd(), ch2Nodes);
// start a third node and wait for the rebalance to end
addClusterEnabledCacheManager(defaultConfig);
cache(2);
TestingUtil.waitForRehashToComplete(cache(0), cache(1), cache(2));
ConsistentHash ch3Nodes = advancedCache(0).getDistributionManager().getReadConsistentHash();
events = rehashListener.removeEvents();
assertEquals(events.size(), 2);
pre = events.get(0);
post = events.get(1);
assertTrue(pre.isPre());
assertEquals(pre.getConsistentHashAtStart(), ch2Nodes);
// we could get this "intermediate" CH with TopologyChanged events, but this should be enough
assertNotNull(pre.getConsistentHashAtEnd());
assertEquals(pre.getMembersAtEnd(), ch3Nodes.getMembers());
assertFalse(post.isPre());
assertEquals(post.getConsistentHashAtStart(), ch2Nodes);
assertEquals(post.getConsistentHashAtEnd(), ch3Nodes);
// stop cache 2 and wait for the rebalance to end
killMember(2);
// this CH might be different than the CH before the 3rd node joined
ConsistentHash chAfterLeave = advancedCache(0).getDistributionManager().getReadConsistentHash();
events = rehashListener.removeEvents();
assertEquals(events.size(), 2);
pre = events.get(0);
post = events.get(1);
assertTrue(pre.isPre());
// we could get this "intermediate" CH with TopologyChanged events, but this should be enough
assertNotNull(pre.getConsistentHashAtStart());
assertEquals(pre.getMembersAtStart(), chAfterLeave.getMembers());
assertEquals(pre.getConsistentHashAtEnd(), chAfterLeave);
assertFalse(post.isPre());
assertEquals(post.getConsistentHashAtStart(), pre.getConsistentHashAtStart());
assertEquals(post.getConsistentHashAtEnd(), pre.getConsistentHashAtEnd());