Skip to content

Commit

Permalink
update: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fengye404 committed Sep 19, 2024
1 parent a3426b7 commit 893f7f3
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 27 deletions.
41 changes: 21 additions & 20 deletions arthas-grpc-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,30 @@
<version>1.5.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>


<!-- 测试用 待删 -->
<!-- <dependency>-->
<!-- <groupId>io.grpc</groupId>-->
<!-- <artifactId>grpc-netty</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.grpc</groupId>-->
<!-- <artifactId>grpc-services</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.baidu</groupId>-->
<!-- <artifactId>jprotobuf</artifactId>-->
<!-- <version>2.4.20</version>-->
<!-- </dependency>-->

<!-- 测试用 -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-services</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
public class ProtobufProxy {
private static final String TEMPLATE_FILE = "/class_template.tpl";

private static final Map<String, ProtobufCodec> codecCache = new ConcurrentHashMap<String, ProtobufCodec>();
private static final Map<String, ProtobufCodec> codecCache = new ConcurrentHashMap<>();

private static Class<?> clazz;

private static MiniTemplator miniTemplator;

private static List<ProtobufField> protobufFields;

public static ProtobufCodec getCodecCacheSide(Class<?> clazz) {
ProtobufCodec codec = codecCache.get(clazz.getName());
public static <T> ProtobufCodec<T> getCodecCacheSide(Class<T> clazz) {
ProtobufCodec<T> codec = codecCache.get(clazz.getName());
if (codec != null) {
return codec;
}
Expand All @@ -46,7 +46,7 @@ public static ProtobufCodec getCodecCacheSide(Class<?> clazz) {
}
}

public static <T> ProtobufCodec create(Class<T> clazz) {
public static <T> ProtobufCodec<T> create(Class<T> clazz) {
Objects.requireNonNull(clazz);
if (clazz.getAnnotation(ProtobufClass.class) == null) {
throw new IllegalArgumentException(clazz + "class is not annotated with @ProtobufClass");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public static String getWriteByteDynamicString(ProtobufField protobufField) {
.append(JAVA_LINE_BREAK).append("}").append(LINE_BREAK);
return sb.toString();
} else if (protobufField.isMap()) {
sb.append("Field.writeMap(").append(CODE_OUTPUT_STREAM_OBJ_NAME).append(",");
sb.append("ProtoBufUtil.writeMap(").append(CODE_OUTPUT_STREAM_OBJ_NAME).append(",");
sb.append(order).append(",").append(dynamicFieldName);

String joinedSentence = getMapFieldGenericParameterString(protobufField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import com.taobao.arthas.grpc.server.protobuf.annotation.ProtobufClass;
import lombok.ToString;

import java.util.List;

Expand All @@ -15,7 +14,6 @@
*/

@ProtobufClass
@ToString
public class ArthasSampleRequest {

private String name;
Expand Down
13 changes: 13 additions & 0 deletions arthas-grpc-server/src/test/java/grpc/GrpcTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package grpc; /**
* @author: 風楪
* @date: 2024/9/19 22:20
*/

/**
* @author: FengYe
* @date: 2024/9/19 22:20
* @description: grpc.GrpcTest
*/
public class GrpcTest {

}
51 changes: 51 additions & 0 deletions arthas-grpc-server/src/test/java/protobuf/ProtoBufTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package protobuf; /**
* @author: 風楪
* @date: 2024/9/19 22:35
*/

import com.taobao.arthas.grpc.server.protobuf.ProtobufCodec;
import com.taobao.arthas.grpc.server.protobuf.ProtobufProxy;
import com.taobao.arthas.grpc.server.service.req.ArthasSampleRequest;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author: FengYe
* @date: 2024/9/19 22:35
* @description: protobuf.ProtoBufTest
*/
public class ProtoBufTest {
@Test
public void testEncodeAndDecode() {
ProtoBufTestReq protoBufTestReq = new ProtoBufTestReq();
protoBufTestReq.setName("test");
protoBufTestReq.setAge(18);
protoBufTestReq.setPrice(100L);
protoBufTestReq.setStatus(ArthasSampleRequest.StatusEnum.START);
List<ProtoBufTestReq.TestClass> list = new ArrayList<>();
list.add(new ProtoBufTestReq.TestClass("test1"));
list.add(new ProtoBufTestReq.TestClass("test2"));
list.add(new ProtoBufTestReq.TestClass("test3"));
Map<String,String> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
protoBufTestReq.setTestList(list);
protoBufTestReq.setTestMap(map);

try {
ProtobufCodec<ProtoBufTestReq> protobufCodec = ProtobufProxy.getCodecCacheSide(ProtoBufTestReq.class);
byte[] encode = protobufCodec.encode(protoBufTestReq);
ProtoBufTestReq decode = protobufCodec.decode(encode);
Assert.assertEquals(protoBufTestReq.toString(), decode.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
129 changes: 129 additions & 0 deletions arthas-grpc-server/src/test/java/protobuf/ProtoBufTestReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package protobuf;/**
* @author: 風楪
* @date: 2024/9/19 22:38
*/

import com.taobao.arthas.grpc.server.protobuf.annotation.ProtobufClass;
import com.taobao.arthas.grpc.server.service.req.ArthasSampleRequest;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author: FengYe
* @date: 2024/9/19 22:38
* @description: ProtoBufTestReq
*/
@ProtobufClass
public class ProtoBufTestReq {
private String name;
private double age;
private long price;
private ArthasSampleRequest.StatusEnum status;
private List<TestClass> testList;
private Map<String, String> testMap;

@ProtobufClass
public enum StatusEnum {
START(1, "开始"),
STOP(2, "结束");

StatusEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}

private int code;
private String desc;
}

@ProtobufClass
public static class TestClass {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

// 注意被 @ProtobufClass 注解的 class 必须添加无参构造函数
public TestClass() {
}

public TestClass(String name) {
this.name = name;
}

@Override
public String toString() {
return "TestClass{" +
"name='" + name + '\'' +
'}';
}
}

public List<TestClass> getTestList() {
return testList;
}

public void setTestList(List<TestClass> testList) {
this.testList = testList;
}

public ArthasSampleRequest.StatusEnum getStatus() {
return status;
}

public void setStatus(ArthasSampleRequest.StatusEnum status) {
this.status = status;
}

public long getPrice() {
return price;
}

public void setPrice(long price) {
this.price = price;
}

public double getAge() {
return age;
}

public void setAge(double age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Map<String, String> getTestMap() {
return testMap;
}

public void setTestMap(Map<String, String> testMap) {
this.testMap = testMap;
}

@Override
public java.lang.String toString() {
return "ProtoBufTestReq{" +
"name='" + name + '\'' +
", age=" + age +
", price=" + price +
", status=" + status +
", testList=" + testList +
", testMap=" + testMap +
'}';
}
}

0 comments on commit 893f7f3

Please sign in to comment.