Search in sources :

Example 61 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project netty by netty.

the class FixedChannelPoolTest method testReleaseDifferentPool.

@Test(expected = IllegalArgumentException.class)
public void testReleaseDifferentPool() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    ChannelPoolHandler handler = new TestChannelPoolHandler();
    ChannelPool pool = new FixedChannelPool(cb, handler, 1, 1);
    ChannelPool pool2 = new FixedChannelPool(cb, handler, 1, 1);
    Channel channel = pool.acquire().syncUninterruptibly().getNow();
    try {
        pool2.release(channel).syncUninterruptibly();
    } finally {
        sc.close().syncUninterruptibly();
        channel.close().syncUninterruptibly();
        group.shutdownGracefully();
    }
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 62 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project netty by netty.

the class FixedChannelPoolTest method testReleaseAfterClosePool.

@Test
public void testReleaseAfterClosePool() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup(1);
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    FixedChannelPool pool = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
    final Future<Channel> acquire = pool.acquire();
    final Channel channel = acquire.get();
    pool.close();
    group.submit(new Runnable() {

        @Override
        public void run() {
        // NOOP
        }
    }).syncUninterruptibly();
    pool.release(channel).syncUninterruptibly();
    sc.close().syncUninterruptibly();
    channel.close().syncUninterruptibly();
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 63 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project netty by netty.

the class FixedChannelPoolTest method testAcquire.

@Test
public void testAcquire() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    CountingChannelPoolHandler handler = new CountingChannelPoolHandler();
    ChannelPool pool = new FixedChannelPool(cb, handler, 1, Integer.MAX_VALUE);
    Channel channel = pool.acquire().syncUninterruptibly().getNow();
    Future<Channel> future = pool.acquire();
    assertFalse(future.isDone());
    pool.release(channel).syncUninterruptibly();
    assertTrue(future.await(1, TimeUnit.SECONDS));
    Channel channel2 = future.getNow();
    assertSame(channel, channel2);
    assertEquals(1, handler.channelCount());
    assertEquals(1, handler.acquiredCount());
    assertEquals(1, handler.releasedCount());
    sc.close().syncUninterruptibly();
    channel2.close().syncUninterruptibly();
    group.shutdownGracefully();
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 64 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project netty by netty.

the class FixedChannelPoolTest method testAcquireTimeout.

@Test(expected = TimeoutException.class)
public void testAcquireTimeout() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    ChannelPoolHandler handler = new TestChannelPoolHandler();
    ChannelPool pool = new FixedChannelPool(cb, handler, ChannelHealthChecker.ACTIVE, AcquireTimeoutAction.FAIL, 500, 1, Integer.MAX_VALUE);
    Channel channel = pool.acquire().syncUninterruptibly().getNow();
    Future<Channel> future = pool.acquire();
    try {
        future.syncUninterruptibly();
    } finally {
        sc.close().syncUninterruptibly();
        channel.close().syncUninterruptibly();
        group.shutdownGracefully();
    }
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 65 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project netty by netty.

the class LocalChannelTest method testLocalAddressReuse.

@Test
public void testLocalAddressReuse() throws Exception {
    for (int i = 0; i < 2; i++) {
        Bootstrap cb = new Bootstrap();
        ServerBootstrap sb = new ServerBootstrap();
        cb.group(group1).channel(LocalChannel.class).handler(new TestHandler());
        sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

            @Override
            public void initChannel(LocalChannel ch) throws Exception {
                ch.pipeline().addLast(new TestHandler());
            }
        });
        Channel sc = null;
        Channel cc = null;
        try {
            // Start server
            sc = sb.bind(TEST_ADDRESS).sync().channel();
            final CountDownLatch latch = new CountDownLatch(1);
            // Connect to the server
            cc = cb.connect(sc.localAddress()).sync().channel();
            final Channel ccCpy = cc;
            cc.eventLoop().execute(new Runnable() {

                @Override
                public void run() {
                    // Send a message event up the pipeline.
                    ccCpy.pipeline().fireChannelRead("Hello, World");
                    latch.countDown();
                }
            });
            assertTrue(latch.await(5, SECONDS));
            // Close the channel
            closeChannel(cc);
            closeChannel(sc);
            sc.closeFuture().sync();
            assertNull(String.format("Expected null, got channel '%s' for local address '%s'", LocalChannelRegistry.get(TEST_ADDRESS), TEST_ADDRESS), LocalChannelRegistry.get(TEST_ADDRESS));
        } finally {
            closeChannel(cc);
            closeChannel(sc);
        }
    }
}
Also used : AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)177 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)87 Channel (io.netty.channel.Channel)84 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)80 EventLoopGroup (io.netty.channel.EventLoopGroup)73 ChannelFuture (io.netty.channel.ChannelFuture)63 Bootstrap (io.netty.bootstrap.Bootstrap)62 Test (org.junit.Test)59 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)48 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)45 InetSocketAddress (java.net.InetSocketAddress)40 SocketChannel (io.netty.channel.socket.SocketChannel)37 LoggingHandler (io.netty.handler.logging.LoggingHandler)36 ChannelPipeline (io.netty.channel.ChannelPipeline)34 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)29 CountDownLatch (java.util.concurrent.CountDownLatch)28 ClosedChannelException (java.nio.channels.ClosedChannelException)27 LocalAddress (io.netty.channel.local.LocalAddress)25 SslContext (io.netty.handler.ssl.SslContext)24 LocalChannel (io.netty.channel.local.LocalChannel)23