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

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

Introduction

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

Prototype

Queue outboundMessages

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

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);/*from w  w  w .  j a  va  2 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.cloudera.livy.client.local.rpc.TestRpc.java

License:Apache License

private void transfer(Rpc serverRpc, Rpc clientRpc) {
    EmbeddedChannel client = (EmbeddedChannel) clientRpc.getChannel();
    EmbeddedChannel server = (EmbeddedChannel) serverRpc.getChannel();

    int count = 0;
    while (!client.outboundMessages().isEmpty()) {
        server.writeInbound(client.readOutbound());
        count++;/*from  ww  w  .  jav a 2  s .  c  om*/
    }
    server.flush();
    LOG.debug("Transferred {} outbound client messages.", count);

    count = 0;
    while (!server.outboundMessages().isEmpty()) {
        client.writeInbound(server.readOutbound());
        count++;
    }
    client.flush();
    LOG.debug("Transferred {} outbound server messages.", count);
}

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);/* w ww  .  j  av a  2  s .co  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 .j  a  v a  2 s. co 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:io.crate.protocols.postgres.MessagesTest.java

License:Apache License

private static void verifyResponse(EmbeddedChannel channel, String response) {
    byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
    ByteBuf buffer = (ByteBuf) channel.outboundMessages().poll();
    assertThat(buffer.readByte(), is((byte) 'C'));
    assertThat(buffer.readInt(), is(responseBytes.length + 4 + 1));
    byte[] string = new byte[9];
    buffer.readBytes(string);/*from  w  w  w  .j  a  v  a  2  s . com*/
    assertThat(string, is(responseBytes));
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testDescribePortalMessage() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, new AlwaysOKNullAuthentication(), null);

    EmbeddedChannel channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    {/*from w ww.j  a  va  2s  . c om*/
        ByteBuf buffer = Unpooled.buffer();

        ClientMessages.sendStartupMessage(buffer, "doc");
        ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)",
                new int[] { PGTypes.get(DataTypes.LONG).oid() });
        ClientMessages.sendBindMessage(buffer, "P1", "S1", Collections.singletonList(1));
        channel.writeInbound(buffer);

        // we're not interested in the startup, parse, or bind replies
        channel.flushOutbound();
        channel.outboundMessages().clear();
    }
    {
        // try portal describe message
        ByteBuf buffer = Unpooled.buffer();
        ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.PORTAL, "P1");
        channel.writeInbound(buffer);

        // we should get back a RowDescription message
        ByteBuf response = channel.readOutbound();
        assertThat(response.readByte(), is((byte) 'T'));
        assertThat(response.readInt(), is(42));
        assertThat(response.readShort(), is((short) 1));
        assertThat(PostgresWireProtocol.readCString(response), is("($1 IN (1, 2, 3))"));

        assertThat(response.readInt(), is(0));
        assertThat(response.readShort(), is((short) 0));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
        assertThat(response.readShort(), is((short) PGTypes.get(DataTypes.BOOLEAN).typeLen()));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
        assertThat(response.readShort(), is((short) 0));
    }
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testDescribeStatementMessage() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, new AlwaysOKNullAuthentication(), null);

    EmbeddedChannel channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    {/*  w  w  w . ja  v a2  s .  c  o m*/
        ByteBuf buffer = Unpooled.buffer();

        ClientMessages.sendStartupMessage(buffer, "doc");
        ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)", new int[0]);
        channel.writeInbound(buffer);

        // we're not interested in the startup, parse, or bind replies
        channel.flushOutbound();
        channel.outboundMessages().clear();
    }
    {
        // try the describe statement variant
        ByteBuf buffer = Unpooled.buffer();
        ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
        channel.writeInbound(buffer);

        // we should get back a ParameterDescription message
        ByteBuf response = channel.readOutbound();
        assertThat(response.readByte(), is((byte) 't'));
        assertThat(response.readInt(), is(10));
        assertThat(response.readShort(), is((short) 1));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).oid()));

        // we should get back a RowDescription message
        response = channel.readOutbound();
        assertThat(response.readByte(), is((byte) 'T'));
        assertThat(response.readInt(), is(42));
        assertThat(response.readShort(), is((short) 1));
        assertThat(PostgresWireProtocol.readCString(response), is("($1 IN (1, 2, 3))"));

        assertThat(response.readInt(), is(0));
        assertThat(response.readShort(), is((short) 0));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
        assertThat(response.readShort(), is((short) PGTypes.get(DataTypes.BOOLEAN).typeLen()));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
        assertThat(response.readShort(), is((short) 0));
    }
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHttp1xOrH2CHandlerHttp1xRequest() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler());
    ByteBuf buff = HTTP_1_1_POST.copy(0, HTTP_1_1_POST.readableBytes());
    ch.writeInbound(buff);//from   w  w w  .java2 s  .c o m
    assertEquals(0, buff.refCnt());
    assertEquals(1, ch.outboundMessages().size());
    HttpRequest req = (HttpRequest) ch.outboundMessages().poll();
    assertEquals("POST", req.method().name());
    assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class));
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHttp1xOrH2CHandlerFragmentedHttp1xRequest() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler());
    ByteBuf buff = HTTP_1_1_POST.copy(0, 1);
    ch.writeInbound(buff);/* ww w. j ava 2 s .  c  o  m*/
    assertEquals(0, buff.refCnt());
    assertEquals(0, ch.outboundMessages().size());
    buff = HTTP_1_1_POST.copy(1, HTTP_1_1_POST.readableBytes() - 1);
    ch.writeInbound(buff);
    assertEquals(0, buff.refCnt());
    assertEquals(1, ch.outboundMessages().size());
    HttpRequest req = (HttpRequest) ch.outboundMessages().poll();
    assertEquals("POST", req.method().name());
    assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class));
}

From source file:io.vertx.core.http.Http2ServerTest.java

License:Open Source License

@Test
public void testHttp1xOrH2CHandlerHttp2Request() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new TestHttp1xOrH2CHandler());
    ByteBuf expected = Unpooled.copiedBuffer(Http1xOrH2CHandler.HTTP_2_PREFACE, StandardCharsets.UTF_8);
    ch.writeInbound(expected);//  w w w.  j  a  v a 2 s .  com
    assertEquals(1, expected.refCnt());
    assertEquals(1, ch.outboundMessages().size());
    ByteBuf res = (ByteBuf) ch.outboundMessages().poll();
    assertEquals(Http1xOrH2CHandler.HTTP_2_PREFACE, res.toString(StandardCharsets.UTF_8));
    assertNull(ch.pipeline().get(TestHttp1xOrH2CHandler.class));
}