From d42068350f6acd49d75e276956399e621e694cfd Mon Sep 17 00:00:00 2001 From: "2590965087@qq.com" <2590968087@qq.com> Date: Sat, 29 Jul 2023 19:02:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=AF=B9=E4=BA=8E=E5=BD=93?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=96=B0=E7=9A=84=E6=9C=80=E5=A4=A7=E6=A0=B8?= =?UTF-8?q?=E5=BF=83=E7=BA=BF=E7=A8=8B=E6=95=B0=E5=A4=A7=E4=BA=8E=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E7=BA=BF=E7=A8=8B=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E4=BC=9A=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/ThreadPoolExecutorUtilTest.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/support/ThreadPoolExecutorUtilTest.java diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/support/ThreadPoolExecutorUtilTest.java b/infra/common/src/test/java/cn/hippo4j/common/executor/support/ThreadPoolExecutorUtilTest.java new file mode 100644 index 00000000..6ce8f5d0 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/ThreadPoolExecutorUtilTest.java @@ -0,0 +1,83 @@ +package cn.hippo4j.common.executor.support; + +import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @author Shizi + * @version 1.0 + * Create by 2023-07-29 18:43 + */ +@Slf4j +public class ThreadPoolExecutorUtilTest { + + private ThreadPoolExecutor executor; + private int corePoolSize; + private int maxPoolSize; + + @Before + public void testSafeSetPoolSize() { + corePoolSize = 2; + maxPoolSize = 4; + executor = new ThreadPoolExecutor( + corePoolSize, + maxPoolSize, + 1L, + TimeUnit.SECONDS, + new ArrayBlockingQueue<>(10) + ); + } + + @Test + public void testEquals(){ + // Test when the new core pool size equals the original maximum pool size. + int newCorePoolSize1 = maxPoolSize; + int newMaxPoolSize1 = 6; + ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize1, newMaxPoolSize1); + Assert.assertEquals(newCorePoolSize1, executor.getCorePoolSize()); + Assert.assertEquals(newMaxPoolSize1, executor.getMaximumPoolSize()); + } + + @Test + public void testGreater(){ + // Test when the new core pool size is greater than the original maximum pool size. + int newCorePoolSize2 = 8; + int newMaxPoolSize2 = 10; + ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize2, newMaxPoolSize2); + Assert.assertEquals(newCorePoolSize2, executor.getCorePoolSize()); + Assert.assertEquals(newMaxPoolSize2, executor.getMaximumPoolSize()); + } + + @Test + public void testLess(){ + // Test when the new core pool size is less than the original maximum pool size. + int newCorePoolSize3 = 3; + int newMaxPoolSize3 = 5; + ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize3, newMaxPoolSize3); + Assert.assertEquals(newCorePoolSize3, executor.getCorePoolSize()); + Assert.assertEquals(newMaxPoolSize3, executor.getMaximumPoolSize()); + } + + @Test + public void testException(){ + // Test when the new core pool size is greater than the new maximum pool size, which should throw an IllegalArgumentException. + int newCorePoolSize4 = 6; + int newMaxPoolSize4 = 4; + try { + ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize4, newMaxPoolSize4); + } catch (IllegalArgumentException e) { + // Expected to throw an exception. + Assert.assertEquals("newCorePoolSize must be smaller than newMaximumPoolSize", e.getMessage()); + log.error("newCorePoolSize must be smaller than newMaximumPoolSize",e.getMessage()); + } + } +} +