use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer 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.NetworkBuffer in project hugegraph-computer by hugegraph.
the class NettyTransportClientTest method testDataUniformity.
@Test
public void testDataUniformity() throws IOException {
NettyTransportClient client = (NettyTransportClient) this.oneClient();
byte[] sourceBytes1 = StringEncoding.encode("test data message");
byte[] sourceBytes2 = StringEncoding.encode("test data edge");
byte[] sourceBytes3 = StringEncoding.encode("test data vertex");
Mockito.doAnswer(invocationOnMock -> {
MessageType type = invocationOnMock.getArgument(0);
NetworkBuffer buffer = invocationOnMock.getArgument(2);
byte[] sourceBytes = null;
switch(type) {
case MSG:
sourceBytes = sourceBytes1;
break;
case EDGE:
sourceBytes = sourceBytes2;
break;
case VERTEX:
sourceBytes = sourceBytes3;
break;
default:
}
byte[] bytes;
if (buffer instanceof FileRegionBuffer) {
String path = ((FileRegionBuffer) buffer).path();
File file = new File(path);
bytes = FileUtils.readFileToByteArray(file);
FileUtils.deleteQuietly(file);
} else {
bytes = buffer.copyToByteArray();
}
Assert.assertArrayEquals(sourceBytes, bytes);
Assert.assertNotSame(sourceBytes, bytes);
return null;
}).when(serverHandler).handle(Mockito.any(), Mockito.eq(1), Mockito.any());
client.startSession();
client.send(MessageType.MSG, 1, ByteBuffer.wrap(sourceBytes1));
client.send(MessageType.EDGE, 1, ByteBuffer.wrap(sourceBytes2));
client.send(MessageType.VERTEX, 1, ByteBuffer.wrap(sourceBytes3));
client.finishSession();
}
use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer 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.NetworkBuffer 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());
}
use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer in project hugegraph-computer by hugegraph.
the class DataMessage method parseWithMemoryBuffer.
/**
* Decoding uses the given ByteBuf as our data.
*/
public static DataMessage parseWithMemoryBuffer(MessageType type, ByteBuf buf) {
int requestId = buf.readInt();
int partition = buf.readInt();
int bodyLength = buf.readInt();
// Slice body and retain it, the readIndex of buf will auto to body end
ByteBuf bodySlice = buf.readRetainedSlice(bodyLength);
NetworkBuffer networkBuffer = new NettyBuffer(bodySlice);
return new DataMessage(type, requestId, partition, networkBuffer);
}
Aggregations