-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathDDJavaSpecification.java
More file actions
84 lines (72 loc) · 2.28 KB
/
Copy pathDDJavaSpecification.java
File metadata and controls
84 lines (72 loc) · 2.28 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
package datadog.trace.test.util;
import datadog.trace.junit.utils.config.WithConfigExtension;
import datadog.trace.junit.utils.context.AllowContextTestingExtension;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith({
CleanConfigStateExtension.class,
WithConfigExtension.class,
AllowContextTestingExtension.class
})
@SuppressForbidden
public class DDJavaSpecification {
private static final long CHECK_TIMEOUT_MS = 3000;
protected boolean assertThreadsEachCleanup = true;
private static volatile boolean ignoreThreadCleanup;
@BeforeAll
static void beforeAll() {
if (getDDThreads().isEmpty()) {
ignoreThreadCleanup = false;
} else {
System.out.println(
"Found DD threads before test started. Ignoring thread cleanup for this test class");
ignoreThreadCleanup = true;
}
}
@AfterAll
static void afterAll() {
checkThreads();
}
@AfterEach
void cleanup() {
if (assertThreadsEachCleanup) {
checkThreads();
}
}
static Set<Thread> getDDThreads() {
return Thread.getAllStackTraces().keySet().stream()
.filter(
t ->
t.getName().startsWith("dd-")
&& !t.getName().equals("dd-task-scheduler")
&& !t.getName().equals("dd-cassandra-session-executor"))
.collect(Collectors.toSet());
}
static void checkThreads() {
if (ignoreThreadCleanup) {
return;
}
long deadline = System.currentTimeMillis() + CHECK_TIMEOUT_MS;
Set<Thread> threads = getDDThreads();
while (System.currentTimeMillis() < deadline && !threads.isEmpty()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
threads = getDDThreads();
}
if (!threads.isEmpty()) {
System.out.println("WARNING: DD threads still active. Forget to close() a tracer?");
List<String> names = threads.stream().map(Thread::getName).collect(Collectors.toList());
System.out.println(names);
}
}
}