}
public void testStreamData() throws Exception
{
final LargeMessageBufferImpl outBuffer = new LargeMessageBufferImpl(new FakeConsumerInternal(),
1024 * 11 + 123,
1);
final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);
final AtomicInteger errors = new AtomicInteger(0);
// Done reading 3 elements
final CountDownLatch done1 = new CountDownLatch(1);
// Done with the thread
final CountDownLatch done2 = new CountDownLatch(1);
final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger totalBytes = new AtomicInteger(0);
Thread treader = new Thread("treader")
{
@Override
public void run()
{
try
{
byte line[] = new byte[1024];
int dataRead = 0;
while (dataRead >= 0)
{
dataRead = input.read(line);
if (dataRead > 0)
{
System.out.println("Read one line with " + dataRead + " bytes");
totalBytes.addAndGet(dataRead);
if (count.incrementAndGet() == 3)
{
done1.countDown();
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
errors.incrementAndGet();
}
finally
{
done1.countDown();
done2.countDown();
}
}
};
treader.setDaemon(true);
treader.start();
for (int i = 0; i < 3; i++)
{
outBuffer.addPacket(new FakePacket(-1, new byte[1024], true, false));
}
outBuffer.setOutputStream(output);
final CountDownLatch waiting = new CountDownLatch(1);
Thread twaiter = new Thread("twaiter")
{
@Override
public void run()
{
try
{
outBuffer.waitCompletion(0);
waiting.countDown();
}
catch (Exception e)
{
e.printStackTrace();
errors.incrementAndGet();
}
}
};
twaiter.setDaemon(true);
twaiter.start();
Assert.assertTrue(done1.await(10, TimeUnit.SECONDS));
Assert.assertEquals(3, count.get());
Assert.assertEquals(1024 * 3, totalBytes.get());
for (int i = 0; i < 8; i++)
{
outBuffer.addPacket(new FakePacket(-1, new byte[1024], true, false));
}
Assert.assertEquals(1, waiting.getCount());
outBuffer.addPacket(new FakePacket(-1, new byte[123], false, false));
Assert.assertTrue(done2.await(10, TimeUnit.SECONDS));
Assert.assertTrue(waiting.await(10, TimeUnit.SECONDS));