Search in sources :

Example 1 with EchoMessageHandler

use of io.netty.test.udt.util.EchoMessageHandler in project netty by netty.

the class UdtNetty method main.

public static void main(final String[] args) throws Exception {
    log.info("init");
    TrafficControl.delay(0);
    final AtomicBoolean isOn = new AtomicBoolean(true);
    final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
    final InetSocketAddress addr2 = UnitHelp.localSocketAddress();
    final ChannelHandler handler1 = new EchoMessageHandler(rate, size);
    final ChannelHandler handler2 = new EchoMessageHandler(null, size);
    final NioEventLoopGroup group1 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup group2 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final Bootstrap peerBoot1 = new Bootstrap();
    peerBoot1.group(group1).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr1).remoteAddress(addr2).handler(handler1);
    final Bootstrap peerBoot2 = new Bootstrap();
    peerBoot2.group(group2).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr2).remoteAddress(addr1).handler(handler2);
    final ChannelFuture peerFuture1 = peerBoot1.connect();
    final ChannelFuture peerFuture2 = peerBoot2.connect();
    CustomReporter.enable(3, TimeUnit.SECONDS);
    Thread.sleep(time);
    isOn.set(false);
    Thread.sleep(1000);
    peerFuture1.channel().close().sync();
    peerFuture2.channel().close().sync();
    Thread.sleep(1000);
    group1.shutdownGracefully();
    group2.shutdownGracefully();
    Metrics.defaultRegistry().shutdown();
    TrafficControl.delay(0);
    log.info("done");
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) EchoMessageHandler(io.netty.test.udt.util.EchoMessageHandler) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandler(io.netty.channel.ChannelHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with EchoMessageHandler

use of io.netty.test.udt.util.EchoMessageHandler in project netty by netty.

the class NioUdtMessageRendezvousChannelTest method basicEcho.

/**
     * verify basic echo message rendezvous
     *
     * FIXME: Re-enable after making it pass on Windows without unncessary tight loop.
     *        https://github.com/netty/netty/issues/2853
     */
@Test(timeout = 10 * 1000)
@Ignore
public void basicEcho() throws Exception {
    final int messageSize = 64 * 1024;
    final int transferLimit = messageSize * 16;
    final Meter rate1 = Metrics.newMeter(NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);
    final Meter rate2 = Metrics.newMeter(NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);
    final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
    final InetSocketAddress addr2 = UnitHelp.localSocketAddress();
    final EchoMessageHandler handler1 = new EchoMessageHandler(rate1, messageSize);
    final EchoMessageHandler handler2 = new EchoMessageHandler(rate2, messageSize);
    final NioEventLoopGroup group1 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup group2 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final Bootstrap boot1 = new Bootstrap();
    boot1.group(group1).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr1).remoteAddress(addr2).handler(handler1);
    final Bootstrap boot2 = new Bootstrap();
    boot2.group(group2).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr2).remoteAddress(addr1).handler(handler2);
    final ChannelFuture connectFuture1 = boot1.connect();
    final ChannelFuture connectFuture2 = boot2.connect();
    while (handler1.meter().count() < transferLimit && handler2.meter().count() < transferLimit) {
        log.info("progress : {} {}", handler1.meter().count(), handler2.meter().count());
        Thread.sleep(1000);
    }
    connectFuture1.channel().close().sync();
    connectFuture2.channel().close().sync();
    log.info("handler1 : {}", handler1.meter().count());
    log.info("handler2 : {}", handler2.meter().count());
    assertTrue(handler1.meter().count() >= transferLimit);
    assertTrue(handler2.meter().count() >= transferLimit);
    assertEquals(handler1.meter().count(), handler2.meter().count());
    group1.shutdownGracefully();
    group2.shutdownGracefully();
    group1.terminationFuture().sync();
    group2.terminationFuture().sync();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Meter(com.yammer.metrics.core.Meter) InetSocketAddress(java.net.InetSocketAddress) EchoMessageHandler(io.netty.test.udt.util.EchoMessageHandler) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)2 ChannelFuture (io.netty.channel.ChannelFuture)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 EchoMessageHandler (io.netty.test.udt.util.EchoMessageHandler)2 InetSocketAddress (java.net.InetSocketAddress)2 Meter (com.yammer.metrics.core.Meter)1 ChannelHandler (io.netty.channel.ChannelHandler)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1