diff --git a/example/example-nacos/src/main/resources/application.yml b/example/example-nacos/src/main/resources/application.yml index 5647a939f..6b71ff6d8 100644 --- a/example/example-nacos/src/main/resources/application.yml +++ b/example/example-nacos/src/main/resources/application.yml @@ -3,17 +3,19 @@ server: spring: application: - name: dynamic-tp-nacos-demo + name: user-data-proxy profiles: active: dev nacos: config: - server-addr: 127.0.0.1:8848 - type: yaml - data-ids: dynamic-tp-nacos-demo-dev.yml,dynamic-tp-nacos-demo-dtp-dev.yml # 线程池配置文件必须要在此处配置 + server-addr: ihuman-nacos:8848 + username: common-user + password: cu123456 + type: YAML + data-ids: dynamic-tp-nacos-demo-dev.yml,user-data-proxy-dtp-dev.yml # 线程池配置文件必须要在此处配置 auto-refresh: true - group: DEFAULT_GROUP + group: COMMON-BIZ bootstrap: enable: true log-enable: true diff --git a/test/test-configcenter/pom.xml b/test/test-configcenter/pom.xml index 34cbfddae..070221493 100644 --- a/test/test-configcenter/pom.xml +++ b/test/test-configcenter/pom.xml @@ -31,6 +31,19 @@ true + + org.dromara.dynamictp + dynamic-tp-spring-boot-starter-nacos + ${revision} + test + + + + com.alibaba.boot + nacos-config-spring-boot-starter + true + + org.springframework spring-aspects diff --git a/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/DtpBaseTest.java b/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/DtpBaseTest.java index 13e52c6a2..7b13088f4 100644 --- a/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/DtpBaseTest.java +++ b/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/DtpBaseTest.java @@ -30,6 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; /** @@ -50,7 +51,7 @@ public class DtpBaseTest { protected ApplicationEventPublisher publisher; @Autowired - protected Environment environment; + protected ConfigurableEnvironment environment; protected static ConfigurableApplicationContext context; diff --git a/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/cloud/CloudRefresherTest.java b/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/cloud/CloudRefresherTest.java index 666c29471..d4bc2c9fb 100644 --- a/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/cloud/CloudRefresherTest.java +++ b/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/cloud/CloudRefresherTest.java @@ -51,7 +51,7 @@ void testCloudRefresh() { } private void mockEnvironmentChange() { - MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); + MutablePropertySources propertySources = this.environment.getPropertySources(); Map tmpMap = Maps.newHashMap(); tmpMap.put("spring.dynamic.tp.executors[0].threadPoolName", "dtpExecutor1"); tmpMap.put("spring.dynamic.tp.executors[0].corePoolSize", 10); diff --git a/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/nacos/NacosRefresherTest.java b/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/nacos/NacosRefresherTest.java new file mode 100644 index 000000000..d328c96d8 --- /dev/null +++ b/test/test-configcenter/src/test/java/org/dromara/dynamictp/test/configcenter/nacos/NacosRefresherTest.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.dynamictp.test.configcenter.nacos; + +import com.alibaba.nacos.api.config.ConfigType; +import com.alibaba.nacos.api.config.listener.AbstractListener; +import com.alibaba.nacos.api.config.listener.Listener; +import com.alibaba.nacos.spring.context.event.config.NacosConfigReceivedEvent; +import com.alibaba.nacos.spring.core.env.NacosPropertySource; +import org.dromara.dynamictp.test.configcenter.DtpBaseTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.core.env.MutablePropertySources; + +import java.util.concurrent.ThreadPoolExecutor; + +import static org.mockito.Mockito.mock; + +/** + * NacosRefresherTest related + * + * @author yanhom + * @since 1.1.7 + */ +class NacosRefresherTest extends DtpBaseTest { + + @Test + void testRefresh() throws InterruptedException { + int corePoolSize = context.getBean("dtpExecutor1", ThreadPoolExecutor.class).getCorePoolSize(); + System.out.println(corePoolSize); + Assertions.assertEquals(6, corePoolSize); + mockConfigChange(); + Thread.sleep(2000L); + corePoolSize = context.getBean("dtpExecutor1", ThreadPoolExecutor.class).getCorePoolSize(); + System.out.println(corePoolSize); + Assertions.assertEquals(10, corePoolSize); + } + + private void mockConfigChange() { + String dataId = "dynamic-tp-demo-dtp-dev.yml"; + String groupId = "DEFAULT_GROUP"; + String type = ConfigType.YAML.getType(); + String content = "spring:\n" + + " dynamic:\n" + + " tp:\n" + + " enabled: true # 是否启用 dynamictp,默认true\n" + + " executors: # 动态线程池配置,都有默认值,采用默认值的可以不配置该项,减少配置量\n" + + " - threadPoolName: dtpExecutor1\n" + + " threadPoolAliasName: 测试线程池 # 线程池别名\n" + + " executorType: common # 线程池类型 common、eager、ordered、scheduled,默认 common\n" + + " corePoolSize: 10 # 核心线程数,默认1\n" + + " maximumPoolSize: 20 # 最大线程数,默认cpu核数\n"; + + Listener listener = new AbstractListener() { + @Override + public void receiveConfigInfo(String config) { + NacosPropertySource newNacosPropertySource = new NacosPropertySource(dataId, groupId, dataId, config, type); + MutablePropertySources propertySources = environment.getPropertySources(); + // replace NacosPropertySource + propertySources.replace(dataId, newNacosPropertySource); + } + }; + listener.receiveConfigInfo(content); + publisher.publishEvent(mock(NacosConfigReceivedEvent.class)); + } +} diff --git a/test/test-configcenter/src/test/resources/application.yml b/test/test-configcenter/src/test/resources/application.yml deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/DtpExecutorTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/DtpExecutorTest.java index d88367627..01e49aa6c 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/DtpExecutorTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/DtpExecutorTest.java @@ -60,7 +60,7 @@ public void mock(MockedStatic mockAlarmManager) { } @RepeatedTest(100) - public void testRunTimeout() { + void testRunTimeout() { Executor dtpExecutor = DtpRegistry.getExecutor("testRunTimeoutDtpExecutor"); dtpExecutor.execute(() -> { try (MockedStatic mockAlarmManager = mockStatic(AlarmManager.class)) { @@ -73,7 +73,7 @@ public void testRunTimeout() { } @RepeatedTest(100) - public void testQueueTimeout() { + void testQueueTimeout() { Executor dtpExecutor = DtpRegistry.getExecutor("testQueueTimeoutDtpExecutor"); dtpExecutor.execute(() -> { try (MockedStatic mockAlarmManager = mockStatic(AlarmManager.class)) { @@ -86,7 +86,7 @@ public void testQueueTimeout() { } @RepeatedTest(100) - public void testRejectedQueueTimeoutCancel() { + void testRejectedQueueTimeoutCancel() { Executor dtpExecutor = DtpRegistry.getExecutor("testRejectedQueueTimeoutCancelDtpExecutor"); dtpExecutor.execute(() -> { try (MockedStatic mockAlarmManager = mockStatic(AlarmManager.class)) { @@ -102,6 +102,4 @@ public void testRejectedQueueTimeoutCancel() { public static void afterAll() throws InterruptedException { // TimeUnit.SECONDS.sleep(100); } - - } diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java index 8dce24484..3839203b0 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java @@ -38,10 +38,10 @@ @ExtendWith(SpringExtension.class) @EnableDynamicTp @EnableAutoConfiguration -public class ScheduledDtpExecutorTest { +class ScheduledDtpExecutorTest { @Test - public void schedule() { + void schedule() { ScheduledDtpExecutor dtpExecutor12 = (ScheduledDtpExecutor) DtpRegistry.getExecutor("dtpExecutor12"); System.out.println(dtpExecutor12.getClass()); dtpExecutor12.scheduleAtFixedRate(() -> { @@ -51,16 +51,15 @@ public void schedule() { dtpExecutor12.shutdownNow(); } - @Test - public void testScheduleJre8Bug() { + void testScheduleJre8Bug() { ScheduledDtpExecutor dtpExecutor13 = (ScheduledDtpExecutor) DtpRegistry.getExecutor("dtpExecutor13"); dtpExecutor13.scheduleAtFixedRate(() -> { }, 10, 5, TimeUnit.SECONDS); dtpExecutor13.shutdownNow(); } @Test - public void testSubNotify() { + void testSubNotify() { ScheduledDtpExecutor dtpExecutor14 = (ScheduledDtpExecutor) DtpRegistry.getExecutor("dtpExecutor14"); dtpExecutor14.scheduleAtFixedRate(() -> { System.out.println("进来了");