Skip to content

Commit

Permalink
test: adjust the test cases related to dynamic configuration (apache#…
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes authored Apr 8, 2024
1 parent 6ec289d commit d3ed690
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 43 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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 @@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public static ConfigurationCache getInstance() {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ class FileConfigurationTest {

@BeforeEach
void setUp() {
ConfigurationCache.clear();
}

@AfterEach
void tearDown() {
ConfigurationCache.clear();
}

@Test
Expand All @@ -43,22 +45,30 @@ void addConfigListener() throws InterruptedException {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@

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;
Expand All @@ -47,8 +44,6 @@
@Order(2)
class RmNettyClientTest {

Logger logger = LoggerFactory.getLogger(getClass());

@BeforeAll
public static void beforeAll() {
RmNettyRemotingClient.getInstance().destroy();
Expand Down Expand Up @@ -86,13 +81,6 @@ public void testCheckFailFast() throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@
@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.
*
Expand Down Expand Up @@ -138,22 +133,14 @@ public void setApplicationId() throws Exception {
@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);
Expand Down

0 comments on commit d3ed690

Please sign in to comment.