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..97383037 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/support/ThreadPoolExecutorUtilTest.java @@ -0,0 +1,95 @@ +/* + * 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 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 java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * Thread pool executor util test + */ +@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()); + } + } +} diff --git a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java index 0038c62d..884aeaa7 100644 --- a/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java +++ b/starters/threadpool/server/src/main/java/cn/hippo4j/springboot/starter/monitor/ReportingEventExecutor.java @@ -45,6 +45,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -136,7 +137,7 @@ public class ReportingEventExecutor implements Runnable, CommandLineRunner, Disp properties.getCollectInterval(), TimeUnit.MILLISECONDS); Integer bufferSize = properties.getTaskBufferSize(); - messageCollectVessel = new ArrayBlockingQueue(bufferSize); + messageCollectVessel = new LinkedBlockingQueue(bufferSize); // Get all data collection components, currently only historical operation data collection. collectors = ApplicationContextHolder.getBeansOfType(Collector.class); // Start reporting monitoring data thread. diff --git a/threadpool/server/common/src/test/java/cn/hippo4j/server/common/base/ServiceExceptionTest.java b/threadpool/server/common/src/test/java/cn/hippo4j/server/common/base/ServiceExceptionTest.java new file mode 100644 index 00000000..fc55129b --- /dev/null +++ b/threadpool/server/common/src/test/java/cn/hippo4j/server/common/base/ServiceExceptionTest.java @@ -0,0 +1,100 @@ +/* + * 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 cn.hippo4j.server.common.base; + +import cn.hippo4j.common.toolkit.Assert; +import cn.hippo4j.server.common.base.exception.ErrorCode; +import cn.hippo4j.server.common.base.exception.ErrorCodeEnum; +import cn.hippo4j.server.common.base.exception.ServiceException; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import org.checkerframework.checker.units.qual.A; +import org.junit.jupiter.api.Test; +import java.util.Objects; + +/** + * Service exception test + */ +public class ServiceExceptionTest { + + @Test + public void ServiceExceptionTest1(){ + ServiceException serviceException = new ServiceException(); + Assert.isTrue(Objects.equals(serviceException.getErrorCode().getCode(), "3")); + Assert.isTrue(Objects.equals(serviceException.getMessage(), "SERVICE_ERROR")); + } + + @Test + public void ServiceExceptionTest2(){ + ErrorCode errorCode = ErrorCodeEnum.LOGIN_TIMEOUT; + ServiceException serviceException = new ServiceException(errorCode); + Assert.isTrue(Objects.equals(serviceException.getErrorCode().getCode(), ErrorCodeEnum.LOGIN_TIMEOUT.getCode())); + Assert.isTrue(Objects.equals(serviceException.getMessage(), ErrorCodeEnum.LOGIN_TIMEOUT.getMessage())); + } + + @Test + public void ServiceExceptionTest3(){ + String message = ErrorCodeEnum.SERVICE_ERROR.getMessage(); + ServiceException serviceException = new ServiceException(message); + Assert.isTrue(Objects.equals(serviceException.getMessage(), message)); + } + + @Test + public void ServiceExceptionTest4(){ + Throwable cause = new Throwable(); + ServiceException serviceException = new ServiceException(cause); + Assert.isTrue(Objects.equals(serviceException.getCause().getMessage(), cause.getMessage())); + } + + @Test + public void ServiceExceptionTest5(){ + String message = ErrorCodeEnum.SERVICE_ERROR.getMessage(); + Throwable cause = new Throwable(); + ServiceException serviceException = new ServiceException(message, cause); + Assert.isTrue(Objects.equals(serviceException.getCause().getMessage(), cause.getMessage())); + Assert.isTrue(Objects.equals(serviceException.getMessage(), message)); + } + + @Test + public void ServiceExceptionTest6(){ + String message = ErrorCodeEnum.SERVICE_ERROR.getMessage(); + Throwable cause = new Throwable(); + ServiceException serviceException = new ServiceException(cause, message); + Assert.isTrue(Objects.equals(serviceException.getCause().getMessage(), cause.getMessage())); + Assert.isTrue(Objects.equals(serviceException.getMessage(), message)); + } + + @Test + public void ServiceExceptionTest7(){ + Throwable cause = new Throwable(); + ErrorCode errorCode = ErrorCodeEnum.LOGIN_TIMEOUT; + ServiceException serviceException = new ServiceException(cause, errorCode); + Assert.isTrue(Objects.equals(serviceException.getCause().getMessage(), cause.getMessage())); + Assert.isTrue(Objects.equals(serviceException.getErrorCode().getCode(), ErrorCodeEnum.LOGIN_TIMEOUT.getCode())); + } + + @Test + public void ServiceExceptionTest8(){ + Throwable cause = new Throwable(); + ErrorCode errorCode = ErrorCodeEnum.LOGIN_TIMEOUT; + String message = ErrorCodeEnum.SERVICE_ERROR.getMessage(); + ServiceException serviceException = new ServiceException(message, cause, errorCode); + Assert.isTrue(Objects.equals(serviceException.getCause().getMessage(), cause.getMessage())); + Assert.isTrue(Objects.equals(serviceException.getErrorCode().getCode(), ErrorCodeEnum.LOGIN_TIMEOUT.getCode())); + Assert.isTrue(Objects.equals(serviceException.getMessage(), message)); + } +}