This is an automated email from the ASF dual-hosted git repository.
funky-eyes 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 1c0369c4af optimize: optimize seata-server/test perf (#8112)
1c0369c4af is described below
commit 1c0369c4af5bee125de9060f5ca35eb04545341e
Author: jimin <[email protected]>
AuthorDate: Sat May 30 20:53:53 2026 +0800
optimize: optimize seata-server/test perf (#8112)
---
.../org/apache/seata/mockserver/MockServer.java | 17 ++--
server/pom.xml | 29 ++++++
test-suite/test-new-version/pom.xml | 8 ++
.../seata/core/rpc/netty/BaseNettyClientTest.java | 62 +++++++++---
.../core/rpc/netty/NettyClientCoverageTest.java | 109 ++++++++++++++++++++
.../seata/core/rpc/netty/mockserver/GrpcTest.java | 8 +-
.../rpc/netty/mockserver/MockFastJson2Test.java | 13 ++-
.../rpc/netty/mockserver/MockGrpcServerTest.java | 12 ++-
.../core/rpc/netty/mockserver/MockServerTest.java | 8 +-
.../netty/mockserver/ProtocolTestConstants.java | 21 +++-
.../AbstractMultiVersionCompatibilityTest.java | 29 ++++--
.../MultiVersionCompatibilityCoverageTest.java | 112 +++++++++++++++++++++
.../rocketmq/SeataMQProducerSendTest.java | 8 +-
.../seata/saga/engine/db/AbstractServerTest.java | 48 ++++++++-
14 files changed, 428 insertions(+), 56 deletions(-)
diff --git
a/mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java
b/mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java
index 68a653e08d..cd6803faaf 100644
--- a/mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java
+++ b/mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java
@@ -25,6 +25,7 @@ import org.apache.seata.common.util.NumberUtils;
import org.apache.seata.common.util.UUIDGenerator;
import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.core.constants.ConfigurationKeys;
+import org.apache.seata.core.rpc.ShutdownHook;
import org.apache.seata.core.rpc.netty.NettyServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -48,6 +49,7 @@ public class MockServer {
private static MockNettyRemotingServer nettyRemotingServer;
private static volatile boolean inited = false;
+ private static volatile int actualPort;
public static final int MOCK_DEFAULT_PORT = 10091;
public static String MOCK_SEATA_PORT_KEY = "SEATA_MOCK_PORT";
@@ -105,21 +107,22 @@ public class MockServer {
coordinator.setRemotingServer(nettyRemotingServer);
nettyRemotingServer.setHandler(coordinator);
nettyRemotingServer.init();
- Runtime.getRuntime().addShutdownHook(new Thread(new
Runnable() {
- @Override
- public void run() {
- LOGGER.info("system is closing , pid info: "
- +
ManagementFactory.getRuntimeMXBean().getName());
- }
- }));
+ ShutdownHook.getInstance()
+ .addDisposable(() -> LOGGER.info("system is
closing , pid info: "
+ +
ManagementFactory.getRuntimeMXBean().getName()));
LOGGER.info(
"pid info: " +
ManagementFactory.getRuntimeMXBean().getName());
+ actualPort = port;
LOGGER.info("MockServer started on port: {}", port);
}
}
}
}
+ public static int getPort() {
+ return actualPort;
+ }
+
public static void close() {
if (inited) {
synchronized (MockServer.class) {
diff --git a/server/pom.xml b/server/pom.xml
index d63d437709..9de1f40786 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -445,6 +445,35 @@
</execution>
</executions>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <forkCount>2</forkCount>
+ <reuseForks>true</reuseForks>
+ <excludes>
+ <exclude>**/cluster/raft/execute/*Test.java</exclude>
+ <exclude>**/cluster/raft/RaftServerTest.java</exclude>
+ </excludes>
+ </configuration>
+ <executions>
+ <execution>
+ <id>raft-tests</id>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ <configuration>
+ <forkCount>1</forkCount>
+ <reuseForks>true</reuseForks>
+ <excludes combine.self="override"/>
+ <includes>
+
<include>**/cluster/raft/execute/*Test.java</include>
+
<include>**/cluster/raft/RaftServerTest.java</include>
+ </includes>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
diff --git a/test-suite/test-new-version/pom.xml
b/test-suite/test-new-version/pom.xml
index 8ae6cadb2c..d19007e030 100644
--- a/test-suite/test-new-version/pom.xml
+++ b/test-suite/test-new-version/pom.xml
@@ -41,6 +41,14 @@
<skip>true</skip>
</configuration>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <forkCount>2</forkCount>
+ <reuseForks>true</reuseForks>
+ </configuration>
+ </plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/BaseNettyClientTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/BaseNettyClientTest.java
index 2538732ab3..8664b97487 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/BaseNettyClientTest.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/BaseNettyClientTest.java
@@ -17,10 +17,10 @@
package org.apache.seata.core.rpc.netty;
import org.apache.seata.common.ConfigurationKeys;
-import org.apache.seata.common.ConfigurationTestHelper;
import org.apache.seata.common.XID;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.common.util.UUIDGenerator;
+import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.server.coordinator.DefaultCoordinator;
import org.apache.seata.server.session.SessionHolder;
import org.junit.jupiter.api.AfterEach;
@@ -42,6 +42,10 @@ public abstract class BaseNettyClientTest {
private static final Logger LOGGER =
LoggerFactory.getLogger(BaseNettyClientTest.class);
+ private String originalGroupList;
+ private String originalServerPort;
+ private String originalShutdownWait;
+
/**
* Get a dynamic available port
*/
@@ -119,19 +123,34 @@ public abstract class BaseNettyClientTest {
serverConfig.setServerListenPort(port);
NettyRemotingServer nettyRemotingServer = new
NettyRemotingServer(workingThreads, serverConfig);
+ AtomicBoolean serverStatus = new AtomicBoolean();
new Thread(() -> {
- SessionHolder.init(null);
-
nettyRemotingServer.setHandler(DefaultCoordinator.getInstance(nettyRemotingServer));
- // set registry
- XID.setIpAddress(NetUtil.getLocalIp());
- XID.setPort(port);
- // init snowflake for transactionId, branchId
- UUIDGenerator.init(1L);
- nettyRemotingServer.init();
+ try {
+ SessionHolder.init(null);
+
nettyRemotingServer.setHandler(DefaultCoordinator.getInstance(nettyRemotingServer));
+ XID.setIpAddress(NetUtil.getLocalIp());
+ XID.setPort(port);
+ UUIDGenerator.init(1L);
+ nettyRemotingServer.init();
+ serverStatus.set(true);
+ } catch (Throwable t) {
+ serverStatus.set(false);
+ LOGGER.error("The seata-server failed to start", t);
+ }
})
.start();
- Thread.sleep(3000); // Simple wait
+ long start = System.nanoTime();
+ long maxWaitNanoTime = 10_000_000_000L;
+ while (System.nanoTime() - start < maxWaitNanoTime) {
+ Thread.sleep(100);
+ if (serverStatus.get()) {
+ break;
+ }
+ }
+ if (!serverStatus.get()) {
+ throw new RuntimeException("Waiting for a while, but the
seata-server did not start successfully.");
+ }
return new ServerInstance(nettyRemotingServer, port);
}
@@ -139,16 +158,31 @@ public abstract class BaseNettyClientTest {
* Configure client to use the specified port
*/
protected void configureClient(int port) {
- ConfigurationTestHelper.putConfig("service.default.grouplist",
"127.0.0.1:" + port);
-
ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
String.valueOf(port));
+ originalGroupList = System.getProperty("service.default.grouplist");
+ originalServerPort =
System.getProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ originalShutdownWait =
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT);
+ System.setProperty("service.default.grouplist", "127.0.0.1:" + port);
+ System.setProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
String.valueOf(port));
+ System.setProperty(ConfigurationKeys.SHUTDOWN_WAIT, "0");
+ ConfigurationCache.clear();
}
/**
* Clean up client configuration
*/
protected void cleanupClientConfig() {
- ConfigurationTestHelper.removeConfig("service.default.grouplist");
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ restoreProperty("service.default.grouplist", originalGroupList);
+ restoreProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
originalServerPort);
+ restoreProperty(ConfigurationKeys.SHUTDOWN_WAIT, originalShutdownWait);
+ ConfigurationCache.clear();
+ }
+
+ private static void restoreProperty(String key, String originalValue) {
+ if (originalValue == null) {
+ System.clearProperty(key);
+ } else {
+ System.setProperty(key, originalValue);
+ }
}
/**
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/NettyClientCoverageTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/NettyClientCoverageTest.java
new file mode 100644
index 0000000000..297bb59fe2
--- /dev/null
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/NettyClientCoverageTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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;
+
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationCache;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class NettyClientCoverageTest extends BaseNettyClientTest {
+
+ @BeforeEach
+ public void saveProperties() {
+ System.clearProperty("service.default.grouplist");
+ System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ System.clearProperty(ConfigurationKeys.SHUTDOWN_WAIT);
+ ConfigurationCache.clear();
+ }
+
+ @AfterEach
+ public void restoreProperties() {
+ System.clearProperty("service.default.grouplist");
+ System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ System.clearProperty(ConfigurationKeys.SHUTDOWN_WAIT);
+ ConfigurationCache.clear();
+ }
+
+ @Test
+ public void testGetDynamicPort() throws Exception {
+ int port = getDynamicPort();
+ Assertions.assertTrue(port > 0 && port <= 65535, "Port should be in
valid range");
+
+ int port2 = getDynamicPort();
+ Assertions.assertTrue(port2 > 0 && port2 <= 65535, "Second port should
also be in valid range");
+ }
+
+ @Test
+ public void testConfigureAndCleanup() {
+ Assertions.assertNull(System.getProperty("service.default.grouplist"));
+
Assertions.assertNull(System.getProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL));
+
Assertions.assertNull(System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+
+ configureClient(12345);
+
+ Assertions.assertEquals("127.0.0.1:12345",
System.getProperty("service.default.grouplist"));
+ Assertions.assertEquals("12345",
System.getProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL));
+ Assertions.assertEquals("0",
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+
+ cleanupClientConfig();
+
+ Assertions.assertNull(System.getProperty("service.default.grouplist"));
+
Assertions.assertNull(System.getProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL));
+
Assertions.assertNull(System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+ }
+
+ @Test
+ public void testConfigureClientPreservesOriginalValues() {
+ System.setProperty("service.default.grouplist", "original-group");
+ System.setProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
"9999");
+ System.setProperty(ConfigurationKeys.SHUTDOWN_WAIT, "30");
+
+ configureClient(12345);
+
+ Assertions.assertEquals("127.0.0.1:12345",
System.getProperty("service.default.grouplist"));
+ Assertions.assertEquals("12345",
System.getProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL));
+ Assertions.assertEquals("0",
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+
+ cleanupClientConfig();
+
+ Assertions.assertEquals("original-group",
System.getProperty("service.default.grouplist"));
+ Assertions.assertEquals("9999",
System.getProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL));
+ Assertions.assertEquals("30",
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+ }
+
+ @Test
+ public void testInitMessageExecutor() {
+ java.util.concurrent.ThreadPoolExecutor executor =
initMessageExecutor();
+ Assertions.assertNotNull(executor);
+ Assertions.assertEquals(5, executor.getCorePoolSize());
+ Assertions.assertEquals(5, executor.getMaximumPoolSize());
+ executor.shutdown();
+ }
+
+ @Test
+ public void testServerInstanceWrapper() throws Exception {
+ int port = getDynamicPort();
+ ServerInstance instance = new ServerInstance(null, port);
+ Assertions.assertEquals(port, instance.getPort());
+ Assertions.assertNull(instance.getServer());
+ Assertions.assertEquals("127.0.0.1:" + port, instance.getAddress());
+ instance.destroy();
+ }
+}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/GrpcTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/GrpcTest.java
index 323b816380..afe3c35f23 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/GrpcTest.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/GrpcTest.java
@@ -21,7 +21,7 @@ import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import org.apache.seata.common.ConfigurationKeys;
-import org.apache.seata.common.ConfigurationTestHelper;
+import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.protocol.generated.GrpcMessageProto;
import org.apache.seata.core.protocol.generated.SeataServiceGrpc;
@@ -47,8 +47,9 @@ public class GrpcTest {
@BeforeAll
public static void before() {
ConfigurationFactory.reload();
- ConfigurationTestHelper.putConfig(
+ System.setProperty(
ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT));
+ ConfigurationCache.clear();
MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT);
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
@@ -62,7 +63,8 @@ public class GrpcTest {
@AfterAll
public static void after() {
// MockServer.close();
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ ConfigurationCache.clear();
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java
index ab271b5811..ce0dd2a462 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java
@@ -17,7 +17,7 @@
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.ConfigurationCache;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.BranchType;
@@ -47,11 +47,9 @@ public class MockFastJson2Test {
@BeforeAll
public static void before() {
ConfigurationFactory.reload();
- ConfigurationTestHelper.putConfig(
+ System.setProperty(
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));
+ ConfigurationCache.clear();
MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT);
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
@@ -60,8 +58,9 @@ public class MockFastJson2Test {
@AfterAll
public static void after() {
// MockServer.close();
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERIALIZE_FOR_RPC);
+ System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ System.clearProperty(ConfigurationKeys.SERIALIZE_FOR_RPC);
+ ConfigurationCache.clear();
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockGrpcServerTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockGrpcServerTest.java
index 1100334574..3afa8bff22 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockGrpcServerTest.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockGrpcServerTest.java
@@ -17,7 +17,7 @@
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.ConfigurationCache;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.BranchType;
@@ -48,9 +48,10 @@ public class MockGrpcServerTest {
@BeforeAll
public static void before() {
ConfigurationFactory.reload();
- ConfigurationTestHelper.putConfig(
+ System.setProperty(
ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT));
-
ConfigurationTestHelper.putConfig(ConfigurationKeys.TRANSPORT_PROTOCOL,
Protocol.GRPC.value);
+ System.setProperty(ConfigurationKeys.TRANSPORT_PROTOCOL,
Protocol.GRPC.value);
+ ConfigurationCache.clear();
MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT);
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
@@ -59,8 +60,9 @@ public class MockGrpcServerTest {
@AfterAll
public static void after() {
// MockServer.close();
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.TRANSPORT_PROTOCOL);
+ System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ System.clearProperty(ConfigurationKeys.TRANSPORT_PROTOCOL);
+ ConfigurationCache.clear();
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java
index a46bd53e92..1a5b5d7b69 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java
@@ -17,7 +17,7 @@
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.ConfigurationCache;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.BranchType;
@@ -47,8 +47,9 @@ public class MockServerTest {
@BeforeAll
public static void before() {
ConfigurationFactory.reload();
- ConfigurationTestHelper.putConfig(
+ System.setProperty(
ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT));
+ ConfigurationCache.clear();
MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT);
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
@@ -57,7 +58,8 @@ public class MockServerTest {
@AfterAll
public static void after() {
// MockServer.close();
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ ConfigurationCache.clear();
TmNettyRemotingClient.getInstance().destroy();
RmNettyRemotingClient.getInstance().destroy();
}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/ProtocolTestConstants.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/ProtocolTestConstants.java
index 15f538af26..82edbf5278 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/ProtocolTestConstants.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/mockserver/ProtocolTestConstants.java
@@ -16,12 +16,31 @@
*/
package org.apache.seata.core.rpc.netty.mockserver;
+import org.apache.seata.config.ConfigurationCache;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
/**
* Mock Constants
**/
public class ProtocolTestConstants {
public static final String APPLICATION_ID = "mock_tx_app_id";
public static final String SERVICE_GROUP = "mock_tx_group";
- public static final int MOCK_SERVER_PORT = 8099;
+ public static final int MOCK_SERVER_PORT = findAvailablePort();
public static final String MOCK_SERVER_ADDRESS = "0.0.0.0:" +
MOCK_SERVER_PORT;
+
+ static {
+ System.setProperty("service.mock.grouplist", "127.0.0.1:" +
MOCK_SERVER_PORT);
+ ConfigurationCache.clear();
+ }
+
+ private static int findAvailablePort() {
+ try (ServerSocket socket = new ServerSocket(0)) {
+ socket.setReuseAddress(true);
+ return socket.getLocalPort();
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to allocate an available port",
e);
+ }
+ }
}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/multiversion/AbstractMultiVersionCompatibilityTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/multiversion/AbstractMultiVersionCompatibilityTest.java
index 622d343702..f929e9830b 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/multiversion/AbstractMultiVersionCompatibilityTest.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/multiversion/AbstractMultiVersionCompatibilityTest.java
@@ -35,7 +35,6 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.seata.common.ConfigurationKeys;
-import org.apache.seata.common.ConfigurationTestHelper;
import org.apache.seata.common.XID;
import org.apache.seata.common.metadata.Instance;
import org.apache.seata.common.metadata.Node;
@@ -43,7 +42,7 @@ import org.apache.seata.common.thread.NamedThreadFactory;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.common.util.UUIDGenerator;
-import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.core.protocol.HeartbeatMessage;
import org.apache.seata.core.protocol.Protocol;
import org.apache.seata.core.protocol.ProtocolConstants;
@@ -130,14 +129,18 @@ public abstract class
AbstractMultiVersionCompatibilityTest {
protected final AtomicReference<Object> responseRef = new
AtomicReference<>();
protected CountDownLatch responseLatch;
private String originalTransportProtocol;
+ private String originalShutdownWait;
// Helper for creating MultiProtocolDecoder with specific version (for V1
tests)
private final MultiProtocolDecoderTest decoderTestHelper = new
MultiProtocolDecoderTest();
@BeforeEach
public void setUp() {
- originalTransportProtocol =
ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.TRANSPORT_PROTOCOL);
-
ConfigurationTestHelper.putConfig(ConfigurationKeys.TRANSPORT_PROTOCOL,
Protocol.SEATA.value);
+ originalTransportProtocol =
System.getProperty(ConfigurationKeys.TRANSPORT_PROTOCOL);
+ originalShutdownWait =
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT);
+ System.setProperty(ConfigurationKeys.TRANSPORT_PROTOCOL,
Protocol.SEATA.value);
+ System.setProperty(ConfigurationKeys.SHUTDOWN_WAIT, "0");
+ ConfigurationCache.clear();
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
clientGroup = new NioEventLoopGroup();
@@ -169,14 +172,20 @@ public abstract class
AbstractMultiVersionCompatibilityTest {
serverWorkingThreads.shutdown();
}
- bossGroup.shutdownGracefully().sync();
- workerGroup.shutdownGracefully().sync();
- clientGroup.shutdownGracefully().sync();
- if (StringUtils.isBlank(originalTransportProtocol)) {
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.TRANSPORT_PROTOCOL);
+ bossGroup.shutdownGracefully(0, 2, TimeUnit.SECONDS).sync();
+ workerGroup.shutdownGracefully(0, 2, TimeUnit.SECONDS).sync();
+ clientGroup.shutdownGracefully(0, 2, TimeUnit.SECONDS).sync();
+ if (originalTransportProtocol == null) {
+ System.clearProperty(ConfigurationKeys.TRANSPORT_PROTOCOL);
} else {
-
ConfigurationTestHelper.putConfig(ConfigurationKeys.TRANSPORT_PROTOCOL,
originalTransportProtocol);
+ System.setProperty(ConfigurationKeys.TRANSPORT_PROTOCOL,
originalTransportProtocol);
}
+ if (originalShutdownWait == null) {
+ System.clearProperty(ConfigurationKeys.SHUTDOWN_WAIT);
+ } else {
+ System.setProperty(ConfigurationKeys.SHUTDOWN_WAIT,
originalShutdownWait);
+ }
+ ConfigurationCache.clear();
}
// ==================== V1 Server Methods (manual, for legacy simulation)
====================
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/multiversion/MultiVersionCompatibilityCoverageTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/multiversion/MultiVersionCompatibilityCoverageTest.java
new file mode 100644
index 0000000000..97d156622c
--- /dev/null
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/core/rpc/netty/multiversion/MultiVersionCompatibilityCoverageTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.multiversion;
+
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.config.ConfigurationCache;
+import org.apache.seata.core.protocol.Protocol;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class MultiVersionCompatibilityCoverageTest extends
AbstractMultiVersionCompatibilityTest {
+
+ @AfterEach
+ @Override
+ public void tearDown() throws InterruptedException {
+ super.tearDown();
+ }
+
+ @Test
+ public void testSetUpSetsPropertiesAndClearsCache() {
+ Assertions.assertEquals(Protocol.SEATA.value,
System.getProperty(ConfigurationKeys.TRANSPORT_PROTOCOL));
+ Assertions.assertEquals("0",
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+ }
+
+ @Test
+ public void testTearDownRestoresProperties() throws InterruptedException {
+ String beforeProtocol =
System.getProperty(ConfigurationKeys.TRANSPORT_PROTOCOL);
+ Assertions.assertNotNull(beforeProtocol, "setUp should have set
TRANSPORT_PROTOCOL");
+
+ tearDown();
+ setUp();
+
+ Assertions.assertEquals(Protocol.SEATA.value,
System.getProperty(ConfigurationKeys.TRANSPORT_PROTOCOL));
+ }
+
+ @Test
+ public void testSetUpPreservesOriginalTransportProtocol() throws
InterruptedException {
+ tearDown();
+
+ System.setProperty(ConfigurationKeys.TRANSPORT_PROTOCOL,
"test-original-value");
+ ConfigurationCache.clear();
+
+ setUp();
+
+ Assertions.assertEquals(Protocol.SEATA.value,
System.getProperty(ConfigurationKeys.TRANSPORT_PROTOCOL));
+
+ tearDown();
+
+ Assertions.assertEquals("test-original-value",
System.getProperty(ConfigurationKeys.TRANSPORT_PROTOCOL));
+
+ System.clearProperty(ConfigurationKeys.TRANSPORT_PROTOCOL);
+ ConfigurationCache.clear();
+ setUp();
+ }
+
+ @Test
+ public void testSetUpPreservesOriginalShutdownWait() throws
InterruptedException {
+ tearDown();
+
+ System.setProperty(ConfigurationKeys.SHUTDOWN_WAIT, "42");
+ ConfigurationCache.clear();
+
+ setUp();
+
+ Assertions.assertEquals("0",
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+
+ tearDown();
+
+ Assertions.assertEquals("42",
System.getProperty(ConfigurationKeys.SHUTDOWN_WAIT));
+
+ System.clearProperty(ConfigurationKeys.SHUTDOWN_WAIT);
+ ConfigurationCache.clear();
+ setUp();
+ }
+
+ @Test
+ public void testToPrettyJsonNull() {
+ Assertions.assertEquals("null",
AbstractMultiVersionCompatibilityTest.toPrettyJson(null));
+ }
+
+ @Test
+ public void testToPrettyJsonSimpleObject() {
+ String result =
AbstractMultiVersionCompatibilityTest.toPrettyJson("hello");
+ Assertions.assertNotNull(result);
+ Assertions.assertTrue(result.contains("hello"));
+ }
+
+ @Test
+ public void testToPrettyJsonMap() {
+ java.util.Map<String, String> map = new java.util.HashMap<>();
+ map.put("key", "value");
+ String result =
AbstractMultiVersionCompatibilityTest.toPrettyJson(map);
+ Assertions.assertNotNull(result);
+ Assertions.assertTrue(result.contains("key"));
+ Assertions.assertTrue(result.contains("value"));
+ }
+}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java
index 118b5a08d8..2e92761f82 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java
@@ -26,7 +26,7 @@ import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.seata.common.ConfigurationKeys;
-import org.apache.seata.common.ConfigurationTestHelper;
+import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.TransactionManager;
@@ -61,8 +61,9 @@ public class SeataMQProducerSendTest {
@BeforeAll
public static void before() throws MQClientException {
- ConfigurationTestHelper.putConfig(
+ System.setProperty(
ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL,
String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT));
+ ConfigurationCache.clear();
MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT);
producer = SeataMQProducerFactory.createSingle(NAME_SERVER, "test");
// should start mq server here
@@ -71,7 +72,8 @@ public class SeataMQProducerSendTest {
@AfterAll
public static void after() {
// MockServer.close();
-
ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
+ ConfigurationCache.clear();
producer.shutdown();
}
diff --git
a/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/AbstractServerTest.java
b/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/AbstractServerTest.java
index c783e4b4ed..be35a52fcb 100644
---
a/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/AbstractServerTest.java
+++
b/test-suite/test-new-version/src/test/java/org/apache/seata/saga/engine/db/AbstractServerTest.java
@@ -19,6 +19,8 @@ package org.apache.seata.saga.engine.db;
import org.apache.seata.common.XID;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.common.util.UUIDGenerator;
+import org.apache.seata.config.ConfigurationCache;
+import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.rpc.ShutdownHook;
import org.apache.seata.core.rpc.netty.NettyRemotingServer;
import org.apache.seata.core.rpc.netty.NettyServerConfig;
@@ -28,6 +30,8 @@ import org.apache.seata.server.metrics.MetricsManager;
import org.apache.seata.server.session.SessionHolder;
import java.io.File;
+import java.io.IOException;
+import java.net.ServerSocket;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -38,10 +42,36 @@ import java.util.concurrent.TimeUnit;
*/
public abstract class AbstractServerTest {
+ private static final int SERVER_PORT = findAvailablePort();
+
+ private static String originalConfigType;
+ private static String originalConfigFileName;
+ private static String originalGroupList;
+
+ static {
+ originalConfigType = System.getProperty("config.type");
+ originalConfigFileName = System.getProperty("config.file.name");
+ originalGroupList = System.getProperty("service.default.grouplist");
+ System.setProperty("config.type", "file");
+ System.setProperty("config.file.name", "file.conf");
+ System.setProperty("service.default.grouplist", "127.0.0.1:" +
SERVER_PORT);
+ ConfigurationFactory.reload();
+ ConfigurationCache.clear();
+ }
+
private static NettyRemotingServer nettyServer;
- private static final ThreadPoolExecutor workingThreads = new
ThreadPoolExecutor(
+ private static final ThreadPoolExecutor WORKING_THREADS = new
ThreadPoolExecutor(
100, 500, 500, TimeUnit.SECONDS, new LinkedBlockingQueue(20000),
new ThreadPoolExecutor.CallerRunsPolicy());
+ private static int findAvailablePort() {
+ try (ServerSocket socket = new ServerSocket(0)) {
+ socket.setReuseAddress(true);
+ return socket.getLocalPort();
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to allocate an available port",
e);
+ }
+ }
+
protected static void startSeataServer() throws InterruptedException {
(new Thread(new Runnable() {
public void run() {
@@ -56,8 +86,8 @@ public abstract class AbstractServerTest {
MetricsManager.get().init();
NettyServerConfig nettyServerConfig = new
NettyServerConfig();
- nettyServerConfig.setServerListenPort(8091);
- nettyServer = new NettyRemotingServer(workingThreads,
nettyServerConfig);
+ nettyServerConfig.setServerListenPort(SERVER_PORT);
+ nettyServer = new NettyRemotingServer(WORKING_THREADS,
nettyServerConfig);
UUIDGenerator.init(parameterParser.getServerNode());
// log store mode : filećdb
SessionHolder.init();
@@ -89,5 +119,17 @@ public abstract class AbstractServerTest {
nettyServer.destroy();
Thread.sleep(5000);
}
+ restoreProperty("config.type", originalConfigType);
+ restoreProperty("config.file.name", originalConfigFileName);
+ restoreProperty("service.default.grouplist", originalGroupList);
+ ConfigurationCache.clear();
+ }
+
+ private static void restoreProperty(String key, String originalValue) {
+ if (originalValue == null) {
+ System.clearProperty(key);
+ } else {
+ System.setProperty(key, originalValue);
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]