This is an automated email from the ASF dual-hosted git repository.

jimin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new baf43646da test: add UT for gRPC Encoder/Decoder (#7221)
baf43646da is described below

commit baf43646da42728e94af9692cf8f7fa8c0e387b2
Author: xingfudeshi <[email protected]>
AuthorDate: Sat Mar 15 21:08:13 2025 +0800

    test: add UT for gRPC Encoder/Decoder (#7221)
---
 changes/en-us/2.x.md                               |   2 +
 changes/zh-cn/2.x.md                               |   2 +
 .../seata/core/rpc/netty/grpc/GrpcDecoderTest.java | 101 +++++++++++++++++++++
 .../seata/core/rpc/netty/grpc/GrpcEncoderTest.java |  65 +++++++++++++
 .../core/rpc/netty/grpc/GrpcHeaderEnumTest.java    |  33 +++++++
 5 files changed, 203 insertions(+)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index e01249399f..f1b931b16a 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -74,6 +74,8 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7189](https://github.com/apache/incubator-seata/pull/7189)] fix the 
runtime exception in the saga test case
 - [[#7199](https://github.com/apache/incubator-seata/pull/7199)] add some UT 
cases for client processor
 - [[#7203](https://github.com/apache/incubator-seata/pull/7203)] Refactored 
tests in rm.datasource.sql.Druid and seata-sqlparser-druid module
+- [[#7221](https://github.com/apache/incubator-seata/pull/7221)] add UT for 
gRPC Encoder/Decode
+
 
 ### refactor:
 
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 7a93afe32c..e981bb87e3 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -74,6 +74,8 @@
 - [[#7189](https://github.com/apache/incubator-seata/pull/7189)] 修复saga测试用例运行异常
 - [[#7199](https://github.com/apache/incubator-seata/pull/7199)] 增加 client 
processor 单测用例
 - [[#7203](https://github.com/apache/incubator-seata/pull/7203)] 重构了 
rm.datasource.sql.Druid 和 seata-sqlparser-druid 模块中的测试
+- [[#7221](https://github.com/apache/incubator-seata/pull/7221)] 增加 gRPC 
Encoder/Decoder的测试用例
+
 
 ### refactor:
 
diff --git 
a/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcDecoderTest.java 
b/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcDecoderTest.java
new file mode 100644
index 0000000000..37b61cd2a2
--- /dev/null
+++ 
b/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcDecoderTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.core.rpc.netty.grpc;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http2.DefaultHttp2DataFrame;
+import io.netty.handler.codec.http2.Http2DataFrame;
+import org.apache.seata.core.protocol.HeartbeatMessage;
+import org.apache.seata.core.protocol.ProtocolConstants;
+import org.apache.seata.core.protocol.RpcMessage;
+import org.apache.seata.core.protocol.generated.GrpcMessageProto;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.verify;
+
+public class GrpcDecoderTest {
+    private GrpcDecoder grpcDecoder;
+    @Mock
+    private ChannelHandlerContext ctx;
+
+    @BeforeEach
+    public void setUp() {
+        MockitoAnnotations.openMocks(this);
+        grpcDecoder = new GrpcDecoder();
+    }
+
+    private byte[] createMessageBytes(GrpcMessageProto proto) {
+        byte[] data = proto.toByteArray();
+        byte[] lengthBytes = new byte[]{0, 0, 0, 0, (byte) data.length};
+        byte[] messageBytes = new byte[lengthBytes.length + data.length];
+        System.arraycopy(lengthBytes, 0, messageBytes, 0, lengthBytes.length);
+        System.arraycopy(data, 0, messageBytes, lengthBytes.length, 
data.length);
+        return messageBytes;
+    }
+
+    private GrpcMessageProto createHeartbeatRequestProto() {
+        return GrpcMessageProto.newBuilder()
+                .setMessageType(ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST)
+                .setId(1)
+                .build();
+    }
+
+    private void verifyRpcMessage(RpcMessage rpcMessage) {
+        assertEquals(ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST, 
rpcMessage.getMessageType());
+        assertEquals(1, rpcMessage.getId());
+        assertEquals(HeartbeatMessage.PING, rpcMessage.getBody());
+    }
+
+    @Test
+    public void testOnDataRead() throws Exception {
+        GrpcMessageProto proto = createHeartbeatRequestProto();
+        ByteBuf byteBuf = Unpooled.wrappedBuffer(createMessageBytes(proto));
+        Http2DataFrame dataFrame = new DefaultHttp2DataFrame(byteBuf);
+
+        grpcDecoder.onDataRead(ctx, dataFrame);
+
+        ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
+        verify(ctx).fireChannelRead(captor.capture());
+        verifyRpcMessage((RpcMessage) captor.getValue());
+    }
+
+    @Test
+    public void testChannelRead() throws Exception {
+        GrpcMessageProto proto = createHeartbeatRequestProto();
+        ByteBuf byteBuf = Unpooled.wrappedBuffer(createMessageBytes(proto));
+        Http2DataFrame dataFrame = new DefaultHttp2DataFrame(byteBuf, true);
+
+        try {
+            grpcDecoder.channelRead(ctx, dataFrame);
+
+            ArgumentCaptor<Object> captor = 
ArgumentCaptor.forClass(Object.class);
+            verify(ctx).fireChannelRead(captor.capture());
+            verifyRpcMessage((RpcMessage) captor.getValue());
+        } finally {
+            if (dataFrame.refCnt() > 0) {
+                dataFrame.release();
+            }
+        }
+    }
+}
diff --git 
a/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcEncoderTest.java 
b/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcEncoderTest.java
new file mode 100644
index 0000000000..63a9aa8999
--- /dev/null
+++ 
b/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcEncoderTest.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.core.rpc.netty.grpc;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelPromise;
+import io.netty.handler.codec.http2.DefaultHttp2DataFrame;
+import io.netty.handler.codec.http2.DefaultHttp2HeadersFrame;
+import org.apache.seata.core.protocol.HeartbeatMessage;
+import org.apache.seata.core.protocol.ProtocolConstants;
+import org.apache.seata.core.protocol.RpcMessage;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.HashMap;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class GrpcEncoderTest {
+    private GrpcEncoder grpcEncoder;
+
+    @Mock
+    private ChannelHandlerContext ctx;
+
+    @Mock
+    private ChannelPromise promise;
+
+    @BeforeEach
+    public void setUp() {
+        MockitoAnnotations.openMocks(this);
+        grpcEncoder = new GrpcEncoder();
+    }
+
+    @Test
+    public void testWrite() throws Exception {
+        RpcMessage rpcMessage = new RpcMessage();
+        rpcMessage.setMessageType(ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST);
+        rpcMessage.setBody(HeartbeatMessage.PING);
+        rpcMessage.setId(1);
+        rpcMessage.setHeadMap(new HashMap<>());
+
+        grpcEncoder.write(ctx, rpcMessage, promise);
+
+        verify(ctx, 
times(1)).writeAndFlush(any(DefaultHttp2HeadersFrame.class));
+        verify(ctx, times(1)).writeAndFlush(any(DefaultHttp2DataFrame.class));
+    }
+}
diff --git 
a/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcHeaderEnumTest.java
 
b/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcHeaderEnumTest.java
new file mode 100644
index 0000000000..00161374e4
--- /dev/null
+++ 
b/core/src/test/java/org/apache/seata/core/rpc/netty/grpc/GrpcHeaderEnumTest.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.core.rpc.netty.grpc;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class GrpcHeaderEnumTest {
+
+    @Test
+    public void testHeaderValues() {
+        assertEquals("grpc-status", GrpcHeaderEnum.GRPC_STATUS.header);
+        assertEquals(":status", GrpcHeaderEnum.HTTP2_STATUS.header);
+        assertEquals("content-type", GrpcHeaderEnum.GRPC_CONTENT_TYPE.header);
+        assertEquals("codec-type", GrpcHeaderEnum.CODEC_TYPE.header);
+        assertEquals("compress-type", GrpcHeaderEnum.COMPRESS_TYPE.header);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to