Skip to content

Commit

Permalink
test : fix mockserverTest fail cause using same port with seata-server (
Browse files Browse the repository at this point in the history
  • Loading branch information
Bughue authored Mar 2, 2024
1 parent be1157d commit ec3a496
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 95 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] unbind xid in TransactionTemplateTest
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] increase common module unit test coverage
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] increase seata-core module unit test coverage
- [[#6325](https://github.com/apache/incubator-seata/pull/6325)] fix mockServerTest fail cause using same port with seata-server

### refactor:
- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] refactor Saga designer using diagram-js
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
- [[#6125](https://github.com/apache/incubator-seata/pull/6125)] TransactionTemplateTest单测unbind xid
- [[#6157](https://github.com/apache/incubator-seata/pull/6157)] 增加common模块单测覆盖率
- [[#6250](https://github.com/apache/incubator-seata/pull/6250)] 增加seata-core模块单测覆盖率
- [[#6325](https://github.com/apache/incubator-seata/pull/6325)] 修复mock-server相关测试用例

### refactor:
- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] 使用diagram-js重构Saga设计器
Expand Down
69 changes: 67 additions & 2 deletions test-mock-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,79 @@
<description>Seata mock server</description>

<build>
<finalName>seata-mock-server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>org.apache.seata.mockserver.MockServer</mainClass>
<!-- <layout>ZIP</layout>-->
<attach>false</attach>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<spring-boot-for-server.version>2.7.17</spring-boot-for-server.version>
<spring-framework-for-server.version>5.3.30</spring-framework-for-server.version>
<snakeyaml-for-server.version>2.0</snakeyaml-for-server.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- junit5 -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit-jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- spring-framework-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring-framework-for-server.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-for-server.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
</exclusion>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml-for-server.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- The actual spring-related dependencies that take effect are from the seata-dependencies module, not the seata-server module-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ protected void doGlobalCommit(GlobalCommitRequest request, GlobalCommitResponse
IntStream.range(0, retry).forEach(i ->
CallRm.branchCommit(remotingServer, branch));
});
branchMap.remove(request.getXid());
globalStatusMap.remove(request.getXid());
}

@Override
Expand All @@ -167,6 +169,8 @@ protected void doGlobalRollback(GlobalRollbackRequest request, GlobalRollbackRes
IntStream.range(0, retry).forEach(i ->
CallRm.branchRollback(remotingServer, branch));
});
branchMap.remove(request.getXid());
globalStatusMap.remove(request.getXid());
}

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

response.setBranchId(branchSession.getBranchId());
response.setResultCode(ResultCode.Success);

// Thread thread = new Thread(() -> {
// try {
// Thread.sleep(1000);
// if (ProtocolConstants.VERSION_0 != Version.calcProtocolVersion(rpcContext.getVersion())) {
// CallRm.deleteUndoLog(remotingServer, resourceId, clientId);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// thread.start();
}

@Override
protected void doBranchReport(BranchReportRequest request, BranchReportResponse response, RpcContext rpcContext) throws TransactionException {
checkMockActionFail(request.getXid());
String xid = request.getXid();
branchMap.compute(xid, (key, val) -> {
if (val != null) {
val.stream().filter(branch -> branch.getBranchId() == request.getBranchId()).forEach(branch -> {
branch.setApplicationData(request.getApplicationData());
});
}
return val;
});
response.setResultCode(ResultCode.Success);
}

Expand All @@ -224,7 +225,7 @@ protected void doGlobalStatus(GlobalStatusRequest request, GlobalStatusResponse
checkMockActionFail(request.getXid());
GlobalStatus globalStatus = globalStatusMap.get(request.getXid());
if (globalStatus == null) {
globalStatus = GlobalStatus.UnKnown;
globalStatus = GlobalStatus.Finished;
}
response.setGlobalStatus(globalStatus);
response.setResultCode(ResultCode.Success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.seata.common.XID;
import org.apache.seata.common.thread.NamedThreadFactory;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.server.ParameterParser;
import org.apache.seata.server.UUIDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,39 +42,62 @@ public class MockServer {
private static ThreadPoolExecutor workingThreads;
private static MockNettyRemotingServer nettyRemotingServer;

private static volatile boolean inited = false;

public static final int DEFAULT_PORT = 8091;

/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
SpringApplication.run(MockServer.class, args);
start();

ParameterParser parameterParser = new ParameterParser(args);
int port = parameterParser.getPort() > 0 ? parameterParser.getPort() : DEFAULT_PORT;
start(port);
}

public static void start() {
workingThreads = new ThreadPoolExecutor(50,
50, 500, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(20000),
new NamedThreadFactory("ServerHandlerThread", 500), new ThreadPoolExecutor.CallerRunsPolicy());
nettyRemotingServer = new MockNettyRemotingServer(workingThreads);

// set registry
XID.setIpAddress(NetUtil.getLocalIp());
XID.setPort(8092);
// init snowflake for transactionId, branchId
UUIDGenerator.init(1L);

MockCoordinator coordinator = MockCoordinator.getInstance();
coordinator.setRemotingServer(nettyRemotingServer);
nettyRemotingServer.setHandler(coordinator);
nettyRemotingServer.init();

LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName());
public static void start(int port) {
if (!inited) {
synchronized (MockServer.class) {
if (!inited) {
inited = true;
workingThreads = new ThreadPoolExecutor(50,
50, 500, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(20000),
new NamedThreadFactory("ServerHandlerThread", 500), new ThreadPoolExecutor.CallerRunsPolicy());
nettyRemotingServer = new MockNettyRemotingServer(workingThreads);

// set registry
XID.setIpAddress(NetUtil.getLocalIp());
XID.setPort(port);
// init snowflake for transactionId, branchId
UUIDGenerator.init(1L);

MockCoordinator coordinator = MockCoordinator.getInstance();
coordinator.setRemotingServer(nettyRemotingServer);
nettyRemotingServer.setHandler(coordinator);
nettyRemotingServer.init();

LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName());
}
}
}


}

public static void close() {
workingThreads.shutdown();
nettyRemotingServer.destroy();
if (inited) {
synchronized (MockServer.class) {
if (inited) {
inited = false;
workingThreads.shutdown();
nettyRemotingServer.destroy();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class CallRm {
public static BranchStatus branchCommit(RemotingServer remotingServer, BranchSession branchSession) {
BranchCommitRequest request = new BranchCommitRequest();
setReq(request, branchSession);

try {
BranchCommitResponse response = (BranchCommitResponse) remotingServer.sendSyncRequest(
branchSession.getResourceId(), branchSession.getClientId(), request, false);
Expand Down Expand Up @@ -98,8 +97,7 @@ private static void setReq(AbstractBranchEndRequest request, BranchSession branc
request.setXid(branchSession.getXid());
request.setBranchId(branchSession.getBranchId());
request.setResourceId(branchSession.getResourceId());
request.setApplicationData("{\"k\":\"v\"}");
request.setBranchType(BranchType.TCC);
// todo AT SAGA
request.setApplicationData(branchSession.getApplicationData());
request.setBranchType(branchSession.getBranchType());
}
}
1 change: 1 addition & 0 deletions test-mock-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#
server:
port: 7091
servicePort: 8091

spring:
application:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.common;

import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.config.ConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* the type ConfigurationTestHelper
**/
public class ConfigurationTestHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationTestHelper.class);
private static final long PUT_CONFIG_TIMEOUT = 60000L;

public static void removeConfig(String dataId) {
putConfig(dataId, null);
}

public static void putConfig(String dataId, String content) {
CountDownLatch countDownLatch = new CountDownLatch(1);
ConfigurationCache.addConfigListener(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, event -> countDownLatch.countDown());
if (content == null) {
System.clearProperty(dataId);
ConfigurationFactory.getInstance().removeConfig(dataId);
return;
}

System.setProperty(dataId, content);
ConfigurationFactory.getInstance().putConfig(dataId, content);

try {
boolean await = countDownLatch.await(PUT_CONFIG_TIMEOUT, TimeUnit.MILLISECONDS);
if(await){
LOGGER.info("putConfig ok, dataId={}", dataId);
}else {
LOGGER.error("putConfig fail, dataId={}", dataId);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static ConcurrentMap<String, Channel> getChannelConcurrentMap(AbstractNet

public static Channel getChannel(TmNettyRemotingClient client) {
return getChannelManager(client)
.acquireChannel(ProtocolTestConstants.SERVER_ADDRESS);
.acquireChannel(ProtocolTestConstants.MOCK_SERVER_ADDRESS);
}
private static NettyClientChannelManager getChannelManager(AbstractNettyRemotingClient remotingClient) {
return remotingClient.getClientChannelManager();
Expand Down
Loading

0 comments on commit ec3a496

Please sign in to comment.