Skip to content

Commit

Permalink
feat: delete task throws exception if running
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanzalu committed Nov 12, 2024
1 parent 9fc0e35 commit 5f8eddb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public boolean stopTask(String taskId) {

@Override
public <V> Task<V> clearTask(String taskId) {
if (tasks.get(taskId).getState() == Task.State.RUNNING) {
throw new IllegalStateException(String.format("task id <%s> is already in RUNNING state", taskId));
}
logger.info("deleting task id <{}>", taskId);
return (Task<V>) tasks.remove(taskId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public List<Task<?>> clearDoneTasks() {

@Override
public <V> Task<V> clearTask(String taskName) {
if (tasks.get(taskName).getState() == Task.State.RUNNING) {
throw new IllegalStateException(String.format("task id <%s> is already in RUNNING state", taskName));
}
logger.info("deleting task id <{}>", taskName);
return (Task<V>) tasks.remove(taskName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public List<Task<?>> clearDoneTasks() {

@Override
public Task<?> clearTask(String taskId) {
if (tasks.get(taskId).getState() == Task.State.RUNNING) {
throw new IllegalStateException(String.format("task id <%s> is already in RUNNING state", taskId));
}
logger.info("deleting task id <{}>", taskId);
return tasks.remove(taskId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ public void test_clear_task_among_two_tasks() throws Exception {
assertThat(taskManager.getTasks()).hasSize(1);
}

@Test(expected = IllegalStateException.class)
public void test_clear_running_task_should_throw_exception() throws Exception {
taskManager.startTask("taskName", User.local(), new HashMap<>());

assertThat(taskManager.getTasks()).hasSize(1);

// in the task runner loop
Task<Serializable> task = taskQueue.poll(1, TimeUnit.SECONDS); // to sync
taskSupplier.progress(task.id,0.5);
nextMessage.await();

assertThat(taskManager.getTask(task.id).getState()).isEqualTo(Task.State.RUNNING);

taskManager.clearTask(task.id);
}

@Test(timeout = 2000)
public void test_task_canceled() throws Exception {
taskManager.startTask("taskName", User.local(), new HashMap<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public void test_clear_the_only_task() throws Exception {
assertThat(taskManager.getTasks()).hasSize(0);
}

@Test(expected = IllegalStateException.class)
public void test_clear_running_task_should_throw_exception() throws Exception {
Task<Integer> task = new Task<>("sleep", User.local(), Map.of("intParameter", 12));

taskManager.startTask(task);
taskManager.shutdownAndAwaitTermination(1, TimeUnit.SECONDS);
taskManager.progress(task.id, 0.5);
assertThat(taskManager.getTask(task.id).getState()).isEqualTo(Task.State.RUNNING);

taskManager.clearTask(task.id);
}

@Test
public void test_progress_on_unknown_task() throws InterruptedException {
taskManager.shutdownAndAwaitTermination(1, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ public void test_clear_task_among_two_tasks() throws Exception {
assertThat(taskManager.getTask(taskView2Id)).isNotNull();
}

@Test(expected = IllegalStateException.class)
public void test_clear_running_task_should_throw_exception() throws Exception {
String taskViewId = taskManager.startTask("sleep", User.local(), new HashMap<>());

taskSupplier.progress(taskViewId,0.5);
assertThat(waitForEvent.await(1, TimeUnit.SECONDS)).isTrue();
assertThat(taskManager.getTask(taskViewId).getState()).isEqualTo(Task.State.RUNNING);

taskManager.clearTask(taskViewId);
}

@Test
public void test_done_task_result_for_file() throws Exception {
String taskViewId = taskManager.startTask("HelloWorld", User.local(), new HashMap<>() {{
Expand Down

0 comments on commit 5f8eddb

Please sign in to comment.