Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE in case of race condition #110

Merged
merged 3 commits into from
Jan 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,19 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
}
for (Future<Run> future : futures.get(p)) {
try {
listener.getLogger().println("Waiting for the completion of " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()));
Run b = future.get();
listener.getLogger().println(HyperlinkNote.encodeTo('/'+ b.getUrl(), b.getFullDisplayName()) + " completed. Result was "+b.getResult());
BuildInfoExporterAction.addBuildInfoExporterAction(build, b.getParent().getFullName(), b.getNumber(), b.getResult());

if(buildStepResult && config.getBlock().mapBuildStepResult(b.getResult())) {
build.setResult(config.getBlock().mapBuildResult(b.getResult()));
if (future != null ) {
listener.getLogger().println("Waiting for the completion of " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()));
Run b = future.get();
listener.getLogger().println(HyperlinkNote.encodeTo('/' + b.getUrl(), b.getFullDisplayName()) + " completed. Result was " + b.getResult());
BuildInfoExporterAction.addBuildInfoExporterAction(build, b.getParent().getFullName(), b.getNumber(), b.getResult());

if (buildStepResult && config.getBlock().mapBuildStepResult(b.getResult())) {
build.setResult(config.getBlock().mapBuildResult(b.getResult()));
} else {
buildStepResult = false;
}
} else {
buildStepResult = false;
listener.getLogger().println("Skipping " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()) + ". The project was not triggered by some reason.");
}
} catch (CancellationException x) {
throw new AbortException(p.getFullDisplayName() +" aborted.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package hudson.plugins.parameterizedtrigger.test;

import com.google.common.collect.ArrayListMultimap;
import hudson.EnvVars;
import hudson.Launcher;
import hudson.model.*;
import hudson.model.Cause.UpstreamCause;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameterFactory;
Expand All @@ -49,6 +52,7 @@
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.IOException;
Expand All @@ -60,11 +64,14 @@
import jenkins.model.Jenkins;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

public class TriggerBuilderTest {

Expand All @@ -75,6 +82,34 @@ private BlockableBuildTriggerConfig createTriggerConfig(String projects) {
return new BlockableBuildTriggerConfig(projects, new BlockingBehaviour("never", "never", "never"), null);
}

@Test
public void testProjectWasEnabledDuringTheBuild() throws Exception {
final Project<?, ?> triggerProject = r.createFreeStyleProject("projectA");
final Project<?, ?> disabledJob = r.createFreeStyleProject("projectC");
final BlockableBuildTriggerConfig config = Mockito.mock(BlockableBuildTriggerConfig.class);
when(config.getProjects(any(EnvVars.class))).thenReturn(disabledJob.getName());
when(config.getBlock()).thenReturn(new BlockingBehaviour(Result.FAILURE, Result.FAILURE, Result.FAILURE));

final ArrayListMultimap<Job, Future<Run>> futures = ArrayListMultimap.create();
when(config.perform3(any(AbstractBuild.class),
Mockito.any(Launcher.class),
Mockito.any(BuildListener.class))).thenReturn(futures);
// Then project is disabled scheduler returns null instead of Future<Run> object
futures.put(disabledJob, null);

final List<Job> jobs = new ArrayList<Job>();
jobs.add(disabledJob);

when(config.getJobs(any(ItemGroup.class), any(EnvVars.class))).thenReturn(jobs);

TriggerBuilder triggerBuilder = new TriggerBuilder(config);
triggerProject.getBuildersList().add(triggerBuilder);

triggerProject.scheduleBuild2(0).get();

assertEquals(Result.SUCCESS, triggerProject.getLastBuild().getResult());
}

@Test
public void testOrderOfLogEntries() throws Exception {
r.createFreeStyleProject("project1");
Expand Down