Skip to content
Permalink
Browse files
Merge pull request #72 from justinharringa/JENKINS-46945
[JENKINS-46945] Run duration set before RunListener.fireCompleted
  • Loading branch information
svanoort committed Dec 4, 2017
2 parents 74985c0 + 8f8ff52 commit bf99a3d518221954e88a317ee1ef0f2a67139409
Showing with 23 additions and 12 deletions.
  1. +1 −1 src/main/java/org/jenkinsci/plugins/workflow/job/WorkflowRun.java
  2. +22 −11 src/test/java/org/jenkinsci/plugins/workflow/job/WorkflowRunTest.java
@@ -644,6 +644,7 @@ private String key() {
/** Handles normal build completion (including errors) but also handles the case that the flow did not even start correctly, for example due to an error in {@link FlowExecution#start}. */
private void finish(@Nonnull Result r, @CheckForNull Throwable t) {
setResult(r);
duration = Math.max(0, System.currentTimeMillis() - getStartTimeInMillis());
LOGGER.log(Level.INFO, "{0} completed: {1}", new Object[] {toString(), getResult()});
if (listener == null) {
LOGGER.log(Level.WARNING, this + " failed to start", t);
@@ -660,7 +661,6 @@ private void finish(@Nonnull Result r, @CheckForNull Throwable t) {
listener.closeQuietly();
}
logsToCopy = null;
duration = Math.max(0, System.currentTimeMillis() - getStartTimeInMillis());
try {
save();
} catch (Exception x) {
@@ -25,18 +25,10 @@
package org.jenkinsci.plugins.workflow.job;

import com.gargoylesoftware.htmlunit.WebResponse;
import com.google.common.collect.ImmutableSet;
import hudson.AbortException;
import hudson.model.AbstractBuild;
import hudson.model.BallColor;
import hudson.model.Executor;
import hudson.model.Item;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.model.User;
import hudson.Extension;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import hudson.model.queue.QueueTaskFuture;
import hudson.security.ACL;
import hudson.security.Permission;
@@ -84,6 +76,8 @@
import org.jvnet.hudson.test.recipes.LocalData;
import org.xml.sax.SAXException;

import javax.annotation.Nonnull;

public class WorkflowRunTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@@ -114,6 +108,23 @@ public class WorkflowRunTest {
r.assertLogContains("param=value", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("PARAM", "value")))));
}

@Issue("JENKINS-46945")
@Test public void durationIsCalculated() throws Exception {
WorkflowJob duration = r.jenkins.createProject(WorkflowJob.class, "duration");
duration.setDefinition(new CpsFlowDefinition("echo \"duration should not be 0 in DurationRunListener\"",true));
r.assertBuildStatusSuccess(duration.scheduleBuild2(0));
assertNotEquals(0L, DurationRunListener.duration);
}

@Extension
public static final class DurationRunListener extends RunListener<Run> {
static long duration = 0L;
@Override
public void onCompleted(Run run, @Nonnull TaskListener listener) {
duration = run.getDuration();
}
}

@Test public void funnyParameters() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("echo \"a.b=${params['a.b']}\"", true));

0 comments on commit bf99a3d

Please sign in to comment.