/**
   * Tests start here.
   *
   * @throws Throwable any exception that can be thrown during the test.
   */
  @org.junit.Test
  public void run() throws Throwable {

    String task1Name = "task1";
    String task2Name = "task2";

    // test submission and event reception
    TaskFlowJob job =
        (TaskFlowJob)
            JobFactory_stax.getFactory()
                .createJob(new File(jobDescriptor.toURI()).getAbsolutePath());
    if (OperatingSystem.getOperatingSystem() == OperatingSystem.windows) {
      ((NativeTask) job.getTask(task1Name))
          .setCommandLine("cmd", "/C", "ping", "127.0.0.1", "-n", "10", ">", "NUL");
    }
    JobId id = SchedulerTHelper.submitJob(job);

    SchedulerTHelper.log("Job submitted, id " + id.toString());

    SchedulerTHelper.log("Waiting for jobSubmitted Event");
    JobState receivedState = SchedulerTHelper.waitForEventJobSubmitted(id);

    Assert.assertEquals(receivedState.getId(), id);

    SchedulerTHelper.log("Waiting for job running");
    JobInfo jInfo = SchedulerTHelper.waitForEventJobRunning(id);
    Assert.assertEquals(jInfo.getJobId(), id);
    Assert.assertEquals(JobStatus.RUNNING, jInfo.getStatus());

    SchedulerTHelper.waitForEventTaskRunning(id, task1Name);
    TaskInfo tInfo = SchedulerTHelper.waitForEventTaskFinished(id, task1Name);

    Assert.assertEquals(TaskStatus.FINISHED, tInfo.getStatus());

    SchedulerTHelper.waitForEventTaskRunning(id, task2Name);
    tInfo = SchedulerTHelper.waitForEventTaskFinished(id, task2Name);

    Assert.assertEquals(TaskStatus.FAULTY, tInfo.getStatus());

    SchedulerTHelper.waitForEventJobFinished(id);
    JobResult res = SchedulerTHelper.getJobResult(id);

    // check that there is one exception in results
    Assert.assertTrue(res.getExceptionResults().size() == 1);

    // remove job
    SchedulerTHelper.removeJob(id);
    SchedulerTHelper.waitForEventJobRemoved(id);
  }
  /**
   * Creates and submit a job from an XML job descriptor, and check, with assertions, event related
   * to this job submission : 1/ job submitted event 2/ job passing from pending to running (with
   * state set to running). 3/ every task passing from pending to running (with state set to
   * running). 4/ every task finish without error ; passing from running to finished (with state set
   * to finished). 5/ and finally job passing from running to finished (with state set to finished).
   * Then returns.
   *
   * <p>This is the simplest events sequence of a job submission. If you need to test specific
   * events or task states (failures, rescheduling etc, you must not use this helper and check
   * events sequence with waitForEvent**() functions.
   *
   * @param jobToSubmit job object to schedule.
   * @return JobId, the job's identifier.
   * @throws Exception if an error occurs at job submission, or during verification of events
   *     sequence.
   */
  public JobId testJobSubmission(Job jobToSubmit) throws Exception {
    Scheduler userInt = getSchedulerInterface();

    JobId id = userInt.submit(jobToSubmit);

    log("Job submitted, id " + id.toString());

    log("Waiting for jobSubmitted");
    JobState receivedState = waitForEventJobSubmitted(id);
    Assert.assertEquals(id, receivedState.getId());

    log("Waiting for job running");
    JobInfo jInfo = waitForEventJobRunning(id);

    Assert.assertEquals(jInfo.getJobId(), id);
    Assert.assertEquals("Job " + jInfo.getJobId(), JobStatus.RUNNING, jInfo.getStatus());

    if (jobToSubmit instanceof TaskFlowJob) {

      for (Task t : ((TaskFlowJob) jobToSubmit).getTasks()) {
        log("Waiting for task running : " + t.getName());
        TaskInfo ti = waitForEventTaskRunning(id, t.getName());
        Assert.assertEquals(t.getName(), ti.getTaskId().getReadableName());
        Assert.assertEquals("Task " + t.getName(), TaskStatus.RUNNING, ti.getStatus());
      }

      for (Task t : ((TaskFlowJob) jobToSubmit).getTasks()) {
        log("Waiting for task finished : " + t.getName());
        TaskInfo ti = waitForEventTaskFinished(id, t.getName());
        Assert.assertEquals(t.getName(), ti.getTaskId().getReadableName());
        if (ti.getStatus() == TaskStatus.FAULTY) {
          TaskResult tres = userInt.getTaskResult(jInfo.getJobId(), t.getName());
          Assert.assertNotNull("Task result of " + t.getName(), tres);
          if (tres.getOutput() != null) {
            System.err.println("Output of failing task (" + t.getName() + ") :");
            System.err.println(tres.getOutput().getAllLogs(true));
          }
          if (tres.hadException()) {
            System.err.println("Exception occurred in task (" + t.getName() + ") :");
            tres.getException().printStackTrace(System.err);
          }
        }
        Assert.assertEquals("Task " + t.getName(), TaskStatus.FINISHED, ti.getStatus());
      }
    }

    log("Waiting for job finished");
    jInfo = waitForEventJobFinished(id);
    Assert.assertEquals("Job " + jInfo.getJobId(), JobStatus.FINISHED, jInfo.getStatus());

    log("Job finished");
    return id;
  }
  /**
   * Tests start here.
   *
   * @throws Throwable any exception that can be thrown during the test.
   */
  @org.junit.Test
  public void run() throws Throwable {

    String task1Name = "Task1";
    String task2Name = "Task2";

    String taskForked1Name = "Fork1";
    String taskForked2Name = "Fork2";
    TaskFlowJob job =
        (TaskFlowJob)
            JobFactory_stax.getFactory()
                .createJob(new File(jobDescriptor.toURI()).getAbsolutePath());
    if (OperatingSystem.getOperatingSystem() == OperatingSystem.windows) {
      ((NativeTask) job.getTask(task1Name))
          .setCommandLine("cmd", "/C", "ping", "127.0.0.1", "-n", "20", ">", "NUL");
      ((NativeTask) job.getTask(taskForked1Name))
          .setCommandLine("cmd", "/C", "ping", "127.0.0.1", "-n", "20", ">", "NUL");
    }
    JobId id = SchedulerTHelper.submitJob(job);

    SchedulerTHelper.log("Job submitted, id " + id.toString());

    SchedulerTHelper.log("Waiting for jobSubmitted Event");
    Job receivedJob = SchedulerTHelper.waitForEventJobSubmitted(id);
    Assert.assertEquals(receivedJob.getId(), id);

    SchedulerTHelper.log("Waiting for job running");
    JobInfo jInfo = SchedulerTHelper.waitForEventJobRunning(id);
    Assert.assertEquals(jInfo.getJobId(), id);
    Assert.assertEquals(JobStatus.RUNNING, jInfo.getStatus());

    SchedulerTHelper.log("check events for task " + task1Name);
    TaskInfo tInfo = SchedulerTHelper.waitForEventTaskRunning(id, task1Name);
    Assert.assertEquals(TaskStatus.RUNNING, tInfo.getStatus());
    tInfo = SchedulerTHelper.waitForEventTaskFinished(id, task1Name);
    Assert.assertEquals(TaskStatus.FINISHED, tInfo.getStatus());

    SchedulerTHelper.log("check events for task " + task2Name);
    tInfo = SchedulerTHelper.waitForEventTaskRunning(id, task2Name);
    Assert.assertEquals(TaskStatus.RUNNING, tInfo.getStatus());
    tInfo = SchedulerTHelper.waitForEventTaskFinished(id, task2Name);
    Assert.assertEquals(TaskStatus.FINISHED, tInfo.getStatus());

    // this task reaches wall time, so finishes with faulty state
    SchedulerTHelper.log("check events for task " + taskForked1Name);
    tInfo = SchedulerTHelper.waitForEventTaskRunning(id, taskForked1Name);
    Assert.assertEquals(TaskStatus.RUNNING, tInfo.getStatus());
    tInfo = SchedulerTHelper.waitForEventTaskFinished(id, taskForked1Name);
    Assert.assertEquals(TaskStatus.FAULTY, tInfo.getStatus());

    // this task reaches wall time, so finishes with faulty state
    SchedulerTHelper.log("check events for task " + taskForked2Name);
    tInfo = SchedulerTHelper.waitForEventTaskRunning(id, taskForked2Name);
    Assert.assertEquals(TaskStatus.RUNNING, tInfo.getStatus());
    tInfo = SchedulerTHelper.waitForEventTaskFinished(id, taskForked2Name);
    Assert.assertEquals(TaskStatus.FAULTY, tInfo.getStatus());

    SchedulerTHelper.log("Waiting for job finished");
    jInfo = SchedulerTHelper.waitForEventJobFinished(id);
    Assert.assertEquals(jInfo.getJobId(), id);
    Assert.assertEquals(JobStatus.FINISHED, jInfo.getStatus());

    // check result are not null
    JobResult res = SchedulerTHelper.getJobResult(id);
    Assert.assertTrue(res.hadException());

    Assert.assertFalse(res.getResult(task1Name).hadException());
    Assert.assertNull(res.getResult(task1Name).getException());

    Assert.assertFalse(res.getResult(task2Name).hadException());
    Assert.assertNull(res.getResult(task2Name).getException());

    Assert.assertFalse(res.getResult(taskForked1Name).hadException());
    Assert.assertNull(res.getResult(taskForked1Name).getException());

    Assert.assertTrue(res.getResult(taskForked2Name).hadException());
    Assert.assertNotNull(res.getResult(taskForked2Name).getException());

    // remove job
    SchedulerTHelper.removeJob(id);
    SchedulerTHelper.waitForEventJobRemoved(id);
  }