Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test : fix mockserverTest fail cause using same port with seata-server #6325

Merged
merged 33 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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 @@ -86,6 +86,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
2 changes: 1 addition & 1 deletion test-mock-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</plugins>
</build>

<dependencies>
<dependencies>
<dependency>
funky-eyes marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.apache.seata</groupId>
<artifactId>seata-server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.seata.common.ConfigurationKeys;
import org.apache.seata.common.XID;
import org.apache.seata.common.thread.NamedThreadFactory;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.config.ConfigurationChangeEvent;
import org.apache.seata.server.ParameterParser;
import org.apache.seata.server.UUIDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,39 +45,66 @@ public class MockServer {
private static ThreadPoolExecutor workingThreads;
private static MockNettyRemotingServer nettyRemotingServer;

private static volatile boolean inited = false;

public static final int DEFAULT_PORT = 8099;

/**
* 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;
System.setProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(port));
ConfigurationCache.getInstance().onChangeEvent(new ConfigurationChangeEvent(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(port)));
Bughue marked this conversation as resolved.
Show resolved Hide resolved
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;
System.clearProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
ConfigurationCache.getInstance().onChangeEvent(new ConfigurationChangeEvent(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, null));
workingThreads.shutdown();
nettyRemotingServer.destroy();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,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.setApplicationData("{\"actionContext\":{\"mock\":\"mock\"}}");
request.setBranchType(BranchType.TCC);
// todo AT SAGA
}
Expand Down
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: 8099

spring:
application:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
*/
package org.apache.seata.core.rpc.netty;

import java.lang.management.ManagementFactory;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import io.netty.channel.Channel;
import org.apache.seata.common.ConfigurationKeys;
import org.apache.seata.common.XID;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.config.ConfigurationChangeEvent;
import org.apache.seata.core.protocol.ResultCode;
import org.apache.seata.core.protocol.transaction.BranchRegisterRequest;
import org.apache.seata.core.protocol.transaction.BranchRegisterResponse;
Expand All @@ -33,16 +30,28 @@
import org.apache.seata.server.coordinator.DefaultCoordinator;
import org.apache.seata.server.session.SessionHolder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
*/
public class TmNettyClientTest extends AbstractServerTest {

private static final Logger LOGGER = LoggerFactory.getLogger(TmNettyClientTest.class);

@BeforeEach
public void init(){
System.setProperty(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, "8091");
ConfigurationCache.getInstance().onChangeEvent(new ConfigurationChangeEvent(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, "8091"));
}

public static ThreadPoolExecutor initMessageExecutor() {
return new ThreadPoolExecutor(100, 500, 500, TimeUnit.SECONDS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ public String insert(Long reqId, Map<String, String> params) {
@Override
public boolean commitTcc(BusinessActionContext actionContext) {
String xid = actionContext.getXid();
System.out.println("commitTcc:" + xid);
System.out.println("commitTcc:" + xid + "," + actionContext.getActionContext());
commitMap.compute(xid, (k, v) -> v == null ? 1 : v + 1);
return true;
}

@Override
public boolean cancel(BusinessActionContext actionContext) {
String xid = actionContext.getXid();
System.out.println("commitTcc:" + xid);
System.out.println("cancelTcc:" + xid + "," + actionContext.getActionContext());
rollbackMap.compute(xid, (k, v) -> v == null ? 1 : v + 1);
System.out.println("cancel");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* the type MockServerTest
*/
public class MockServerTest {

static String RESOURCE_ID = "mock-action";

@BeforeAll
public static void before() {
MockServer.start();
MockServer.start(ProtocolTestConstants.SERVER_PORT);
}

@AfterAll
Expand All @@ -45,29 +48,29 @@ public static void after() {
@Test
public void testCommit() throws TransactionException {
String xid = doTestCommit(0);
Assertions.assertEquals(Action1Impl.getCommitTimes(xid), 1);
Assertions.assertEquals(Action1Impl.getRollbackTimes(xid), 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(Action1Impl.getCommitTimes(xid), 3);
Assertions.assertEquals(Action1Impl.getRollbackTimes(xid), 0);
Assertions.assertEquals(3, Action1Impl.getCommitTimes(xid));
Assertions.assertEquals(0, Action1Impl.getRollbackTimes(xid));
}

@Test
public void testRollback() throws TransactionException {
String xid = doTestRollback(0);
Assertions.assertEquals(Action1Impl.getCommitTimes(xid), 0);
Assertions.assertEquals(Action1Impl.getRollbackTimes(xid), 1);
Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid));
Assertions.assertEquals(1, Action1Impl.getRollbackTimes(xid));
}

@Test
public void testRollbackRetry() throws TransactionException {
String xid = doTestRollback(2);
Assertions.assertEquals(Action1Impl.getCommitTimes(xid), 0);
Assertions.assertEquals(Action1Impl.getRollbackTimes(xid), 3);
Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid));
Assertions.assertEquals(3, Action1Impl.getRollbackTimes(xid));
}

private static String doTestCommit(int times) throws TransactionException {
Expand All @@ -78,7 +81,7 @@ private static String doTestCommit(int times) throws TransactionException {
MockCoordinator.getInstance().setExpectedRetry(xid, times);
Long branchId = rm.branchRegister(BranchType.AT, RESOURCE_ID, "1", xid, "1", "1");
GlobalStatus commit = tm.commit(xid);
Assertions.assertEquals(commit, GlobalStatus.Committed);
Assertions.assertEquals(GlobalStatus.Committed, commit);
return xid;

}
Expand All @@ -91,7 +94,7 @@ private static String doTestRollback(int times) throws TransactionException {
MockCoordinator.getInstance().setExpectedRetry(xid, times);
Long branchId = rm.branchRegister(BranchType.AT, RESOURCE_ID, "1", xid, "1", "1");
GlobalStatus rollback = tm.rollback(xid);
Assertions.assertEquals(rollback, GlobalStatus.Rollbacked);
Assertions.assertEquals(GlobalStatus.Rollbacked, rollback);
return xid;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package org.apache.seata.core.rpc.netty.mockserver;

import org.apache.seata.mockserver.MockServer;

/**
* Mock Constants
*
**/
public class ProtocolTestConstants {
public static final String APPLICATION_ID = "my_app_test";
public static final String SERVICE_GROUP = "default_tx_group";
public static final String SERVER_ADDRESS = "0.0.0.0:8091";
public static final String SERVICE_GROUP = "mock_tx_group";
public static final int SERVER_PORT = MockServer.DEFAULT_PORT;
public static final String SERVER_ADDRESS = "0.0.0.0:" + SERVER_PORT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.seata.core.rpc.netty.mockserver;

import java.util.concurrent.ConcurrentMap;

import io.netty.channel.Channel;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.core.exception.TransactionException;
Expand All @@ -37,6 +35,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentMap;

/**
* rm client test
**/
Expand All @@ -47,14 +47,13 @@ public class RmClientTest {

@BeforeAll
public static void before() {
MockServer.start();
MockServer.start(ProtocolTestConstants.SERVER_PORT);
}

@AfterAll
public static void after() {
MockServer.close();
}

@Test
public void testRm() throws TransactionException {
String resourceId = "mock-action";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ public class TmClientTest {

@BeforeAll
public static void before() {
MockServer.start();
MockServer.start(ProtocolTestConstants.SERVER_PORT);
}

@AfterAll
public static void after() {
MockServer.close();
}

@Test
public void testTm() throws Exception {

TransactionManager tm = getTm();

//globalBegin:TYPE_GLOBAL_BEGIN = 1 , TYPE_GLOBAL_BEGIN_RESULT = 2
Expand Down Expand Up @@ -86,6 +84,7 @@ public void testTm() throws Exception {
GlobalStatus rollback2 = tm.rollback(xid);
LOGGER.info("globalRollback ok:" + rollback2);
// TODO expected response fail , but DefaultTransactionManager ignore resultCode
MockServer.close();
}

@NotNull
Expand Down
2 changes: 2 additions & 0 deletions test/src/test/resources/file.conf
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ transport {
service {
#transaction service group mapping
vgroupMapping.default_tx_group = "default"
vgroupMapping.mock_tx_group = "mock"
#only support when registry.type=file, please don't set multiple addresses
default.grouplist = "127.0.0.1:8091"
mock.grouplist = "127.0.0.1:8099"
#disable seata
disableGlobalTransaction = false
}
Expand Down
Loading