Example usage for io.netty.channel.embedded EmbeddedChannel inboundMessages

List of usage examples for io.netty.channel.embedded EmbeddedChannel inboundMessages

Introduction

In this page you can find the example usage for io.netty.channel.embedded EmbeddedChannel inboundMessages.

Prototype

Queue inboundMessages

To view the source code for io.netty.channel.embedded EmbeddedChannel inboundMessages.

Click Source Link

Usage

From source file:com.cloudera.livy.client.local.rpc.TestKryoMessageCodec.java

License:Apache License

@Test
public void testEmbeddedChannel() throws Exception {
    EmbeddedChannel c = new EmbeddedChannel(new LoggingHandler(getClass()), new KryoMessageCodec(0));
    c.writeAndFlush(MESSAGE);/*w ww.  ja  v a2  s . co m*/
    assertEquals(1, c.outboundMessages().size());
    assertFalse(MESSAGE.getClass().equals(c.outboundMessages().peek().getClass()));
    c.writeInbound(c.readOutbound());
    assertEquals(1, c.inboundMessages().size());
    assertEquals(MESSAGE, c.readInbound());
    c.close();
}

From source file:com.github.sparkfy.network.ProtocolSuite.java

License:Apache License

private void testServerToClient(Message msg) {
    EmbeddedChannel serverChannel = new EmbeddedChannel(new FileRegionEncoder(), new MessageEncoder());
    serverChannel.writeOutbound(msg);/*from   w w  w . ja  v  a2 s. c o m*/

    EmbeddedChannel clientChannel = new EmbeddedChannel(NettyUtils.createFrameDecoder(), new MessageDecoder());

    while (!serverChannel.outboundMessages().isEmpty()) {
        clientChannel.writeInbound(serverChannel.readOutbound());
    }

    assertEquals(1, clientChannel.inboundMessages().size());
    assertEquals(msg, clientChannel.readInbound());
}

From source file:com.github.sparkfy.network.ProtocolSuite.java

License:Apache License

private void testClientToServer(Message msg) {
    EmbeddedChannel clientChannel = new EmbeddedChannel(new FileRegionEncoder(), new MessageEncoder());
    clientChannel.writeOutbound(msg);//from  ww  w .  ja  va2 s. c  o m

    EmbeddedChannel serverChannel = new EmbeddedChannel(NettyUtils.createFrameDecoder(), new MessageDecoder());

    while (!clientChannel.outboundMessages().isEmpty()) {
        serverChannel.writeInbound(clientChannel.readOutbound());
    }

    assertEquals(1, serverChannel.inboundMessages().size());
    assertEquals(msg, serverChannel.readInbound());
}

From source file:org.apache.spark.network.ProtocolSuite.java

License:Apache License

private void testServerToClient(Message msg) {
    EmbeddedChannel serverChannel = new EmbeddedChannel(new FileRegionEncoder(), MessageEncoder.INSTANCE);
    serverChannel.writeOutbound(msg);/*from   ww w  .  j  av  a2  s .  c  om*/

    EmbeddedChannel clientChannel = new EmbeddedChannel(NettyUtils.createFrameDecoder(),
            MessageDecoder.INSTANCE);

    while (!serverChannel.outboundMessages().isEmpty()) {
        clientChannel.writeOneInbound(serverChannel.readOutbound());
    }

    assertEquals(1, clientChannel.inboundMessages().size());
    assertEquals(msg, clientChannel.readInbound());
}

From source file:org.apache.spark.network.ProtocolSuite.java

License:Apache License

private void testClientToServer(Message msg) {
    EmbeddedChannel clientChannel = new EmbeddedChannel(new FileRegionEncoder(), MessageEncoder.INSTANCE);
    clientChannel.writeOutbound(msg);//w  ww.  java  2s  .com

    EmbeddedChannel serverChannel = new EmbeddedChannel(NettyUtils.createFrameDecoder(),
            MessageDecoder.INSTANCE);

    while (!clientChannel.outboundMessages().isEmpty()) {
        serverChannel.writeOneInbound(clientChannel.readOutbound());
    }

    assertEquals(1, serverChannel.inboundMessages().size());
    assertEquals(msg, serverChannel.readInbound());
}

