Browse Source

Test: Try to alleviate deadlocks while running demo tests.

modulesv2
weisj 3 years ago
parent
commit
94ccdd99ad
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 41
      core/src/test/java/com/github/weisj/darklaf/core/test/TestUtils.java

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

@ -24,7 +24,10 @@ package com.github.weisj.darklaf.core.test;
import java.awt.Window; import java.awt.Window;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@ -39,7 +42,7 @@ final class TestUtils {
private TestUtils() {} private TestUtils() {}
private static final Object lock = new Object(); private static final Lock LOCK = new ReentrantLock();
static void ensureLafInstalled() { static void ensureLafInstalled() {
ensureLafInstalled(new IntelliJTheme()); ensureLafInstalled(new IntelliJTheme());
@ -50,42 +53,32 @@ final class TestUtils {
} }
static void ensureLafInstalled(final Theme theme, final boolean alwaysInstall) { static void ensureLafInstalled(final Theme theme, final boolean alwaysInstall) {
synchronized (lock) {
if (alwaysInstall || !LafManager.isInstalled() || !LafManager.getInstalledTheme().equals(theme)) {
runOnSwingThreadNotThrowing(() -> LafManager.install(theme));
}
}
}
static void runOnThreadNotThrowing(final Runnable action) {
AtomicReference<Exception> exceptionRef = new AtomicReference<>();
try { try {
new Thread(() -> { if (LOCK.tryLock(200, TimeUnit.MILLISECONDS)) {
try { if (alwaysInstall || !LafManager.isInstalled() || !LafManager.getInstalledTheme().equals(theme)) {
action.run(); runOnSwingThreadNotThrowing(() -> LafManager.install(theme));
} catch (final Exception e) {
exceptionRef.set(e);
} }
}).start(); }
} catch (final Exception e) { } catch (InterruptedException e) {
e.printStackTrace(); Assertions.fail(e);
Assertions.fail(e.getMessage(), e);
}
if (exceptionRef.get() != null) {
Assertions.fail(exceptionRef.get().getMessage(), exceptionRef.get());
} }
} }
static void runOnSwingThreadNotThrowing(final Lambdas.CheckedRunnable<? extends Exception> action) { static void runOnSwingThreadNotThrowing(final Lambdas.CheckedRunnable<? extends Exception> action) {
AtomicReference<Exception> exceptionRef = new AtomicReference<>(); AtomicReference<Exception> exceptionRef = new AtomicReference<>();
try { try {
SwingUtilities.invokeAndWait(() -> { Runnable task = () -> {
try { try {
action.run(); action.run();
} catch (final Exception e) { } catch (final Exception e) {
exceptionRef.set(e); exceptionRef.set(e);
} }
}); };
if (SwingUtilities.isEventDispatchThread()) {
task.run();
} else {
SwingUtilities.invokeAndWait(task);
}
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
Assertions.fail(e.getMessage(), e); Assertions.fail(e.getMessage(), e);

Loading…
Cancel
Save