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

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

Introduction

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

Prototype

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

Source Link

Document

Read data from the outbound.

Usage

From source file:alluxio.client.block.stream.NettyPacketReaderTest.java

License:Apache License

/**
 * Validates the read request sent.//from  w w w.  j av  a2 s  . c  o m
 *
 * @param channel the channel
 * @param offset the offset
 * @param length the length
 * @param cancel whether it is a cancel request
 */
private void validateReadRequestSent(final EmbeddedChannel channel, long offset, long length, boolean cancel) {
    Object request = CommonUtils.waitForResult("read request", new Function<Void, Object>() {
        @Override
        public Object apply(Void v) {
            return channel.readOutbound();
        }
    }, WaitForOptions.defaults().setTimeout(Constants.MINUTE_MS));

    Assert.assertTrue(request != null);
    Assert.assertTrue(request instanceof RPCProtoMessage);
    Assert.assertEquals(null, ((RPCProtoMessage) request).getPayloadDataBuffer());
    Protocol.ReadRequest readRequest = ((RPCProtoMessage) request).getMessage().getMessage();
    Assert.assertEquals(BLOCK_ID, readRequest.getId());
    Assert.assertEquals(offset, readRequest.getOffset());
    Assert.assertEquals(length, readRequest.getLength());
    Assert.assertEquals(cancel, readRequest.getCancel());
}

From source file:alluxio.client.block.stream.NettyPacketWriterTest.java

License:Apache License

/**
 * Verifies the packets written. After receiving the last packet, it will also send an EOF to
 * the channel./*from   ww w .  j  a v  a 2 s. c  om*/
 *
 * @param checksumStart the start position to calculate the checksum
 * @param checksumEnd the end position to calculate the checksum
 * @return the checksum of the data read starting from checksumStart
 */
private Future<Long> verifyWriteRequests(final EmbeddedChannel channel, final long checksumStart,
        final long checksumEnd) {
    return EXECUTOR.submit(new Callable<Long>() {
        @Override
        public Long call() {
            try {
                long checksum = 0;
                long pos = 0;
                while (true) {
                    RPCProtoMessage request = (RPCProtoMessage) CommonUtils.waitForResult("wrtie request",
                            new Function<Void, Object>() {
                                @Override
                                public Object apply(Void v) {
                                    return channel.readOutbound();
                                }
                            }, WaitForOptions.defaults().setTimeout(Constants.MINUTE_MS));
                    validateWriteRequest(request.getMessage().<Protocol.WriteRequest>getMessage(), pos);

                    DataBuffer buffer = request.getPayloadDataBuffer();
                    // Last packet.
                    if (buffer == null) {
                        channel.writeInbound(RPCProtoMessage.createOkResponse(null));
                        return checksum;
                    }
                    try {
                        Assert.assertTrue(buffer instanceof DataNettyBufferV2);
                        ByteBuf buf = (ByteBuf) buffer.getNettyOutput();
                        while (buf.readableBytes() > 0) {
                            if (pos >= checksumStart && pos <= checksumEnd) {
                                checksum += BufferUtils.byteToInt(buf.readByte());
                            } else {
                                buf.readByte();
                            }
                            pos++;
                        }
                    } finally {
                        buffer.release();
                    }
                }
            } catch (Throwable throwable) {
                LOG.error("Failed to verify write requests.", throwable);
                Assert.fail();
                throw throwable;
            }
        }
    });
}

From source file:alluxio.client.block.stream.UfsFallbackLocalFilePacketWriterTest.java

License:Apache License

/**
 * Verifies the packets written. After receiving the last packet, it will also send an EOF to
 * the channel./* w  w  w . j a  v a 2 s. com*/
 *
 * @return the checksum of the data read starting from checksumStart
 */
