mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
junit busy wait replaced with socket accept (IDEA-27082)
This commit is contained in:
@@ -39,8 +39,13 @@ import gnu.trove.THashSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Collection;
|
||||
|
||||
public class TestPackage extends TestObject {
|
||||
@@ -90,25 +95,32 @@ public class TestPackage extends TestObject {
|
||||
LOG.error(e);
|
||||
}
|
||||
|
||||
findTestsWithProgress(new FindCallback() {
|
||||
public void found(@NotNull final Collection<PsiClass> classes, final boolean isJunit4) {
|
||||
addClassesListToJavaParameters(classes, new Function<PsiElement, String>() {
|
||||
@Nullable
|
||||
public String fun(PsiElement element) {
|
||||
if (element instanceof PsiClass) {
|
||||
return JavaExecutionUtil.getRuntimeQualifiedName((PsiClass)element);
|
||||
try {
|
||||
final ServerSocket serverSocket = new ServerSocket(0);
|
||||
myJavaParameters.getProgramParametersList().add("-socket" + serverSocket.getLocalPort());
|
||||
findTestsWithProgress(new FindCallback() {
|
||||
public void found(@NotNull final Collection<PsiClass> classes, final boolean isJunit4) {
|
||||
addClassesListToJavaParameters(classes, new Function<PsiElement, String>() {
|
||||
@Nullable
|
||||
public String fun(PsiElement element) {
|
||||
if (element instanceof PsiClass) {
|
||||
return JavaExecutionUtil.getRuntimeQualifiedName((PsiClass)element);
|
||||
}
|
||||
else if (element instanceof PsiMethod) {
|
||||
PsiMethod method = (PsiMethod)element;
|
||||
return JavaExecutionUtil.getRuntimeQualifiedName(method.getContainingClass()) + "," + method.getName();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if (element instanceof PsiMethod) {
|
||||
PsiMethod method = (PsiMethod)element;
|
||||
return JavaExecutionUtil.getRuntimeQualifiedName(method.getContainingClass()) + "," + method.getName();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}, packageName, false, isJunit4);
|
||||
}
|
||||
}, filter);
|
||||
}, packageName, false, isJunit4);
|
||||
}
|
||||
}, filter, serverSocket);
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private TestClassFilter getClassFilter(final PsiPackage aPackage) throws JUnitUtil.NoJUnitException {
|
||||
@@ -165,7 +177,7 @@ public class TestPackage extends TestObject {
|
||||
}
|
||||
}
|
||||
|
||||
private static void findTestsWithProgress(final FindCallback callback, final TestClassFilter classFilter) {
|
||||
private static void findTestsWithProgress(final FindCallback callback, final TestClassFilter classFilter, final ServerSocket serverSocket) {
|
||||
if (isSyncSearch()) {
|
||||
THashSet<PsiClass> classes = new THashSet<PsiClass>();
|
||||
boolean isJUnit4 = ConfigurationUtil.findAllTestClasses(classFilter, classes);
|
||||
@@ -176,20 +188,65 @@ 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) {
|
||||
isJunit4[0] = ConfigurationUtil.findAllTestClasses(classFilter, classes);
|
||||
try {
|
||||
final Socket socket = serverSocket.accept();
|
||||
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
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]);
|
||||
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();
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
|
||||
try {
|
||||
serverSocket.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,12 @@ import com.intellij.rt.execution.junit.segments.SegmentedOutputStream;
|
||||
import junit.textui.TestRunner;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Before rename or move
|
||||
@@ -33,6 +36,7 @@ public class JUnitStarter {
|
||||
public static final int VERSION = 5;
|
||||
public static final String IDE_VERSION = "-ideVersion";
|
||||
public static final String JUNIT4_PARAMETER = "-junit4";
|
||||
private static final String SOCKET = "-socket";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
SegmentedOutputStream out = new SegmentedOutputStream(System.out);
|
||||
@@ -63,6 +67,7 @@ public class JUnitStarter {
|
||||
|
||||
private static boolean processParameters(Vector args, final List listeners) {
|
||||
boolean isJunit4 = false;
|
||||
String tempFilePath = null;
|
||||
Vector result = new Vector(args.size());
|
||||
for (int i = 0; i < args.size(); i++) {
|
||||
String arg = (String)args.get(i);
|
||||
@@ -88,19 +93,33 @@ public class JUnitStarter {
|
||||
}
|
||||
continue;
|
||||
} else if (arg.startsWith("@")) {
|
||||
while(new File(arg.substring(1)).length() == 0); //wait for test cases
|
||||
tempFilePath = arg.substring(1);
|
||||
} else if (arg.startsWith(SOCKET)) {
|
||||
final int port = Integer.parseInt(arg.substring(SOCKET.length()));
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(arg.substring(1)));
|
||||
final Socket socket = new Socket(InetAddress.getLocalHost(), port); //start collecting tests
|
||||
final ServerSocket serverSocket = new ServerSocket(0);
|
||||
try {
|
||||
isJunit4 |= JUNIT4_PARAMETER.equals(reader.readLine());
|
||||
final BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
try {
|
||||
bufferedWriter.write(String.valueOf(serverSocket.getLocalPort())); //write port to sync
|
||||
}
|
||||
finally {
|
||||
bufferedWriter.close();
|
||||
socket.close();
|
||||
}
|
||||
serverSocket.accept();
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
serverSocket.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
isJunit4 = isJUnit4(isJunit4, tempFilePath);
|
||||
continue;
|
||||
}
|
||||
result.addElement(arg);
|
||||
}
|
||||
@@ -113,6 +132,22 @@ public class JUnitStarter {
|
||||
return isJunit4;
|
||||
}
|
||||
|
||||
private static boolean isJUnit4(boolean junit4, String tempFilePath) {
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(tempFilePath));
|
||||
try {
|
||||
junit4 |= JUNIT4_PARAMETER.equals(reader.readLine());
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return junit4;
|
||||
}
|
||||
|
||||
public static boolean checkVersion(String[] args, SegmentedOutputStream notifications) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String arg = args[i];
|
||||
|
||||
Reference in New Issue
Block a user