Skip to content
Permalink
Browse files
fix copyToByteArray(): remove duplicate() (#67)
  • Loading branch information
coderzc committed Jul 5, 2021
1 parent f3e617c commit 3f929ac39ce82ffb140d8311766cedb050d9591d
Showing 4 changed files with 55 additions and 3 deletions.
@@ -69,7 +69,7 @@ public ByteBuf nettyByteBuf() {

@Override
public byte[] copyToByteArray() {
return ByteBufUtil.getBytes(this.buf.duplicate(),
return ByteBufUtil.getBytes(this.buf,
this.buf.readerIndex(),
this.buf.readableBytes(),
true);
@@ -116,19 +116,28 @@ public void testCopyToByteArray() {
ByteBuffer byteBuffer2 = ByteBuffer.allocate(bytesSource.length);
byteBuffer2 = byteBuffer2.put(bytesSource);
byteBuffer2.flip();

int position = byteBuffer2.position();
int remaining = byteBuffer2.remaining();
NioManagedBuffer nioManagedBuffer2 = new NioManagedBuffer(byteBuffer2);
byte[] bytes2 = nioManagedBuffer2.copyToByteArray();
Assert.assertArrayEquals(bytesSource, bytes2);
Assert.assertNotSame(bytesSource, bytes2);
Assert.assertEquals(position, byteBuffer2.position());
Assert.assertEquals(remaining, byteBuffer2.remaining());

ByteBuf buf3 = Unpooled.directBuffer(bytesSource.length);
try {
buf3 = buf3.writeBytes(bytesSource);
int readerIndex = buf3.readerIndex();
int readableBytes = buf3.readableBytes();
NettyManagedBuffer nettyManagedBuffer3 =
new NettyManagedBuffer(buf3);
byte[] bytes3 = nettyManagedBuffer3.copyToByteArray();
Assert.assertArrayEquals(bytesSource, bytes3);
Assert.assertNotSame(bytesSource, bytes3);
Assert.assertEquals(readerIndex, buf3.readerIndex());
Assert.assertEquals(readableBytes, buf3.readableBytes());
} finally {
buf3.release();
}
@@ -31,7 +31,6 @@
import org.junit.Before;
import org.mockito.Mockito;

import com.baidu.hugegraph.computer.suite.unit.UnitTestBase;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.config.Config;
import com.baidu.hugegraph.computer.core.network.ClientHandler;
@@ -45,6 +44,7 @@
import com.baidu.hugegraph.computer.core.network.TransportUtil;
import com.baidu.hugegraph.computer.core.network.connection.ConnectionManager;
import com.baidu.hugegraph.computer.core.network.connection.TransportConnectionManager;
import com.baidu.hugegraph.computer.suite.unit.UnitTestBase;
import com.baidu.hugegraph.config.ConfigOption;
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.testutil.Whitebox;
@@ -92,6 +92,7 @@ public void setup() {
Configurator.setAllLevels("com.baidu.hugegraph", Level.DEBUG);
OPTIONS.put(ComputerOptions.TRANSPORT_SERVER_HOST, host);
OPTIONS.put(ComputerOptions.TRANSPORT_IO_MODE, "AUTO");
OPTIONS.put(ComputerOptions.TRANSPORT_SERVER_PORT, "0");
this.initOption();
Object[] objects = new Object[OPTIONS.size() * 2];
Set<Map.Entry<ConfigOption<?>, String>> kvs = OPTIONS.entrySet();
@@ -36,16 +36,17 @@
import org.mockito.Mockito;
import org.slf4j.Logger;

import com.baidu.hugegraph.computer.suite.unit.UnitTestBase;
import com.baidu.hugegraph.computer.core.common.ComputerContext;
import com.baidu.hugegraph.computer.core.common.exception.ComputerException;
import com.baidu.hugegraph.computer.core.common.exception.TransportException;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.network.ConnectionId;
import com.baidu.hugegraph.computer.core.network.TransportConf;
import com.baidu.hugegraph.computer.core.network.buffer.ManagedBuffer;
import com.baidu.hugegraph.computer.core.network.message.Message;
import com.baidu.hugegraph.computer.core.network.message.MessageType;
import com.baidu.hugegraph.computer.core.util.StringEncoding;
import com.baidu.hugegraph.computer.suite.unit.UnitTestBase;
import com.baidu.hugegraph.concurrent.BarrierEvent;
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.testutil.Whitebox;
@@ -145,6 +146,47 @@ public void testSend() throws IOException {
}
}

@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);
ManagedBuffer 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 = buffer.copyToByteArray();
Assert.assertArrayEquals(sourceBytes, bytes);
Assert.assertNotSame(sourceBytes, bytes);

byte[] bytes2 = buffer.copyToByteArray();
Assert.assertArrayEquals(sourceBytes, bytes2);
Assert.assertNotSame(sourceBytes, bytes2);
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();
}

@Test
public void testStartSessionWithTimeout() throws IOException {
NettyTransportClient client = (NettyTransportClient) this.oneClient();

0 comments on commit 3f929ac

Please sign in to comment.