make tests stoppable via IDEA 'Stop' button on unix (don't wait forever in a shutdown hook)

This commit is contained in:
peter
2011-04-07 19:50:12 +02:00
parent 0e39064f48
commit 82263976e3
3 changed files with 36 additions and 20 deletions
@@ -71,7 +71,6 @@ import org.picocontainer.MutablePicoContainer;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.*;
@@ -245,25 +244,17 @@ public class ApplicationImpl extends ComponentManagerImpl implements Application
if (isDisposed() || isDisposeInProgress()) {
return;
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
ApplicationManagerEx.setApplication(ApplicationImpl.this);
try {
saveAll();
}
finally {
disposeSelf();
}
ShutDownTracker.invokeAndWait(isUnitTestMode(), new Runnable() {
public void run() {
ApplicationManagerEx.setApplication(ApplicationImpl.this);
try {
saveAll();
}
});
}
catch (InterruptedException e) {
LOG.error(e);
}
catch (InvocationTargetException e) {
LOG.error(e);
}
finally {
disposeSelf();
}
}
});
}
});
}
@@ -683,7 +683,7 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
ShutDownTracker.getInstance().registerShutdownTask(new Runnable() {
@Override
public void run() {
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
ShutDownTracker.invokeAndWait(true, new Runnable() {
@Override
public void run() {
closeAndDeleteProject();
@@ -16,9 +16,11 @@
package com.intellij.openapi.util;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.concurrency.Semaphore;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@@ -127,4 +129,27 @@ public class ShutDownTracker implements Runnable {
private synchronized <T> T removeLast(LinkedList<T> list) {
return list.isEmpty()? null : list.removeLast();
}
public static void invokeAndWait(boolean timed, Runnable runnable) {
if (timed) {
final Semaphore semaphore = new Semaphore();
semaphore.down();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
semaphore.up();
}
});
if (!semaphore.waitFor(1000)) {
return;
}
}
try {
SwingUtilities.invokeAndWait(runnable);
}
catch (Exception e) {
LOG.error(e);
}
}
}