Skip to content

Commit 57920dd

Browse files
committed
Fix null task handling in stat provider
1 parent ac1be2f commit 57920dd

2 files changed

Lines changed: 91 additions & 2 deletions

File tree

core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolStatProvider.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void incQueueTimeoutCount(int count) {
161161
}
162162

163163
public void startQueueTimeoutTask(Runnable r) {
164-
if (queueTimeout <= 0) {
164+
if (queueTimeout <= 0 || r == null) {
165165
return;
166166
}
167167
HashedWheelTimer timer = ContextManagerHelper.getBean(HashedWheelTimer.class);
@@ -170,13 +170,16 @@ public void startQueueTimeoutTask(Runnable r) {
170170
}
171171

172172
public void cancelQueueTimeoutTask(Runnable r) {
173+
if (r == null) {
174+
return;
175+
}
173176
Optional.ofNullable(queueTimeoutMap.remove(r))
174177
.map(SoftReference::get)
175178
.ifPresent(Timeout::cancel);
176179
}
177180

178181
public void startRunTimeoutTask(Thread t, Runnable r) {
179-
if (runTimeout <= 0) {
182+
if (runTimeout <= 0 || r == null) {
180183
return;
181184
}
182185
HashedWheelTimer timer = ContextManagerHelper.getBean(HashedWheelTimer.class);
@@ -185,16 +188,25 @@ public void startRunTimeoutTask(Thread t, Runnable r) {
185188
}
186189

187190
public void cancelRunTimeoutTask(Runnable r) {
191+
if (r == null) {
192+
return;
193+
}
188194
Optional.ofNullable(runTimeoutMap.remove(r))
189195
.map(SoftReference::get)
190196
.ifPresent(Timeout::cancel);
191197
}
192198

193199
public void startTask(Runnable r) {
200+
if (r == null) {
201+
return;
202+
}
194203
stopWatchMap.put(r, System.currentTimeMillis());
195204
}
196205

197206
public void completeTask(Runnable r) {
207+
if (r == null) {
208+
return;
209+
}
198210
Optional.ofNullable(stopWatchMap.remove(r))
199211
.ifPresent(millis -> {
200212
long rt = System.currentTimeMillis() - millis;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.support;
19+
20+
import org.dromara.dynamictp.core.executor.DtpExecutor;
21+
import org.dromara.dynamictp.core.support.ExecutorWrapper;
22+
import org.dromara.dynamictp.core.support.ThreadPoolStatProvider;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.util.concurrent.LinkedBlockingQueue;
27+
import java.util.concurrent.TimeUnit;
28+
29+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
30+
31+
/**
32+
* ThreadPoolStatProvider test
33+
*
34+
* @author yanhom
35+
* @since 1.2.2
36+
*/
37+
class ThreadPoolStatProviderTest {
38+
39+
private DtpExecutor dtpExecutor;
40+
41+
@AfterEach
42+
void tearDown() {
43+
if (dtpExecutor != null) {
44+
dtpExecutor.shutdownNow();
45+
}
46+
}
47+
48+
@Test
49+
void testNullRunnableOperationsDoNotThrow() {
50+
ThreadPoolStatProvider provider = createProvider();
51+
52+
assertDoesNotThrow(() -> provider.startTask(null));
53+
assertDoesNotThrow(() -> provider.completeTask(null));
54+
assertDoesNotThrow(() -> provider.cancelRunTimeoutTask(null));
55+
assertDoesNotThrow(() -> provider.cancelQueueTimeoutTask(null));
56+
}
57+
58+
@Test
59+
void testStartAndCompleteTaskWorkNormally() {
60+
ThreadPoolStatProvider provider = createProvider();
61+
Runnable task = () -> {
62+
};
63+
64+
assertDoesNotThrow(() -> {
65+
provider.startTask(task);
66+
TimeUnit.MILLISECONDS.sleep(5);
67+
provider.completeTask(task);
68+
});
69+
}
70+
71+
private ThreadPoolStatProvider createProvider() {
72+
dtpExecutor = new DtpExecutor(1, 1, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
73+
dtpExecutor.setThreadPoolName("stat-provider-test");
74+
ExecutorWrapper wrapper = new ExecutorWrapper(dtpExecutor);
75+
return wrapper.getThreadPoolStatProvider();
76+
}
77+
}

0 commit comments

Comments
 (0)