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 d3ed690a09 test: adjust the test cases related to dynamic configuration (#6456) d3ed690a09 is described below commit d3ed690a09e4a7298f5438ad0fc36a8382ab35e1 Author: funkye <jian...@apache.org> AuthorDate: Mon Apr 8 23:23:11 2024 +0800 test: adjust the test cases related to dynamic configuration (#6456) --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + .../config/CachedConfigurationChangeListener.java | 8 +++--- .../apache/seata/config/ConfigurationCache.java | 7 +++++ .../seata/config/ConfigurationChangeListener.java | 6 ++--- .../apache/seata/config/FileConfigurationTest.java | 30 ++++++++++++++-------- .../seata/core/rpc/netty/RmNettyClientTest.java | 12 --------- .../seata/core/rpc/netty/TmNettyClientTest.java | 15 +---------- 8 files changed, 37 insertions(+), 43 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index c5d20a9308..a4aac47335 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -143,6 +143,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#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 - [[#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 ### refactor: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 416ab9cc47..8dca48ebda 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -142,6 +142,7 @@ - [[#6250](https://github.com/apache/incubator-seata/pull/6250)] 增加seata-core模块单测覆盖率 - [[#6325](https://github.com/apache/incubator-seata/pull/6325)] 修复mock-server相关测试用例 - [[#6430](https://github.com/apache/incubator-seata/pull/6430)] 增加 common 模块单元测试覆盖率 +- [[#6456](https://github.com/apache/incubator-seata/pull/6456)] 调整动态配置监听测试用例 ### refactor: diff --git a/config/seata-config-core/src/main/java/org/apache/seata/config/CachedConfigurationChangeListener.java b/config/seata-config-core/src/main/java/org/apache/seata/config/CachedConfigurationChangeListener.java index 074eadc586..526bc4b53f 100644 --- a/config/seata-config-core/src/main/java/org/apache/seata/config/CachedConfigurationChangeListener.java +++ b/config/seata-config-core/src/main/java/org/apache/seata/config/CachedConfigurationChangeListener.java @@ -18,11 +18,11 @@ package org.apache.seata.config; public interface CachedConfigurationChangeListener extends ConfigurationChangeListener { - ConfigurationCache CONFIGURATION_CACHE = ConfigurationCache.getInstance(); + ConfigurationChangeListener CONFIGURATION_CACHE = ConfigurationCache.getInstance(); @Override - default void afterEvent(ConfigurationChangeEvent event) { - ConfigurationChangeListener listener = (ConfigurationChangeListener)CONFIGURATION_CACHE; - listener.onProcessEvent(event); + default void beforeEvent(ConfigurationChangeEvent event) { + CONFIGURATION_CACHE.onProcessEvent(event); } + } diff --git a/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationCache.java b/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationCache.java index 6bc8573c20..0ef162e4f1 100644 --- a/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationCache.java +++ b/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationCache.java @@ -44,6 +44,13 @@ public class ConfigurationCache implements ConfigurationChangeListener { return ConfigurationCacheInstance.INSTANCE; } + @Override + public void onProcessEvent(ConfigurationChangeEvent event) { + beforeEvent(event); + onChangeEvent(event); + afterEvent(event); + } + @Override public void onChangeEvent(ConfigurationChangeEvent event) { ObjectWrapper oldWrapper = CONFIG_CACHE.get(event.getDataId()); diff --git a/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationChangeListener.java b/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationChangeListener.java index 3c69735e9c..9453931e13 100644 --- a/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationChangeListener.java +++ b/config/seata-config-core/src/main/java/org/apache/seata/config/ConfigurationChangeListener.java @@ -40,9 +40,9 @@ public interface ConfigurationChangeListener { /** * The constant EXECUTOR_SERVICE. */ - ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(CORE_LISTENER_THREAD, MAX_LISTENER_THREAD, - Integer.MAX_VALUE, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), - new NamedThreadFactory("configListenerOperate", MAX_LISTENER_THREAD)); + ExecutorService EXECUTOR_SERVICE = + new ThreadPoolExecutor(CORE_LISTENER_THREAD, MAX_LISTENER_THREAD, Integer.MAX_VALUE, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(), new NamedThreadFactory("configListenerOperate", MAX_LISTENER_THREAD)); /** * Process. 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 5c754e0b8b..ee0b3a83b7 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 @@ -31,10 +31,12 @@ class FileConfigurationTest { @BeforeEach void setUp() { + ConfigurationCache.clear(); } @AfterEach void tearDown() { + ConfigurationCache.clear(); } @Test @@ -43,22 +45,30 @@ class FileConfigurationTest { CountDownLatch countDownLatch = new CountDownLatch(1); String dataId = "service.disableGlobalTransaction"; boolean value = fileConfig.getBoolean(dataId); - fileConfig.addConfigListener(dataId, new CachedConfigurationChangeListener() { - @Override - public void onChangeEvent(ConfigurationChangeEvent event) { - Assertions.assertEquals(Boolean.parseBoolean(event.getNewValue()), - !Boolean.parseBoolean(event.getOldValue())); - countDownLatch.countDown(); - } + fileConfig.addConfigListener(dataId, (CachedConfigurationChangeListener)event -> { + Assertions.assertEquals(Boolean.parseBoolean(event.getNewValue()), + !Boolean.parseBoolean(event.getOldValue())); + System.out.println("oldValue:" + event.getOldValue() + ",newValue:" + event.getNewValue()); + countDownLatch.countDown(); }); System.setProperty(dataId, String.valueOf(!value)); - countDownLatch.await(2, TimeUnit.SECONDS); - System.setProperty("file.listener.enabled", "false"); + countDownLatch.await(10, TimeUnit.SECONDS); + System.out.println(fileConfig.getBoolean(dataId)); + System.out.println(value); + Assertions.assertNotEquals(fileConfig.getBoolean(dataId), value); //wait for loop safety, loop time is LISTENER_CONFIG_INTERVAL=1s - Thread.sleep(1500); + CountDownLatch countDownLatch2 = new CountDownLatch(1); + fileConfig.addConfigListener("file.listener.enabled", (CachedConfigurationChangeListener)event -> { + if (!Boolean.parseBoolean(event.getNewValue())) { + countDownLatch2.countDown(); + } + }); + System.setProperty("file.listener.enabled", "false"); + countDownLatch2.await(10, TimeUnit.SECONDS); System.setProperty(dataId, String.valueOf(value)); //sleep for a period of time to simulate waiting for a cache refresh.Actually, it doesn't trigger. Thread.sleep(1000); + boolean currentValue = fileConfig.getBoolean(dataId); Assertions.assertNotEquals(value, currentValue); System.setProperty(dataId, String.valueOf(!value)); diff --git a/core/src/test/java/org/apache/seata/core/rpc/netty/RmNettyClientTest.java b/core/src/test/java/org/apache/seata/core/rpc/netty/RmNettyClientTest.java index 1b365501b5..1709246e88 100644 --- a/core/src/test/java/org/apache/seata/core/rpc/netty/RmNettyClientTest.java +++ b/core/src/test/java/org/apache/seata/core/rpc/netty/RmNettyClientTest.java @@ -23,10 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.seata.common.ConfigurationKeys; import org.apache.seata.common.exception.FrameworkException; -import org.apache.seata.config.CachedConfigurationChangeListener; import org.apache.seata.config.ConfigurationCache; -import org.apache.seata.config.ConfigurationChangeEvent; -import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.core.model.Resource; import org.apache.seata.core.model.ResourceManager; import org.junit.jupiter.api.AfterAll; @@ -47,8 +44,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @Order(2) class RmNettyClientTest { - Logger logger = LoggerFactory.getLogger(getClass()); - @BeforeAll public static void beforeAll() { RmNettyRemotingClient.getInstance().destroy(); @@ -86,13 +81,6 @@ class RmNettyClientTest { resourceMap.put("jdbc:xx://localhost/test", mockResource); Mockito.when(resourceManager.getManagedResources()).thenReturn(resourceMap); newClient.setResourceManager(resourceManager); - System.setProperty("file.listener.enabled", "true"); - ConfigurationFactory.getInstance().addConfigListener(ConfigurationKeys.ENABLE_RM_CLIENT_CHANNEL_CHECK_FAIL_FAST, new CachedConfigurationChangeListener() { - @Override - public void onChangeEvent(ConfigurationChangeEvent event) { - logger.info("dataId:{}, value: {}, oldValue: {}", event.getDataId(), event.getNewValue(), event.getOldValue()); - } - }); System.setProperty(ConfigurationKeys.ENABLE_RM_CLIENT_CHANNEL_CHECK_FAIL_FAST, "true"); ConfigurationCache.clear(); Assertions.assertThrows(FrameworkException.class, newClient::init); diff --git a/core/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java b/core/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java index 924711438f..410b69e2b3 100644 --- a/core/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java +++ b/core/src/test/java/org/apache/seata/core/rpc/netty/TmNettyClientTest.java @@ -47,11 +47,6 @@ import org.slf4j.LoggerFactory; @Order(1) public class TmNettyClientTest { - Logger logger = LoggerFactory.getLogger(getClass()); - - private static final ThreadPoolExecutor workingThreads = new ThreadPoolExecutor(100, 500, 500, TimeUnit.SECONDS, - new LinkedBlockingQueue<>(20000), new ThreadPoolExecutor.CallerRunsPolicy()); - /** * Test get instance. * @@ -138,22 +133,14 @@ public class TmNettyClientTest { @AfterAll public static void afterAll() { TmNettyRemotingClient.getInstance().destroy(); + System.setProperty(ConfigurationKeys.ENABLE_TM_CLIENT_CHANNEL_CHECK_FAIL_FAST, "false"); } @Test public void testCheckFailFast() throws Exception { TmNettyRemotingClient.getInstance().destroy(); - System.setProperty(ConfigurationKeys.ENABLE_TM_CLIENT_CHANNEL_CHECK_FAIL_FAST, "false"); TmNettyRemotingClient tmClient = TmNettyRemotingClient.getInstance("fail_fast", "default_tx_group"); System.setProperty("file.listener.enabled", "true"); - ConfigurationFactory.getInstance().addConfigListener(ConfigurationKeys.ENABLE_TM_CLIENT_CHANNEL_CHECK_FAIL_FAST, - new CachedConfigurationChangeListener() { - @Override - public void onChangeEvent(ConfigurationChangeEvent event) { - logger.info("dataId:{}, value: {}, oldValue: {}", event.getDataId(), event.getNewValue(), - event.getOldValue()); - } - }); System.setProperty(ConfigurationKeys.ENABLE_TM_CLIENT_CHANNEL_CHECK_FAIL_FAST, "true"); ConfigurationCache.clear(); Assertions.assertThrows(FrameworkException.class, tmClient::init); --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org