Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixbug/install snapshot bug #80

Merged
merged 16 commits into from
Apr 1, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public Message handleGetFile(GetFileRequest request, RpcRequestClosure done) {
final int read = reader
.readFile(dataBuffer, request.getFilename(), request.getOffset(), request.getCount());
responseBuilder.setReadSize(read);
if (read == -1) {
responseBuilder.setEof(true);
}
responseBuilder.setEof(read == -1);
final ByteBuffer buf = dataBuffer.getBuffer();
buf.flip();
if (!buf.hasRemaining()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ protected int readFileWithMeta(ByteBufferCollector buf, String fileName, Message
} else {
final long fsize = file.length();
if (fsize < 0) {
LOG.warn("Invlaid file length {}", filePath);
LOG.warn("Invalid file length {}", filePath);
return -1;
}
if (fsize == offset + maxCount) {
if (fsize == offset + nread) {
return -1;
} else {
return totalRead;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import com.alipay.sofa.jraft.test.TestUtils;
import com.google.protobuf.Message;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class FileServiceTest {
Expand Down Expand Up @@ -107,8 +109,41 @@ public void testGetFileData() throws IOException {
Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(bizContext, asyncContext));
assertTrue(msg instanceof RpcRequests.GetFileResponse);
RpcRequests.GetFileResponse response = (RpcRequests.GetFileResponse) msg;
assertEquals(response.getEof(), true);
assertTrue(response.getEof());
assertEquals("jraft is great!", new String(response.getData().toByteArray()));
assertEquals(-1, response.getReadSize());
}

private String writeLargeData() throws IOException {
File file = new File(this.path + File.separator + "data");
String data = "jraft is great!";
for (int i = 0; i < 1000; i++) {
FileUtils.writeStringToFile(file, data, true);
}
return data;
}

@Test
public void testGetLargeFileData() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个测试还是不够的,还是希望能模拟真实的情况,节点添加下安装较大的 snapshot ,可以模拟出来的。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个测试更类似单元测试,放到 FileServiceTest 更合适,这里更多还是模拟运行的集成测试。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试还没完成,这个只是 FileService 的测试,在 NodeTest 中还会补充一个 InstallSnapshot 的测试

final String data = writeLargeData();
long readerId = FileService.getInstance().addReader(this.fileReader);
RpcRequests.GetFileRequest request = RpcRequests.GetFileRequest.newBuilder().setCount(2048)
.setFilename("data").setOffset(0).setReaderId(readerId).build();
BizContext bizContext = Mockito.mock(BizContext.class);
AsyncContext asyncContext = Mockito.mock(AsyncContext.class);
Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(bizContext, asyncContext));
assertTrue(msg instanceof RpcRequests.GetFileResponse);
RpcRequests.GetFileResponse response = (RpcRequests.GetFileResponse) msg;
assertFalse(response.getEof());
final byte[] sourceArray = data.getBytes();
final byte[] respData = response.getData().toByteArray();
final int length = sourceArray.length;
int offset = 0;
while (offset + length <= respData.length) {
final byte[] respArray = new byte[length];
System.arraycopy(respData, offset, respArray, 0, length);
assertArrayEquals(sourceArray, respArray);
offset += length;
}
}
}