Skip to content

Commit

Permalink
Action for running tests in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
petrovic-d committed Nov 22, 2024
1 parent 41483e9 commit a3b539b
Show file tree
Hide file tree
Showing 41 changed files with 996 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.netbeans.modules.gradle.api.execute.RunUtils;
import org.netbeans.modules.gradle.actions.ActionToTaskUtils;
import org.netbeans.modules.gradle.execute.GradleExecutorOptionsPanel;
import org.netbeans.modules.gradle.spi.actions.GradleActionsProvider;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -215,6 +214,9 @@ static String taskName(Project project, String action, Lookup lkp) {
case ActionProvider.COMMAND_TEST:
title = TXT_Test(prjLabel);
break;
case ActionProvider.COMMAND_TEST_PARALLEL:
title = TXT_Test(prjLabel);
break;
case ActionProvider.COMMAND_RUN_SINGLE:
title = TXT_Run(dobjName);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
</profiles>
<!-- @end region="configuration-declaration-xml" -->

<action name="test.parallel">
<args>--parallel --rerun-tasks ${taskWithProjects}</args>
</action>
<apply-for plugins="java">
<action name="test.single">
<args>cleanTest test --tests "${selectedClass}"</args>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,26 @@ public final class ExplicitProcessParameters {
final int position;
private final List<String> launcherArguments;
private final List<String> arguments;
private final List<String> projects;
private final boolean replaceArgs;
private final boolean replaceLauncherArgs;
private final File workingDirectory;
private final Map<String, String> environmentVars;

private ExplicitProcessParameters(int position, List<String> launcherArguments,
List<String> arguments, boolean appendArgs, boolean appendLauncherArgs,
File workingDirectory, Map<String, String> environmentVars) {
File workingDirectory, Map<String, String> environmentVars, List<String> projects) {
this.position = position;
this.launcherArguments = launcherArguments == null ? null : Collections.unmodifiableList(launcherArguments);
this.arguments = arguments == null ? null : Collections.unmodifiableList(arguments);
this.projects = projects == null ? null : Collections.unmodifiableList(projects);
this.replaceArgs = appendArgs;
this.replaceLauncherArgs = appendLauncherArgs;
this.workingDirectory = workingDirectory;
this.environmentVars = environmentVars == null ? null : Collections.unmodifiableMap(environmentVars);
}

private static final ExplicitProcessParameters EMPTY = new ExplicitProcessParameters(0, null, null, false, false, null, null);
private static final ExplicitProcessParameters EMPTY = new ExplicitProcessParameters(0, null, null, false, false, null, null, null);

/**
* Returns an empty instance of parameters that has no effect. DO NOT check for emptiness by
Expand Down Expand Up @@ -146,6 +148,16 @@ public boolean isEmpty() {
public List<String> getArguments() {
return arguments;
}

/**
* Returns the projects to be passed. Returns {@code null} if the object does not
* want to alter the project list.
* @return projects to be passed or {@code null} if the project list should not be altered.
*/
public List<String> getProjects() {
return projects;
}


/**
* Returns the launcher arguments to be passed. Returns {@code null} if the object does not
Expand Down Expand Up @@ -282,6 +294,7 @@ public static final class Builder {
private int position = 0;
private List<String> launcherArguments = null;
private List<String> arguments = null;
private List<String> projects = null;
private Boolean replaceArgs;
private Boolean replaceLauncherArgs;
private File workingDirectory = null;
Expand Down Expand Up @@ -336,6 +349,42 @@ public Builder args(@NullAllowed String... args) {
return args(Arrays.asList(args));
}

private void initProjects() {
if (projects == null) {
projects = new ArrayList<>();
}
}

/**
* Appends a single project. {@code null} is ignored.
* @param p project
* @return the builder
*/
public Builder project(@NullAllowed String p) {
if (p == null) {
return this;
}
initProjects();
projects.add(p);
return this;
}

/**
* Appends projects in the list. {@code null} is ignored as well as {@code null}
* items in the list.
* @param projects project list
* @return the builder
*/
public Builder projects(@NullAllowed List<String> projects) {
if (projects == null) {
return this;
}
// init even if the list is empty.
initProjects();
projects.forEach(this::project);
return this;
}

private void initLauncherArgs() {
if (launcherArguments == null) {
launcherArguments = new ArrayList<>();
Expand Down Expand Up @@ -524,7 +573,7 @@ public ExplicitProcessParameters build() {
return new ExplicitProcessParameters(position, launcherArguments, arguments,
// if no args / launcher args given and no explicit instruction on append,
// make the args appending.
aa, apa, workingDirectory, environmentVars);
aa, apa, workingDirectory, environmentVars, projects);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public interface ActionProvider {
*/
String COMMAND_TEST_SINGLE = "test.single"; // NOI18N

/**
* Standard command for running tests in parallel on given projects sub-modules
*/
String COMMAND_TEST_PARALLEL = "test.parallel"; // NOI18N

/**
* Standard command for running the project in debugger
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class JavaActionProvider extends DefaultGradleActionsProvider {
COMMAND_RUN,
COMMAND_DEBUG,
COMMAND_TEST,
COMMAND_TEST_PARALLEL,
COMMAND_TEST_SINGLE,
COMMAND_DEBUG_TEST_SINGLE,
COMMAND_RUN_SINGLE_METHOD,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.gradle.java;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;
import org.netbeans.modules.gradle.api.NbGradleProject;
import org.netbeans.modules.gradle.spi.actions.ReplaceTokenProvider;
import org.netbeans.spi.project.ActionProvider;
import org.netbeans.spi.project.ProjectServiceProvider;
import org.openide.util.Lookup;

/**
*
* @author Dusan Petrovic
*/
@ProjectServiceProvider(
service = ReplaceTokenProvider.class,
projectType = NbGradleProject.GRADLE_PROJECT_TYPE
)
public class ProjectsTokenProvider implements ReplaceTokenProvider {

private static final String TASK_WITH_PROJECTS = "taskWithProjects"; //NOI18N
private static final Set<String> SUPPORTED = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
TASK_WITH_PROJECTS
)));

@Override
public Set<String> getSupportedTokens() {
return SUPPORTED;
}

@Override
public Map<String, String> createReplacements(String action, Lookup context) {
String taskName = getTaskForAction(action);
if (taskName == null) {
return new HashMap<>();
}
return getProjectsWithTaskReplacement(taskName, context);
}

private String getTaskForAction(String action) {
return switch (action) {
case ActionProvider.COMMAND_TEST_PARALLEL -> "test"; //NOI18N
default -> null;
};
}

private Map<String,String> getProjectsWithTaskReplacement(String taskName, Lookup context) {
ExplicitProcessParameters parameters = context.lookup(ExplicitProcessParameters.class);
List<String> projects = parameters.getProjects();
if (projects == null || projects.isEmpty()) {
return Map.of(TASK_WITH_PROJECTS, taskName);
}
StringBuilder resultTask = new StringBuilder();
for (String project : projects) {
resultTask.append(project)
.append(":") //NOI18N
.append(taskName)
.append(" ");//NOI18N
}
return Map.of(TASK_WITH_PROJECTS, resultTask.toString().trim());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
-->
<!DOCTYPE actions SYSTEM "action-mapping.dtd">
<actions>
<action name="test.parallel">
<args>--parallel --rerun-tasks ${taskWithProjects}</args>
</action>
<apply-for plugins="java">
<action name="test.single">
<args>"${cleanTestTaskName}" "${testTaskName}" --tests "${selectedClass}"</args>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.gradle.tooling.events.test.TestStartEvent;
import org.gradle.tooling.events.test.TestSuccessResult;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.gsf.testrunner.api.CommonUtils;
import org.netbeans.modules.gsf.testrunner.api.CoreManager;
import org.netbeans.modules.gsf.testrunner.api.Report;
Expand All @@ -62,13 +63,13 @@
*
* @author Laszlo Kishalmi
*/
@ProjectServiceProvider(service = GradleProgressListenerProvider.class, projectType = NbGradleProject.GRADLE_PLUGIN_TYPE + "/java")
@ProjectServiceProvider(service = GradleProgressListenerProvider.class, projectType = NbGradleProject.GRADLE_PROJECT_TYPE )
public final class GradleTestProgressListener implements ProgressListener, GradleProgressListenerProvider {

private final Project project;
TestSession session;
private final Map<String, TestSession> sessions = new ConcurrentHashMap<>();

Map<String, Testcase> runningTests = new ConcurrentHashMap<>();
private Map<TestSession, Map<String, Testcase>> runningTests = new ConcurrentHashMap<>();

public GradleTestProgressListener(Project project) {
this.project = project;
Expand Down Expand Up @@ -131,17 +132,19 @@ private void processTestProgress(TestProgressEvent evt) {
}

private void processTestOutput(TestOutputEvent evt) {
TestSession session = sessions.get(getSessionKey(evt.getDescriptor()));
assert session != null;
TestOutputDescriptor desc = evt.getDescriptor();
OperationDescriptor parent = desc.getParent();
CoreManager manager = getManager();
String msg = desc.getMessage();
if (msg != null && msg.endsWith("\n")) {
msg = msg.substring(0, msg.length() - 1);
if (manager != null) {
if (manager != null && session != null) {
manager.displayOutput(session, msg, desc.getDestination().equals(Destination.StdErr));
}
if (parent instanceof JvmTestOperationDescriptor) {
Testcase tc = runningTests.get(getTestOpKey((JvmTestOperationDescriptor) parent));
Testcase tc = runningTests.get(session).get(getTestOpKey((JvmTestOperationDescriptor) parent));
if (tc != null) {
tc.addOutputLines(Arrays.asList(msg.split("\\R")));
}
Expand All @@ -151,8 +154,9 @@ private void processTestOutput(TestOutputEvent evt) {


private void sessionStart(TestStartEvent evt) {
session = new TestSession(evt.getDisplayName(), project, TestSession.SessionType.TEST);
runningTests.clear();
String key = getSessionKey(evt.getDescriptor());
TestSession session = sessions.computeIfAbsent(key, name -> new TestSession(name, getProject(key), TestSession.SessionType.TEST));
runningTests.put(session, new ConcurrentHashMap<>());
CoreManager manager = getManager();
if (manager != null) {
manager.registerNodeFactory();
Expand All @@ -161,7 +165,9 @@ private void sessionStart(TestStartEvent evt) {
}

private void sessionFinish(TestFinishEvent evt) {
runningTests.clear();
TestSession session = sessions.remove(getSessionKey(evt.getDescriptor()));
assert session != null;
runningTests.remove(session);
CoreManager manager = getManager();
if (manager != null) {
manager.sessionFinished(session);
Expand All @@ -172,6 +178,8 @@ private void suiteStart(TestStartEvent evt, JvmTestOperationDescriptor op) {
}

private void suiteFinish(TestFinishEvent evt, JvmTestOperationDescriptor op) {
TestSession session = sessions.get(getSessionKey(evt.getDescriptor()));
assert session != null;
TestOperationResult result = evt.getResult();
TestSuite currentSuite = session.getCurrentSuite();
String suiteName = GradleTestSuite.suiteName(op);
Expand All @@ -186,6 +194,7 @@ private void suiteFinish(TestFinishEvent evt, JvmTestOperationDescriptor op) {
}

private void caseStart(TestStartEvent evt, JvmTestOperationDescriptor op) {
TestSession session = sessions.get(getSessionKey(evt.getDescriptor()));
assert session != null;
assert op.getParent() != null;
TestSuite currentSuite = session.getCurrentSuite();
Expand All @@ -198,12 +207,14 @@ private void caseStart(TestStartEvent evt, JvmTestOperationDescriptor op) {
}
}
Testcase tc = new GradleTestcase(op, session);
runningTests.put(getTestOpKey(op), tc);
runningTests.get(session).put(getTestOpKey(op), tc);
session.addTestCase(tc);
}

private void caseFinish(TestFinishEvent evt, JvmTestOperationDescriptor op) {
Testcase tc = runningTests.get(getTestOpKey(op));
TestSession session = sessions.get(getSessionKey(evt.getDescriptor()));
assert session != null;
Testcase tc = runningTests.get(session).remove(getTestOpKey(op));
if (tc != null) {
TestOperationResult result = evt.getResult();
long time = result.getEndTime() - result.getStartTime();
Expand Down Expand Up @@ -241,11 +252,38 @@ private void caseFinish(TestFinishEvent evt, JvmTestOperationDescriptor op) {
}

}
runningTests.remove(getTestOpKey(op));
}

}

private static String GRADLE_TEST_RUN = "Gradle Test Run :";
private static String TEST = ":test";

private Project getProject(String key) {
if (key != null && key.startsWith(GRADLE_TEST_RUN)) {
key = key.substring(GRADLE_TEST_RUN.length());
if (key.endsWith(TEST)) {
key = key.substring(0, key.length() - TEST.length()).trim();
if (!key.isEmpty()) {
for (Project containedPrj : ProjectUtils.getContainedProjects(project, false)) {
if (key.equals(containedPrj.getProjectDirectory().getName())) {
return containedPrj;
}
}
}
}
}
return project;
}

private static String getSessionKey(OperationDescriptor op) {
String id = "";
for (OperationDescriptor descriptor = op; descriptor != null; descriptor = descriptor.getParent()) {
id = descriptor.getName();
}
return id;
}

private static JvmTestOperationDescriptor getSuiteOpDesc(JvmTestOperationDescriptor op, String className) {
for (JvmTestOperationDescriptor descriptor = op; descriptor != null; descriptor = (JvmTestOperationDescriptor) descriptor.getParent()) {
if (className == null || className.equals(descriptor.getClassName())) {
Expand Down
Loading

0 comments on commit a3b539b

Please sign in to comment.