Search in sources :

Example 36 with LocalChannel

use of io.netty.channel.local.LocalChannel 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 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 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);
    expectedException.expect(IllegalStateException.class);
    channel1.close().syncUninterruptibly();
    try {
        pool.release(channel1).syncUninterruptibly();
    } finally {
        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) ExpectedException(org.junit.rules.ExpectedException) EventLoopGroup(io.netty.channel.EventLoopGroup) 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 37 with LocalChannel

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

the class SingleThreadEventLoopTest method testRegistrationAfterShutdown2.

@Test(timeout = 10000)
@SuppressWarnings("deprecation")
public void testRegistrationAfterShutdown2() throws Exception {
    loopA.shutdown();
    final CountDownLatch latch = new CountDownLatch(1);
    Channel ch = new LocalChannel();
    ChannelPromise promise = ch.newPromise();
    promise.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            latch.countDown();
        }
    });
    // Disable logging temporarily.
    Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    List<Appender<ILoggingEvent>> appenders = new ArrayList<Appender<ILoggingEvent>>();
    for (Iterator<Appender<ILoggingEvent>> i = root.iteratorForAppenders(); i.hasNext(); ) {
        Appender<ILoggingEvent> a = i.next();
        appenders.add(a);
        root.detachAppender(a);
    }
    try {
        ChannelFuture f = loopA.register(promise);
        f.awaitUninterruptibly();
        assertFalse(f.isSuccess());
        assertThat(f.cause(), is(instanceOf(RejectedExecutionException.class)));
        // Ensure the listener was notified.
        assertFalse(latch.await(1, TimeUnit.SECONDS));
        assertFalse(ch.isOpen());
    } finally {
        for (Appender<ILoggingEvent> a : appenders) {
            root.addAppender(a);
        }
    }
}
Also used : Appender(ch.qos.logback.core.Appender) LocalChannel(io.netty.channel.local.LocalChannel) LocalChannel(io.netty.channel.local.LocalChannel) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 38 with LocalChannel

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

the class SingleThreadEventLoopTest method testRegistrationAfterShutdown.

@Test(timeout = 10000)
@SuppressWarnings("deprecation")
public void testRegistrationAfterShutdown() throws Exception {
    loopA.shutdown();
    // Disable logging temporarily.
    Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    List<Appender<ILoggingEvent>> appenders = new ArrayList<Appender<ILoggingEvent>>();
    for (Iterator<Appender<ILoggingEvent>> i = root.iteratorForAppenders(); i.hasNext(); ) {
        Appender<ILoggingEvent> a = i.next();
        appenders.add(a);
        root.detachAppender(a);
    }
    try {
        ChannelFuture f = loopA.register(new LocalChannel());
        f.awaitUninterruptibly();
        assertFalse(f.isSuccess());
        assertThat(f.cause(), is(instanceOf(RejectedExecutionException.class)));
        assertFalse(f.channel().isOpen());
    } finally {
        for (Appender<ILoggingEvent> a : appenders) {
            root.addAppender(a);
        }
    }
}
Also used : Appender(ch.qos.logback.core.Appender) LocalChannel(io.netty.channel.local.LocalChannel) ArrayList(java.util.ArrayList) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.Test)

Example 39 with LocalChannel

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

the class BaseChannelTest method getLocalServerBootstrap.

ServerBootstrap getLocalServerBootstrap() {
    EventLoopGroup serverGroup = new DefaultEventLoopGroup();
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(serverGroup);
    sb.channel(LocalServerChannel.class);
    sb.childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
        }
    });
    return sb;
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 40 with LocalChannel

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

the class DefaultChannelPipelineTest method testCancelWrite.

@Test
public void testCancelWrite() throws Exception {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    group.register(pipeline.channel());
    ChannelPromise promise = pipeline.channel().newPromise();
    assertTrue(promise.cancel(false));
    ByteBuf buffer = Unpooled.buffer();
    assertEquals(1, buffer.refCnt());
    ChannelFuture future = pipeline.write(buffer, promise);
    assertTrue(future.isCancelled());
    assertEquals(0, buffer.refCnt());
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

LocalChannel (io.netty.channel.local.LocalChannel)53 Test (org.junit.Test)49 LocalServerChannel (io.netty.channel.local.LocalServerChannel)16 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)14 LocalAddress (io.netty.channel.local.LocalAddress)14 Bootstrap (io.netty.bootstrap.Bootstrap)13 Channel (io.netty.channel.Channel)12 EventLoopGroup (io.netty.channel.EventLoopGroup)12 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)11 LocalEventLoopGroup (io.netty.channel.local.LocalEventLoopGroup)11 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)8 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)7 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 TimeoutException (java.util.concurrent.TimeoutException)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 ExpectedException (org.junit.rules.ExpectedException)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 OioSocketChannel (io.netty.channel.socket.oio.OioSocketChannel)3