public static void waitForRehashToComplete(Cache... caches) {
int gracetime = 90000; // 90 seconds
long giveup = System.currentTimeMillis() + gracetime;
for (Cache c : caches) {
StateTransferManager stateTransferManager = extractComponent(c, StateTransferManager.class);
DefaultRebalancePolicy rebalancePolicy = (DefaultRebalancePolicy) TestingUtil.extractGlobalComponent(c.getCacheManager(), RebalancePolicy.class);
Address cacheAddress = c.getAdvancedCache().getRpcManager().getAddress();
while (true) {
CacheTopology cacheTopology = stateTransferManager.getCacheTopology();
boolean rebalanceInProgress = stateTransferManager.isStateTransferInProgress();
boolean chIsBalanced = !rebalanceInProgress && rebalancePolicy.isBalanced(cacheTopology.getCurrentCH());
boolean chContainsAllMembers = cacheTopology.getCurrentCH().getMembers().size() == caches.length;
if (chIsBalanced && chContainsAllMembers)
break;
if (System.currentTimeMillis() > giveup) {