From source file:reactor.ipc.netty.NettyOutboundTest.java

License:Open Source License

@Test
public void sendFileWithoutTlsUsesFileRegion() throws URISyntaxException, IOException {
    List<Class<?>> messageClasses = new ArrayList<>(2);

    EmbeddedChannel channel = new EmbeddedChannel(new MessageToMessageEncoder<FileRegion>() {

        @Override/*from  ww  w. j a v a 2 s .  co  m*/
        protected void encode(ChannelHandlerContext ctx, FileRegion msg, List<Object> out) throws Exception {
            ByteArrayOutputStream bais = new ByteArrayOutputStream();
            WritableByteChannel wbc = Channels.newChannel(bais);

            msg.transferTo(wbc, msg.position());
            out.add(new String(bais.toByteArray()));
        }
    }, new MessageToMessageEncoder<Object>() {
        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
            messageClasses.add(msg.getClass());
            ReferenceCountUtil.retain(msg);
            out.add(msg);
        }
    });
    NettyContext mockContext = () -> channel;
    NettyOutbound outbound = new NettyOutbound() {
        @Override
        public NettyContext context() {
            return mockContext;
        }

        @Override
        public FileChunkedStrategy getFileChunkedStrategy() {
            return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
        }
    };
    channel.writeOneOutbound(1);

    outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then().block();

    assertThat(channel.inboundMessages()).isEmpty();
    assertThat(channel.outboundMessages()).hasSize(2);
    assertThat(messageClasses).containsExactly(Integer.class, DefaultFileRegion.class);

    assertThat(channel.outboundMessages()).element(1).asString().startsWith(
            "This is an UTF-8 file that is larger than 1024 bytes.\nIt contains accents like .\nGARBAGE")
            .endsWith("GARBAGE\nEnd of File");
}

From source file:reactor.ipc.netty.NettyOutboundTest.java

License:Open Source License

@Test
public void sendFileWithForceChunkedFileUsesStrategyChunks()
        throws URISyntaxException, NoSuchAlgorithmException, IOException {
    List<Class<?>> messageWritten = new ArrayList<>(2);
    EmbeddedChannel channel = new EmbeddedChannel(
            //outbound: pipeline reads inverted
            //transform the ByteBuf chunks into Strings:
            new MessageToMessageEncoder<ByteBuf>() {
                @Override/*from  w  w  w.ja  v  a  2  s  . com*/
                protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
                        throws Exception {
                    out.add(msg.toString(CharsetUtil.UTF_8));
                }
            },
            //transform the ChunkedFile into ByteBuf chunks:
            new ChunkedWriteHandler(),
            //helps to ensure a ChunkedFile was written outs
            new MessageToMessageEncoder<Object>() {
                @Override
                protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out)
                        throws Exception {
                    messageWritten.add(msg.getClass());
                    out.add(msg);
                }
            });
    NettyContext mockContext = () -> channel;
    NettyOutbound outbound = new NettyOutbound() {
        @Override
        public NettyContext context() {
            return mockContext;
        }

        @Override
        public FileChunkedStrategy getFileChunkedStrategy() {
            return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
        }
    };
    Path path = Paths.get(getClass().getResource("/largeFile.txt").getFile());

    channel.writeOneOutbound(1);
    outbound.sendFileChunked(path, 0, Files.size(path)).then().block();

    assertThat(channel.inboundMessages()).isEmpty();
    assertThat(messageWritten).containsExactly(Integer.class, ChunkedFile.class);

    assertThat(channel.outboundMessages()).hasSize(3).element(1).asString().startsWith(
            "This is an UTF-8 file that is larger than 1024 bytes.\nIt contains accents like .\nGARBAGE")
            .endsWith("1024 mark here ->");

    assertThat(channel.outboundMessages()).last().asString().startsWith("<- 1024 mark here")
            .endsWith("End of File");
}