Search in sources :

Example 1 with NettyChannelHandler

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();
        }
    }
}
Also used : NettyChannelHandler(edu.snu.mist.common.stream.NettyChannelHandler) Channel(io.netty.channel.Channel) NettyTextMessageOutputReceiver(edu.snu.mist.common.stream.textmessage.NettyTextMessageOutputReceiver) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

NettyChannelHandler (edu.snu.mist.common.stream.NettyChannelHandler)1 NettyTextMessageOutputReceiver (edu.snu.mist.common.stream.textmessage.NettyTextMessageOutputReceiver)1 Channel (io.netty.channel.Channel)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1