This is an automated email from the ASF dual-hosted git repository. jianbin pushed a commit to branch 2.x in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push: new 1ee83136c3 test: fix FileConfigurationTest and MockServerTest fail (#6484) 1ee83136c3 is described below commit 1ee83136c3d637106e9d3cc86e55b6ebc9e649ff Author: funkye <jian...@apache.org> AuthorDate: Mon Apr 22 15:35:56 2024 +0800 test: fix FileConfigurationTest and MockServerTest fail (#6484) --- changes/en-us/2.x.md | 2 ++ changes/zh-cn/2.x.md | 2 ++ .../org/apache/seata/config/FileConfiguration.java | 35 +++++++++++----------- .../apache/seata/config/FileConfigurationTest.java | 24 ++++++++------- .../session/redis/RedisDistributedLockerTest.java | 3 +- .../org/apache/seata/mockserver/MockServer.java | 7 ++++- .../seata/common/ConfigurationTestHelper.java | 35 +++++++++++++--------- .../seata/core/rpc/netty/TmNettyClientTest.java | 4 +-- .../core/rpc/netty/mockserver/MockServerTest.java | 17 +++++++++-- .../rocketmq/SeataMQProducerSendTest.java | 3 +- 10 files changed, 81 insertions(+), 51 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index cd2c4546b6..e9b7549c2e 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -149,6 +149,8 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6430](https://github.com/apache/incubator-seata/pull/6430)] increase common module unit test coverage - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] adjust the test cases related to dynamic configuration - [[#6466](https://github.com/apache/incubator-seata/pull/6466)] support redis integration testing +- [[#6484](https://github.com/apache/incubator-seata/pull/6484)] fix FileConfigurationTest and MockServerTest fail + ### refactor: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 667e968ce5..93ac367895 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -145,6 +145,8 @@ - [[#6430](https://github.com/apache/incubator-seata/pull/6430)] 增加 common 模块单元测试覆盖率 - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] 调整动态配置监听测试用例 - [[#6466](https://github.com/apache/incubator-seata/pull/6466)] 支持redis的集成测试 +- [[#6484](https://github.com/apache/incubator-seata/pull/6484)] 修复FileConfigurationTest和MockServerTest失败 + ### refactor: diff --git a/config/seata-config-core/src/main/java/org/apache/seata/config/FileConfiguration.java b/config/seata-config-core/src/main/java/org/apache/seata/config/FileConfiguration.java index 08e8be181f..8c775fe2c9 100644 --- a/config/seata-config-core/src/main/java/org/apache/seata/config/FileConfiguration.java +++ b/config/seata-config-core/src/main/java/org/apache/seata/config/FileConfiguration.java @@ -381,25 +381,27 @@ public class FileConfiguration extends AbstractConfiguration { @Override public void onChangeEvent(ConfigurationChangeEvent event) { - Boolean enabled = Boolean.valueOf(System.getProperty("file.listener.enabled", "true")); - while (enabled) { - for (String dataId : dataIdMap.keySet()) { - try { - String currentConfig = - ConfigurationFactory.getInstance().getLatestConfig(dataId, null, DEFAULT_CONFIG_TIMEOUT); - if (StringUtils.isNotBlank(currentConfig)) { - String oldConfig = listenedConfigMap.get(dataId); - if (ObjectUtils.notEqual(currentConfig, oldConfig)) { - listenedConfigMap.put(dataId, currentConfig); - event.setDataId(dataId).setNewValue(currentConfig).setOldValue(oldConfig); - - for (ConfigurationChangeListener listener : dataIdMap.get(dataId)) { - listener.onProcessEvent(event); + while (true) { + boolean enabled = Boolean.parseBoolean(System.getProperty("file.listener.enabled", "true")); + if (enabled) { + for (String dataId : dataIdMap.keySet()) { + try { + String currentConfig = ConfigurationFactory.getInstance().getLatestConfig(dataId, null, + DEFAULT_CONFIG_TIMEOUT); + if (StringUtils.isNotBlank(currentConfig)) { + String oldConfig = listenedConfigMap.get(dataId); + if (ObjectUtils.notEqual(currentConfig, oldConfig)) { + listenedConfigMap.put(dataId, currentConfig); + event.setDataId(dataId).setNewValue(currentConfig).setOldValue(oldConfig); + + for (ConfigurationChangeListener listener : dataIdMap.get(dataId)) { + listener.onProcessEvent(event); + } } } + } catch (Exception exx) { + LOGGER.error("fileListener execute error, dataId :{}", dataId, exx); } - } catch (Exception exx) { - LOGGER.error("fileListener execute error, dataId :{}", dataId, exx); } } try { @@ -407,7 +409,6 @@ public class FileConfiguration extends AbstractConfiguration { } catch (InterruptedException e) { LOGGER.error("fileListener thread sleep error:{}", e.getMessage()); } - enabled = Boolean.valueOf(System.getProperty("file.listener.enabled", "true")); } } diff --git a/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java b/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java index cee604a17b..7abc905fa1 100644 --- a/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java +++ b/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java @@ -19,9 +19,9 @@ package org.apache.seata.config; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,35 +30,39 @@ class FileConfigurationTest { Logger logger = LoggerFactory.getLogger(FileConfigurationTest.class); - @BeforeEach - void setUp() { + @BeforeAll + static void setUp() { System.setProperty("file.listener.enabled", "true"); ConfigurationCache.clear(); } - @AfterEach - void tearDown() { + @AfterAll + static void tearDown() { ConfigurationCache.clear(); System.setProperty("file.listener.enabled", "true"); } @Test void addConfigListener() throws InterruptedException { + logger.info("addConfigListener"); + ConfigurationFactory.reload(); Configuration fileConfig = ConfigurationFactory.getInstance(); CountDownLatch countDownLatch = new CountDownLatch(1); String dataId = "service.disableGlobalTransaction"; boolean value = fileConfig.getBoolean(dataId); fileConfig.addConfigListener(dataId, (CachedConfigurationChangeListener)event -> { + logger.info("before dataId: {}, oldValue: {}, newValue: {}", event.getDataId(), event.getOldValue(), + event.getNewValue()); Assertions.assertEquals(Boolean.parseBoolean(event.getNewValue()), !Boolean.parseBoolean(event.getOldValue())); - logger.info("dataId: {}, oldValue: {}, newValue: {}", event.getDataId(), event.getOldValue(), + logger.info("after dataId: {}, oldValue: {}, newValue: {}", event.getDataId(), event.getOldValue(), event.getNewValue()); countDownLatch.countDown(); }); System.setProperty(dataId, String.valueOf(!value)); - countDownLatch.await(60, TimeUnit.SECONDS); - logger.info("dataId: {}, oldValue: {}", dataId, value); - logger.info("dataId: {}, currenValue: {}", dataId, fileConfig.getBoolean(dataId)); + logger.info(System.currentTimeMillis()+", dataId: {}, oldValue: {}", dataId, value); + countDownLatch.await(60,TimeUnit.SECONDS); + logger.info(System.currentTimeMillis()+", dataId: {}, currenValue: {}", dataId, fileConfig.getBoolean(dataId)); Assertions.assertNotEquals(fileConfig.getBoolean(dataId), value); //wait for loop safety, loop time is LISTENER_CONFIG_INTERVAL=1s CountDownLatch countDownLatch2 = new CountDownLatch(1); diff --git a/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java b/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java index b571a9a40d..d4f38a2bcc 100644 --- a/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java +++ b/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java @@ -121,9 +121,8 @@ public class RedisDistributedLockerTest { boolean d = distributedLocker.acquireLock(new DistributedLockDO(retryRollbacking, lockValue + 2, 2000L)); Assertions.assertFalse(d); - //sleep 60s try { - Thread.sleep(2000); + Thread.sleep(2100); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/test-mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java b/test-mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java index 36e95b7b75..f0fe04e455 100644 --- a/test-mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java +++ b/test-mock-server/src/main/java/org/apache/seata/mockserver/MockServer.java @@ -80,7 +80,12 @@ public class MockServer { coordinator.setRemotingServer(nettyRemotingServer); nettyRemotingServer.setHandler(coordinator); nettyRemotingServer.init(); - + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + close(); + } + })); LOGGER.info("pid info: " + ManagementFactory.getRuntimeMXBean().getName()); } } diff --git a/test/src/test/java/org/apache/seata/common/ConfigurationTestHelper.java b/test/src/test/java/org/apache/seata/common/ConfigurationTestHelper.java index d8708605e0..c3b3ec2326 100644 --- a/test/src/test/java/org/apache/seata/common/ConfigurationTestHelper.java +++ b/test/src/test/java/org/apache/seata/common/ConfigurationTestHelper.java @@ -16,11 +16,11 @@ */ package org.apache.seata.common; +import java.lang.reflect.Method; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.apache.seata.config.CachedConfigurationChangeListener; -import org.apache.seata.config.ConfigurationChangeEvent; import org.apache.seata.config.ConfigurationFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,34 +33,41 @@ public class ConfigurationTestHelper { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationTestHelper.class); private static final long PUT_CONFIG_TIMEOUT = 60000L; + static { + System.setProperty("config.type","file"); + System.setProperty("config.file.name","file.conf"); + System.setProperty("file.listener.enabled","true"); + try { + Method method = ConfigurationFactory.class.getDeclaredMethod("reload"); + method.setAccessible(true); + method.invoke( null); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + public static void removeConfig(String dataId) { putConfig(dataId, null); } public static void putConfig(String dataId, String content) { CountDownLatch countDownLatch = new CountDownLatch(1); - ConfigurationFactory.getInstance().addConfigListener(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, - new CachedConfigurationChangeListener() { - @Override - public void onChangeEvent(ConfigurationChangeEvent event) { - countDownLatch.countDown(); - } - }); + + ConfigurationFactory.getInstance().addConfigListener(dataId, + (CachedConfigurationChangeListener)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); + if (await) { + LOGGER.info("putConfig ok, dataId={}, value={}", dataId, ConfigurationFactory.getInstance().getConfig(dataId)); + } else { + LOGGER.error("putConfig fail, dataId={}, value={}", dataId, ConfigurationFactory.getInstance().getConfig(dataId)); } } catch (InterruptedException e) { throw new RuntimeException(e); diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java b/test/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java index 532198e1ae..9701862d7b 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java @@ -59,7 +59,7 @@ public class TmNettyClientTest extends AbstractServerTest { public static ThreadPoolExecutor initMessageExecutor() { return new ThreadPoolExecutor(100, 500, 500, TimeUnit.SECONDS, - new LinkedBlockingQueue(20000), new ThreadPoolExecutor.CallerRunsPolicy()); + new LinkedBlockingQueue(20000), new ThreadPoolExecutor.CallerRunsPolicy()); } /** @@ -185,7 +185,7 @@ public class TmNettyClientTest extends AbstractServerTest { Assertions.assertNotNull(branchRegisterResponse); Assertions.assertEquals(ResultCode.Failed, branchRegisterResponse.getResultCode()); Assertions.assertEquals("TransactionException[Could not found global transaction xid = 127.0.0.1:8091:1249853, may be has finished.]", - branchRegisterResponse.getMsg()); + branchRegisterResponse.getMsg()); nettyRemotingServer.destroy(); tmNettyRemotingClient.destroy(); } diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java index 3d926e769a..08606e20b3 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockServerTest.java @@ -22,6 +22,8 @@ import org.apache.seata.core.exception.TransactionException; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.model.GlobalStatus; import org.apache.seata.core.model.TransactionManager; +import org.apache.seata.core.rpc.netty.RmNettyRemotingClient; +import org.apache.seata.core.rpc.netty.TmNettyRemotingClient; import org.apache.seata.mockserver.MockCoordinator; import org.apache.seata.mockserver.MockServer; import org.apache.seata.rm.DefaultResourceManager; @@ -29,6 +31,8 @@ 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; /** * the type MockServerTest @@ -37,16 +41,22 @@ public class MockServerTest { static String RESOURCE_ID = "mock-action"; + Logger logger = LoggerFactory.getLogger(MockServerTest.class); + @BeforeAll public static void before() { ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT)); MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT); + TmNettyRemotingClient.getInstance().destroy(); + RmNettyRemotingClient.getInstance().destroy(); } @AfterAll public static void after() { - MockServer.close(); + //MockServer.close(); ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL); + TmNettyRemotingClient.getInstance().destroy(); + RmNettyRemotingClient.getInstance().destroy(); } @Test @@ -87,7 +97,7 @@ public class MockServerTest { RmClientTest.testRm(); } - private static String doTestCommit(int times) throws TransactionException { + private String doTestCommit(int times) throws TransactionException { TransactionManager tm = TmClientTest.getTm(); DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID); @@ -100,11 +110,12 @@ public class MockServerTest { } - private static String doTestRollback(int times) throws TransactionException { + private String doTestRollback(int times) throws TransactionException { TransactionManager tm = TmClientTest.getTm(); DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID); String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP, "test", 60000); + logger.info("doTestRollback xid:{}", xid); MockCoordinator.getInstance().setExpectedRetry(xid, times); Long branchId = rm.branchRegister(BranchType.TCC, RESOURCE_ID, "1", xid, "{\"mock\":\"mock\"}", "1"); GlobalStatus rollback = tm.rollback(xid); diff --git a/test/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java b/test/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java index e1e778f7c3..c35fdb9feb 100644 --- a/test/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java +++ b/test/src/test/java/org/apache/seata/integration/rocketmq/SeataMQProducerSendTest.java @@ -49,6 +49,7 @@ import java.util.concurrent.TimeUnit; /** * seata mq producer test **/ +@Disabled public class SeataMQProducerSendTest { private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducerSendTest.class); @@ -75,7 +76,6 @@ public class SeataMQProducerSendTest { } @Test - @Disabled public void testSendCommit() throws MQBrokerException, RemotingException, InterruptedException, MQClientException, TransactionException { TransactionManager tm = getTmAndBegin(); @@ -91,7 +91,6 @@ public class SeataMQProducerSendTest { } @Test - @Disabled public void testSendRollback() throws MQBrokerException, RemotingException, InterruptedException, MQClientException, TransactionException { TransactionManager tm = getTmAndBegin(); --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org