From 196238b28f331882a88bba8c574a27ab65f553b9 Mon Sep 17 00:00:00 2001 From: atomFix <43168451+atomFix@users.noreply.github.com> Date: Tue, 22 Nov 2022 18:54:49 +0800 Subject: [PATCH] completed #728 (#1005) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * unit test: ExecutorFactoryTest 补充测试用例 #728 * unit test #728 : format of normative notes * unit test #728 : format of normative notes to En; eliminate the impact of common objects on the current test flow in other scenarios. Co-authored-by: liukairong1 --- .../common/executor/ExecutorFactoryTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ExecutorFactoryTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ExecutorFactoryTest.java index 8cd8e010..f25e3721 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ExecutorFactoryTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/executor/ExecutorFactoryTest.java @@ -17,5 +17,74 @@ package cn.hippo4j.common.executor; +import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; +import cn.hippo4j.common.toolkit.MapUtil; +import cn.hippo4j.common.toolkit.ReflectUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.stream.IntStream; + public final class ExecutorFactoryTest { + + ThreadFactory threadFactory = new ThreadFactoryBuilder().prefix("test").build(); + + /** + * data range min + */ + Integer rangeMin = 1; + /** + * data range max + */ + Integer rangeMax = 10; + /** + * default test index + */ + Integer defaultIndex = 0; + + @Test + public void assertNewSingleScheduledExecutorService() { + // init data snapshot + ThreadPoolManager poolManager = (ThreadPoolManager) ReflectUtil.getFieldValue(ExecutorFactory.Managed.class, "THREAD_POOL_MANAGER"); + String poolName = (String) ReflectUtil.getFieldValue(ExecutorFactory.Managed.class, "DEFAULT_NAMESPACE"); + Map>> manager = (Map>>) ReflectUtil.getFieldValue(poolManager, "resourcesManager"); + Map> initRelationMap = manager.get(poolName); + int defaultManagerSize = manager.size(); + int defaultRelationSize = MapUtil.isEmpty(initRelationMap) ? 0 : initRelationMap.size(); + + // test begin + ScheduledExecutorService executorService = ExecutorFactory.Managed.newSingleScheduledExecutorService(String.format("test-group-%s", defaultIndex), threadFactory); + + Assert.assertNotNull(executorService); + + // check default init + Assert.assertEquals(1, manager.size() - defaultManagerSize); + + // check multiple registrations and check to see if it is still an instance + IntStream.rangeClosed(rangeMin, rangeMax).forEach(index -> ExecutorFactory.Managed.newSingleScheduledExecutorService(String.format("test-group-%s", index), threadFactory)); + Assert.assertEquals(1, manager.size() - defaultManagerSize); + + // check group size + Map> relationMap = manager.get(poolName); + Assert.assertEquals(11, relationMap.size() - defaultRelationSize); + // check the number of threads between the group and the thread pool + IntStream.rangeClosed(rangeMin, rangeMax).forEach(index -> { + String relationKey = String.format("test-group-%s", index); + Assert.assertNotNull(relationMap.get(relationKey)); + Assert.assertEquals(1, relationMap.get(relationKey).size()); + }); + + // instantiate the same group a second time and check the corresponding quantitative relationship + IntStream.rangeClosed(defaultIndex, rangeMax).forEach(index -> ExecutorFactory.Managed.newSingleScheduledExecutorService(String.format("test-group-%s", index), threadFactory)); + // chek group size + Assert.assertEquals(11, manager.get(poolName).size() - defaultRelationSize); + // check the number of threads between the group and the thread pool + IntStream.rangeClosed(rangeMin, rangeMax).forEach(index -> Assert.assertEquals(2, relationMap.get(String.format("test-group-%s", index)).size())); + } + }