public void testSelfExcludingJobs() throws Exception {

    BuildBlockerProperty theProperty = new BuildBlockerProperty();
    theProperty.setBlockingJobs("SelfExcluding_.*");

    FreeStyleProject theJob1 = createFreeStyleProject("SelfExcluding_Job1");
    theJob1.addProperty(theProperty);
    assertTrue(theJob1.getBuilds().isEmpty());

    FreeStyleProject theJob2 = createFreeStyleProject("SelfExcluding_Job2");
    theJob2.addProperty(theProperty);
    assertTrue(theJob1.getBuilds().isEmpty());

    // allow executing two simultanious jobs
    int theOldNumExecutors = Hudson.getInstance().getNumExecutors();
    Hudson.getInstance().setNumExecutors(2);

    Future<FreeStyleBuild> theFuture1 = theJob1.scheduleBuild2(0);
    Future<FreeStyleBuild> theFuture2 = theJob2.scheduleBuild2(0);

    long theStartTime = System.currentTimeMillis();
    long theEndTime = theStartTime;
    while ((!theFuture1.isDone() || !theFuture2.isDone()) && theEndTime < theStartTime + 5000) {
      theEndTime = System.currentTimeMillis();
    }

    // if more then five seconds have passed, we assume its a deadlock.
    assertTrue(theEndTime < theStartTime + 5000);

    // restore changed settings
    Hudson.getInstance().setNumExecutors(theOldNumExecutors);
    theJob2.delete();
    theJob1.delete();
  }
  /**
   * One test for all for faster execution.
   *
   * @throws Exception
   */
  public void testCanRun() throws Exception {
    // init slave
    LabelAtom slaveLabel = new LabelAtom("slave");
    LabelAtom masterLabel = new LabelAtom("master");

    DumbSlave slave = this.createSlave(slaveLabel);
    SlaveComputer c = slave.getComputer();
    c.connect(false).get(); // wait until it's connected
    if (c.isOffline()) {
      fail("Slave failed to go online: " + c.getLog());
    }

    BuildBlockerQueueTaskDispatcher dispatcher = new BuildBlockerQueueTaskDispatcher();

    String blockingJobName = "blockingJob";

    Shell shell = new Shell("sleep 1");

    Future<FreeStyleBuild> future1 = createBlockingProject("xxx", shell, masterLabel);
    Future<FreeStyleBuild> future2 = createBlockingProject(blockingJobName, shell, masterLabel);
    Future<FreeStyleBuild> future3 = createBlockingProject("yyy", shell, slaveLabel);
    // add project to slave
    FreeStyleProject project = this.createFreeStyleProject();
    project.setAssignedLabel(slaveLabel);

    Queue.BuildableItem item =
        new Queue.BuildableItem(
            new Queue.WaitingItem(Calendar.getInstance(), project, new ArrayList<Action>()));

    CauseOfBlockage causeOfBlockage = dispatcher.canRun(item);

    assertNull(causeOfBlockage);

    BuildBlockerProperty property = new BuildBlockerProperty();

    property.setBlockingJobs(".*ocki.*");

    project.addProperty(property);

    causeOfBlockage = dispatcher.canRun(item);
    assertNotNull(causeOfBlockage);

    assertEquals(
        "Blocking job " + blockingJobName + " is running.", causeOfBlockage.getShortDescription());

    while (!(future1.isDone() && future2.isDone() && future3.isDone())) {
      // wait until jobs are done.
    }
  }
  public void testMultipleExecutors() throws Exception {

    // Job1 runs for 1 second, no dependencies
    FreeStyleProject theJob1 = createFreeStyleProject("MultipleExecutor_Job1");
    theJob1.getBuildersList().add(new Shell("sleep 1; exit 0"));
    assertTrue(theJob1.getBuilds().isEmpty());

    // Job2 returns immediatly but can't run while Job1 is running.
    FreeStyleProject theJob2 = createFreeStyleProject("MultipleExecutor_Job2");
    {
      BuildBlockerProperty theProperty = new BuildBlockerProperty();
      theProperty.setBlockingJobs("MultipleExecutor_Job1");
      theJob2.addProperty(theProperty);
    }
    assertTrue(theJob1.getBuilds().isEmpty());

    // allow executing two simultanious jobs
    int theOldNumExecutors = Hudson.getInstance().getNumExecutors();
    Hudson.getInstance().setNumExecutors(2);

    Future<FreeStyleBuild> theFuture1 = theJob1.scheduleBuild2(0);
    Future<FreeStyleBuild> theFuture2 = theJob2.scheduleBuild2(0);
    while (!theFuture1.isDone() || !theFuture2.isDone()) {
      // let the jobs process
    }

    // check if job2 was not started before job1 was finished
    Run theRun1 = theJob1.getLastBuild();
    Run theRun2 = theJob2.getLastBuild();
    assertTrue(theRun1.getTimeInMillis() + theRun1.getDuration() <= theRun2.getTimeInMillis());

    // restore changed settings
    Hudson.getInstance().setNumExecutors(theOldNumExecutors);
    theJob2.delete();
    theJob1.delete();
  }