-
Notifications
You must be signed in to change notification settings - Fork 213
Notify GraphListener extensions prior to listeners added via FlowExecution.addListener
#1052
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
Changes from all commits
e6e6484
c66bb9d
7ea2178
4291e87
4241907
dc0ef61
33ca269
11321e4
c518cf8
2f7ceec
49661ef
5df8a3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,6 +111,7 @@ void newStartNode(FlowStartNode n) throws IOException { | |
| } | ||
| execution.flowStartNodeActions.clear(); | ||
| } // may be unset from loadProgramFailed | ||
| n.addAction(new TimingAction()); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We handle |
||
| synchronized (execution) { | ||
| this.head = execution.startNodes.push(n); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,26 @@ | ||
| package org.jenkinsci.plugins.workflow; | ||
|
|
||
| import static org.awaitility.Awaitility.await; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| import hudson.ExtensionList; | ||
| import hudson.model.Run; | ||
| import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
| import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution; | ||
| import org.jenkinsci.plugins.workflow.flow.GraphListener; | ||
| import org.jenkinsci.plugins.workflow.graph.FlowEndNode; | ||
| import org.jenkinsci.plugins.workflow.graph.FlowNode; | ||
| import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
| import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
| import org.junit.Assert; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.ErrorCollector; | ||
| import org.jvnet.hudson.test.JenkinsRule; | ||
| import org.jvnet.hudson.test.LoggerRule; | ||
| import org.jvnet.hudson.test.TestExtension; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Serializable; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
@@ -22,12 +29,15 @@ | |
|
|
||
| public class GraphListenerTest | ||
| { | ||
| @ClassRule | ||
| public static JenkinsRule r = new JenkinsRule(); | ||
| @Rule | ||
| public JenkinsRule r = new JenkinsRule(); | ||
|
|
||
| @Rule | ||
| public LoggerRule logging = new LoggerRule(); | ||
|
|
||
| @Rule | ||
| public ErrorCollector errors = new ErrorCollector(); | ||
|
|
||
| private static final String LOG_MESSAGE = "some problem here"; | ||
|
|
||
| @Issue("JENKINS-54890") | ||
|
|
@@ -47,7 +57,7 @@ public void listener() | |
| Assert.assertTrue( "cannot find listener exception message", found > 0 ); | ||
| } | ||
|
|
||
| @TestExtension | ||
| @TestExtension("listener") | ||
| public static class TestGraphListener | ||
| implements GraphListener, Serializable | ||
| { | ||
|
|
@@ -61,4 +71,33 @@ public void onNewHead( FlowNode flowNode ) | |
| throw new NullPointerException( LOG_MESSAGE ); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void listenersRunBeforeBuildCompletion() throws Exception { | ||
| var listener = ExtensionList.lookupSingleton(CheckBuildCompletionListener.class); | ||
| listener.errors = errors; | ||
| var p = r.createProject(WorkflowJob.class); | ||
| p.setDefinition(new CpsFlowDefinition("echo 'test'", true)); | ||
| var b = r.buildAndAssertSuccess(p); | ||
| await().until(() -> listener.done); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the current code, this |
||
| } | ||
|
|
||
| @TestExtension("listenersRunBeforeBuildCompletion") | ||
| public static class CheckBuildCompletionListener implements GraphListener { | ||
| private ErrorCollector errors; | ||
| private boolean done; | ||
|
|
||
| @Override | ||
| public void onNewHead(FlowNode node) { | ||
| if (node instanceof FlowEndNode) { | ||
| try { | ||
| var b = (WorkflowRun) node.getExecution().getOwner().getExecutable(); | ||
| errors.checkThat("Listeners should always run before build completion", b.isLogUpdated(), is(true)); | ||
| } catch (IOException e) { | ||
| errors.addError(e); | ||
| } | ||
| done = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seemed preferable to handle the reversal of this list when listeners are added rather than each time listeners are notified. In practice I think this list will normally only contain
WorkflowRun$GraphLandWorkflowRun$NodePrintListener, althoughWorkflowRun$FailOnLoadListenerwill be added temporarily.