Skip to content
Permalink
Browse files

Don't trigger builds triggered by the same event

Now any builds are triggered even if existing builds triggerd by the same
event are running.

This patch prevents to trigger build according to the below policy:

* Project has triggered/running build triggered by the same event.
* The event trigger builds has still running build.

This would fix JENKINS-24445.

Fix for JENKINS-24445

Task-Url: https://issues.jenkins-ci.org/browse/JENKINS-24445
  • Loading branch information...
rinrinne committed Sep 1, 2014
1 parent ef6eea5 commit 407fcae820ea9a3be7c8ed0f5f50545fb30bdff3
@@ -325,6 +325,21 @@ public boolean isBuilding(GerritTriggeredEvent event) {
}
}

/**
* Checks the memory if the project is triggered by the event.
*
* @param project the project.
* @param event the event.
* @return true if so.
*/
public boolean isTriggered(AbstractProject project, GerritTriggeredEvent event) {
if (project == null || event == null) {
return false;
} else {
return memory.isTriggered(event, project);
}
}

/**
* Finds the GerritCause for a build if there is one.
*
@@ -835,6 +835,14 @@ private boolean isInteresting(GerritTriggeredEvent event) {
return false;
}

if (ToGerritRunListener.getInstance().isProjectTriggeredAndIncomplete(myProject, event)) {
logger.trace("Already triggered and imcompleted.");
return false;
} else if (ToGerritRunListener.getInstance().isTriggered(myProject, event)) {
logger.trace("Already triggered.");
return false;
}

if (!shouldTriggerOnEventType(event)) {
return false;
}
@@ -1057,7 +1057,7 @@ public void testGerritEventNotInteresting() {

trigger.gerritEvent(event);

verifyZeroInteractions(listener);
verify(listener, never()).onTriggered(same(project), same(event));
verify(project).isBuildable();
verifyNoMoreInteractions(project);
}
@@ -1129,7 +1129,7 @@ public void testGerritEventSilentMode() {

trigger.gerritEvent(event);

verifyNoMoreInteractions(listener);
verify(listener, never()).onTriggered(same(project), same(event));

verify(project).scheduleBuild2(
anyInt(),
@@ -1167,7 +1167,7 @@ public void testGerritEventManualEventSilentMode() {

trigger.gerritEvent(event);

verifyNoMoreInteractions(listener);
verify(listener, never()).onTriggered(same(project), same(event));

verify(project).scheduleBuild2(
anyInt(),
@@ -36,6 +36,7 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.DuplicatesUtil;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
import com.sonymobile.tools.gerrit.gerritevents.mock.SshdServerMock;

import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Cause;
@@ -46,12 +47,14 @@
import hudson.model.Queue.Task;
import hudson.model.Result;
import hudson.util.RunList;

import org.apache.sshd.SshServer;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.SleepBuilder;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.recipes.LocalData;

import java.util.Arrays;
import java.util.List;

import static com.sonymobile.tools.gerrit.gerritevents.mock.SshdServerMock.GERRIT_STREAM_EVENTS;
@@ -117,24 +120,86 @@ public void testDynamicTriggeredBuild() throws Exception {
public void testDoubleTriggeredBuild() throws Exception {
GerritServer gerritServer = PluginImpl.getInstance().getServer(PluginImpl.DEFAULT_SERVER_NAME);
FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
project.getBuildersList().add(new SleepBuilder(5000));
server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
boolean started = false;

gerritServer.triggerEvent(Setup.createPatchsetCreated());
while (!started) {
if (project.isBuilding()) {
started = true;
}
Thread.sleep(1000);
}
gerritServer.triggerEvent(Setup.createPatchsetCreated());

RunList<FreeStyleBuild> builds = DuplicatesUtil.waitForBuilds(project, 1, 5000);
FreeStyleBuild build = builds.get(0);
assertSame(Result.SUCCESS, build.getResult());
assertEquals(1, project.getBuilds().size());

int count = 0;
for (Cause cause : build.getCauses()) {
if (cause instanceof GerritCause) {
count++;
assertNotNull(((GerritCause)cause).getContext());
assertNotNull(((GerritCause)cause).getEvent());
while (project.isBuilding() || project.isInQueue()) {
Thread.sleep(1000);
}

int size = 0;
for (FreeStyleBuild build : project.getBuilds()) {
assertSame(Result.SUCCESS, build.getResult());
int count = 0;
for (Cause cause : build.getCauses()) {
if (cause instanceof GerritCause) {
count++;
assertNotNull(((GerritCause)cause).getContext());
assertNotNull(((GerritCause)cause).getEvent());
}
}
assertEquals(1, count);
size++;
}
assertEquals(1, count);
assertEquals(1, size);
}

/**
* Tests to trigger a build from a project with the same patch set twice.
* Expecting one build to be scheduled with one cause.
* And builds are not triggered if build is not building but other builds triggered by a event is building.
*
* @throws Exception if so.
*/
@LocalData
public void testDoubleTriggeredBuildWithProjects() throws Exception {
GerritServer gerritServer = PluginImpl.getInstance().getServer(PluginImpl.DEFAULT_SERVER_NAME);
FreeStyleProject project1 = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
FreeStyleProject project2 = DuplicatesUtil.createGerritTriggeredJob(this, "projectY");
project1.getBuildersList().add(new SleepBuilder(5000));
server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
boolean started = false;

gerritServer.triggerEvent(Setup.createPatchsetCreated());
while (!started) {
if (project1.isBuilding()) {
started = true;
}
Thread.sleep(1000);
}
gerritServer.triggerEvent(Setup.createPatchsetCreated());

while (project1.isBuilding() || project1.isInQueue()) {
Thread.sleep(1000);
}

int size = 0;
for (FreeStyleProject project : Arrays.asList(project1, project2)) {
for (FreeStyleBuild build : project.getBuilds()) {
assertSame(Result.SUCCESS, build.getResult());
int count = 0;
for (Cause cause : build.getCauses()) {
if (cause instanceof GerritCause) {
count++;
assertNotNull(((GerritCause)cause).getContext());
assertNotNull(((GerritCause)cause).getEvent());
}
}
assertEquals(1, count);
size++;
}
}
assertEquals(2, size);
}

