}
protected void testhelper(boolean useDispatcher) throws Throwable {
final int count=8;
final ExecutorService executor=Executors.newFixedThreadPool(count);
final CountDownLatch latch=new CountDownLatch(count);
final JChannel[] channels=new JChannel[count];
final Task[] tasks=new Task[count];
final long start=System.currentTimeMillis();
for(int i=0;i < count;i++) {
if(i == 0)
channels[i]=createChannel(true, count);
else
channels[i]=createChannel(channels[0]);
tasks[i]=new Task(latch, channels[i],useDispatcher);
changeMergeInterval(channels[i]);
changeViewBundling(channels[i]);
replaceDiscoveryProtocol(channels[i]);
}
for(final Task t:tasks) {
executor.execute(t);
}
int timeoutToConverge=120;
boolean successConnecting = false;
try {
// Wait for all channels to finish connecting
successConnecting=latch.await(timeoutToConverge, TimeUnit.SECONDS);
if(successConnecting) {
log.info("All connected. Converging...");
for(Task t:tasks) {
Throwable ex=t.getException();
if(ex != null)
throw ex;
}
// Wait for all channels to have the correct number of members in their
// current view
boolean converged=false;
for(int counter=0;counter < timeoutToConverge && !converged;SECONDS.sleep(1),counter++) {
for(final JChannel channel:channels) {
converged=channel.getView() != null && channel.getView().size() == count;
if(!converged)
break;
}
}
final long duration=System.currentTimeMillis() - start;
log.info("Converged to a single group after " + duration
+ " ms; group is:\n");
for(int i=0;i < channels.length;i++) {
log.info("#" + (i + 1)
+ ": "
+ channels[i].getAddress()
+ ": "
+ channels[i].getView());
}
}
for(final JChannel channel:channels) {
assertEquals("View ok for channel " + channel.getAddress(), count, channel.getView().size());
}
assertTrue("All channels were succefully connected",successConnecting);
}
finally {
Util.sleep(2500);
executor.shutdownNow();
log.info("closing channels: ");
for(JChannel ch:channels) {
ch.close();
//there are sometimes big delays until entire cluster shuts down
//use sleep to make a smoother shutdown so we avoid false positives