runAllCombinations(new Checker()
{
@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);
factory = makeSenderFactory(true,statsCollector, batchOutgoingMessagesDelayMillis); // distruptible sender factory
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),"Test Sender for " + i));
for (Thread thread : threadsToJoinOn)
thread.start();
//===========================================
//===========================================
// check that one sender has failed since this is disruptable.
assertTrue(TestUtils.poll(baseTimeoutMillis, statsCollector, new TestUtils.Condition<BasicStatsCollector>()
{
@Override
public boolean conditionMet(BasicStatsCollector o) throws Throwable
{
return o.getMessagesNotSentCount() > 0;
}
}));
//===========================================
//===========================================
// check that ONLY one failed (the others didn't) when we're not batching. Otherwise
// more may fail.
Thread.sleep(10);
if (batchOutgoingMessagesDelayMillis >= 0) // if we're batching then we only expect a failure but we don't know how many
assertTrue(statsCollector.getMessagesNotSentCount() > 0);
else
assertEquals(1,statsCollector.getMessagesNotSentCount());
//===========================================
// all of the counts should increase.
long[] curCounts = new long[numThreads];
int i = 0;
for (SenderRunnable sender : senders)
curCounts[i++] = sender.sentMessageCount.get();
// ==========================================
// go until they are all higher. The Senders should still be running
// and sending successfully (only one failed once) so all counts should
// be increasing.
boolean allHigher = false;
for (long endTime = System.currentTimeMillis() + numThreads * (baseTimeoutMillis); endTime > System.currentTimeMillis() && !allHigher;)
{
allHigher = true;
Thread.sleep(1);
i = 0;
for (SenderRunnable sender : senders)
if (curCounts[i++] >= sender.sentMessageCount.get())
allHigher = false;
}
assertTrue(allHigher);
// ==========================================
// ==========================================
// Stop the senders.
for (SenderRunnable sender : senders)
sender.keepGoing.set(false);
// wait until all threads are stopped
for (Thread t : threadsToJoinOn)
t.join(5000);
// make sure everything actually stopped.
for (SenderRunnable sender : senders)
assertTrue(sender.isStopped.get());
// ==========================================
}
finally
{
if (factory != null)
factory.stop();
if (adaptor != null)
adaptor.stop();
}
}