Example usage for io.netty.buffer UnpooledByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer UnpooledByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer UnpooledByteBufAllocator DEFAULT.

Prototype

UnpooledByteBufAllocator DEFAULT

To view the source code for io.netty.buffer UnpooledByteBufAllocator DEFAULT.

Click Source Link

Document

Default instance which uses leak-detection for direct buffers.

Usage

From source file:org.neo4j.bolt.v1.transport.socket.SocketTransportHandlerTest.java

License:Open Source License

@Test
public void logsAndClosesConnectionOnUnexpectedExceptions() throws Throwable {
    // Given/*  ww w  .java2  s  . c om*/
    Session session = mock(Session.class);
    Channel ch = mock(Channel.class);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(ch);

    when(ch.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);

    AssertableLogProvider logging = new AssertableLogProvider();

    SocketTransportHandler handler = new SocketTransportHandler(protocolChooser(session), logging);

    // And Given a session has been established
    handler.channelRead(ctx, handshake());

    // When
    Throwable cause = new Throwable("Oh no!");
    handler.exceptionCaught(ctx, cause);

    // Then
    verify(session).close();
    logging.assertExactly(inLog(SocketTransportHandler.class)
            .error(equalTo("Fatal error occurred when handling a client connection: Oh no!"), is(cause)));
}

From source file:org.neo4j.bolt.v1.transport.socket.SocketTransportHandlerTest.java

License:Open Source License

private ByteBuf handshake() {
    ByteBuf buf = UnpooledByteBufAllocator.DEFAULT.buffer();
    buf.writeInt(0x6060B017);/*ww w . j a  v a 2  s  .c  o  m*/
    buf.writeInt(1);
    buf.writeInt(0);
    buf.writeInt(0);
    buf.writeInt(0);
    return buf;
}

From source file:org.neo4j.ndp.transport.socket.ChunkedOutputTest.java

License:Open Source License

@Before
public void setup() {
    ChannelHandlerContext ch = mock(ChannelHandlerContext.class);
    when(ch.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(ch.writeAndFlush(any())).thenAnswer(new Answer<Object>() {
        @Override/*w w w .  ja  v a 2 s . com*/
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ByteBuf byteBuf = (ByteBuf) invocation.getArguments()[0];
            writtenData.limit(writtenData.position() + byteBuf.readableBytes());
            byteBuf.readBytes(writtenData);
            return null;
        }
    });

    out.setTargetChannel(ch);
}

From source file:org.opendaylight.netide.netiplib.OpenFlowMessage.java

License:Open Source License

@Override
public byte[] getPayload() {
    SerializerRegistry registry = new SerializerRegistryImpl();
    registry.init();/*  ww w . ja va  2  s  .c om*/
    SerializationFactory factory = new SerializationFactory();
    factory.setSerializerTable(registry);
    ByteBuf output = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.messageToBuffer(getOfVersion(), output, getOfMessage());
    byte[] rawPayload = new byte[output.readableBytes()];
    output.getBytes(0, rawPayload);
    return rawPayload;
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.FlowModInputMessageFactoryTest.java

License:Open Source License

@Test
public void test() throws Exception {
    FlowModInput expectedMessage = createMessage();
    SerializerRegistry registry = new SerializerRegistryImpl();
    registry.init();//from   w ww.  jav a  2  s. com
    OFSerializer<FlowModInput> serializer = registry
            .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, FlowModInput.class));
    ByteBuf originalBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(expectedMessage, originalBuffer);

    // TODO: Skipping first 4 bytes due to the way deserializer is
    // implemented
    // Skipping version, type and length from OF header
    originalBuffer.skipBytes(4);
    FlowModInput deserializedMessage = BufferHelper.deserialize(flowFactory, originalBuffer);
    Assert.assertEquals("Wrong version", expectedMessage.getVersion(), deserializedMessage.getVersion());
    Assert.assertEquals("Wrong XId", expectedMessage.getXid(), deserializedMessage.getXid());
    Assert.assertEquals("Wrong cookie", expectedMessage.getCookie(), deserializedMessage.getCookie());
    Assert.assertEquals("Wrong cookie mask", expectedMessage.getCookieMask(),
            deserializedMessage.getCookieMask());
    Assert.assertEquals("Wrong table id", expectedMessage.getTableId().getValue(),
            deserializedMessage.getTableId().getValue());
    Assert.assertEquals("Wrong command", expectedMessage.getCommand().getIntValue(),
            deserializedMessage.getCommand().getIntValue());
    Assert.assertEquals("Wrong idle timeout", expectedMessage.getIdleTimeout(),
            deserializedMessage.getIdleTimeout());
    Assert.assertEquals("Wrong hard timeout", expectedMessage.getHardTimeout(),
            deserializedMessage.getHardTimeout());
    Assert.assertEquals("Wrong priority", expectedMessage.getPriority(), deserializedMessage.getPriority());
    Assert.assertEquals("Wrong buffer id ", expectedMessage.getBufferId(), deserializedMessage.getBufferId());
    Assert.assertEquals("Wrong out port", expectedMessage.getOutPort().getValue(),
            deserializedMessage.getOutPort().getValue());
    Assert.assertEquals("Wrong out group", expectedMessage.getOutGroup(), deserializedMessage.getOutGroup());
    Assert.assertEquals("Wrong number of flags", expectedMessage.getFlags().getValue().length,
            deserializedMessage.getFlags().getValue().length);
    for (int i = 0; i < expectedMessage.getFlags().getValue().length; i++) {
        Assert.assertEquals("Wrong flag", expectedMessage.getFlags().getValue()[i],
                deserializedMessage.getFlags().getValue()[i]);
    }
    Assert.assertEquals("Wrong match", expectedMessage.getMatch(), deserializedMessage.getMatch());
    Assert.assertEquals("Wrong number of instructions", expectedMessage.getInstruction().size(),
            deserializedMessage.getInstruction().size());
    int i = 0;
    for (Instruction ins : expectedMessage.getInstruction()) {
        Assert.assertEquals("Wrong instruction", ins, deserializedMessage.getInstruction().get(i));
        i++;
    }
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.GetFeaturesInputFactoryTest.java

