@Test
  public void abortBuild() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject("test1");
    p.getBuildersList().add(new Shell("sleep 6000"));

    WorkflowJob foo = j.jenkins.createProject(WorkflowJob.class, "foo");
    foo.setDefinition(
        new CpsFlowDefinition(StringUtils.join(Arrays.asList("build('test1');"), "\n")));

    QueueTaskFuture<WorkflowRun> q = foo.scheduleBuild2(0);
    WorkflowRun b = q.getStartCondition().get();

    CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get();
    e.waitForSuspension();

    FreeStyleBuild fb = null;
    while (fb == null) {
      fb = p.getBuildByNumber(1);
      Thread.sleep(10);
    }
    fb.getExecutor().interrupt();

    j.assertBuildStatus(Result.ABORTED, j.waitForCompletion(fb));
    j.assertBuildStatus(Result.FAILURE, q.get());
  }
  private void assertSnippet(@NonNull String snippetName, boolean useSandbox) throws Exception {
    WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, snippetName);
    job.setDefinition(
        new CpsFlowDefinition(FileLoaderDSL.getSampleSnippet(snippetName), useSandbox));

    // Run job
    QueueTaskFuture<WorkflowRun> runFuture = job.scheduleBuild2(0, new Action[0]);
    assertThat("build was actually scheduled", runFuture, Matchers.notNullValue());
    WorkflowRun run = runFuture.get();

    j.assertBuildStatus(Result.SUCCESS, run);
  }
 @Issue("JENKINS-28063")
 @Test
 public void coalescedQueue() throws Exception {
   FreeStyleProject ds = j.createFreeStyleProject("ds");
   ds.setConcurrentBuild(true);
   ds.getBuildersList().add(new SleepBuilder(3000));
   WorkflowJob us = j.jenkins.createProject(WorkflowJob.class, "us");
   us.setDefinition(new CpsFlowDefinition("echo \"triggered #${build('ds').number}\"", true));
   QueueTaskFuture<WorkflowRun> us1F = us.scheduleBuild2(0);
   us1F.waitForStart(); // make sure we do not coalesce the us `Queue.Item`s
   QueueTaskFuture<WorkflowRun> us2F = us.scheduleBuild2(0);
   WorkflowRun us1 = us1F.get();
   assertEquals(1, us1.getNumber());
   j.assertLogContains("triggered #1", us1);
   WorkflowRun us2 = us2F.get();
   assertEquals(2, us2.getNumber());
   j.assertLogContains("triggered #1", us2);
   FreeStyleBuild ds1 = ds.getLastBuild();
   assertEquals(1, ds1.getNumber());
   assertEquals(2, ds1.getCauses().size()); // 2× UpstreamCause
 }
  @Test
  public void cancelBuildQueue() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject("test1");
    p.getBuildersList().add(new Shell("sleep 6000"));

    WorkflowJob foo = j.jenkins.createProject(WorkflowJob.class, "foo");
    foo.setDefinition(
        new CpsFlowDefinition(StringUtils.join(Arrays.asList("build('test1');"), "\n")));

    j.jenkins.setNumExecutors(0); // should force freestyle build to remain in the queue?

    QueueTaskFuture<WorkflowRun> q = foo.scheduleBuild2(0);

    WorkflowRun b = q.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get();
    e.waitForSuspension();

    Queue.Item[] items = j.jenkins.getQueue().getItems();
    assertEquals(1, items.length);
    j.jenkins.getQueue().cancel(items[0]);

    j.assertBuildStatus(Result.FAILURE, q.get());
  }