}
@Test
public void testTaskExecution() {
final Integer[] data = new Integer[]{0};
Task task = new Task() {
@Override
public void run() {
for(int i = 0; i < 2; i++) {
data[0]++;
updateProgress(i, 2);
if(isCancelled()) {
return;
}
}
}
};
assertEquals("Task progress initially 0", 0, task.getProgress());
Long taskId = taskService.schedule(task);
assertEquals("Task can be found by ID", task, taskService.getTask(taskId));
taskService.start(taskId);
assertEquals("Task was executed", 2, data[0]);
assertEquals("Task progress is 100", 100, task.getProgress());
assertTrue("Task is done after finish", task.isDone());
assertNotNull("Task NOT removed from service after done",
taskService.getTask(taskId));
taskService.finish(taskId);
assertNull("Task removed from service with finish",
taskService.getTask(taskId));
data[0] = 0;
task.reset();
assertEquals("Task progress is 0 after reset", 0, task.getProgress());
// cancel should stop task at 50%
task.cancel();
Long newTaskId = taskService.scheduleAndStart(task);
assertFalse("Unique id generated", taskId.equals(newTaskId));
assertEquals("Cancelled after 50%", 1, data[0]);
assertTrue("Task was cancelled", task.isCancelled());
assertEquals("Progress is 50%", 50, task.getProgress());
// done if cancelled?
assertTrue("Task is done", task.isDone());
}