License:Open Source License

@Test
public void test() throws Exception {
    GetFeaturesInput expectedMessage = createMessage();
    SerializerRegistry registry = new SerializerRegistryImpl();
    registry.init();/*from w w  w  .  j  av  a 2 s  .  c  o  m*/
    OFSerializer<GetFeaturesInput> serializer = registry
            .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, GetFeaturesInput.class));
    ByteBuf originalBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(expectedMessage, originalBuffer);

    // TODO: Skipping first 4 bytes due to the way deserializer is
    // implemented
    // Skipping version, type and length from OF header
    originalBuffer.skipBytes(4);
    GetFeaturesInput deserializedMessage = BufferHelper.deserialize(factory, originalBuffer);
    Assert.assertEquals("Wrong version", expectedMessage.getVersion(), deserializedMessage.getVersion());
    Assert.assertEquals("Wrong XId", expectedMessage.getXid(), deserializedMessage.getXid());
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.PacketOutInputMessageFactoryTest.java

License:Open Source License

@Test
public void test() throws Exception {
    PacketOutInput expectedMessage = createMessage();
    SerializerRegistry registry = new SerializerRegistryImpl();
    registry.init();/*from  w  w w  .jav  a2  s  . c o  m*/
    NetIdePacketOutInputMessageFactory serializer = new NetIdePacketOutInputMessageFactory();
    serializer.injectSerializerRegistry(registry);
    ByteBuf originalBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(expectedMessage, originalBuffer);

    // TODO: Skipping first 4 bytes due to the way deserializer is
    // implemented
    // Skipping version, type and length from OF header
    originalBuffer.skipBytes(4);
    PacketOutInput deserializedMessage = BufferHelper.deserialize(factory, originalBuffer);
    Assert.assertEquals("Wrong version", expectedMessage.getVersion(), deserializedMessage.getVersion());
    Assert.assertEquals("Wrong XId", expectedMessage.getXid(), deserializedMessage.getXid());
    Assert.assertEquals("Wrong buffer Id", expectedMessage.getBufferId(), deserializedMessage.getBufferId());
    Assert.assertEquals("Wrong In Port", expectedMessage.getInPort().getValue(),
            deserializedMessage.getInPort().getValue());
    Assert.assertEquals("Wrong Numbers of actions", expectedMessage.getAction().size(),
            deserializedMessage.getAction().size());
    int i = 0;
    for (Action a : expectedMessage.getAction()) {
        Assert.assertEquals("Wrong action", a, deserializedMessage.getAction().get(i));
        i++;
    }
    Assert.assertArrayEquals("Wrong data", expectedMessage.getData(), deserializedMessage.getData());
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.BarrierReplyMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() {
    BarrierReplyMessageFactory serializer = new BarrierReplyMessageFactory();
    SerializerRegistry registry = new NetIdeSerializerRegistryImpl();
    registry.init();/*from   w  w w.j  a v  a  2  s.com*/
    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 8);
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.EchoOutputMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() {
    EchoOutputMessageFactory serializer = new EchoOutputMessageFactory();
    SerializerRegistry registry = new NetIdeSerializerRegistryImpl();
    registry.init();/*  w ww  . j  a v a 2s. c om*/
    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 24);
    Assert.assertArrayEquals("Wrong data", message.getData(),
            serializedBuffer.readBytes(serializedBuffer.readableBytes()).array());
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.EchoRequestMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() {
    EchoRequestMessageFactory serializer = new EchoRequestMessageFactory();
    SerializerRegistry registry = new NetIdeSerializerRegistryImpl();
    registry.init();/* ww  w .ja  v  a 2s .  co m*/
    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 24);
    Assert.assertArrayEquals("Wrong data", message.getData(),
            serializedBuffer.readBytes(serializedBuffer.readableBytes()).array());
}