Search in sources :

Example 1 with ByteBufferBackedChannelBuffer

use of org.jboss.netty.buffer.ByteBufferBackedChannelBuffer 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);
}
Also used : RpcFrameDecoder(org.apache.hadoop.oncrpc.RpcUtil.RpcFrameDecoder) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) Test(org.junit.Test)

Example 2 with ByteBufferBackedChannelBuffer

use of org.jboss.netty.buffer.ByteBufferBackedChannelBuffer in project neo4j by neo4j.

the class ServerTest method message.

private MessageEvent message(RequestType reqType, RequestContext ctx, Channel serverToClientChannel, Serializer payloadSerializer) throws IOException {
    ByteBuffer backingBuffer = ByteBuffer.allocate(1024);
    protocol.serializeRequest(new RecordingChannel(), new ByteBufferBackedChannelBuffer(backingBuffer), reqType, ctx, payloadSerializer);
    MessageEvent event = mock(MessageEvent.class);
    when(event.getMessage()).thenReturn(new ByteBufferBackedChannelBuffer(backingBuffer));
    when(event.getChannel()).thenReturn(serverToClientChannel);
    return event;
}
Also used : MessageEvent(org.jboss.netty.channel.MessageEvent) ByteBuffer(java.nio.ByteBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer)

Example 3 with ByteBufferBackedChannelBuffer

use of org.jboss.netty.buffer.ByteBufferBackedChannelBuffer 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());
}
Also used : RpcFrameDecoder(org.apache.hadoop.oncrpc.RpcUtil.RpcFrameDecoder) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)3 ByteBufferBackedChannelBuffer (org.jboss.netty.buffer.ByteBufferBackedChannelBuffer)3 RpcFrameDecoder (org.apache.hadoop.oncrpc.RpcUtil.RpcFrameDecoder)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 Channel (org.jboss.netty.channel.Channel)2 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)2 Test (org.junit.Test)2 MessageEvent (org.jboss.netty.channel.MessageEvent)1