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

}
protected void setListenPort(int listenPort) {
this.serverBootstrap.setListenPort(listenPort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ public void init() {
*
* @param messageExecutor the message executor
*/
public MockNettyRemotingServer(ThreadPoolExecutor messageExecutor,int port) {
public MockNettyRemotingServer(ThreadPoolExecutor messageExecutor) {
super(messageExecutor, new NettyServerConfig());
setListenPort(port);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
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.config.ConfigurationFactory;
import org.apache.seata.server.ParameterParser;
import org.apache.seata.server.UUIDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -50,23 +55,28 @@ public class MockServer {
*/
public static void main(String[] args) {
SpringApplication.run(MockServer.class, args);
start();

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

public static void start() {
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, 8099);
nettyRemotingServer = new MockNettyRemotingServer(workingThreads);

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

Expand All @@ -84,7 +94,16 @@ public static void start() {
}

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 @@ -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 @@ -23,9 +23,9 @@
import org.apache.seata.mockserver.MockCoordinator;
import org.apache.seata.mockserver.MockServer;
import org.apache.seata.rm.DefaultResourceManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class MockServerTest {
Expand All @@ -34,13 +34,13 @@ public class MockServerTest {

private static volatile boolean inited = false;

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

// @AfterAll
public static void after() {
@AfterEach
public void after() {
MockServer.close();
Bughue marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -80,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.assertTrue(commit == GlobalStatus.Committed || commit == GlobalStatus.Finished);
Assertions.assertEquals(GlobalStatus.Committed, commit);
return xid;

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
public class ProtocolTestConstants {
public static final String APPLICATION_ID = "my_app_test";
public static final String SERVICE_GROUP = "mock_tx_group";
public static final String SERVER_PORT = "8099";
public static final int 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 @@ -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 @@ -30,11 +28,15 @@
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;

import java.util.concurrent.ConcurrentMap;

/**
* rm client test
**/
Expand All @@ -43,10 +45,17 @@ public class RmClientTest {

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

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

@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,10 +40,17 @@ public class TmClientTest {

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

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

@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 Expand Up @@ -77,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
Loading