int count = names.length;
List<FlushTestReceiver> channels = new ArrayList<FlushTestReceiver>(count);
try {
// Create a semaphore and take all its permits
Semaphore semaphore = new Semaphore(count);
semaphore.acquire(count);
// Create channels and their threads that will block on the
// semaphore
boolean first = true;
for (String channelName : names) {
FlushTestReceiver channel = null;
if (first)
channel = new FlushTestReceiver(channelName, semaphore, 0, connectType);
else {
channel = new FlushTestReceiver((JChannel) channels.get(0).getChannel(),
channelName, semaphore, 0, connectType);
}
channels.add(channel);
// Release one ticket at a time to allow the thread to start working
channel.start();
semaphore.release(1);
if (first)
Util.sleep(3000); // minimize changes of a merge happening
first = false;
}
Channel[] tmp = new Channel[channels.size()];
int cnt = 0;
for (FlushTestReceiver receiver : channels)
tmp[cnt++] = receiver.getChannel();
Util.blockUntilViewsReceived(30000, 1000, tmp);
// Reacquire the semaphore tickets; when we have them all
// we know the threads are done
semaphore.tryAcquire(count, 40, TimeUnit.SECONDS);
Util.sleep(1000); //let all events propagate...
for (FlushTestReceiver app : channels)
app.getChannel().setReceiver(null);
for (FlushTestReceiver app : channels)