test: add ThreadPoolRunStateHandler (#1200)

* test: add ThreadPoolRunStateHandler

* test: add ThreadPoolRunStateHandler

* test: add ThreadPoolRunStateHandler
pull/991/merge
Pan_Yujie 2 years ago committed by GitHub
parent 254b587111
commit 35cf3a2791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,5 +20,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -0,0 +1,127 @@
/*
* 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.core.executor.state;
import cn.hippo4j.common.model.ManyThreadPoolRunStateInfo;
import cn.hippo4j.common.model.ThreadPoolRunStateInfo;
import cn.hippo4j.common.toolkit.BeanUtil;
import cn.hippo4j.common.toolkit.ByteConvertUtil;
import cn.hippo4j.common.toolkit.MemoryUtil;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static cn.hippo4j.core.toolkit.IdentifyUtil.CLIENT_IDENTIFICATION_VALUE;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class ThreadPoolRunStateHandlerTest {
@Mock
ConfigurableEnvironment environment;
ThreadPoolRunStateInfo poolRunStateInfo = new ThreadPoolRunStateInfo();
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
poolRunStateInfo.setCurrentLoad(poolRunStateInfo.getCurrentLoad() + "%");
poolRunStateInfo.setPeakLoad(poolRunStateInfo.getPeakLoad() + "%");
poolRunStateInfo.setTpId("1");
}
@Test
void testSupplement() {
long used = MemoryUtil.heapMemoryUsed();
long max = MemoryUtil.heapMemoryMax();
String memoryProportion = StringUtil.newBuilder(
"Allocation: ",
ByteConvertUtil.getPrintSize(used),
" / Maximum available: ",
ByteConvertUtil.getPrintSize(max)
);
String ipAddress = "127.0.0.1";
poolRunStateInfo.setHost(ipAddress);
poolRunStateInfo.setMemoryProportion(memoryProportion);
poolRunStateInfo.setFreeMemory(ByteConvertUtil.getPrintSize(Math.subtractExact(max, used)));
String threadPoolId = poolRunStateInfo.getTpId();
try (final MockedStatic<GlobalThreadPoolManage> globalThreadPoolManage = mockStatic(GlobalThreadPoolManage.class)) {
globalThreadPoolManage.when(() -> GlobalThreadPoolManage.getExecutorService("1")).thenReturn(new DynamicThreadPoolWrapper());
DynamicThreadPoolWrapper executorService = GlobalThreadPoolManage.getExecutorService(threadPoolId);
Assertions.assertNotNull(executorService);
}
DynamicThreadPoolWrapper dynamicThreadPoolWrapperMock = mock(DynamicThreadPoolWrapper.class);
when(dynamicThreadPoolWrapperMock.getExecutor()).thenReturn(new ThreadPoolExecutor(2, 2, 2000, TimeUnit.SECONDS, new SynchronousQueue<>()));
ThreadPoolExecutor pool = dynamicThreadPoolWrapperMock.getExecutor();
Assertions.assertNotNull(pool);
String rejectedName;
rejectedName = "java.util.concurrent.ThreadPoolExecutor.AbortPolicy";
poolRunStateInfo.setRejectedName(rejectedName);
ManyThreadPoolRunStateInfo manyThreadPoolRunStateInfo = BeanUtil.convert(poolRunStateInfo, ManyThreadPoolRunStateInfo.class);
manyThreadPoolRunStateInfo.setIdentify(CLIENT_IDENTIFICATION_VALUE);
String active = environment.getProperty("spring.profiles.active", "UNKNOWN");
manyThreadPoolRunStateInfo.setActive("TRUE");
String threadPoolState = ThreadPoolStatusHandler.getThreadPoolState(pool);
manyThreadPoolRunStateInfo.setState(threadPoolState);
Assertions.assertNotNull(manyThreadPoolRunStateInfo);
}
@Test
void testGetHeapMemory() {
try (MockedStatic<MemoryUtil> memoryUtil = mockStatic(MemoryUtil.class)) {
memoryUtil.when(MemoryUtil::heapMemoryUsed).thenReturn(57534464L);
memoryUtil.when(MemoryUtil::heapMemoryMax).thenReturn(8566865920L);
Assertions.assertEquals(8566865920L, MemoryUtil.heapMemoryMax());
Assertions.assertEquals(57534464L, MemoryUtil.heapMemoryUsed());
}
}
@Test
void testMemoryProportion() {
long used = 57534464L;
long max = 8566865920L;
String memoryProportion = StringUtil.newBuilder(
"Allocation: ",
ByteConvertUtil.getPrintSize(used),
" / Maximum available: ",
ByteConvertUtil.getPrintSize(max));
Assertions.assertEquals("Allocation: 54.87MB / Maximum available: 7.98GB", memoryProportion);
}
}

@ -1,3 +1,20 @@
/*
* 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.auth.filter;
import cn.hippo4j.common.toolkit.ReflectUtil;

@ -1,3 +1,20 @@
/*
* 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.auth.service.impl;
import org.junit.Assert;
@ -7,7 +24,7 @@ class UserServiceImplTest {
@Test
void checkPasswordLength() {
//密码为null、空串、过短、过长都会抛出异常
// 密码为null、空串、过短、过长都会抛出异常
UserServiceImpl userService = new UserServiceImpl(null, null, null);
Assert.assertThrows(RuntimeException.class, () -> userService.checkPasswordLength(null));
Assert.assertThrows(RuntimeException.class, () -> userService.checkPasswordLength(""));

Loading…
Cancel
Save