junit: cancel server socket accept

This commit is contained in:
anna
2010-03-31 20:52:43 +04:00
parent e74959b4ef
commit 2d504526f4
3 changed files with 79 additions and 60 deletions
@@ -15,6 +15,7 @@
*/
package com.intellij.openapi.progress.impl;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
@@ -320,12 +321,15 @@ public class ProgressManagerImpl extends ProgressManager {
progressIndicator = new EmptyProgressIndicator();
}
else {
final BackgroundableProcessIndicator indicator = new BackgroundableProcessIndicator(task);
final Project project = task.getProject();
Disposer.register(ApplicationManager.getApplication(), indicator);
progressIndicator = indicator;
progressIndicator = new BackgroundableProcessIndicator(task);
}
runProcessWithProgressAsynchronously(task, progressIndicator);
}
public static void runProcessWithProgressAsynchronously(final Task.Backgroundable task, final ProgressIndicator progressIndicator) {
if (progressIndicator instanceof Disposable) {
Disposer.register(ApplicationManager.getApplication(), (Disposable)progressIndicator);
}
final Runnable process = new TaskRunnable(task, progressIndicator);
@@ -26,8 +26,9 @@ import com.intellij.execution.testframework.TestSearchScope;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
import com.intellij.openapi.progress.impl.ProgressManagerImpl;
import com.intellij.openapi.project.DumbModeAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
@@ -39,10 +40,9 @@ import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
@@ -187,65 +187,81 @@ public class TestPackage extends TestObject {
final THashSet<PsiClass> classes = new THashSet<PsiClass>();
final boolean[] isJunit4 = new boolean[1];
ProgressManager.getInstance().run(new Task.Backgroundable(classFilter.getProject(), ExecutionBundle.message("seaching.test.progress.title"), true) {
int myPort = -1;
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
final Socket socket = serverSocket.accept();
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final Task.Backgroundable task =
new Task.Backgroundable(classFilter.getProject(), ExecutionBundle.message("seaching.test.progress.title"), true) {
int myPort = -1;
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
myPort = Integer.parseInt(bufferedReader.readLine());
} finally {
bufferedReader.close();
socket.close();
}
}
catch (IOException e) {
LOG.info(e);
}
isJunit4[0] = ConfigurationUtil.findAllTestClasses(classFilter, classes);
}
@Override
public void onSuccess() {
callback.found(classes, isJunit4[0]);
connect();
}
@Override
public void onCancel() {
connect();
}
@Override
public DumbModeAction getDumbModeAction() {
return DumbModeAction.WAIT;
}
private void connect() {
Socket socket = null;
try {
socket = new Socket(InetAddress.getLocalHost(), myPort);
}
catch (IOException e) {
LOG.info(e);
}
finally {
try {
if (socket != null) socket.close();
final Socket socket = serverSocket.accept();
final DataInputStream is = new DataInputStream(socket.getInputStream());
try {
myPort = is.readInt();
}
finally {
is.close();
}
}
catch (IOException e) {
LOG.info(e);
}
isJunit4[0] = ConfigurationUtil.findAllTestClasses(classFilter, classes);
}
@Override
public void onSuccess() {
callback.found(classes, isJunit4[0]);
connect();
}
@Override
public void onCancel() {
connect();
}
@Override
public DumbModeAction getDumbModeAction() {
return DumbModeAction.WAIT;
}
private void connect() {
Socket socket = null;
try {
serverSocket.close();
socket = new Socket(InetAddress.getLocalHost(), myPort);
}
catch (IOException e) {
catch (Throwable e) {
LOG.info(e);
}
finally {
try {
if (socket != null) socket.close();
}
catch (Throwable e) {
LOG.info(e);
}
try {
serverSocket.close();
}
catch (Throwable e) {
LOG.info(e);
}
}
}
};
ProgressManagerImpl.runProcessWithProgressAsynchronously(task, new BackgroundableProcessIndicator(task) {
@Override
public void cancel() {
try {//ensure that serverSocket.accept was interrupted
if (!serverSocket.isClosed()) {
new Socket(InetAddress.getLocalHost(), serverSocket.getLocalPort());
}
}
catch (Throwable e) {
LOG.info(e);
}
super.cancel();
}
});
}
@@ -254,7 +270,7 @@ public class TestPackage extends TestObject {
return ApplicationManager.getApplication().isUnitTestMode();
}
public static interface FindCallback {
public interface FindCallback {
/**
* Invoked in dispatch thread
*/
@@ -100,13 +100,12 @@ public class JUnitStarter {
final Socket socket = new Socket(InetAddress.getLocalHost(), port); //start collecting tests
final ServerSocket serverSocket = new ServerSocket(0);
try {
final BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
final DataOutputStream os = new DataOutputStream(socket.getOutputStream());
try {
bufferedWriter.write(String.valueOf(serverSocket.getLocalPort())); //write port to sync
os.writeInt(serverSocket.getLocalPort()); //write port to sync
}
finally {
bufferedWriter.close();
socket.close();
os.close();
}
serverSocket.accept();
}