Skip to content

Commit

Permalink
add configcenter test case
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhom1314 committed Dec 24, 2023
1 parent b5ec844 commit 7638ab6
Show file tree
Hide file tree
Showing 15 changed files with 424 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public class TpExecutorProps {
private boolean notifyEnabled = true;

/**
* Task execute timeout, unit (ms), just for statistics.
* Task execute timeout, unit (ms).
*/
private long runTimeout = 0;

/**
* If try interrupt task when timeout.
* If try interrupt thread when run timeout.
*/
private boolean tryInterrupt = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void tryPrintError(Runnable r, Throwable t) {
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("DynamicTp execute, thread {} throw exception, traceId {}",
Thread.currentThread(), MDC.get(TRACE_ID), e);
Thread.currentThread(), MDC.get(TRACE_ID), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ public class ThreadPoolBuilder {
private boolean notifyEnabled = true;

/**
* Task execute timeout, unit (ms), just for statistics.
* Task execute timeout, unit (ms).
*/
private long runTimeout = 0;

/**
* If try interrupt task when timeout.
* If try interrupt thread when run timeout.
*/
private boolean tryInterrupt = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
import java.util.concurrent.atomic.AtomicInteger;

/**
* AbstractRedistRateLimiter related
* AbstractRedisRateLimiter related
*
* @author yanhom
* @since 1.0.8
**/
@SuppressWarnings("all")
public abstract class AbstractRedistRateLimiter implements RedisRateLimiter<List<Long>> {
public abstract class AbstractRedisRateLimiter implements RedisRateLimiter<List<Long>> {

private static final String SCRIPT_PATH = "/scripts/";

Expand All @@ -50,7 +50,7 @@ public abstract class AbstractRedistRateLimiter implements RedisRateLimiter<List

protected static final AtomicInteger COUNTER = new AtomicInteger(0);

public AbstractRedistRateLimiter(String scriptName) {
public AbstractRedisRateLimiter(String scriptName) {
DefaultRedisScript redisScript = new DefaultRedisScript<>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource(SCRIPT_PATH + scriptName)));
redisScript.setResultType(List.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author yanhom
* @since 1.0.8
**/
public class SlidingWindowRateLimiter extends AbstractRedistRateLimiter {
public class SlidingWindowRateLimiter extends AbstractRedisRateLimiter {

public SlidingWindowRateLimiter() {
super(RateLimitEnum.SLIDING_WINDOW.getScriptName());
Expand Down
33 changes: 33 additions & 0 deletions test/test-configcenter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,37 @@
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>dynamic-tp-test-configcenter</artifactId>

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

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

<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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;

import org.dromara.dynamictp.core.spring.EnableDynamicTp;
import org.dromara.dynamictp.core.spring.YamlPropertySourceFactory;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
* DtpBaseTest related
*
* @author yanhom
* @since 1.1.7
*/
@EnableDynamicTp
@EnableAutoConfiguration
@SpringBootTest(classes = {DtpBaseTest.class})
@PropertySource(value = "classpath:/dynamic-tp-demo-dtp-dev.yml", factory = YamlPropertySourceFactory.class)
@ComponentScan(basePackages = "org.dromara.dynamictp.test.configcenter")
@AutoConfigureAfter(ConfigurationPropertiesRebinderAutoConfiguration.class)
public class DtpBaseTest {

@Autowired
protected ApplicationEventPublisher publisher;

@Autowired
protected Environment environment;

protected static ConfigurableApplicationContext context;

@BeforeAll
public static void setUp() {
context = SpringApplication.run(DtpBaseTest.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.apollo;

import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.YmlConfigFile;
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
import com.google.common.collect.Maps;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

import java.util.Map;
import java.util.Properties;

import static com.ctrip.framework.apollo.core.ConfigConsts.CONFIG_FILE_CONTENT_KEY;

/**
* ApolloInitListener related
*
* @author yanhom
* @since 1.1.0
*/
public class ApolloInitListener implements SpringApplicationRunListener {

public ApolloInitListener(SpringApplication application, String[] args) {

}

@Override
public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> tmpMap = Maps.newHashMap();
tmpMap.put(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, "dynamic-tp-demo-dtp-dev.yml");
propertySources.addFirst(new MapPropertySource("DtpRefreshTestPropertySource", tmpMap));

YmlConfigFile configFile = (YmlConfigFile) ConfigService.getConfigFile("dynamic-tp-demo-dtp-dev", ConfigFileFormat.YML);
Properties newProperties = new Properties();
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: 6 # 核心线程数,默认1\n" +
" maximumPoolSize: 8 # 最大线程数,默认cpu核数\n" +
" queueCapacity: 2000 # 队列容量,默认1024\n";
newProperties.setProperty(CONFIG_FILE_CONTENT_KEY, content);
configFile.onRepositoryChange("dynamic-tp-demo-dtp-dev.yml", newProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.apollo;

import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.YamlConfigFile;
import org.dromara.dynamictp.test.configcenter.DtpBaseTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Properties;
import java.util.concurrent.ThreadPoolExecutor;

import static com.ctrip.framework.apollo.core.ConfigConsts.CONFIG_FILE_CONTENT_KEY;

/**
* CloudRefreshTest related
*
* @author yanhom
* @since 1.1.7
*/
class ApolloRefresherTest 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(6000L);
corePoolSize = context.getBean("dtpExecutor1", ThreadPoolExecutor.class).getCorePoolSize();
System.out.println(corePoolSize);
Assertions.assertEquals(10, corePoolSize);
}

private void mockConfigChange() {
YamlConfigFile configFile = (YamlConfigFile) ConfigService.getConfigFile("dynamic-tp-demo-dtp-dev", ConfigFileFormat.YML);
Properties newProperties = new Properties();
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";
newProperties.setProperty(CONFIG_FILE_CONTENT_KEY, content);
configFile.onRepositoryChange("dynamic-tp-demo-dtp-dev.yml", newProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.cloud;

import org.dromara.dynamictp.common.properties.DtpProperties;
import org.dromara.dynamictp.starter.cloud.nacos.refresher.CloudNacosRefresher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* RefresherConfig related
*
* @author yanhom
* @since 1.1.0
*/
@Configuration
public class CloudRefresherConfig {

@Bean
public CloudNacosRefresher cloudNacosRefresher(DtpProperties dtpProperties) {
return new CloudNacosRefresher(dtpProperties);
}
}
Loading

0 comments on commit 7638ab6

Please sign in to comment.