Skip to content

Commit 92900ba

Browse files
authored
caching in test method, leading with usecase when a second thread is running the test (#541)
1 parent c464070 commit 92900ba

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed
Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
package com.mageddo.utils;
22

3+
import com.mageddo.commons.lang.Singletons;
4+
5+
import java.util.Set;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
import java.util.function.Predicate;
8+
39
public class Tests {
10+
11+
private static final String JUNIT_FRAMEWORK_PACKAGE = "org.junit.";
12+
private static final AtomicInteger hotCallsStat = new AtomicInteger();
13+
private static final Class<Tests> CACHE_KEY = Tests.class;
14+
415
public static boolean inTest() {
5-
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
6-
if (element.getClassName().startsWith("org.junit.")) {
16+
return Singletons.createOrGet(CACHE_KEY, Tests::inTestHotLoad);
17+
}
18+
19+
static boolean inTestHotLoad() {
20+
hotCallsStat.incrementAndGet();
21+
return findAllThreads()
22+
.stream()
23+
.anyMatch(Tests::hashJunitInStackTrace);
24+
}
25+
26+
private static Set<Thread> findAllThreads(){
27+
return Thread.getAllStackTraces().keySet();
28+
};
29+
30+
private static boolean hashJunitInStackTrace(final Thread thread) {
31+
return hashClassInStackTrace(
32+
thread,
33+
(element) -> element.getClassName().startsWith(JUNIT_FRAMEWORK_PACKAGE)
34+
);
35+
}
36+
37+
private static boolean hashClassInStackTrace(Thread thread, final Predicate<StackTraceElement> p) {
38+
for (final var element : thread.getStackTrace()) {
39+
if (p.test(element)) {
740
return true;
841
}
942
}
1043
return false;
1144
}
45+
46+
static int getHotCallsStat() {
47+
return hotCallsStat.get();
48+
}
49+
50+
static void resetCache(){
51+
hotCallsStat.set(0);
52+
Singletons.clear(CACHE_KEY);
53+
}
1254
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.mageddo.utils;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
class TestsTest {
10+
11+
@BeforeEach
12+
void beforeEach(){
13+
Tests.resetCache();
14+
}
15+
16+
@Test
17+
void mustCacheInTestCalls(){
18+
19+
for (int i = 0; i < 3; i++) {
20+
assertTrue(Tests.inTest());
21+
}
22+
23+
assertEquals(1, Tests.getHotCallsStat());
24+
}
25+
26+
@Test
27+
void mustBeJunitTest(){
28+
assertTrue(Tests.inTestHotLoad());
29+
}
30+
31+
@Test
32+
void hotCallsAreNotCached(){
33+
for (int i = 0; i < 3; i++) {
34+
assertTrue(Tests.inTestHotLoad());
35+
}
36+
assertEquals(Tests.getHotCallsStat(), 3);
37+
}
38+
39+
@Test
40+
void mustBeJunitTestEvenWhenRunningInBackground() throws Exception {
41+
try(final var executor = Executors.newThreadExecutor()){
42+
final var task = executor.submit(Tests::inTestHotLoad);
43+
assertTrue(task.get());
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)