Skip to content

Commit ec3a496

Browse files
authored
test : fix mockserverTest fail cause using same port with seata-server (#6325)
1 parent be1157d commit ec3a496

File tree

16 files changed

+260
-95
lines changed

16 files changed

+260
-95
lines changed

changes/en-us/2.x.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Add changes here for all PR submitted to the 2.x branch.
121121
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] unbind xid in TransactionTemplateTest
122122
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] increase common module unit test coverage
123123
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] increase seata-core module unit test coverage
124+
- [[#6325](https://github.com/apache/incubator-seata/pull/6325)] fix mockServerTest fail cause using same port with seata-server
124125

125126
### refactor:
126127
- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] refactor Saga designer using diagram-js

changes/zh-cn/2.x.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] TransactionTemplateTest单测unbind xid
125125
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] 增加common模块单测覆盖率
126126
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] 增加seata-core模块单测覆盖率
127+
- [[#6325](https://github.com/apache/incubator-seata/pull/6325)] 修复mock-server相关测试用例
127128

128129
### refactor:
129130
- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] 使用diagram-js重构Saga设计器

test-mock-server/pom.xml

+67-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,79 @@
3232
<description>Seata mock server</description>
3333

3434
<build>
35+
<finalName>seata-mock-server</finalName>
3536
<plugins>
3637
<plugin>
37-
<groupId>org.apache.maven.plugins</groupId>
38-
<artifactId>maven-deploy-plugin</artifactId>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-maven-plugin</artifactId>
40+
<version>${spring-boot.version}</version>
41+
<configuration>
42+
<mainClass>org.apache.seata.mockserver.MockServer</mainClass>
43+
<!-- <layout>ZIP</layout>-->
44+
<attach>false</attach>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<goals>
49+
<goal>repackage</goal>
50+
</goals>
51+
</execution>
52+
</executions>
3953
</plugin>
4054
</plugins>
4155
</build>
4256

57+
<properties>
58+
<spring-boot-for-server.version>2.7.17</spring-boot-for-server.version>
59+
<spring-framework-for-server.version>5.3.30</spring-framework-for-server.version>
60+
<snakeyaml-for-server.version>2.0</snakeyaml-for-server.version>
61+
</properties>
62+
<dependencyManagement>
63+
<dependencies>
64+
<!-- junit5 -->
65+
<dependency>
66+
<groupId>org.junit</groupId>
67+
<artifactId>junit-bom</artifactId>
68+
<version>${junit-jupiter.version}</version>
69+
<type>pom</type>
70+
<scope>import</scope>
71+
</dependency>
72+
73+
<!-- spring-framework-->
74+
<dependency>
75+
<groupId>org.springframework</groupId>
76+
<artifactId>spring-framework-bom</artifactId>
77+
<version>${spring-framework-for-server.version}</version>
78+
<type>pom</type>
79+
<scope>import</scope>
80+
</dependency>
81+
82+
<!-- spring-boot -->
83+
<dependency>
84+
<groupId>org.springframework.boot</groupId>
85+
<artifactId>spring-boot-dependencies</artifactId>
86+
<version>${spring-boot-for-server.version}</version>
87+
<exclusions>
88+
<exclusion>
89+
<groupId>org.springframework</groupId>
90+
<artifactId>spring-framework-bom</artifactId>
91+
</exclusion>
92+
<exclusion>
93+
<groupId>org.yaml</groupId>
94+
<artifactId>snakeyaml</artifactId>
95+
</exclusion>
96+
</exclusions>
97+
<type>pom</type>
98+
<scope>import</scope>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.yaml</groupId>
102+
<artifactId>snakeyaml</artifactId>
103+
<version>${snakeyaml-for-server.version}</version>
104+
</dependency>
105+
</dependencies>
106+
</dependencyManagement>
107+
43108
<dependencies>
44109
<!-- The actual spring-related dependencies that take effect are from the seata-dependencies module, not the seata-server module-->
45110
<dependency>

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ protected void doGlobalCommit(GlobalCommitRequest request, GlobalCommitResponse
147147
IntStream.range(0, retry).forEach(i ->
148148
CallRm.branchCommit(remotingServer, branch));
149149
});
150+
branchMap.remove(request.getXid());
151+
globalStatusMap.remove(request.getXid());
150152
}
151153

152154
@Override
@@ -167,6 +169,8 @@ protected void doGlobalRollback(GlobalRollbackRequest request, GlobalRollbackRes
167169
IntStream.range(0, retry).forEach(i ->
168170
CallRm.branchRollback(remotingServer, branch));
169171
});
172+
branchMap.remove(request.getXid());
173+
globalStatusMap.remove(request.getXid());
170174
}
171175

