runAllCombinations(new Checker()
{
@Override
public void check(int port, boolean localhost, long batchOutgoingMessagesDelayMillis) throws Throwable
{
final StatsCollector statsCollector = new StatsCollectorFactoryCoda().createStatsCollector(new ClusterId("test", "test-cluster"), new Destination(){});
// we're going to batch no matter what the parameter says.
// We want the batching timeout to be really small
batchOutgoingMessagesDelayMillis = 175;
SenderFactory factory = null;
TcpReceiver adaptor = null;
try
{
//===========================================
// setup the sender and receiver
adaptor = new TcpReceiver(null,getFailFast());
adaptor.setStatsCollector(statsCollector);
StringListener receiver = new StringListener();
adaptor.setListener(receiver);
// we want to keep track of the number of bytes written
final long[] flushByteCounts = new long[4]; //
factory = makeSenderFactory(new BatchingOutputStreamWatcher(flushByteCounts),
statsCollector,batchOutgoingMessagesDelayMillis);
if (port > 0) adaptor.setPort(port);
if (localhost) adaptor.setUseLocalhost(localhost);
//===========================================
adaptor.start(); // start the adaptor
Destination destination = adaptor.getDestination(); // get the destination
// send a message
Sender sender = factory.getSender(destination);
// send messageBytes
byte[] messageBytes = "Hello".getBytes();
int sentBytesPerMessage = messageBytes.length + tcpTransportHeaderSize;
sender.send(messageBytes);
Thread.sleep(batchOutgoingMessagesDelayMillis * 2);
sender.send(messageBytes);
sender.send(messageBytes);
Thread.sleep(batchOutgoingMessagesDelayMillis * 2);
sender.send(messageBytes);
// now numBytesLastFlush should be set to the num of bytes that were last flushed.
// numBytesSent should be the total bytes, even those still in the buffer.
// Therefore numBytesSent - numBytesLastFlush should be the number of bytes waiting.
// These are asserted below.
// we've now sent one message more than what's required to cause a flush.
// wait for it to be received.
for (long endTime = System.currentTimeMillis() + baseTimeoutMillis;
endTime > System.currentTimeMillis() && receiver.numMessages.get() < 4;)
Thread.sleep(1);
Thread.sleep(10);
assertEquals(4,receiver.numMessages.get());
// verify everything came over ok.
assertEquals(1,receiver.receivedStringMessages.size());
assertEquals("Hello",receiver.receivedStringMessages.iterator().next());
assertEquals(sentBytesPerMessage,flushByteCounts[0]);
assertEquals(sentBytesPerMessage * 2,flushByteCounts[1]);
assertEquals(sentBytesPerMessage,flushByteCounts[2]);
assertEquals(0, flushByteCounts[3]);
// verify the histogram
Histogram histogram = ((TcpSender)sender).getBatchingHistogram();
assertEquals(calcMean(1,2,1),histogram.mean(),0.0000001);
assertEquals(3,histogram.count());
}
finally
{
if (factory != null)
factory.stop();
if (adaptor != null)
adaptor.stop();
statsCollector.stop();
}
}
@Override