-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathValuesTestCommon.java
More file actions
118 lines (101 loc) · 3.81 KB
/
Copy pathValuesTestCommon.java
File metadata and controls
118 lines (101 loc) · 3.81 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
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.values;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.io.AbstractCloseable;
import net.openhft.chronicle.core.io.AbstractReferenceCounted;
import net.openhft.chronicle.core.onoes.ExceptionKey;
import net.openhft.chronicle.core.onoes.Slf4jExceptionHandler;
import net.openhft.chronicle.core.threads.CleaningThread;
import net.openhft.chronicle.core.threads.ThreadDump;
import net.openhft.chronicle.core.time.SystemTimeProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Predicate;
/**
* Base class for values tests.
* <p>
* Enables reference tracing and starts exception recording before each test.
* At the end of a test it verifies that all resources have been released,
* no unexpected threads remain and only declared exceptions were logged.
*/
public class ValuesTestCommon {
private ThreadDump threadDump;
private Map<ExceptionKey, Integer> exceptions;
private final Map<Predicate<ExceptionKey>, String> expectedExceptions = new LinkedHashMap<>();
@BeforeEach
public void beforeEachValuesTestCommon() {
enableReferenceTracing();
threadDump();
recordExceptions();
}
public void enableReferenceTracing() {
AbstractReferenceCounted.enableReferenceTracing();
}
/**
* Fails the test if any {@code AbstractReferenceCounted} instances have not
* reached a reference count of zero.
*/
private void assertReferencesReleased() {
AbstractReferenceCounted.assertReferencesReleased();
}
public void threadDump() {
threadDump = new ThreadDump();
}
private void checkThreadDump() {
threadDump.assertNoNewThreads();
}
public void recordExceptions() {
exceptions = Jvm.recordExceptions();
}
/**
* Registers an expected log entry containing the supplied message.
*/
public void expectException(String message) {
expectException(k -> k.message.contains(message) ||
(k.throwable != null &&
k.throwable.getMessage().contains(message)), message);
}
/**
* Registers an expected log entry that matches the given predicate.
*
* @param predicate test for matching exception keys
* @param description text used if the expected entry is missing
*/
private void expectException(Predicate<ExceptionKey> predicate, String description) {
expectedExceptions.put(predicate, description);
}
/**
* Verifies that only declared exceptions were recorded during the test.
* Any unexpected entry causes the test to fail after dumping the log.
*/
private void checkExceptions() {
for (Map.Entry<Predicate<ExceptionKey>, String> expectedException :
expectedExceptions.entrySet()) {
if (!exceptions.keySet().removeIf(expectedException.getKey())) {
Slf4jExceptionHandler.WARN.on(getClass(),
"No error for " + expectedException.getValue());
}
}
expectedExceptions.clear();
if (Jvm.hasException(exceptions)) {
Jvm.dumpException(exceptions);
Jvm.resetExceptionHandlers();
throw new AssertionError(exceptions.keySet());
}
}
@AfterEach
public void afterChecks() {
SystemTimeProvider.CLOCK = SystemTimeProvider.INSTANCE;
CleaningThread.performCleanup(Thread.currentThread());
// find any discarded resources.
System.gc();
AbstractCloseable.waitForCloseablesToClose(100);
assertReferencesReleased();
checkThreadDump();
checkExceptions();
}
}