Skip to content

Commit 69ea011

Browse files
committed
Test: Try to alleviate deadlocks while running demo tests.
1 parent 85e556f commit 69ea011

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

core/src/test/java/com/github/weisj/darklaf/core/test/TestUtils.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import java.awt.Window;
2525
import java.awt.event.WindowEvent;
2626
import java.lang.reflect.InvocationTargetException;
27+
import java.util.concurrent.TimeUnit;
2728
import java.util.concurrent.atomic.AtomicReference;
29+
import java.util.concurrent.locks.Lock;
30+
import java.util.concurrent.locks.ReentrantLock;
2831

2932
import javax.swing.SwingUtilities;
3033

@@ -39,7 +42,7 @@ final class TestUtils {
3942

4043
private TestUtils() {}
4144

42-
private static final Object lock = new Object();
45+
private static final Lock LOCK = new ReentrantLock();
4346

4447
static void ensureLafInstalled() {
4548
ensureLafInstalled(new IntelliJTheme());
@@ -50,42 +53,32 @@ static void ensureLafInstalled(final Theme theme) {
5053
}
5154

5255
static void ensureLafInstalled(final Theme theme, final boolean alwaysInstall) {
53-
synchronized (lock) {
54-
if (alwaysInstall || !LafManager.isInstalled() || !LafManager.getInstalledTheme().equals(theme)) {
55-
runOnSwingThreadNotThrowing(() -> LafManager.install(theme));
56-
}
57-
}
58-
}
59-
60-
static void runOnThreadNotThrowing(final Runnable action) {
61-
AtomicReference<Exception> exceptionRef = new AtomicReference<>();
6256
try {
63-
new Thread(() -> {
64-
try {
65-
action.run();
66-
} catch (final Exception e) {
67-
exceptionRef.set(e);
57+
if (LOCK.tryLock(200, TimeUnit.MILLISECONDS)) {
58+
if (alwaysInstall || !LafManager.isInstalled() || !LafManager.getInstalledTheme().equals(theme)) {
59+
runOnSwingThreadNotThrowing(() -> LafManager.install(theme));
6860
}
69-
}).start();
70-
} catch (final Exception e) {
71-
e.printStackTrace();
72-
Assertions.fail(e.getMessage(), e);
73-
}
74-
if (exceptionRef.get() != null) {
75-
Assertions.fail(exceptionRef.get().getMessage(), exceptionRef.get());
61+
}
62+
} catch (InterruptedException e) {
63+
Assertions.fail(e);
7664
}
7765
}
7866

7967
static void runOnSwingThreadNotThrowing(final Lambdas.CheckedRunnable<? extends Exception> action) {
8068
AtomicReference<Exception> exceptionRef = new AtomicReference<>();
8169
try {
82-
SwingUtilities.invokeAndWait(() -> {
70+
Runnable task = () -> {
8371
try {
8472
action.run();
8573
} catch (final Exception e) {
8674
exceptionRef.set(e);
8775
}
88-
});
76+
};
77+
if (SwingUtilities.isEventDispatchThread()) {
78+
task.run();
79+
} else {
80+
SwingUtilities.invokeAndWait(task);
81+
}
8982
} catch (final InterruptedException e) {
9083
e.printStackTrace();
9184
Assertions.fail(e.getMessage(), e);

0 commit comments

Comments
 (0)