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

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

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public <T> T readInbound() 

Source Link

Document

Return received data from this Channel

Usage

From source file:alluxio.worker.netty.CodecTest.java

License:Apache License

/**
 * Waits for one request./*from   w  ww. j  a  v a 2 s .  c  o m*/
 *
 * @return the request
 */
private Object waitForOneRequest(final EmbeddedChannel channel) {
    return CommonUtils.waitForResult("response from the channel", new Function<Void, Object>() {
        @Override
        public Object apply(Void v) {
            return channel.readInbound();
        }
    }, WaitForOptions.defaults().setTimeoutMs(Constants.MINUTE_MS));
}

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  w w  . j  a va  2s .c  o 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.nettybook.ch7.junit.DelimiterBasedFrameDecoderTest.java

License:Apache License

@Test
public void testDecoder() {
    String writeData = "\r\n\r\n";
    String firstResponse = "\r\n";
    String secondResponse = "\r\n";

    DelimiterBasedFrameDecoder decoder = new DelimiterBasedFrameDecoder(8192, false,
            Delimiters.lineDelimiter());
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(decoder);

    ByteBuf request = Unpooled.wrappedBuffer(writeData.getBytes());
    boolean result = embeddedChannel.writeInbound(request);
    assertTrue(result);//www.ja  va 2  s . c  o m

    ByteBuf response = null;

    response = (ByteBuf) embeddedChannel.readInbound();
    assertEquals(firstResponse, response.toString(Charset.defaultCharset()));

    response = (ByteBuf) embeddedChannel.readInbound();
    assertEquals(secondResponse, response.toString(Charset.defaultCharset()));

    embeddedChannel.finish();
}

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);// ww w . ja  v a 2 s .com

    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   w  w w  .ja va2s. c om

    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:com.linkedin.r2.transport.http.client.TestRAPClientCodec.java

License:Apache License

@Test(dataProvider = "responseData")
public void testResponseDecoder(int status, String entity, HttpHeaders headers, String[] cookies) {
    final EmbeddedChannel ch = new EmbeddedChannel(new RAPClientCodec());

    ByteBuf content = Unpooled.copiedBuffer(entity, CHARSET);
    FullHttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.valueOf(status), content);
    nettyResponse.headers().set(headers);
    for (String cookie : cookies) {
        nettyResponse.headers().add(HttpHeaderNames.SET_COOKIE, cookie);
    }// w w w. j ava  2  s  .c o  m

    ch.writeInbound(nettyResponse);
    RestResponse response = (RestResponse) ch.readInbound();

    Assert.assertEquals(response.getStatus(), status);
    Assert.assertEquals(response.getEntity().asString(CHARSET), entity);
    assertList(response.getCookies(),
            nettyResponse.headers().getAll(HttpConstants.RESPONSE_COOKIE_HEADER_NAME));

    for (Map.Entry<String, String> header : nettyResponse.headers()) {
        if (!header.getKey().equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
            List<String> values = response.getHeaderValues(header.getKey());
            Assert.assertNotNull(values);
            Assert.assertTrue(values.contains(header.getValue()));
        }
    }
    // make sure the incoming ByteBuf is released
    Assert.assertEquals(content.refCnt(), 0);

    ch.finish();
}

From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java

License:Apache License

@NotNull
private List<Record> collect10NetflowV5MessagesFromChannel(EmbeddedChannel ch, int packetLength) {
    List<Record> records = new LinkedList<>();
    for (int i = 0; i < 10; i++) {
        Object object = ch.readInbound();
        assertNotNull(object);/*from  w ww  .j ava2  s.  co m*/
        assertThat(object, is(instanceOf(NetflowV5Message.class)));
        NetflowV5Message msg = (NetflowV5Message) object;
        // fix packet length for test; it passes in MAX_LENGTH by default
        msg.setLength(packetLength);
        Record record = RecordCreator.create();
        msg.populateRecord(record);
        records.add(record);
    }
    return records;
}

From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java

License:Apache License

@NotNull
private List<Record> collectNetflowV9MessagesFromChannel(EmbeddedChannel ch, int numMessages,
        List<NetflowV9Message> messages) {
    List<Record> records = new LinkedList<>();
    for (int i = 0; i < numMessages; i++) {
        Object object = ch.readInbound();
        assertNotNull(object);/* w  w  w  .  j  a  v  a  2 s. c o  m*/
        assertThat(object, is(instanceOf(NetflowV9Message.class)));
        NetflowV9Message msg = (NetflowV9Message) object;
        messages.add(msg);
        Record record = RecordCreator.create();
        msg.populateRecord(record);
        records.add(record);
    }
    return records;
}

From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java

License:Apache License

@Test
public void testTimestamps() {

    EmbeddedChannel ch = new EmbeddedChannel(makeNetflowDecoder());

    final long uptime = RandomUtils.nextLong(0L, 1000000L);
    final long seconds = RandomUtils.nextLong(0L, 1500000000L);
    final long nanos = RandomUtils.nextLong(0L, 1000000000L - 1L);
    NetflowTestUtil.writeV5NetflowHeader(ch, 1, uptime, seconds, nanos, 0L, 0, 0, 0);

    final long first = RandomUtils.nextLong(uptime + 1L, 2000000L);
    final long last = RandomUtils.nextLong(first + 1L, 3000000L);
    NetflowTestUtil.writeV5NetflowFlowRecord(ch, 0, 0, 0, 0, 0, 1, 1, first, last, 0, 0, 0, 0, 0, 0, 0, 0, 0);

    Object obj = ch.readInbound();
    assertThat(obj, instanceOf(NetflowV5Message.class));
    NetflowV5Message msg = (NetflowV5Message) obj;

    assertThat(msg.getCount(), equalTo(1));
    assertThat(msg.getSeconds(), equalTo(seconds));
    assertThat(msg.getNanos(), equalTo(nanos));
    assertThat(msg.getUptime(), equalTo(uptime));

    final long expectedTimestamp = seconds * 1000 + (nanos / 1000000);
    assertThat(msg.getTimestamp(), equalTo(expectedTimestamp));

    final long expectedFirst = expectedTimestamp - uptime + first;
    assertThat(msg.getFirst(), equalTo(expectedFirst));

    final long expectedLast = expectedTimestamp - uptime + last;
    assertThat(msg.getLast(), equalTo(expectedLast));
}

From source file:com.streamsets.pipeline.lib.parser.net.TestDelimitedLengthFieldBasedFrameDecoder.java

License:Apache License

@Test
public void testMaxFrameLengthOverflow() throws Exception {
    Charset charset = CharsetUtil.ISO_8859_1;
    // maxFrameLength plus adjustment would overflow an int
    final long numBytes = Integer.MAX_VALUE - 1;
    final int lengthAdjustment = 10;
    EmbeddedChannel ch = getTestChannel(charset, (int) numBytes, lengthAdjustment, true);

    //this is a bad frame, but will still test the overflow condition
    String longString = String.valueOf(numBytes) + " abcd";

    try {// w  w  w  .  j  a  v a 2s . c  o  m
        ch.writeInbound(Unpooled.copiedBuffer(longString, charset));
        Assert.fail("TooLongFrameException should have been thrown");
    } catch (TooLongFrameException ignored) {
        //ignored
    }
    Assert.assertNull(ch.readInbound());

    ch.close();
}