172176
@Override
@@ -192,23 +196,20 @@ protected void doBranchRegister(BranchRegisterRequest request, BranchRegisterRes
192196

193197
response.setBranchId(branchSession.getBranchId());
194198
response.setResultCode(ResultCode.Success);
195-
196-
// Thread thread = new Thread(() -> {
197-
// try {
198-
// Thread.sleep(1000);
199-
// if (ProtocolConstants.VERSION_0 != Version.calcProtocolVersion(rpcContext.getVersion())) {
200-
// CallRm.deleteUndoLog(remotingServer, resourceId, clientId);
201-
// }
202-
// } catch (Exception e) {
203-
// e.printStackTrace();
204-
// }
205-
// });
206-
// thread.start();
207199
}
208200

209201
@Override
210202
protected void doBranchReport(BranchReportRequest request, BranchReportResponse response, RpcContext rpcContext) throws TransactionException {
211203
checkMockActionFail(request.getXid());
204+
String xid = request.getXid();
205+
branchMap.compute(xid, (key, val) -> {
206+
if (val != null) {
207+
val.stream().filter(branch -> branch.getBranchId() == request.getBranchId()).forEach(branch -> {
208+
branch.setApplicationData(request.getApplicationData());
209+
});
210+
}
211+
return val;
212+
});
212213
response.setResultCode(ResultCode.Success);
213214
}
214215

@@ -224,7 +225,7 @@ protected void doGlobalStatus(GlobalStatusRequest request, GlobalStatusResponse
224225
checkMockActionFail(request.getXid());
225226
GlobalStatus globalStatus = globalStatusMap.get(request.getXid());
226227
if (globalStatus == null) {
227-
globalStatus = GlobalStatus.UnKnown;
228+
globalStatus = GlobalStatus.Finished;
228229
}
229230
response.setGlobalStatus(globalStatus);
230231
response.setResultCode(ResultCode.Success);

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

+46-22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.seata.common.XID;
2525
import org.apache.seata.common.thread.NamedThreadFactory;
2626
import org.apache.seata.common.util.NetUtil;
27+
import org.apache.seata.server.ParameterParser;
2728
import org.apache.seata.server.UUIDGenerator;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
@@ -41,39 +42,62 @@ public class MockServer {
4142
private static ThreadPoolExecutor workingThreads;
4243
private static MockNettyRemotingServer nettyRemotingServer;
4344

45+
private static volatile boolean inited = false;
46+
47+
public static final int DEFAULT_PORT = 8091;
48+
4449
/**
4550
* The entry point of application.
4651
*
4752
* @param args the input arguments
4853
*/
4954
public static void main(String[] args) {
5055
SpringApplication.run(MockServer.class, args);
51-
start();
56+
57+
ParameterParser parameterParser = new ParameterParser(args);
58+
int port = parameterParser.getPort() > 0 ? parameterParser.getPort() : DEFAULT_PORT;
59+
start(port);
5260
}
5361

54-
public static void start() {
55-
workingThreads = new ThreadPoolExecutor(50,
56-
50, 500, TimeUnit.SECONDS,
57-
new LinkedBlockingQueue<>(20000),
58-
new NamedThreadFactory("ServerHandlerThread", 500), new ThreadPoolExecutor.CallerRunsPolicy());
59-
nettyRemotingServer = new MockNettyRemotingServer(workingThreads);
60-
61-
// set registry
62-
XID.setIpAddress(NetUtil.getLocalIp());
63-
XID.setPort(8092);
64-
// init snowflake for transactionId, branchId
65-
UUIDGenerator.init(1L);
66-
67-
MockCoordinator coordinator = MockCoordinator.getInstance();
68-
coordinator.setRemotingServer(nettyRemotingServer);
69-
nettyRemotingServer.setHandler(coordinator);
70-
nettyRemotingServer.init();
71-
72-
LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName());
62+
public static void start(int port) {
63+
if (!inited) {
64+
synchronized (MockServer.class) {
65+
if (!inited) {
66+
inited = true;
67+
workingThreads = new ThreadPoolExecutor(50,
68+
50, 500, TimeUnit.SECONDS,
69+
new LinkedBlockingQueue<>(20000),
70+
new NamedThreadFactory("ServerHandlerThread", 500), new ThreadPoolExecutor.CallerRunsPolicy());
71+
nettyRemotingServer = new MockNettyRemotingServer(workingThreads);
72+
73+
// set registry
74+
XID.setIpAddress(NetUtil.getLocalIp());
75+
XID.setPort(port);
76+
// init snowflake for transactionId, branchId
77+
UUIDGenerator.init(1L);
78+
79+
MockCoordinator coordinator = MockCoordinator.getInstance();
80+
coordinator.setRemotingServer(nettyRemotingServer);
81+
nettyRemotingServer.setHandler(coordinator);
82+
nettyRemotingServer.init();
83+
84+
LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName());
85+
}
86+
}
87+
}
88+
89+
7390
}
7491

7592
public static void close() {
76-
workingThreads.shutdown();
77-
nettyRemotingServer.destroy();
93+
if (inited) {
94+
synchronized (MockServer.class) {
95+
if (inited) {
96+
inited = false;
97+
workingThreads.shutdown();
98+
nettyRemotingServer.destroy();
99+
}
100+
}
101+
}
78102
}
79103
}

