Skip to content

Commit

Permalink
Merge pull request #384 from dromara/dev
Browse files Browse the repository at this point in the history
add configcenter for nacos test case
  • Loading branch information
yanhom1314 committed Dec 30, 2023
2 parents 07613ad + 297fffa commit 134d707
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 17 deletions.
12 changes: 7 additions & 5 deletions example/example-nacos/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/test-configcenter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-spring-boot-starter-nacos</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -50,7 +51,7 @@ public class DtpBaseTest {
protected ApplicationEventPublisher publisher;

@Autowired
protected Environment environment;
protected ConfigurableEnvironment environment;

protected static ConfigurableApplicationContext context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void testCloudRefresh() {
}

private void mockEnvironmentChange() {
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
MutablePropertySources propertySources = this.environment.getPropertySources();
Map<String, Object> tmpMap = Maps.newHashMap();
tmpMap.put("spring.dynamic.tp.executors[0].threadPoolName", "dtpExecutor1");
tmpMap.put("spring.dynamic.tp.executors[0].corePoolSize", 10);
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void mock(MockedStatic<AlarmManager> mockAlarmManager) {
}

@RepeatedTest(100)
public void testRunTimeout() {
void testRunTimeout() {
Executor dtpExecutor = DtpRegistry.getExecutor("testRunTimeoutDtpExecutor");
dtpExecutor.execute(() -> {
try (MockedStatic<AlarmManager> mockAlarmManager = mockStatic(AlarmManager.class)) {
Expand All @@ -73,7 +73,7 @@ public void testRunTimeout() {
}

@RepeatedTest(100)
public void testQueueTimeout() {
void testQueueTimeout() {
Executor dtpExecutor = DtpRegistry.getExecutor("testQueueTimeoutDtpExecutor");
dtpExecutor.execute(() -> {
try (MockedStatic<AlarmManager> mockAlarmManager = mockStatic(AlarmManager.class)) {
Expand All @@ -86,7 +86,7 @@ public void testQueueTimeout() {
}

@RepeatedTest(100)
public void testRejectedQueueTimeoutCancel() {
void testRejectedQueueTimeoutCancel() {
Executor dtpExecutor = DtpRegistry.getExecutor("testRejectedQueueTimeoutCancelDtpExecutor");
dtpExecutor.execute(() -> {
try (MockedStatic<AlarmManager> mockAlarmManager = mockStatic(AlarmManager.class)) {
Expand All @@ -102,6 +102,4 @@ public void testRejectedQueueTimeoutCancel() {
public static void afterAll() throws InterruptedException {
// TimeUnit.SECONDS.sleep(100);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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(() -> {
Expand All @@ -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("进来了");
Expand Down

0 comments on commit 134d707

Please sign in to comment.