Search in sources :

Example 96 with LocalAddress

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

the class SimpleChannelPoolTest method testBoundedChannelPoolSegment.

@Test
public void testBoundedChannelPoolSegment() 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, ChannelHealthChecker.ACTIVE) {

        private final Queue<Channel> queue = new LinkedBlockingQueue<Channel>(1);

        @Override
        protected Channel pollChannel() {
            return queue.poll();
        }

        @Override
        protected boolean offerChannel(Channel ch) {
            return queue.offer(ch);
        }
    };
    Channel channel = pool.acquire().sync().getNow();
    final Channel channel2 = pool.acquire().sync().getNow();
    pool.release(channel).syncUninterruptibly().getNow();
    assertThrows(IllegalStateException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            pool.release(channel2).syncUninterruptibly();
        }
    });
    channel2.close().sync();
    assertEquals(2, handler.channelCount());
    assertEquals(2, handler.acquiredCount());
    assertEquals(1, handler.releasedCount());
    sc.close().sync();
    channel.close().sync();
    channel2.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) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 97 with LocalAddress

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

the class SimpleChannelPoolTest method testCloseAsync.

@Test
public void testCloseAsync() throws Exception {
    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 SimpleChannelPool pool = new SimpleChannelPool(bootstrap, new CountingChannelPoolHandler());
    Channel ch1 = pool.acquire().syncUninterruptibly().getNow();
    Channel ch2 = pool.acquire().syncUninterruptibly().getNow();
    pool.release(ch1).get(1, TimeUnit.SECONDS);
    pool.release(ch2).get(1, TimeUnit.SECONDS);
    // Assert that returned channels are open before close
    assertTrue(ch1.isOpen());
    assertTrue(ch2.isOpen());
    // Close asynchronously with timeout
    pool.closeAsync().get(1, TimeUnit.SECONDS);
    // Assert channels were indeed closed
    assertFalse(ch1.isOpen());
    assertFalse(ch2.isOpen());
    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 98 with LocalAddress

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

the class DefaultChannelPipelineTest method testCancelConnect.

@Test
public void testCancelConnect() {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    group.register(pipeline.channel());
    ChannelPromise promise = pipeline.channel().newPromise();
    assertTrue(promise.cancel(false));
    ChannelFuture future = pipeline.connect(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)

Example 99 with LocalAddress

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

the class AbstractChannelPoolMapTest method testMap.

@Test
public void testMap() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(getLocalAddrId());
    final Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    AbstractChannelPoolMap<EventLoop, SimpleChannelPool> poolMap = new AbstractChannelPoolMap<EventLoop, SimpleChannelPool>() {

        @Override
        protected SimpleChannelPool newPool(EventLoop key) {
            return new SimpleChannelPool(cb.clone(key), new TestChannelPoolHandler());
        }
    };
    EventLoop loop = group.next();
    assertFalse(poolMap.iterator().hasNext());
    assertEquals(0, poolMap.size());
    final SimpleChannelPool pool = poolMap.get(loop);
    assertEquals(1, poolMap.size());
    assertTrue(poolMap.iterator().hasNext());
    assertSame(pool, poolMap.get(loop));
    assertTrue(poolMap.remove(loop));
    assertFalse(poolMap.remove(loop));
    assertFalse(poolMap.iterator().hasNext());
    assertEquals(0, poolMap.size());
    assertThrows(ConnectException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            pool.acquire().syncUninterruptibly();
        }
    });
    poolMap.close();
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) EventLoop(io.netty.channel.EventLoop) Bootstrap(io.netty.bootstrap.Bootstrap) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 100 with LocalAddress

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

the class FixedChannelPoolTest method testCloseAsync.

@Test
public void testCloseAsync() throws ExecutionException, InterruptedException {
    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
    final Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    final FixedChannelPool pool = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
    pool.acquire().get();
    pool.acquire().get();
    final ChannelPromise closePromise = sc.newPromise();
    pool.closeAsync().addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            assertEquals(0, pool.acquiredChannelCount());
            sc.close(closePromise).syncUninterruptibly();
        }
    }).awaitUninterruptibly();
    closePromise.awaitUninterruptibly();
}
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) ChannelPromise(io.netty.channel.ChannelPromise) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Future(io.netty.util.concurrent.Future) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) 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