This is an automated email from the ASF dual-hosted git repository.
elserj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ratis.git
The following commit(s) were added to refs/heads/master by this push:
new a0203d4 RATIS-1027.Implement un-finished tests in
TestLogServiceProtoUtil
a0203d4 is described below
commit a0203d46f23a75605d920dc39683c4e393c46750
Author: amaliujia <[email protected]>
AuthorDate: Fri Aug 7 17:59:17 2020 -0700
RATIS-1027.Implement un-finished tests in TestLogServiceProtoUtil
Closes #172
Signed-off-by: Josh Elser <[email protected]>
---
.../ratis/logservice/util/LogServiceProtoUtil.java | 6 +-
.../logservice/util/TestLogServiceProtoUtil.java | 104 +++++++++++++++------
2 files changed, 79 insertions(+), 31 deletions(-)
diff --git
a/ratis-logservice/src/main/java/org/apache/ratis/logservice/util/LogServiceProtoUtil.java
b/ratis-logservice/src/main/java/org/apache/ratis/logservice/util/LogServiceProtoUtil.java
index 89b53cb..a743e89 100644
---
a/ratis-logservice/src/main/java/org/apache/ratis/logservice/util/LogServiceProtoUtil.java
+++
b/ratis-logservice/src/main/java/org/apache/ratis/logservice/util/LogServiceProtoUtil.java
@@ -233,10 +233,10 @@ public class LogServiceProtoUtil {
public static AppendLogEntryReplyProto toAppendLogReplyProto(List<Long> ids,
Throwable t) {
AppendLogEntryReplyProto.Builder builder =
AppendLogEntryReplyProto.newBuilder();
- if (t!= null) {
+ if (t != null) {
builder.setException(toLogException(t));
- } else if (ids != null){
- for(long id: ids) {
+ } else if (ids != null) {
+ for (long id: ids) {
builder.addRecordId(id);
}
}
diff --git
a/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java
b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java
index 3187868..fa736f3 100644
---
a/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java
+++
b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/TestLogServiceProtoUtil.java
@@ -20,22 +20,26 @@ package org.apache.ratis.logservice.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import java.io.IOException;
+import java.nio.charset.Charset;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import org.apache.ratis.logservice.api.LogName;
import org.apache.ratis.logservice.api.LogStream;
+import org.apache.ratis.logservice.api.LogStream.State;
import org.apache.ratis.logservice.proto.LogServiceProtos.*;
-import org.junit.Ignore;
+import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
import org.junit.Test;
public class TestLogServiceProtoUtil {
-
@Test
public void testAppendRequest() {
LogName name = LogName.of("test");
- List<byte[]> entries = new ArrayList<byte[]>();
+ List<byte[]> entries = new ArrayList<>();
byte[] e1 = new byte[] {1,1};
byte[] e2 = new byte[] {2,2};
entries.add(e1);
@@ -47,20 +51,45 @@ public class TestLogServiceProtoUtil {
assertEquals(2, request.getDataCount());
assertTrue(TestUtils.equals(e1, request.getData(0).toByteArray()));
assertTrue(TestUtils.equals(e2, request.getData(1).toByteArray()));
-
}
@Test
public void testAppendReply() {
- List<byte[]> entries = new ArrayList<byte[]>();
- byte[] e1 = new byte[] {1,1};
- byte[] e2 = new byte[] {2,2};
- entries.add(e1);
- entries.add(e2);
-
- AppendLogEntryReplyProto proto =
+ List<Long> ids = Arrays.asList(1L, 2L, 3L);
+ IOException ioException = new IOException("test");
+
+ StackTraceElement[] trace = ioException.getStackTrace();
+ StringBuffer buf = new StringBuffer();
+ for (StackTraceElement el: trace) {
+ buf.append(el.toString()).append("\n");
+ }
+ String strace = buf.toString();
+ LogServiceException expectedLogServiceException =
+ LogServiceException.newBuilder()
+ .setExceptionClassName("java.io.IOException")
+ .setErrorMsg("test")
+ .setStacktrace(ByteString.copyFrom(strace,
Charset.defaultCharset()))
+ .build();
+
+ AppendLogEntryReplyProto proto1 =
+ LogServiceProtoUtil.toAppendLogReplyProto(ids, null);
+ assertEquals(LogServiceException.getDefaultInstance(),
proto1.getException());
+ assertEquals(ids, proto1.getRecordIdList());
+
+ AppendLogEntryReplyProto proto2 =
+ LogServiceProtoUtil.toAppendLogReplyProto(null, ioException);
+ assertEquals(expectedLogServiceException, proto2.getException());
+ assertEquals(Collections.EMPTY_LIST, proto2.getRecordIdList());
+
+ AppendLogEntryReplyProto proto3 =
+ LogServiceProtoUtil.toAppendLogReplyProto(ids, ioException);
+ assertEquals(expectedLogServiceException, proto3.getException());
+ assertEquals(Collections.EMPTY_LIST, proto3.getRecordIdList());
+
+ AppendLogEntryReplyProto proto4 =
LogServiceProtoUtil.toAppendLogReplyProto(null, null);
- //TODO finish test
+ assertEquals(LogServiceException.getDefaultInstance(),
proto4.getException());
+ assertEquals(Collections.EMPTY_LIST, proto4.getRecordIdList());
}
@Test
@@ -74,12 +103,11 @@ public class TestLogServiceProtoUtil {
assertEquals(name.getName(), request.getLogName().getName());
assertEquals(100, request.getStartRecordId());
assertEquals(5, request.getNumRecords());
-
}
@Test
public void testReadReply() {
- List<byte[]> entries = new ArrayList<byte[]>();
+ List<byte[]> entries = new ArrayList<>();
byte[] e1 = new byte[] {1,1};
byte[] e2 = new byte[] {2,2};
entries.add(e1);
@@ -95,7 +123,6 @@ public class TestLogServiceProtoUtil {
@Test
public void testGetLengthReply() {
-
long len = 100;
GetLogLengthReplyProto proto =
LogServiceProtoUtil.toGetLogLengthReplyProto(len, null);
@@ -120,7 +147,6 @@ public class TestLogServiceProtoUtil {
@Test
public void testGetStartIndexReply() {
-
long index = 100;
GetLogStartIndexReplyProto proto =
LogServiceProtoUtil.toGetLogStartIndexReplyProto(index, null);
@@ -137,12 +163,31 @@ public class TestLogServiceProtoUtil {
@Test
public void testSyncReply() {
-
- SyncLogReplyProto proto =
+ IOException ioException = new IOException("test");
+
+ StackTraceElement[] trace = ioException.getStackTrace();
+ StringBuffer buf = new StringBuffer();
+ for (StackTraceElement el: trace) {
+ buf.append(el.toString()).append("\n");
+ }
+ String strace = buf.toString();
+ LogServiceException expectedLogServiceException =
+ LogServiceException.newBuilder()
+ .setExceptionClassName("java.io.IOException")
+ .setErrorMsg("test")
+ .setStacktrace(ByteString.copyFrom(strace,
Charset.defaultCharset()))
+ .build();
+
+ SyncLogReplyProto proto1 =
LogServiceProtoUtil.toSyncLogReplyProto(0, null);
- //TODO finish test
- }
+ assertEquals(LogServiceException.getDefaultInstance(),
proto1.getException());
+ assertEquals(0, proto1.getLastRecordId());
+ SyncLogReplyProto proto2 =
+ LogServiceProtoUtil.toSyncLogReplyProto(1, ioException);
+ assertEquals(expectedLogServiceException, proto2.getException());
+ assertEquals(0, proto2.getLastRecordId());
+ }
@Test
public void testListLogsReply() {
@@ -150,7 +195,6 @@ public class TestLogServiceProtoUtil {
//TODO finish test
}
-
//GET STATE
@Test
public void testGetStateRequest() {
@@ -158,16 +202,20 @@ public class TestLogServiceProtoUtil {
LogServiceRequestProto proto =
LogServiceProtoUtil.toGetStateRequestProto(name);
GetStateRequestProto request = proto.getGetState();
assertEquals(name.getName(), request.getLogName().getName());
- //TODO finish
}
@Test
- @Ignore
public void testGetStateReply() {
- LogStream logStream = null;
- GetStateReplyProto proto =
LogServiceProtoUtil.toGetStateReplyProto(LogStream.State.OPEN);
- //TODO finish
-
+ GetStateReplyProto protoOpen =
LogServiceProtoUtil.toGetStateReplyProto(LogStream.State.OPEN);
+ assertEquals(LogStreamState.OPEN, protoOpen.getState());
+ GetStateReplyProto protoClosed =
LogServiceProtoUtil.toGetStateReplyProto(State.CLOSED);
+ assertEquals(LogStreamState.CLOSED, protoClosed.getState());
+ GetStateReplyProto protoArchiving=
LogServiceProtoUtil.toGetStateReplyProto(LogStream.State.ARCHIVING);
+ assertEquals(LogStreamState.ARCHIVING, protoArchiving.getState());
+ GetStateReplyProto protoArchived =
LogServiceProtoUtil.toGetStateReplyProto(State.ARCHIVED);
+ assertEquals(LogStreamState.ARCHIVED, protoArchived.getState());
+ GetStateReplyProto protoDeleted =
LogServiceProtoUtil.toGetStateReplyProto(LogStream.State.DELETED);
+ assertEquals(LogStreamState.DELETED, protoDeleted.getState());
}
@@ -179,6 +227,6 @@ public class TestLogServiceProtoUtil {
LogStream.State.CLOSED);
ChangeStateLogRequestProto request = proto.getChangeState();
assertEquals(name.getName(), request.getLogName().getName());
- //TODO finish
+ assertEquals(LogStreamState.CLOSED, request.getState());
}
}