Search in sources :

Example 66 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class SmtpResponseDecoderTest method testDecodeOneLineResponseChunked.

@Test
public void testDecodeOneLineResponseChunked() {
    EmbeddedChannel channel = newChannel();
    assertFalse(channel.writeInbound(newBuffer("200 Ok")));
    assertTrue(channel.writeInbound(newBuffer("\r\n")));
    assertTrue(channel.finish());
    SmtpResponse response = channel.readInbound();
    assertEquals(200, response.code());
    List<CharSequence> sequences = response.details();
    assertEquals(1, sequences.size());
    assertEquals("Ok", sequences.get(0).toString());
    assertNull(channel.readInbound());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 67 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class CombinedChannelDuplexHandlerTest method testOutboundEvents.

@Test
public void testOutboundEvents() {
    final Queue<Event> queue = new ArrayDeque<Event>();
    ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter();
    ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_ADDED);
        }

        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_REMOVED);
        }

        @Override
        public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            queue.add(Event.BIND);
        }

        @Override
        public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            queue.add(Event.CONNECT);
        }

        @Override
        public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.DISCONNECT);
        }

        @Override
        public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.CLOSE);
        }

        @Override
        public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.DEREGISTER);
        }

        @Override
        public void read(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.READ);
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            queue.add(Event.WRITE);
        }

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.FLUSH);
        }
    };
    CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler = new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(inboundHandler, outboundHandler);
    EmbeddedChannel channel = new EmbeddedChannel();
    channel.pipeline().addFirst(handler);
    doOutboundOperations(channel);
    assertEquals(Event.HANDLER_ADDED, queue.poll());
    assertEquals(Event.BIND, queue.poll());
    assertEquals(Event.CONNECT, queue.poll());
    assertEquals(Event.WRITE, queue.poll());
    assertEquals(Event.FLUSH, queue.poll());
    assertEquals(Event.READ, queue.poll());
    assertEquals(Event.CLOSE, queue.poll());
    assertEquals(Event.CLOSE, queue.poll());
    assertEquals(Event.DEREGISTER, queue.poll());
    handler.removeOutboundHandler();
    assertEquals(Event.HANDLER_REMOVED, queue.poll());
    // These should not be handled by the inboundHandler anymore as it was removed before
    doOutboundOperations(channel);
    // Should have not received any more events as it was removed before via removeInboundHandler()
    assertTrue(queue.isEmpty());
    assertTrue(channel.finish());
    assertTrue(queue.isEmpty());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ArrayDeque(java.util.ArrayDeque) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 68 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class CombinedChannelDuplexHandlerTest method testPromisesPassed.

@Test(timeout = 3000)
public void testPromisesPassed() {
    ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {

        @Override
        public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(outboundHandler, new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(new ChannelInboundHandlerAdapter(), new ChannelOutboundHandlerAdapter()));
    ChannelPipeline pipeline = ch.pipeline();
    ChannelPromise promise = ch.newPromise();
    pipeline.connect(new InetSocketAddress(0), null, promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.bind(new InetSocketAddress(0), promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.close(promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.disconnect(promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.write("test", promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.deregister(promise);
    promise.syncUninterruptibly();
    ch.finish();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 69 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class CombinedChannelDuplexHandlerTest method testInboundEvents.

@Test
public void testInboundEvents() {
    final Queue<Event> queue = new ArrayDeque<Event>();
    ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_ADDED);
        }

        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_REMOVED);
        }

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.REGISTERED);
        }

        @Override
        public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.UNREGISTERED);
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.ACTIVE);
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.INACTIVE);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            queue.add(Event.CHANNEL_READ);
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.CHANNEL_READ_COMPLETE);
        }

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            queue.add(Event.USER_EVENT_TRIGGERED);
        }

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.CHANNEL_WRITABILITY_CHANGED);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            queue.add(Event.EXCEPTION_CAUGHT);
        }
    };
    CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler = new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(inboundHandler, new ChannelOutboundHandlerAdapter());
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.pipeline().fireChannelWritabilityChanged();
    channel.pipeline().fireUserEventTriggered(MSG);
    channel.pipeline().fireChannelRead(MSG);
    channel.pipeline().fireChannelReadComplete();
    assertEquals(Event.HANDLER_ADDED, queue.poll());
    assertEquals(Event.REGISTERED, queue.poll());
    assertEquals(Event.ACTIVE, queue.poll());
    assertEquals(Event.CHANNEL_WRITABILITY_CHANGED, queue.poll());
    assertEquals(Event.USER_EVENT_TRIGGERED, queue.poll());
    assertEquals(Event.CHANNEL_READ, queue.poll());
    assertEquals(Event.CHANNEL_READ_COMPLETE, queue.poll());
    handler.removeInboundHandler();
    assertEquals(Event.HANDLER_REMOVED, queue.poll());
    // These should not be handled by the inboundHandler anymore as it was removed before
    channel.pipeline().fireChannelWritabilityChanged();
    channel.pipeline().fireUserEventTriggered(MSG);
    channel.pipeline().fireChannelRead(MSG);
    channel.pipeline().fireChannelReadComplete();
    // Should have not received any more events as it was removed before via removeInboundHandler()
    assertTrue(queue.isEmpty());
    assertTrue(channel.finish());
    assertTrue(queue.isEmpty());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 70 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class ChannelInitializerTest method testAddChannelInitializer.

private static void testAddChannelInitializer(final boolean first) {
    final AtomicBoolean called = new AtomicBoolean();
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelHandler handler = new ChannelInitializer<Channel>() {

                @Override
                protected void initChannel(Channel ch) throws Exception {
                    called.set(true);
                }
            };
            if (first) {
                ch.pipeline().addFirst(handler);
            } else {
                ch.pipeline().addLast(handler);
            }
        }
    });
    channel.finish();
    assertTrue(called.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocalServerChannel(io.netty.channel.local.LocalServerChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)446 Test (org.junit.Test)364 ByteBuf (io.netty.buffer.ByteBuf)162 HttpResponse (io.netty.handler.codec.http.HttpResponse)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)28 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)25 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 HttpRequest (io.netty.handler.codec.http.HttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 InetSocketAddress (java.net.InetSocketAddress)17 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)15 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)14 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)11 ArrayList (java.util.ArrayList)11 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)10 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)10 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)10 UUID (java.util.UUID)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7