From a80a71740770ed08418f10b4ee7f517e0d259d4e Mon Sep 17 00:00:00 2001 From: yanhom Date: Sun, 22 Sep 2024 23:54:42 +0800 Subject: [PATCH] add test case for reset tp props with global config --- .../core/spring/PropertiesBinderTest.java | 31 ++++++++++++++ .../src/test/resources/demo-dtp-dev.yml | 40 +++++++++++++++++-- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/PropertiesBinderTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/PropertiesBinderTest.java index fc421807e..98837616c 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/PropertiesBinderTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/spring/PropertiesBinderTest.java @@ -24,6 +24,7 @@ import org.dromara.dynamictp.core.support.BinderHelper; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; @@ -43,6 +44,7 @@ factory = YamlPropertySourceFactory.class) @SpringBootTest(classes = PropertiesBinderTest.class) @EnableAutoConfiguration +@TestInstance(TestInstance.Lifecycle.PER_CLASS) class PropertiesBinderTest { @Autowired @@ -78,7 +80,36 @@ void testBindDtpPropertiesWithEnvironment() { Assertions.assertEquals(threadPoolName, dtpProperties.getExecutors().get(0).getThreadPoolName()); String executorType = environment.getProperty("spring.dynamic.tp.globalExecutorProps.executorType"); Assertions.assertEquals(executorType, dtpProperties.getExecutors().get(1).getExecutorType()); + } + + @Test + void testResetDtpWithGlobalConfig() { + DtpProperties dtpProperties = DtpProperties.getInstance(); + BinderHelper.bindDtpProperties(environment, dtpProperties); + + Assertions.assertEquals(1, dtpProperties.getExecutors().get(1).getCorePoolSize()); + Assertions.assertEquals(18, dtpProperties.getExecutors().get(1).getMaximumPoolSize()); + Assertions.assertEquals(201, dtpProperties.getExecutors().get(1).getRunTimeout()); + Assertions.assertEquals(80, dtpProperties.getExecutors().get(0).getNotifyItems().get(0).getThreshold()); + Assertions.assertEquals(81, dtpProperties.getExecutors().get(1).getNotifyItems().get(0).getThreshold()); + } + + @Test + void testResetAdapterTpWithGlobalConfig() { + DtpProperties dtpProperties = DtpProperties.getInstance(); + BinderHelper.bindDtpProperties(environment, dtpProperties); + + Assertions.assertEquals(400, dtpProperties.getUndertowTp().getMaximumPoolSize()); + Assertions.assertEquals(201, dtpProperties.getTomcatTp().getRunTimeout()); + Assertions.assertEquals(101, dtpProperties.getTomcatTp().getQueueTimeout()); + Assertions.assertIterableEquals(Lists.newArrayList("ttl"), dtpProperties.getTomcatTp().getTaskWrapperNames()); + Assertions.assertEquals(5, dtpProperties.getTomcatTp().getNotifyItems().size()); + + Assertions.assertEquals(201, dtpProperties.getRocketMqTp().get(0).getRunTimeout()); + Assertions.assertEquals(1, dtpProperties.getRocketMqTp().get(1).getCorePoolSize()); + Assertions.assertEquals(81, dtpProperties.getRocketMqTp().get(0).getNotifyItems().get(0).getThreshold()); + Assertions.assertEquals(82, dtpProperties.getRocketMqTp().get(1).getNotifyItems().get(0).getThreshold()); } } diff --git a/test/test-core/src/test/resources/demo-dtp-dev.yml b/test/test-core/src/test/resources/demo-dtp-dev.yml index 2772afcab..801645617 100644 --- a/test/test-core/src/test/resources/demo-dtp-dev.yml +++ b/test/test-core/src/test/resources/demo-dtp-dev.yml @@ -45,8 +45,41 @@ spring: corePoolSize: 200 maximumPoolSize: 400 keepAliveTime: 60 + - threadPoolName: group2#topic2 + maximumPoolSize: 200 + keepAliveTime: 60 + notifyItems: # 报警项,不配置自动会按默认值配置(变更通知、容量报警、活性报警、拒绝报警、任务超时报警) + - type: capacity # 报警项类型,查看源码 NotifyTypeEnum枚举类 + enabled: true + threshold: 82 # 报警阈值 + platforms: [ ding,wechat ] # 可选配置,不配置默认拿上层platforms配置的所以平台 + interval: 120 # 报警间隔(单位:s) + globalExecutorProps: - executorType: eager + corePoolSize: 1 + waitForTasksToCompleteOnShutdown: false # 参考spring线程池设计,优雅关闭线程池 + awaitTerminationSeconds: 5 # 单位(s) + preStartAllCoreThreads: false # 是否预热所有核心线程,默认false + runTimeout: 201 # 任务执行超时阈值,目前只做告警用,单位(ms) + queueTimeout: 101 # 任务在队列等待超时阈值,目前只做告警用,单位(ms) + taskWrapperNames: [ "ttl" ] # 任务包装器名称,集成TaskWrapper接口 + notifyItems: # 报警项,不配置自动会按默认值配置(变更通知、容量报警、活性报警、拒绝报警、任务超时报警) + - type: capacity # 报警项类型,查看源码 NotifyTypeEnum枚举类 + enabled: true + threshold: 81 # 报警阈值 + platforms: [ ding,wechat ] # 可选配置,不配置默认拿上层platforms配置的所以平台 + interval: 120 # 报警间隔(单位:s) + - type: change + enabled: true + - type: liveness + enabled: true + threshold: 80 + - type: reject + enabled: true + threshold: 1 + - type: queue_timeout + enabled: true + threshold: 1 executors: # 动态线程池配置,都有默认值,采用默认值的可以不配置该项,减少配置量 - threadPoolName: dtpExecutor1 executorType: common # 线程池类型common、eager:适用于io密集型 @@ -70,8 +103,6 @@ spring: threshold: 80 # 报警阈值 platforms: [ding,wechat] # 可选配置,不配置默认拿上层platforms配置的所以平台 interval: 120 # 报警间隔(单位:s) - countToTrigger: 3 - op: > - type: change enabled: true - type: liveness @@ -86,4 +117,5 @@ spring: - type: queue_timeout enabled: true threshold: 1 - - threadPoolName: dtpExecutor2 \ No newline at end of file + - threadPoolName: dtpExecutor2 + maximumPoolSize: 18 \ No newline at end of file