@Override
public void check(int port, boolean localhost, long batchOutgoingMessagesDelayMillis) throws Throwable
{
SenderFactory factory = null;
TcpReceiver adaptor = null;
BasicStatsCollector statsCollector = new BasicStatsCollector();
try
{
//===========================================
// setup the sender and receiver
adaptor = new TcpReceiver(null,getFailFast());
adaptor.setStatsCollector(statsCollector);
StringListener receiver = new StringListener();
adaptor.setListener(receiver);
factory = makeSenderFactory(false,statsCollector,batchOutgoingMessagesDelayMillis); // not disruptible
if (port > 0) adaptor.setPort(port);
if (localhost) adaptor.setUseLocalhost(localhost);
//===========================================
adaptor.start(); // start the adaptor
Destination destination = adaptor.getDestination(); // get the destination
//===========================================
// Start up sender threads and save the off
ArrayList<Thread> threadsToJoinOn = new ArrayList<Thread>();
SenderRunnable[] senders = new SenderRunnable[numThreads];
for (int i = 0; i < numThreads; i++)
threadsToJoinOn.add(i,new Thread(
senders[i] = new SenderRunnable(destination,i,factory).stopOnNormalFailure(true),
"Test Sender for " + i));
for (Thread thread : threadsToJoinOn)
thread.start();
//===========================================
//===========================================
// Wait until they all send a messgage or two
for (SenderRunnable sender : senders)
for (long endTime = System.currentTimeMillis() + numThreads * baseTimeoutMillis;
endTime > System.currentTimeMillis() && sender.sentMessageCount.get() == 0;) Thread.sleep(1);
// assert that they all sent a message
for (SenderRunnable sender : senders)
assertTrue(sender.sentMessageCount.get() > 0);
//===========================================
// wait until everything's been received.
for (long endTime = System.currentTimeMillis() + numThreads * baseTimeoutMillis;
endTime > System.currentTimeMillis() && receiver.receivedStringMessages.size() != numThreads;)
Thread.sleep(1);
// that the right messages were received will be checked later.
//===========================================
// just to stretch the code coverage, make the listener throw an exception
long numMessages = receiver.numMessages.get();
receiver.throwThisOnce.set(new RuntimeException("Yo!"));
for (long endTime = System.currentTimeMillis() + numThreads * baseTimeoutMillis;
endTime > System.currentTimeMillis() && receiver.numMessages.get() <= numMessages;) Thread.sleep(1);
assertTrue(receiver.numMessages.get() > numMessages);
// now make sure we're still receiving messages by making sure the count is STILL going up.
numMessages = receiver.numMessages.get();
for (long endTime = System.currentTimeMillis() + numThreads * baseTimeoutMillis;
endTime > System.currentTimeMillis() && receiver.numMessages.get() <= numMessages;) Thread.sleep(1);
assertTrue(receiver.numMessages.get() > numMessages);
//===========================================
assertEquals(0L,statsCollector.getMessagesNotSentCount());
// pull the rug out on the adaptor
adaptor.stop();
// wait until the total number of failed messages == 10 * numThreads