test-mock-server/src/main/java/org/apache/seata/mockserver/call/CallRm.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class CallRm {
4545
public static BranchStatus branchCommit(RemotingServer remotingServer, BranchSession branchSession) {
4646
BranchCommitRequest request = new BranchCommitRequest();
4747
setReq(request, branchSession);
48-
4948
try {
5049
BranchCommitResponse response = (BranchCommitResponse) remotingServer.sendSyncRequest(
5150
branchSession.getResourceId(), branchSession.getClientId(), request, false);
@@ -98,8 +97,7 @@ private static void setReq(AbstractBranchEndRequest request, BranchSession branc
9897
request.setXid(branchSession.getXid());
9998
request.setBranchId(branchSession.getBranchId());
10099
request.setResourceId(branchSession.getResourceId());
101-
request.setApplicationData("{\"k\":\"v\"}");
102-
request.setBranchType(BranchType.TCC);
103-
// todo AT SAGA
100+
request.setApplicationData(branchSession.getApplicationData());
101+
request.setBranchType(branchSession.getBranchType());
104102
}
105103
}

test-mock-server/src/main/resources/application.yml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#
1717
server:
1818
port: 7091
19+
servicePort: 8091
1920

2021
spring:
2122
application:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.common;
18+
19+
import org.apache.seata.config.ConfigurationCache;
20+
import org.apache.seata.config.ConfigurationFactory;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import java.util.concurrent.CountDownLatch;
25+
import java.util.concurrent.TimeUnit;
26+
27+
/**
28+
* the type ConfigurationTestHelper
29+
**/
30+
public class ConfigurationTestHelper {
31+
32+
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationTestHelper.class);
33+
private static final long PUT_CONFIG_TIMEOUT = 60000L;
34+
35+
public static void removeConfig(String dataId) {
36+
putConfig(dataId, null);
37+
}
38+
39+
public static void putConfig(String dataId, String content) {
40+
CountDownLatch countDownLatch = new CountDownLatch(1);
41+
ConfigurationCache.addConfigListener(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, event -> countDownLatch.countDown());
42+
if (content == null) {
43+
System.clearProperty(dataId);
44+
ConfigurationFactory.getInstance().removeConfig(dataId);
45+
return;
46+
}
47+
48+
System.setProperty(dataId, content);
49+
ConfigurationFactory.getInstance().putConfig(dataId, content);
50+
51+
try {
52+
boolean await = countDownLatch.await(PUT_CONFIG_TIMEOUT, TimeUnit.MILLISECONDS);
53+
if(await){
54+
LOGGER.info("putConfig ok, dataId={}", dataId);
55+
}else {
56+
LOGGER.error("putConfig fail, dataId={}", dataId);
57+
}
58+
} catch (InterruptedException e) {
59+
throw new RuntimeException(e);
60+
}
61+
62+
}
63+
}

test/src/test/java/org/apache/seata/core/rpc/netty/ChannelManagerTestHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static ConcurrentMap<String, Channel> getChannelConcurrentMap(AbstractNet
3232

3333
public static Channel getChannel(TmNettyRemotingClient client) {
3434
return getChannelManager(client)
35-
.acquireChannel(ProtocolTestConstants.SERVER_ADDRESS);
35+
.acquireChannel(ProtocolTestConstants.MOCK_SERVER_ADDRESS);
3636
}
3737
private static NettyClientChannelManager getChannelManager(AbstractNettyRemotingClient remotingClient) {
3838
return remotingClient.getClientChannelManager();

0 commit comments

Comments
 (0)