@Test(timeout = DEFAULT_TEST_TIMEOUT)
@DirtiesContext
public void testPrepareForShutdown() throws InterruptedException {
assertEquals(SchedulerStatus.STARTED, schedulerManager.getStatus());
Task task = new Task("name", "groupName", "sysExecJob", false, true, "sleep 5", defaultTimerSchedule);
taskManager.save(task);
schedulerManager.runTaskNow(task);
Thread.sleep(1000); // give task time to get started
assertTrue(taskRunManager.isRunning(task.getId()));
schedulerManager.prepareForShutdown();
// task is still running, so should still be in "preparing for shutdown" mode
assertEquals(SchedulerStatus.PREPARING_FOR_SHUTDOWN, schedulerManager.getStatus());
waitForTask(task);
// task no longer running, ready
assertEquals(SchedulerStatus.READY_FOR_SHUTDOWN, schedulerManager.getStatus());
schedulerManager.runTaskNow(task);
Thread.sleep(1000); // give task time to get started
assertFalse(taskRunManager.isRunning(task.getId())); // scheduler should not run anything anymore
assertEquals(SchedulerStatus.READY_FOR_SHUTDOWN, schedulerManager.getStatus());
schedulerManager.shutdown();
assertEquals(SchedulerStatus.SHUTDOWN, schedulerManager.getStatus());
}