-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathThreadUtilsTest.java
More file actions
106 lines (92 loc) · 2.91 KB
/
ThreadUtilsTest.java
File metadata and controls
106 lines (92 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package datadog.environment;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
public class ThreadUtilsTest {
@Test
public void threadId() throws InterruptedException {
Thread thread = new Thread("foo");
thread.start();
try {
// always works on Thread's where getId isn't overridden by child class
assertEquals(thread.getId(), ThreadUtils.threadId(thread));
} finally {
thread.join();
}
}
@Test
public void supportsVirtualThreads() {
assertEquals(
JavaVersion.getRuntimeVersion().isAtLeast(21), ThreadUtils.supportsVirtualThreads());
}
@Test
public void isVirtualThread_false() throws InterruptedException {
Thread thread = new Thread("foo");
thread.start();
try {
assertFalse(ThreadUtils.isVirtual(thread));
} finally {
thread.join();
}
}
@Test
public void isCurrentThreadVirtual_false() throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
assertFalse(executor.submit(() -> ThreadUtils.isCurrentThreadVirtual()).get());
} finally {
executor.shutdown();
}
}
@Test
public void isVirtualThread_true() throws InterruptedException {
if (!ThreadUtils.supportsVirtualThreads()) return;
Thread vThread = startVirtualThread(() -> {});
try {
assertTrue(ThreadUtils.isVirtual(vThread));
} finally {
vThread.join();
}
}
@Test
public void isCurrentThreadVirtual_true() throws InterruptedException {
if (!ThreadUtils.supportsVirtualThreads()) return;
AtomicBoolean result = new AtomicBoolean();
Thread vThread =
startVirtualThread(
() -> {
result.set(ThreadUtils.isCurrentThreadVirtual());
});
vThread.join();
assertTrue(result.get());
}
/*
* Should only be called on JVMs that support virtual threads
*/
static final Thread startVirtualThread(Runnable runnable) {
MethodHandle h_startVThread;
try {
h_startVThread =
MethodHandles.lookup()
.findStatic(
Thread.class,
"startVirtualThread",
MethodType.methodType(Thread.class, Runnable.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
try {
return (Thread) h_startVThread.invoke(runnable);
} catch (Throwable e) {
throw new IllegalStateException(e);
}
}
}