private Future<WriteSummary> getUfsWrite(final EmbeddedChannel channel) {
    return EXECUTOR.submit(new Callable<WriteSummary>() {
        @Override
        public WriteSummary call() throws TimeoutException, InterruptedException {
            try {
                long checksum = 0;
                long pos = 0;
                long len = 0;
                while (true) {
                    RPCProtoMessage request = (RPCProtoMessage) CommonUtils.waitForResult("write request",
                            () -> channel.readOutbound(),
                            WaitForOptions.defaults().setTimeoutMs(Constants.MINUTE_MS));
                    Protocol.WriteRequest writeRequest = request.getMessage().asWriteRequest();
                    validateWriteRequest(writeRequest, pos);
                    DataBuffer buffer = request.getPayloadDataBuffer();
                    // Last packet.
                    if (writeRequest.hasEof() && writeRequest.getEof()) {
                        assertTrue(buffer == null);
                        channel.writeInbound(RPCProtoMessage.createOkResponse(null));
                        return new WriteSummary(len, checksum);
                    }
                    // UFS block init
                    if (writeRequest.getCreateUfsBlockOptions().hasBytesInBlockStore()) {
                        assertTrue(buffer == null);
                        pos += writeRequest.getCreateUfsBlockOptions().getBytesInBlockStore();
                        continue;
                    }
                    try {
                        Assert.assertTrue(buffer instanceof DataNettyBufferV2);
                        ByteBuf buf = (ByteBuf) buffer.getNettyOutput();
                        while (buf.readableBytes() > 0) {
                            checksum += BufferUtils.byteToInt(buf.readByte());
                            pos++;
                            len++;
                        }
                    } finally {
                        buffer.release();
                    }
                }
            } catch (Throwable throwable) {
                fail("Failed to verify write requests." + throwable.getMessage());
                throw throwable;
            }
        }
    });
}

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

License:Apache License

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

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

License:Apache License

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

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

License:Apache License

/**
 * Waits for one read response messsage.
 *
 * @return the read response//from   www.j  a v  a 2 s . co  m
 */
protected Object waitForOneResponse(final EmbeddedChannel channel) {
    return CommonUtils.waitForResult("response from the channel", new Function<Void, Object>() {
        @Override
        public Object apply(Void v) {
            return channel.readOutbound();
        }
    }, WaitForOptions.defaults().setTimeout(Constants.MINUTE_MS));
}

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

License:Apache License

private Object waitForResponse(final EmbeddedChannel channel) {
    return CommonUtils.waitForResult("response from the channel.", new Function<Void, Object>() {
        @Override/*from   ww  w. ja  v a 2s .com*/
        public Object apply(Void v) {
            return channel.readOutbound();
        }
    }, WaitForOptions.defaults().setTimeout(Constants.MINUTE_MS));
}

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

License:Apache License

/**
 * Waits for a response./*from w  w w . ja va 2  s . c om*/
 *
 * @return the response
 */
protected Object waitForResponse(final EmbeddedChannel channel) {
    return CommonUtils.waitForResult("response from the channel.", new Function<Void, Object>() {
        @Override
        public Object apply(Void v) {
            return channel.readOutbound();
        }
    }, WaitForOptions.defaults().setTimeout(Constants.MINUTE_MS));
}

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

License:Apache License

/**
 * Waits for one read response message./*from w  w  w .j a  v  a  2  s  . c om*/
 *
 * @return the read response
 */
protected Object waitForOneResponse(final EmbeddedChannel channel) {
    return CommonUtils.waitForResult("response from the channel", new Function<Void, Object>() {
        @Override
        public Object apply(Void v) {
            return channel.readOutbound();
        }
    }, WaitForOptions.defaults().setTimeoutMs(Constants.MINUTE_MS));
}

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

License:Apache License

private Object waitForResponse(final EmbeddedChannel channel) {
    return CommonUtils.waitForResult("response from the channel.", new Function<Void, Object>() {
        @Override/*from   www.  j a v  a  2 s .  c o m*/
        public Object apply(Void v) {
            return channel.readOutbound();
        }
    }, WaitForOptions.defaults().setTimeoutMs(Constants.MINUTE_MS));
}