Search in sources :

Example 41 with LocalAddress

use of io.netty.channel.local.LocalAddress in project netty by netty.

the class SimpleChannelPoolTest method testChannelAcquiredException.

@Test
public void testChannelAcquiredException() throws InterruptedException {
    final LocalAddress addr = new LocalAddress(getLocalAddrId());
    final EventLoopGroup group = new DefaultEventLoopGroup();
    // Start server
    final ServerBootstrap sb = new ServerBootstrap().group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        protected void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    final Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    // Create pool, acquire and return channels
    final Bootstrap bootstrap = new Bootstrap().channel(LocalChannel.class).group(group).remoteAddress(addr);
    final NullPointerException exception = new NullPointerException();
    final SimpleChannelPool pool = new SimpleChannelPool(bootstrap, new ChannelPoolHandler() {

        @Override
        public void channelReleased(Channel ch) {
        }

        @Override
        public void channelAcquired(Channel ch) {
            throw exception;
        }

        @Override
        public void channelCreated(Channel ch) {
        }
    });
    try {
        pool.acquire().sync();
    } catch (NullPointerException e) {
        assertSame(e, exception);
    }
    sc.close().sync();
    pool.close();
    group.shutdownGracefully();
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 42 with LocalAddress

use of io.netty.channel.local.LocalAddress in project netty by netty.

the class SimpleChannelPoolTest method testAcquire.

@Test
public void testAcquire() throws Exception {
    EventLoopGroup group = new DefaultEventLoopGroup();
    LocalAddress addr = new LocalAddress(getLocalAddrId());
    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).sync().channel();
    CountingChannelPoolHandler handler = new CountingChannelPoolHandler();
    final ChannelPool pool = new SimpleChannelPool(cb, handler);
    Channel channel = pool.acquire().sync().getNow();
    pool.release(channel).syncUninterruptibly();
    final Channel channel2 = pool.acquire().sync().getNow();
    assertSame(channel, channel2);
    assertEquals(1, handler.channelCount());
    pool.release(channel2).syncUninterruptibly();
    // Should fail on multiple release calls.
    assertThrows(IllegalArgumentException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            pool.release(channel2).syncUninterruptibly();
        }
    });
    assertFalse(channel.isActive());
    assertEquals(2, handler.acquiredCount());
    assertEquals(2, handler.releasedCount());
    sc.close().sync();
    pool.close();
    group.shutdownGracefully();
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Executable(org.junit.jupiter.api.function.Executable) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 43 with LocalAddress

use of io.netty.channel.local.LocalAddress in project netty by netty.

the class SimpleChannelPoolTest method testUnhealthyChannelIsOfferedWhenNoHealthCheckRequested.

/**
 * Tests that if channel was unhealthy it is was offered back to the pool because
 * it was requested not to validate channel health on release.
 *
 * @throws Exception
 */
@Test
public void testUnhealthyChannelIsOfferedWhenNoHealthCheckRequested() throws Exception {
    EventLoopGroup group = new DefaultEventLoopGroup();
    LocalAddress addr = new LocalAddress(getLocalAddrId());
    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 CountingChannelPoolHandler();
    ChannelPool pool = new SimpleChannelPool(cb, handler, ChannelHealthChecker.ACTIVE, false);
    Channel channel1 = pool.acquire().syncUninterruptibly().getNow();
    channel1.close().syncUninterruptibly();
    Future<Void> releaseFuture = pool.release(channel1, channel1.eventLoop().<Void>newPromise()).syncUninterruptibly();
    assertThat(releaseFuture.isSuccess(), CoreMatchers.is(true));
    Channel channel2 = pool.acquire().syncUninterruptibly().getNow();
    // verifying that in fact the channel2 is different that means is not pulled from the pool
    assertNotSame(channel1, channel2);
    sc.close().syncUninterruptibly();
    channel2.close().syncUninterruptibly();
    pool.close();
    group.shutdownGracefully();
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 44 with LocalAddress

use of io.netty.channel.local.LocalAddress in project netty by netty.

the class SimpleChannelPoolTest method testUnhealthyChannelIsNotOffered.

/**
 * Tests that if channel was unhealthy it is not offered back to the pool.
 *
 * @throws Exception
 */
@Test
public void testUnhealthyChannelIsNotOffered() throws Exception {
    EventLoopGroup group = new DefaultEventLoopGroup();
    LocalAddress addr = new LocalAddress(getLocalAddrId());
    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 CountingChannelPoolHandler();
    ChannelPool pool = new SimpleChannelPool(cb, handler);
    Channel channel1 = pool.acquire().syncUninterruptibly().getNow();
    pool.release(channel1).syncUninterruptibly();
    Channel channel2 = pool.acquire().syncUninterruptibly().getNow();
    // first check that when returned healthy then it actually offered back to the pool.
    assertSame(channel1, channel2);
    channel1.close().syncUninterruptibly();
    pool.release(channel1).syncUninterruptibly();
    Channel channel3 = pool.acquire().syncUninterruptibly().getNow();
    // channel1 was not healthy anymore so it should not get acquired anymore.
    assertNotSame(channel1, channel3);
    sc.close().syncUninterruptibly();
    channel3.close().syncUninterruptibly();
    pool.close();
    group.shutdownGracefully();
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 45 with LocalAddress

use of io.netty.channel.local.LocalAddress in project netty by netty.

the class DefaultChannelPipelineTest method testCancelBind.

// Tests for https://github.com/netty/netty/issues/2349
@Test
public void testCancelBind() {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    group.register(pipeline.channel());
    ChannelPromise promise = pipeline.channel().newPromise();
    assertTrue(promise.cancel(false));
    ChannelFuture future = pipeline.bind(new LocalAddress("test"), promise);
    assertTrue(future.isCancelled());
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) Test(org.junit.jupiter.api.Test)

Aggregations

LocalAddress (io.netty.channel.local.LocalAddress)116 Bootstrap (io.netty.bootstrap.Bootstrap)66 LocalChannel (io.netty.channel.local.LocalChannel)66 LocalServerChannel (io.netty.channel.local.LocalServerChannel)66 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)63 Channel (io.netty.channel.Channel)61 Test (org.junit.Test)47 Test (org.junit.jupiter.api.Test)39 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)31 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)28 ConnectException (java.net.ConnectException)27 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)27 Subscription (rx.Subscription)27 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)26 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)26 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)25 ChannelFuture (io.netty.channel.ChannelFuture)24 SSLException (javax.net.ssl.SSLException)22