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 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,7 @@ public void close(ChannelHandlerContext ctx, ChannelPromise future) throws Excep
}

}
protected void setListenPort(int listenPort) {
this.serverBootstrap.setListenPort(listenPort);
}
}
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>
<groupId>org.apache.seata</groupId>
<artifactId>seata-server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public void init() {
*
* @param messageExecutor the message executor
*/
public MockNettyRemotingServer(ThreadPoolExecutor messageExecutor) {
public MockNettyRemotingServer(ThreadPoolExecutor messageExecutor,int port) {
super(messageExecutor, new NettyServerConfig());
setListenPort(port);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class MockServer {
private static ThreadPoolExecutor workingThreads;
private static MockNettyRemotingServer nettyRemotingServer;

private static volatile boolean inited = false;

/**
* The entry point of application.
*
Expand All @@ -52,24 +54,33 @@ public static void main(String[] args) {
}

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);
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, 8099);

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

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

// set registry
XID.setIpAddress(NetUtil.getLocalIp());
XID.setPort(8092);
// init snowflake for transactionId, branchId
UUIDGenerator.init(1L);
LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName());
}
}
}

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

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

public static void close() {
Expand Down
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 @@ -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 @@ -32,42 +32,44 @@ public class MockServerTest {

static String RESOURCE_ID = "mock-action";

private static volatile boolean inited = false;

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

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

@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 +80,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.assertTrue(commit == GlobalStatus.Committed || commit == GlobalStatus.Finished);
return xid;

}
Expand All @@ -91,7 +93,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.assertTrue(rollback == GlobalStatus.Rollbacked || rollback == GlobalStatus.Finished);
return xid;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
**/
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 String SERVER_PORT = "8099";
public static final String SERVER_ADDRESS = "0.0.0.0:" + SERVER_PORT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import org.apache.seata.mockserver.MockServer;
import org.apache.seata.rm.DefaultResourceManager;
import org.apache.seata.rm.RMClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -45,18 +43,10 @@ public class RmClientTest {

protected static final Logger LOGGER = LoggerFactory.getLogger(RmClientTest.class);

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

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

@Test
public void testRm() throws TransactionException {
MockServer.start();
String resourceId = "mock-action";
String xid = "1111";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,10 @@ public class TmClientTest {

protected static final Logger LOGGER = LoggerFactory.getLogger(TmClientTest.class);

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

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

@Test
public void testTm() throws Exception {

MockServer.start();
TransactionManager tm = getTm();

//globalBegin:TYPE_GLOBAL_BEGIN = 1 , TYPE_GLOBAL_BEGIN_RESULT = 2
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