Skip to content

Commit

Permalink
check for UpstreamCause as well as children
Browse files Browse the repository at this point in the history
  • Loading branch information
ENPHASEENERGY\mhuisman authored and matthuisman committed Nov 6, 2023
1 parent 9c7e7fb commit c61dcc4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public UpstreamCauseStrategy() {}
private UpstreamCause getUpstreamCause(@NonNull Queue.Item item) {
List<Cause> causes = item.getCauses();
for (Cause cause : causes) {
if (cause.getClass() == UpstreamCause.class) {
if (UpstreamCause.class.isAssignableFrom(cause.getClass())) {
return (UpstreamCause) cause;
}
}
Expand Down
28 changes: 23 additions & 5 deletions src/test/java/jenkins/advancedqueue/test/UpstreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hudson.model.Cause;
import hudson.model.Cause.UpstreamCause;
import hudson.model.Cause.UserIdCause;
import org.jenkinsci.plugins.workflow.support.steps.build.BuildUpstreamCause;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -27,7 +28,7 @@ public class UpstreamTest {
public void testOrphanDownstreamJob() throws Exception {
// Job 0 should run with default priority, as upstream build is unknown
TestRunListener.init(new ExpectedItem("Job 0", 5));
jobHelper.scheduleProjects(createUpstreamCause("Job X", 987)).go();
jobHelper.scheduleProjects(createUpstreamCause("Job X", 987, UpstreamCause.class)).go();
j.waitUntilNoActivity();

TestRunListener.assertStartedItems();
Expand All @@ -44,24 +45,41 @@ public void testUserJobAndAssociatedDownstreamJob() throws Exception {
// Downstream job 1 should run with priority of upstream job build 1
TestRunListener.init(new ExpectedItem("Downstream1", 1));
jobHelper
.scheduleProject("Downstream1", createUpstreamCause("Upstream", 1))
.scheduleProject("Downstream1", createUpstreamCause("Upstream", 1, UpstreamCause.class))
.go();
j.waitUntilNoActivity();

// Downstream job 2 should run with priority of upstream job build 2 (not present, i.e. default priority
// should be used)
TestRunListener.init(new ExpectedItem("Downstream2", 5));
jobHelper
.scheduleProject("Downstream2", createUpstreamCause("Upstream", 2))
.scheduleProject("Downstream2", createUpstreamCause("Upstream", 2, UpstreamCause.class))
.go();
j.waitUntilNoActivity();

TestRunListener.assertStartedItems();
}

@Test
@LocalData
public void testExtended() throws Exception {
// Upstream job should run with high priority (user triggered)
TestRunListener.init(new ExpectedItem("Upstream", 1));
jobHelper.scheduleProject("Upstream", new UserIdCause()).go();
j.waitUntilNoActivity();

// Downstream job 1 should run with priority of upstream job build 1
TestRunListener.init(new ExpectedItem("Downstream1", 1));
jobHelper
.scheduleProject("Downstream1", createUpstreamCause("Upstream", 1, BuildUpstreamCause.class))
.go();
j.waitUntilNoActivity();

TestRunListener.assertStartedItems();
}

@CheckForNull
private UpstreamCause createUpstreamCause(final String upstreamProject, final int upstreamBuild) throws Exception {
final Class<?> clazz = UpstreamCause.class;
private UpstreamCause createUpstreamCause(final String upstreamProject, final int upstreamBuild, final Class<?> clazz) throws Exception {
final Constructor<?>[] constructors = clazz.getDeclaredConstructors();

for (final Constructor<?> cons : constructors) {
Expand Down

0 comments on commit c61dcc4

Please sign in to comment.