use of edu.snu.mist.common.stream.NettyChannelHandler in project mist by snuspl.
the class NettyTextSinkTest method testNettyTextSink.
/**
* Test whether the created sinks by NettyTextSinkFactory send data stream correctly to output receiver.
* It creates 4 sinks and send outputs to output receiver.
* @throws Exception
*/
@Test(timeout = 4000L)
public void testNettyTextSink() throws Exception {
final int numSinks = 4;
final List<String> outputStream = Arrays.asList("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "In in leo nec erat fringilla mattis eu non massa.", "Cras quis diam suscipit, commodo enim id, pulvinar nunc.");
final CountDownLatch countDownLatch = new CountDownLatch(numSinks * outputStream.size());
final CountDownLatch channelCountDown = new CountDownLatch(numSinks);
final Map<Channel, List<String>> channelListMap = new ConcurrentHashMap<>();
final NettyChannelHandler channelHandler = new TestReceiverChannelHandler(channelCountDown, countDownLatch, channelListMap);
// Create output receiver
try (final NettyTextMessageOutputReceiver outputReceiver = new NettyTextMessageOutputReceiver(SERVER_ADDR, SERVER_PORT, channelHandler)) {
// create netty sinks
final List<Sink<String>> sinks = new LinkedList<>();
// Create sinks
for (int i = 0; i < numSinks; i++) {
final Sink<String> sink = new NettyTextSink(SERVER_ADDR, SERVER_PORT, nettySharedResource, stringIdentifierFactory);
sinks.add(sink);
}
// Wait until all sinks connect to output receiver
channelCountDown.await();
outputStream.forEach((output) -> {
for (final Sink<String> sink : sinks) {
sink.handle(output);
}
});
// Wait until all data are sent to source
countDownLatch.await();
for (List<String> received : channelListMap.values()) {
Assert.assertEquals(outputStream, received);
}
// Closes
for (final Sink<String> sink : sinks) {
sink.close();
}
}
}
Aggregations