test discovery: use batch API for all clients

This commit is contained in:
Sergey Ignatov
2018-09-07 18:37:01 +03:00
parent bd90443fd1
commit f01b37156f
3 changed files with 39 additions and 39 deletions
@@ -8,19 +8,19 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.annotations.SerializedName;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Couple;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import com.intellij.util.io.HttpRequests;
import com.intellij.util.io.RequestBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.net.URLEncoder.encode;
import java.util.function.Function;
import java.util.stream.Collectors;
public class IntellijTestDiscoveryProducer implements TestDiscoveryProducer {
private static final String INTELLIJ_TEST_DISCOVERY_HOST = "http://intellij-test-discovery.labs.intellij.net";
@@ -28,28 +28,27 @@ public class IntellijTestDiscoveryProducer implements TestDiscoveryProducer {
@NotNull
@Override
public MultiMap<String, String> getDiscoveredTests(@NotNull Project project,
@NotNull String classFQName,
@Nullable String methodName,
@NotNull List<Couple<String>> classesAndMethods,
byte frameworkId) {
if (!ApplicationManager.getApplication().isInternal()) {
return MultiMap.emptyInstance();
return MultiMap.empty();
}
try {
String url = url(classFQName, methodName);
LOG.debug(url);
List<String> bareClasses = ContainerUtil.newSmartList();
List<Couple<String>> allTogether = ContainerUtil.newSmartList();
RequestBuilder r = HttpRequests.request(url)
.productNameAsUserAgent()
.gzip(true);
return r.connect(request -> {
MultiMap<String, String> map = new MultiMap<>();
TestsSearchResult result = new ObjectMapper().readValue(request.getInputStream(), TestsSearchResult.class);
result.getTests().forEach((classFqn, testMethodName) -> map.putValues(classFqn, testMethodName));
return map;
classesAndMethods.forEach(couple -> {
if (couple.second == null) bareClasses.add(couple.first);
else allTogether.add(couple);
});
MultiMap<String, String> result = new MultiMap<>();
result.putAllValues(request(allTogether, couple -> "\"" + couple.first + "." + couple.second + "\"", "methods"));
result.putAllValues(request(bareClasses, s -> "\"" + s + "\"", "classes"));
return result;
}
catch (HttpRequests.HttpStatusException http) {
LOG.debug("No tests found for class: '" + classFQName + "', method: '" + methodName + "'", http);
LOG.debug("No tests found", http);
}
catch (IOException e) {
LOG.debug(e);
@@ -57,10 +56,18 @@ public class IntellijTestDiscoveryProducer implements TestDiscoveryProducer {
return MultiMap.empty();
}
private static String url(@NotNull String classFQName, @Nullable String methodName) throws UnsupportedEncodingException {
return INTELLIJ_TEST_DISCOVERY_HOST + "/search/tests/" + (methodName == null ?
"by-class?fqn=" + encode(classFQName, "UTF-8") :
"by-method?fqn=" + encode(classFQName + "." + methodName, "UTF-8"));
@NotNull
private static <T> MultiMap<String, String> request(List<T> collection, Function<T, String> toString, String what) throws IOException {
if (collection.isEmpty()) return MultiMap.empty();
String url = INTELLIJ_TEST_DISCOVERY_HOST + "/search/tests/by-" + what;
LOG.debug(url);
return HttpRequests.post(url, "application/json").productNameAsUserAgent().gzip(true).connect(r -> {
r.write(collection.stream().map(toString).collect(Collectors.joining(",", "[", "]")));
TestsSearchResult search = new ObjectMapper().readValue(r.getInputStream(), TestsSearchResult.class);
MultiMap<String, String> result = new MultiMap<>();
search.getTests().forEach((classFqn, testMethodName) -> result.putValues(classFqn, testMethodName));
return result;
});
}
@Override
@@ -2,21 +2,24 @@
package com.intellij.execution.testDiscovery;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Couple;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class LocalTestDiscoveryProducer implements TestDiscoveryProducer {
@Override
@NotNull
public MultiMap<String, String> getDiscoveredTests(@NotNull Project project,
@NotNull String classFQName,
@Nullable String methodName,
@NotNull List<Couple<String>> classesAndMethods,
byte frameworkId) {
MultiMap<String, String> result = new MultiMap<>();
TestDiscoveryIndex instance = TestDiscoveryIndex.getInstance(project);
return methodName == null ?
instance.getTestsByClassName(classFQName, frameworkId) :
instance.getTestsByMethodName(classFQName, methodName, frameworkId);
classesAndMethods.forEach(couple -> result.putAllValues(couple.second == null ?
instance.getTestsByClassName(couple.first, frameworkId) :
instance.getTestsByMethodName(couple.first, couple.second, frameworkId)));
return result;
}
@Override
@@ -25,18 +25,8 @@ public interface TestDiscoveryProducer {
@NotNull
MultiMap<String, String> getDiscoveredTests(@NotNull Project project,
@NotNull String classFQName,
@Nullable String methodName,
byte frameworkId);
@NotNull
default MultiMap<String, String> getDiscoveredTests(@NotNull Project project,
@NotNull List<Couple<String>> classesAndMethods,
byte frameworkId) {
MultiMap<String, String> result = new MultiMap<>();
classesAndMethods.forEach(couple -> result.putAllValues(getDiscoveredTests(project, couple.first, couple.second, frameworkId)));
return result;
}
byte frameworkId);
boolean isRemote();