/**
@@ -146,33 +211,40 @@ public void testDoubleTriggeredBuild() throws Exception {
public void testDoubleTriggeredBuildsOfDifferentChange() throws Exception {
GerritServer gerritServer = PluginImpl.getInstance().getServer(PluginImpl.DEFAULT_SERVER_NAME);
FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
project.getBuildersList().add(new SleepBuilder(5000));
server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
boolean started = false;

gerritServer.triggerEvent(Setup.createPatchsetCreated());
while (!started) {
if (project.isBuilding()) {
started = true;
}
Thread.sleep(1000);
}
PatchsetCreated patchsetCreated = Setup.createPatchsetCreated();
patchsetCreated.getChange().setNumber("2000");
gerritServer.triggerEvent(patchsetCreated);

RunList<FreeStyleBuild> builds = DuplicatesUtil.waitForBuilds(project, 2, 10000);
assertEquals(2, builds.size());
assertSame(Result.SUCCESS, builds.get(0).getResult());
assertSame(Result.SUCCESS, builds.get(1).getResult());

int count = 0;
for (Cause cause : builds.get(0).getCauses()) {
if (cause instanceof GerritCause) {
count++;
}
while (project.isBuilding() || project.isInQueue()) {
Thread.sleep(1000);
}
assertEquals(1, count);
count = 0;
for (Cause cause : builds.get(1).getCauses()) {
if (cause instanceof GerritCause) {
count++;
assertNotNull(((GerritCause)cause).getContext());
assertNotNull(((GerritCause)cause).getEvent());

int size = 0;
for (FreeStyleBuild build : project.getBuilds()) {
assertSame(Result.SUCCESS, build.getResult());
int count = 0;
for (Cause cause : build.getCauses()) {
if (cause instanceof GerritCause) {
count++;
assertNotNull(((GerritCause)cause).getContext());
assertNotNull(((GerritCause)cause).getEvent());
}
}
assertEquals(1, count);
size++;
}
assertEquals(1, count);
assertEquals(2, size);
}

/**

0 comments on commit 407fcae

Please sign in to comment.
You can’t perform that action at this time.