You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by ji...@apache.org on 2024/10/31 02:49:25 UTC

(incubator-seata) branch 2.x updated: feature: add fastjson2 serializer support (#6904)

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

jianbin 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 bf0d11a394 feature: add fastjson2 serializer support (#6904)
bf0d11a394 is described below

commit bf0d11a3944cf0b641472e55899bd437f250bde1
Author: GoodBoyCoder <go...@outlook.com>
AuthorDate: Thu Oct 31 10:49:21 2024 +0800

    feature: add fastjson2 serializer support (#6904)
---
 all/pom.xml                                        |   5 +
 changes/en-us/2.x.md                               |   3 +-
 changes/zh-cn/2.x.md                               |   1 +
 .../core/serializer/SerializerServiceLoader.java   |   3 +-
 .../seata/core/serializer/SerializerType.java      |   7 ++
 dependencies/pom.xml                               |   6 +
 serializer/pom.xml                                 |   1 +
 serializer/seata-serializer-all/pom.xml            |   5 +
 .../pom.xml                                        |  27 ++---
 .../Fastjson2Serializer.java                       |  35 ++++++
 .../Fastjson2SerializerFactory.java                |  73 ++++++++++++
 .../org.apache.seata.core.serializer.Serializer    |  17 +++
 .../fastjson2/Fastjson2SerializerTest.java         |  85 +++++++++++++
 .../rpc/netty/mockserver/MockFastJson2Test.java    | 132 +++++++++++++++++++++
 14 files changed, 383 insertions(+), 17 deletions(-)

diff --git a/all/pom.xml b/all/pom.xml
index 7e4eb9ab29..dfffb07fee 100644
--- a/all/pom.xml
+++ b/all/pom.xml
@@ -272,6 +272,11 @@
             <artifactId>seata-serializer-hessian</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.seata</groupId>
+            <artifactId>seata-serializer-fastjson2</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.seata</groupId>
             <artifactId>seata-compressor-gzip</artifactId>
diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 2becd817c3..768ce3ebc1 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -3,11 +3,12 @@ Add changes here for all PR submitted to the 2.x branch.
 <!-- Please add the `changes` to the following location(feature/bugfix/optimize/test) based on the type of PR -->
 
 ### feature:
-
+- [[#6904](https://github.com/apache/incubator-seata/pull/6904)] add fastjson2 serializer support
 - [[#6876](https://github.com/apache/incubator-seata/pull/6876)] support kingbase
 - [[#6881](https://github.com/apache/incubator-seata/pull/6881)] support grpc
 - [[#6864](https://github.com/apache/incubator-seata/pull/6864)] support shentong database
 
+
 ### bugfix:
 - [[#6899](https://github.com/apache/incubator-seata/pull/6899)] fix file.conf read failed after package
 - [[#6890](https://github.com/apache/incubator-seata/pull/6890)] fix designerJson to standardJson: subStateMachine compensateState cannot be recognized
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 4329620e14..7a5366ec66 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -3,6 +3,7 @@
 <!-- 请根据PR的类型添加 `变更记录` 到以下对应位置(feature/bugfix/optimize/test) 下 -->
 
 ### feature:
+- [[#6904](https://github.com/apache/incubator-seata/pull/6904)] 增加Fastjson2序列化Rpc消息支持
 - [[#6876](https://github.com/apache/incubator-seata/pull/6876)] 支持人大金仓数据库(kingbase)
 - [[#6881](https://github.com/apache/incubator-seata/pull/6881)] client和server支持grpc协议
 - [[#6864](https://github.com/apache/incubator-seata/pull/6864)] 支持神通数据库(oscar)
diff --git a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java
index 0aa9bd340e..359867b510 100644
--- a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java
+++ b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java
@@ -30,6 +30,7 @@ import org.apache.seata.core.constants.ConfigurationKeys;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.seata.core.serializer.SerializerType.FASTJSON2;
 import static org.apache.seata.core.serializer.SerializerType.HESSIAN;
 import static org.apache.seata.core.serializer.SerializerType.KRYO;
 import static org.apache.seata.core.serializer.SerializerType.PROTOBUF;
@@ -46,7 +47,7 @@ public final class SerializerServiceLoader {
     private static final Logger LOGGER = LoggerFactory.getLogger(SerializerServiceLoader.class);
     private static final Configuration CONFIG = ConfigurationFactory.getInstance();
 
-    private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[]{SEATA, PROTOBUF, KRYO, HESSIAN};
+    private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[]{SEATA, PROTOBUF, KRYO, HESSIAN, FASTJSON2};
 
     private final static Map<String, Serializer> SERIALIZER_MAP = new HashMap<>();
 
diff --git a/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java b/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java
index c60067e72b..56fd8136d1 100644
--- a/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java
+++ b/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java
@@ -63,6 +63,13 @@ public enum SerializerType {
      * Math.pow(2, 5)
      */
     JACKSON((byte)0x32),
+
+    /**
+     * The fastjson2.
+     * <p>
+     * Math.pow(2, 6)
+     */
+    FASTJSON2((byte)0x64),
     ;
 
     private final byte code;
diff --git a/dependencies/pom.xml b/dependencies/pom.xml
index 15178f1416..6b93148e61 100644
--- a/dependencies/pom.xml
+++ b/dependencies/pom.xml
@@ -81,6 +81,7 @@
         <kryo.version>5.4.0</kryo.version>
         <kryo-serializers.version>0.45</kryo-serializers.version>
         <hessian.version>4.0.63</hessian.version>
+        <fastjson2.version>2.0.52</fastjson2.version>
         <groovy.version>2.4.4</groovy.version>
         <zstd.version>1.5.0-4</zstd.version>
         <xstream.version>1.4.20</xstream.version>
@@ -662,6 +663,11 @@
                 <artifactId>hessian</artifactId>
                 <version>${hessian.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.alibaba.fastjson2</groupId>
+                <artifactId>fastjson2</artifactId>
+                <version>${fastjson2.version}</version>
+            </dependency>
             <dependency>
                 <groupId>org.apache.commons</groupId>
                 <artifactId>commons-compress</artifactId>
diff --git a/serializer/pom.xml b/serializer/pom.xml
index 6b06a80f4e..246ba6c275 100644
--- a/serializer/pom.xml
+++ b/serializer/pom.xml
@@ -37,6 +37,7 @@
         <module>seata-serializer-seata</module>
         <module>seata-serializer-kryo</module>
         <module>seata-serializer-hessian</module>
+        <module>seata-serializer-fastjson2</module>
     </modules>
 
 </project>
diff --git a/serializer/seata-serializer-all/pom.xml b/serializer/seata-serializer-all/pom.xml
index a772be672a..a0d816153b 100644
--- a/serializer/seata-serializer-all/pom.xml
+++ b/serializer/seata-serializer-all/pom.xml
@@ -50,5 +50,10 @@
             <artifactId>seata-serializer-hessian</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>seata-serializer-fastjson2</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 </project>
diff --git a/serializer/seata-serializer-all/pom.xml b/serializer/seata-serializer-fastjson2/pom.xml
similarity index 66%
copy from serializer/seata-serializer-all/pom.xml
copy to serializer/seata-serializer-fastjson2/pom.xml
index a772be672a..b586ecac99 100644
--- a/serializer/seata-serializer-all/pom.xml
+++ b/serializer/seata-serializer-fastjson2/pom.xml
@@ -17,7 +17,8 @@
     limitations under the License.
 
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
         <groupId>org.apache.seata</groupId>
@@ -25,30 +26,26 @@
         <version>${revision}</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
-    <artifactId>seata-serializer-all</artifactId>
-    <name>seata-serializer-all ${project.version}</name>
-    <description>serializer-all for Seata built with Maven</description>
+    <artifactId>seata-serializer-fastjson2</artifactId>
+    <packaging>jar</packaging>
+    <name>seata-serializer-fastjson2 ${project.version}</name>
+    <description>serializer-fastjson2 for Seata built with Maven</description>
 
     <dependencies>
         <dependency>
             <groupId>${project.groupId}</groupId>
-            <artifactId>seata-serializer-seata</artifactId>
+            <artifactId>seata-common</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
-            <artifactId>seata-serializer-protobuf</artifactId>
+            <artifactId>seata-core</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
-            <groupId>${project.groupId}</groupId>
-            <artifactId>seata-serializer-kryo</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>${project.groupId}</groupId>
-            <artifactId>seata-serializer-hessian</artifactId>
-            <version>${project.version}</version>
+            <groupId>com.alibaba.fastjson2</groupId>
+            <artifactId>fastjson2</artifactId>
         </dependency>
     </dependencies>
-</project>
+
+</project>
\ No newline at end of file
diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java
new file mode 100644
index 0000000000..9c9e081db0
--- /dev/null
+++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java
@@ -0,0 +1,35 @@
+/*
+ * 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.serializer.fastjson2;
+
+import com.alibaba.fastjson2.JSONB;
+import org.apache.seata.common.loader.LoadLevel;
+import org.apache.seata.core.serializer.Serializer;
+
+@LoadLevel(name = "FASTJSON2")
+public class Fastjson2Serializer implements Serializer {
+
+    @Override
+    public <T> byte[] serialize(T t) {
+        return JSONB.toBytes(t, Fastjson2SerializerFactory.getInstance().getJsonWriterFeatureList());
+    }
+
+    @Override
+    public <T> T deserialize(byte[] bytes) {
+        return (T) JSONB.parseObject(bytes, Object.class, Fastjson2SerializerFactory.getInstance().getFilter(), Fastjson2SerializerFactory.getInstance().getJsonReaderFeatureList());
+    }
+}
diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java
new file mode 100644
index 0000000000..5e2ef29de5
--- /dev/null
+++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java
@@ -0,0 +1,73 @@
+/*
+ * 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.serializer.fastjson2;
+
+import com.alibaba.fastjson2.JSONReader;
+import com.alibaba.fastjson2.JSONWriter;
+import com.alibaba.fastjson2.filter.Filter;
+import org.apache.seata.core.serializer.SerializerSecurityRegistry;
+
+public class Fastjson2SerializerFactory {
+    private Filter autoTypeFilter;
+
+    private JSONReader.Feature[] jsonReaderFeature;
+
+    private  JSONWriter.Feature[] jsonWriterFeature;
+    private static final class InstanceHolder {
+        public static final Fastjson2SerializerFactory INSTANCE = new Fastjson2SerializerFactory();
+    }
+
+    public Fastjson2SerializerFactory() {
+        autoTypeFilter = JSONReader.autoTypeFilter(true, SerializerSecurityRegistry.getAllowClassType().toArray(new Class[]{}));
+
+        jsonReaderFeature = new JSONReader.Feature[]{
+            JSONReader.Feature.UseDefaultConstructorAsPossible,
+            // If not configured, it will be serialized based on public field and getter methods by default.
+            // After configuration, it will be deserialized based on non-static fields (including private).
+            // It will be safer under FieldBased configuration
+            JSONReader.Feature.FieldBased,
+            JSONReader.Feature.IgnoreAutoTypeNotMatch,
+            JSONReader.Feature.UseNativeObject
+        };
+
+        jsonWriterFeature = new JSONWriter.Feature[]{
+            JSONWriter.Feature.WriteClassName,
+            JSONWriter.Feature.FieldBased,
+            JSONWriter.Feature.ReferenceDetection,
+            JSONWriter.Feature.WriteNulls,
+            JSONWriter.Feature.NotWriteDefaultValue,
+            JSONWriter.Feature.NotWriteHashMapArrayListClassName,
+            JSONWriter.Feature.WriteNameAsSymbol
+        };
+    }
+
+    public static Fastjson2SerializerFactory getInstance() {
+        return Fastjson2SerializerFactory.InstanceHolder.INSTANCE;
+    }
+
+    public Filter getFilter() {
+        return autoTypeFilter;
+    }
+
+    public JSONReader.Feature[] getJsonReaderFeatureList() {
+        return jsonReaderFeature;
+    }
+
+    public JSONWriter.Feature[] getJsonWriterFeatureList() {
+        return jsonWriterFeature;
+    }
+}
diff --git a/serializer/seata-serializer-fastjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer b/serializer/seata-serializer-fastjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer
new file mode 100644
index 0000000000..1dab3f811c
--- /dev/null
+++ b/serializer/seata-serializer-fastjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer
@@ -0,0 +1,17 @@
+#
+# 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.
+#
+org.apache.seata.serializer.fastjson2.Fastjson2Serializer
\ No newline at end of file
diff --git a/serializer/seata-serializer-fastjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java b/serializer/seata-serializer-fastjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java
new file mode 100644
index 0000000000..d0dc131467
--- /dev/null
+++ b/serializer/seata-serializer-fastjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.serializer.fastjson2;
+
+import org.apache.seata.core.exception.TransactionExceptionCode;
+import org.apache.seata.core.model.BranchStatus;
+import org.apache.seata.core.model.BranchType;
+import org.apache.seata.core.protocol.ResultCode;
+import org.apache.seata.core.protocol.transaction.BranchCommitRequest;
+import org.apache.seata.core.protocol.transaction.BranchCommitResponse;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class Fastjson2SerializerTest {
+
+    private static Fastjson2Serializer fastjson2Serializer;
+
+    @BeforeAll
+    public static void before() {
+        fastjson2Serializer = new Fastjson2Serializer();
+    }
+
+
+    @Test
+    public void testBranchCommitRequest() {
+
+        BranchCommitRequest branchCommitRequest = new BranchCommitRequest();
+        branchCommitRequest.setBranchType(BranchType.AT);
+        branchCommitRequest.setXid("xid");
+        branchCommitRequest.setResourceId("resourceId");
+        branchCommitRequest.setBranchId(20190809);
+        branchCommitRequest.setApplicationData("app");
+
+        byte[] bytes = fastjson2Serializer.serialize(branchCommitRequest);
+        BranchCommitRequest t = fastjson2Serializer.deserialize(bytes);
+
+        assertThat(t.getTypeCode()).isEqualTo(branchCommitRequest.getTypeCode());
+        assertThat(t.getBranchType()).isEqualTo(branchCommitRequest.getBranchType());
+        assertThat(t.getXid()).isEqualTo(branchCommitRequest.getXid());
+        assertThat(t.getResourceId()).isEqualTo(branchCommitRequest.getResourceId());
+        assertThat(t.getBranchId()).isEqualTo(branchCommitRequest.getBranchId());
+        assertThat(t.getApplicationData()).isEqualTo(branchCommitRequest.getApplicationData());
+
+    }
+
+    @Test
+    public void testBranchCommitResponse() {
+
+        BranchCommitResponse branchCommitResponse = new BranchCommitResponse();
+        branchCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchTransactionNotExist);
+        branchCommitResponse.setBranchId(20190809);
+        branchCommitResponse.setBranchStatus(BranchStatus.PhaseOne_Done);
+        branchCommitResponse.setMsg("20190809");
+        branchCommitResponse.setXid("20190809");
+        branchCommitResponse.setResultCode(ResultCode.Failed);
+
+        byte[] bytes = fastjson2Serializer.serialize(branchCommitResponse);
+        BranchCommitResponse t = fastjson2Serializer.deserialize(bytes);
+
+        assertThat(t.getTransactionExceptionCode()).isEqualTo(branchCommitResponse.getTransactionExceptionCode());
+        assertThat(t.getBranchId()).isEqualTo(branchCommitResponse.getBranchId());
+        assertThat(t.getBranchStatus()).isEqualTo(branchCommitResponse.getBranchStatus());
+        assertThat(t.getMsg()).isEqualTo(branchCommitResponse.getMsg());
+        assertThat(t.getResultCode()).isEqualTo(branchCommitResponse.getResultCode());
+
+    }
+
+}
diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java
new file mode 100644
index 0000000000..4ad06ebe85
--- /dev/null
+++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java
@@ -0,0 +1,132 @@
+/*
+ * 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.mockserver;
+
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.common.ConfigurationTestHelper;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionException;
+import org.apache.seata.core.model.BranchType;
+import org.apache.seata.core.model.GlobalStatus;
+import org.apache.seata.core.model.TransactionManager;
+import org.apache.seata.core.rpc.netty.RmNettyRemotingClient;
+import org.apache.seata.core.rpc.netty.TmNettyRemotingClient;
+import org.apache.seata.core.serializer.SerializerType;
+import org.apache.seata.mockserver.MockCoordinator;
+import org.apache.seata.mockserver.MockServer;
+import org.apache.seata.rm.DefaultResourceManager;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * the type MockServerTest
+ */
+public class MockFastJson2Test {
+
+    static String RESOURCE_ID = "mock-action";
+
+    Logger logger = LoggerFactory.getLogger(MockFastJson2Test.class);
+
+    @BeforeAll
+    public static void before() {
+        ConfigurationFactory.reload();
+        ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT));
+        //Enable it when testing is needed. The settings here will affect the global configuration.
+        //ConfigurationTestHelper.putConfig(ConfigurationKeys.SERIALIZE_FOR_RPC, String.valueOf(SerializerType.FASTJSON2));
+        MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT);
+        TmNettyRemotingClient.getInstance().destroy();
+        RmNettyRemotingClient.getInstance().destroy();
+    }
+
+    @AfterAll
+    public static void after() {
+        //MockServer.close();
+        ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+        ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERIALIZE_FOR_RPC);
+        TmNettyRemotingClient.getInstance().destroy();
+        RmNettyRemotingClient.getInstance().destroy();
+    }
+
+    @Test
+    public void testCommit() throws TransactionException {
+        String xid = doTestCommit(0);
+        Assertions.assertEquals(1, Action1Impl.getCommitTimes(xid));
+        Assertions.assertEquals(0, Action1Impl.getRollbackTimes(xid));
+    }
+
+    @Test
+    public void testCommitRetry() throws TransactionException {
+        String xid = doTestCommit(2);
+        Assertions.assertEquals(3, Action1Impl.getCommitTimes(xid));
+        Assertions.assertEquals(0, Action1Impl.getRollbackTimes(xid));
+    }
+
+    @Test
+    public void testRollback() throws TransactionException {
+        String xid = doTestRollback(0);
+        Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid));
+        Assertions.assertEquals(1, Action1Impl.getRollbackTimes(xid));
+    }
+
+    @Test
+    public void testRollbackRetry() throws TransactionException {
+        String xid = doTestRollback(2);
+        Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid));
+        Assertions.assertEquals(3, Action1Impl.getRollbackTimes(xid));
+    }
+
+    @Test
+    public void testTm() throws Exception {
+        TmClientTest.testTm();
+    }
+
+    @Test
+    public void testRm() throws Exception {
+        RmClientTest.testRm();
+    }
+
+    private String doTestCommit(int times) throws TransactionException {
+        TransactionManager tm = TmClientTest.getTm();
+        DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID);
+
+        String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP, "test-commit", 60000);
+        MockCoordinator.getInstance().setExpectedRetry(xid, times);
+        Long branchId = rm.branchRegister(BranchType.TCC, RESOURCE_ID, "1", xid, "{\"mock\":\"mock\"}", "1");
+        GlobalStatus commit = tm.commit(xid);
+        Assertions.assertEquals(GlobalStatus.Committed, commit);
+        return xid;
+
+    }
+
+    private String doTestRollback(int times) throws TransactionException {
+        TransactionManager tm = TmClientTest.getTm();
+        DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID);
+
+        String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP, "test-rollback", 60000);
+        logger.info("doTestRollback xid:{}", xid);
+        MockCoordinator.getInstance().setExpectedRetry(xid, times);
+        Long branchId = rm.branchRegister(BranchType.TCC, RESOURCE_ID, "1", xid, "{\"mock\":\"mock\"}", "1");
+        GlobalStatus rollback = tm.rollback(xid);
+        Assertions.assertEquals(GlobalStatus.Rollbacked, rollback);
+        return xid;
+
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org