@Test
public void testTaskTimeout() {

    Counter counter = registry.counter("task_timeout", "class", "WorkflowMonitor", "taskType", "test");
    long counterCount = counter.count();
    TaskDef taskType = new TaskDef();
    taskType.setName("test");
    taskType.setTimeoutPolicy(TimeoutPolicy.RETRY);
    taskType.setTimeoutSeconds(1);
    Task task = new Task();
    task.setTaskType(taskType.getName());
    task.setStartTime(System.currentTimeMillis() - 2_000);        //2 seconds ago!
    task.setStatus(Status.IN_PROGRESS);
    deciderService.checkTaskTimeout(taskType, task);
    //Task should be marked as timed out
    assertEquals(Status.TIMED_OUT, task.getStatus());
    assertNotNull(task.getReasonForIncompletion());
    assertEquals(++counterCount, counter.count());
    taskType.setTimeoutPolicy(TimeoutPolicy.ALERT_ONLY);
    task.setStatus(Status.IN_PROGRESS);
    task.setReasonForIncompletion(null);
    deciderService.checkTaskTimeout(taskType, task);
    //Nothing will happen
    assertEquals(Status.IN_PROGRESS, task.getStatus());
    assertNull(task.getReasonForIncompletion());
    assertEquals(++counterCount, counter.count());
    boolean exception = false;
    taskType.setTimeoutPolicy(TimeoutPolicy.TIME_OUT_WF);
    task.setStatus(Status.IN_PROGRESS);
    task.setReasonForIncompletion(null);
    try {
        deciderService.checkTaskTimeout(taskType, task);
    } catch (TerminateWorkflowException tw) {
        exception = true;
    }
    assertTrue(exception);
    assertEquals(Status.TIMED_OUT, task.getStatus());
    assertNotNull(task.getReasonForIncompletion());
    assertEquals(++counterCount, counter.count());
    taskType.setTimeoutPolicy(TimeoutPolicy.TIME_OUT_WF);
    task.setStatus(Status.IN_PROGRESS);
    task.setReasonForIncompletion(null);
    deciderService.checkTaskTimeout(null, task);    //this will be a no-op
    assertEquals(Status.IN_PROGRESS, task.getStatus());
    assertNull(task.getReasonForIncompletion());
    assertEquals(counterCount, counter.count());
}