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 2 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 disabled.");
Copy link
Member

@oleg-nenashev oleg-nenashev Dec 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only one cause of the potential null futures IIRC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions what to put?
"The project was disabled or something went wrong"? :)

Copy link
Member

@oleg-nenashev oleg-nenashev Dec 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several cases:

  • perform3 Javadoc: // When a project is disabled or the configuration is not yet saved f will always be null and we're caught in a loop, therefore we need to check for it
  • BuildTriggerConfig#schedule: job is not an instance of ParameterizedJobMixIn.ParameterizedJob
  • ParameterizedJobMixIn#schedule2() returns null due to whatever other reason: Job is disabled, job is not buildable (e.g. triggering of Matrix Configuration), or Jenkins just does not like you (dispatcher extensions return null) :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can put The project was not triggered by some reason or

The project was not triggered because one of possible cases happened: 
1) Project was disabled in moment then this build started 
2) Configuration of that project was not saved then this build started 
3) Project is not instance of "ParameterizedJobMixIn.ParameterizedJob"
4) Jenkins just does not like you (dispatcher extensions return null instead of running this project) :(

Or keep only first part Skipping....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The project was not triggered by some reason" is fine for me. It's rather an RFE to Jenkins core in order to get a good CauseOfRejection as a response

}
} 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