use of org.apache.hadoop.oncrpc.RpcUtil.RpcFrameDecoder in project hadoop by apache.
the class TestFrameDecoder method testSingleFrame.
@Test
public void testSingleFrame() {
RpcFrameDecoder decoder = new RpcFrameDecoder();
// Test "Length field is not received yet"
ByteBuffer buffer = ByteBuffer.allocate(1);
ChannelBuffer buf = new ByteBufferBackedChannelBuffer(buffer);
ChannelBuffer channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
assertTrue(channelBuffer == null);
// Test all bytes are not received yet
byte[] fragment = new byte[4 + 9];
// final fragment
fragment[0] = (byte) (1 << 7);
fragment[1] = 0;
fragment[2] = 0;
// fragment size = 10 bytes
fragment[3] = (byte) 10;
assertTrue(XDR.isLastFragment(fragment));
assertTrue(XDR.fragmentSize(fragment) == 10);
buffer = ByteBuffer.allocate(4 + 9);
buffer.put(fragment);
buffer.flip();
buf = new ByteBufferBackedChannelBuffer(buffer);
channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
assertTrue(channelBuffer == null);
}
use of org.apache.hadoop.oncrpc.RpcUtil.RpcFrameDecoder in project hadoop by apache.
the class TestFrameDecoder method testMultipleFrames.
@Test
public void testMultipleFrames() {
RpcFrameDecoder decoder = new RpcFrameDecoder();
// Test multiple frames
byte[] fragment1 = new byte[4 + 10];
// not final fragment
fragment1[0] = 0;
fragment1[1] = 0;
fragment1[2] = 0;
// fragment size = 10 bytes
fragment1[3] = (byte) 10;
assertFalse(XDR.isLastFragment(fragment1));
assertTrue(XDR.fragmentSize(fragment1) == 10);
// decoder should wait for the final fragment
ByteBuffer buffer = ByteBuffer.allocate(4 + 10);
buffer.put(fragment1);
buffer.flip();
ChannelBuffer buf = new ByteBufferBackedChannelBuffer(buffer);
ChannelBuffer channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
assertTrue(channelBuffer == null);
byte[] fragment2 = new byte[4 + 10];
// final fragment
fragment2[0] = (byte) (1 << 7);
fragment2[1] = 0;
fragment2[2] = 0;
// fragment size = 10 bytes
fragment2[3] = (byte) 10;
assertTrue(XDR.isLastFragment(fragment2));
assertTrue(XDR.fragmentSize(fragment2) == 10);
buffer = ByteBuffer.allocate(4 + 10);
buffer.put(fragment2);
buffer.flip();
buf = new ByteBufferBackedChannelBuffer(buffer);
channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
assertTrue(channelBuffer != null);
// Complete frame should have to total size 10+10=20
assertEquals(20, channelBuffer.readableBytes());
}
Aggregations