use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.
the class ReceiverUtil method consumeBuffer.
public static void consumeBuffer(byte[] bytes, Consumer<NetworkBuffer> consumer) {
ByteBuf buf = Unpooled.directBuffer(bytes.length);
try {
buf = buf.writeBytes(bytes);
NettyBuffer buff = new NettyBuffer(buf);
consumer.accept(buff);
} finally {
buf.release();
}
}
use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.
the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecode.
@Test
public void testSendMsgWithFrameDecode() {
FrameDecoder frameDecoder = new FrameDecoder();
EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
ByteBuf buf = buffer.nettyByteBuf();
StartMessage.INSTANCE.encode(buf);
boolean writeInbound = embeddedChannel.writeInbound(buf);
Assert.assertTrue(writeInbound);
Assert.assertTrue(embeddedChannel.finish());
buffer.release();
}
use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.
the class NettyEncodeDecodeHandlerTest method testMessageRelease.
@Test
public void testMessageRelease() {
int requestId = 99;
int partition = 1;
byte[] bytes = StringEncoding.encode("mock msg");
ByteBuf buf = Unpooled.directBuffer().writeBytes(bytes);
try {
NettyBuffer managedBuffer = new NettyBuffer(buf);
DataMessage dataMessage = new DataMessage(MessageType.MSG, requestId, partition, managedBuffer);
Assert.assertEquals("DataMessage[messageType=MSG," + "sequenceNumber=99,partition=1,hasBody=true," + "bodyLength=8]", dataMessage.toString());
Assert.assertEquals(1, managedBuffer.referenceCount());
dataMessage.release();
Assert.assertEquals(0, managedBuffer.referenceCount());
} finally {
if (buf.refCnt() > 0) {
buf.release();
}
}
}
use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.
the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecodeMagicError.
@Test
public void testSendMsgWithFrameDecodeMagicError() {
FrameDecoder frameDecoder = new FrameDecoder();
EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
short magicError = 10;
ByteBuf buf = buffer.nettyByteBuf();
StartMessage.INSTANCE.encode(buf);
buf.setShort(0, magicError);
embeddedChannel.writeInbound(buf);
Assert.assertFalse(embeddedChannel.finish());
Assert.assertNull(embeddedChannel.readInbound());
}
use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.
the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecodeVersionError.
@Test
public void testSendMsgWithFrameDecodeVersionError() {
FrameDecoder frameDecoder = new FrameDecoder();
EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
byte versionError = 10;
ByteBuf buf = buffer.nettyByteBuf();
StartMessage.INSTANCE.encode(buf);
buf.setByte(2, versionError);
embeddedChannel.writeInbound(buf);
Assert.assertFalse(embeddedChannel.finish());
Assert.assertNull(embeddedChannel.readInbound());
}
Aggregations