Skip to content

Commit 6cb9310

Browse files
authored
[ISSUE apache#4540] Add unit test for ThreadUtils (apache#5111)
1 parent 85f9af6 commit 6cb9310

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
19+
package org.apache.eventmesh.common.utils;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
25+
import java.util.concurrent.TimeUnit;
26+
27+
import org.junit.jupiter.api.Test;
28+
29+
class ThreadUtilsTest {
30+
31+
@Test
32+
void testRandomPauseBetweenMinAndMax() {
33+
34+
long min = 1000;
35+
long max = 5000;
36+
37+
long start = System.currentTimeMillis();
38+
ThreadUtils.randomPause(min, max, TimeUnit.MILLISECONDS);
39+
long end = System.currentTimeMillis();
40+
41+
long pause = end - start;
42+
43+
assertTrue(pause >= min && pause <= max, "Pause time should be between min and max");
44+
}
45+
46+
@Test
47+
void testRandomPauseWithInterruption() {
48+
49+
Thread.currentThread().interrupt();
50+
ThreadUtils.randomPause(1000, 2000, TimeUnit.MILLISECONDS);
51+
assertTrue(Thread.currentThread().isInterrupted());
52+
}
53+
54+
@Test
55+
void testDeprecatedSleep() {
56+
57+
ThreadUtils.sleep(1000);
58+
assertTrue(true, "Method should execute without any exception");
59+
}
60+
61+
@Test
62+
void testSleepWithTimeOutAndTimeUnit() throws InterruptedException {
63+
64+
ThreadUtils.sleepWithThrowException(5000, TimeUnit.MILLISECONDS);
65+
assertTrue(true, "Method should execute without any exception");
66+
}
67+
68+
@Test
69+
void testSleepWithNullTimeUnit() throws InterruptedException {
70+
71+
ThreadUtils.sleepWithThrowException(5000, null);
72+
assertTrue(true, "Method should not throw any exception with null TimeUnit");
73+
}
74+
75+
@Test
76+
void testSleepWithThrowExceptionInterruption() {
77+
Thread.currentThread().interrupt();
78+
79+
assertThrows(InterruptedException.class, () -> {
80+
ThreadUtils.sleepWithThrowException(5000, TimeUnit.MILLISECONDS);
81+
});
82+
}
83+
84+
@Test
85+
void testGetPIDWithRealProcessId() {
86+
87+
long pid = ThreadUtils.getPID();
88+
assertTrue(pid > 0);
89+
90+
long cashedPId = ThreadUtils.getPID();
91+
assertEquals(pid, cashedPId);
92+
}
93+
94+
@Test
95+
void testGetPIDWithMultiThread() throws InterruptedException {
96+
97+
final long[] pid1 = new long[1];
98+
final long[] pid2 = new long[1];
99+
100+
Thread thread1 = new Thread(() -> {
101+
pid1[0] = ThreadUtils.getPID();
102+
assertTrue(pid1[0] > 0);
103+
});
104+
105+
Thread thread2 = new Thread(() -> {
106+
pid2[0] = ThreadUtils.getPID();
107+
assertTrue(pid2[0] > 0);
108+
});
109+
110+
thread1.start();
111+
thread2.start();
112+
113+
thread1.join();
114+
thread2.join();
115+
116+
assertEquals(pid1[0], pid2[0]);
117+
}
118+
}

0 commit comments

Comments
 (0)