Skip to content

Commit 6c2dffd

Browse files
yanhom1314claude
andcommitted
test: add unit tests for ExecutorConverter
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 898d53b commit 6c2dffd

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.test.core.converter;
19+
20+
import org.dromara.dynamictp.common.entity.ThreadPoolStats;
21+
import org.dromara.dynamictp.common.entity.TpMainFields;
22+
import org.dromara.dynamictp.core.converter.ExecutorConverter;
23+
import org.dromara.dynamictp.core.executor.DtpExecutor;
24+
import org.dromara.dynamictp.core.support.ExecutorWrapper;
25+
import org.dromara.dynamictp.core.support.ThreadPoolBuilder;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.Test;
28+
29+
import java.util.concurrent.TimeUnit;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
36+
/**
37+
* ExecutorConverter test
38+
*
39+
* @author yanhom
40+
* @since 1.2.2
41+
*/
42+
class ExecutorConverterTest {
43+
44+
private DtpExecutor dtpExecutor;
45+
46+
@AfterEach
47+
void tearDown() {
48+
if (dtpExecutor != null) {
49+
dtpExecutor.shutdownNow();
50+
}
51+
}
52+
53+
@Test
54+
void testToMainFields() {
55+
dtpExecutor = ThreadPoolBuilder.newBuilder()
56+
.threadPoolName("converter-main")
57+
.corePoolSize(4)
58+
.maximumPoolSize(8)
59+
.keepAliveTime(60)
60+
.timeUnit(TimeUnit.SECONDS)
61+
.buildDynamic();
62+
63+
ExecutorWrapper wrapper = ExecutorWrapper.of(dtpExecutor);
64+
TpMainFields fields = ExecutorConverter.toMainFields(wrapper);
65+
66+
assertEquals("converter-main", fields.getThreadPoolName());
67+
assertEquals(4, fields.getCorePoolSize());
68+
assertEquals(8, fields.getMaxPoolSize());
69+
assertEquals(60, fields.getKeepAliveTime());
70+
assertNotNull(fields.getQueueType());
71+
assertNotNull(fields.getRejectType());
72+
assertFalse(fields.isAllowCoreThreadTimeOut());
73+
}
74+
75+
@Test
76+
void testToMainFieldsWithAllowCoreThreadTimeOut() {
77+
dtpExecutor = ThreadPoolBuilder.newBuilder()
78+
.threadPoolName("converter-timeout")
79+
.allowCoreThreadTimeOut(true)
80+
.buildDynamic();
81+
82+
ExecutorWrapper wrapper = ExecutorWrapper.of(dtpExecutor);
83+
TpMainFields fields = ExecutorConverter.toMainFields(wrapper);
84+
85+
assertTrue(fields.isAllowCoreThreadTimeOut());
86+
}
87+
88+
@Test
89+
void testToMetrics() {
90+
dtpExecutor = ThreadPoolBuilder.newBuilder()
91+
.threadPoolName("converter-metrics")
92+
.corePoolSize(2)
93+
.maximumPoolSize(4)
94+
.keepAliveTime(30)
95+
.timeUnit(TimeUnit.SECONDS)
96+
.buildDynamic();
97+
98+
ExecutorWrapper wrapper = ExecutorWrapper.of(dtpExecutor);
99+
ThreadPoolStats stats = ExecutorConverter.toMetrics(wrapper);
100+
101+
assertNotNull(stats);
102+
assertEquals("converter-metrics", stats.getPoolName());
103+
assertEquals(2, stats.getCorePoolSize());
104+
assertEquals(4, stats.getMaximumPoolSize());
105+
assertTrue(stats.isDynamic());
106+
assertNotNull(stats.getQueueType());
107+
}
108+
109+
@Test
110+
void testToMetricsContainsPerformanceData() {
111+
dtpExecutor = ThreadPoolBuilder.newBuilder()
112+
.threadPoolName("converter-perf")
113+
.buildDynamic();
114+
115+
// Simulate a task completion through stat provider
116+
ExecutorWrapper wrapper = ExecutorWrapper.of(dtpExecutor);
117+
Runnable task = () -> { };
118+
wrapper.getThreadPoolStatProvider().startTask(task);
119+
wrapper.getThreadPoolStatProvider().completeTask(task);
120+
121+
ThreadPoolStats stats = ExecutorConverter.toMetrics(wrapper);
122+
123+
assertNotNull(stats);
124+
assertEquals("converter-perf", stats.getPoolName());
125+
}
126+
127+
@Test
128+
void testToMetricsPoolSize() {
129+
dtpExecutor = ThreadPoolBuilder.newBuilder()
130+
.threadPoolName("converter-size")
131+
.corePoolSize(3)
132+
.maximumPoolSize(6)
133+
.buildDynamic();
134+
135+
ExecutorWrapper wrapper = ExecutorWrapper.of(dtpExecutor);
136+
ThreadPoolStats stats = ExecutorConverter.toMetrics(wrapper);
137+
138+
assertEquals(3, stats.getCorePoolSize());
139+
assertEquals(6, stats.getMaximumPoolSize());
140+
assertEquals(0, stats.getActiveCount());
141+
assertEquals(0, stats.getPoolSize());
142+
}
143+
}

0 commit comments

Comments
 (0)