Skip to content

Commit f8e0bd5

Browse files
authored
test: fix the exception caused by the disorder of test case execution order (#7580)
1 parent c83ab1f commit f8e0bd5

File tree

56 files changed

+242
-322
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+242
-322
lines changed

changes/en-us/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Add changes here for all PR submitted to the 2.x branch.
5050

5151
- [[#7541](https://github.com/seata/seata/pull/7541)] fix jakarta UT failed in jdk17+
5252
- [[#7540](https://github.com/seata/seata/pull/7540)] fix port of mock server
53+
- [[#7580](https://github.com/seata/seata/pull/7580)] fix the exception caused by the disorder of test case execution order
5354

5455

5556
### refactor:

changes/zh-cn/2.x.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
- [[#7541](https://github.com/seata/seata/pull/7541)] 修复 jakarta 依赖在 jdk17+ 单测失败问题
5050
- [[#7540](https://github.com/seata/seata/pull/7540)] 修复mock server端口冲突问题
5151
- [[#7578](https://github.com/seata/seata/pull/7578)] zstd解压由jni改为ZstdInputStream
52-
52+
- [[#7580](https://github.com/seata/seata/pull/7580)] 修复测试用例顺序错乱导致的异常
5353

5454
### refactor:
5555

core/src/main/java/org/apache/seata/core/rpc/netty/RmNettyRemotingClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ public void destroy() {
320320
super.destroy();
321321
initialized.getAndSet(false);
322322
instance = null;
323+
transactionServiceGroup = null;
323324
}
324325

325326
@Override

mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.apache.seata.common.util.NetUtil;
2424
import org.apache.seata.common.util.NumberUtils;
2525
import org.apache.seata.common.util.UUIDGenerator;
26+
import org.apache.seata.config.ConfigurationCache;
27+
import org.apache.seata.core.constants.ConfigurationKeys;
2628
import org.apache.seata.core.rpc.netty.NettyServerConfig;
2729
import org.slf4j.Logger;
2830
import org.slf4j.LoggerFactory;
@@ -74,6 +76,10 @@ public static void start(int port) {
7476
if (!inited) {
7577
synchronized (MockServer.class) {
7678
if (!inited) {
79+
ConfigurationCache.clear();
80+
// Clear the property for any of the supported events
81+
System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
82+
System.clearProperty("server.port");
7783
inited = true;
7884
workingThreads = new ThreadPoolExecutor(
7985
50,

server/src/main/java/org/apache/seata/server/ServerRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ public void destroy() throws Exception {
8282
if (LOGGER.isDebugEnabled()) {
8383
LOGGER.debug("destoryAll starting");
8484
}
85-
8685
for (Disposable disposable : DISPOSABLE_LIST) {
8786
disposable.destroy();
8887
}
8988

9089
if (LOGGER.isDebugEnabled()) {
9190
LOGGER.debug("destoryAll finish");
9291
}
92+
DISPOSABLE_LIST.clear();
9393
}
9494

9595
@Override

server/src/main/java/org/apache/seata/server/session/SessionHolder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.util.concurrent.CompletableFuture;
4949

5050
import static java.io.File.separator;
51-
import static org.apache.seata.common.ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL;
5251
import static org.apache.seata.common.DefaultValues.DEFAULT_DISTRIBUTED_LOCK_EXPIRE_TIME;
5352
import static org.apache.seata.common.DefaultValues.DEFAULT_SEATA_GROUP;
5453
import static org.apache.seata.common.DefaultValues.DEFAULT_SESSION_STORE_FILE_DIR;
@@ -125,11 +124,11 @@ public static void init(SessionMode sessionMode) {
125124
String vGroupMappingStorePath =
126125
CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_VGROUP_MAPPING_STORE_FILE_DIR)
127126
+ separator
128-
+ System.getProperty(SERVER_SERVICE_PORT_CAMEL);
127+
+ XID.getPort();
129128
String sessionStorePath =
130129
CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_SESSION_STORE_FILE_DIR)
131130
+ separator
132-
+ System.getProperty(SERVER_SERVICE_PORT_CAMEL);
131+
+ XID.getPort();
133132
if (StringUtils.isBlank(sessionStorePath) || StringUtils.isBlank(vGroupMappingStorePath)) {
134133
throw new StoreException("the {store.file.dir} is empty.");
135134
}

server/src/main/java/org/apache/seata/server/spring/listener/ServerApplicationListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private void setTargetPort(ConfigurableEnvironment environment, String port, boo
114114
// add property source to the first position
115115
Properties pro = new Properties();
116116
pro.setProperty(SERVER_SERVICE_PORT_CONFIG, port);
117+
pro.setProperty(SERVER_SERVICE_PORT_CAMEL, port);
117118
environment.getPropertySources().addFirst(new PropertiesPropertySource("serverProperties", pro));
118119
}
119120
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.server;
18+
19+
import org.apache.seata.config.ConfigurationCache;
20+
import org.apache.seata.config.ConfigurationFactory;
21+
import org.apache.seata.core.constants.ConfigurationKeys;
22+
import org.junit.jupiter.api.AfterAll;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.test.annotation.DirtiesContext;
27+
import org.springframework.test.context.TestPropertySource;
28+
29+
@TestPropertySource(properties = {"server.port=${random.int[10000,60000]}"})
30+
@SpringBootTest
31+
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
32+
public abstract class BaseSpringBootTest {
33+
34+
@BeforeAll
35+
public static void beforeAll() {
36+
System.setProperty(ConfigurationKeys.SHUTDOWN_WAIT, "1");
37+
ConfigurationCache.clear();
38+
System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
39+
}
40+
41+
@AfterAll
42+
public static void afterAll() {
43+
ConfigurationCache.clear();
44+
System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
45+
}
46+
47+
@AfterEach
48+
public void AfterEach() {
49+
ConfigurationFactory.reload();
50+
System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
51+
}
52+
}

server/src/test/java/org/apache/seata/server/DynamicPortTestConfig.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

server/src/test/java/org/apache/seata/server/LoaderConfTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@
1919
import org.junit.jupiter.api.Assertions;
2020
import org.junit.jupiter.api.BeforeAll;
2121
import org.junit.jupiter.api.Test;
22-
import org.springframework.boot.test.context.SpringBootTest;
2322
import org.springframework.context.ApplicationContext;
24-
import org.springframework.context.annotation.Import;
2523
import org.springframework.core.env.Environment;
2624

2725
/**
2826
*
2927
*/
30-
@SpringBootTest
31-
@Import(DynamicPortTestConfig.class)
32-
public class LoaderConfTest {
28+
public class LoaderConfTest extends BaseSpringBootTest {
3329

3430
static Environment environment;
3531

0 commit comments

Comments
 (0)