Fix exception handling

This commit is contained in:
huanghongxun
2019-02-26 08:17:46 +08:00
parent cae32cff4c
commit 15a75a69b3
2 changed files with 87 additions and 59 deletions

View File

@@ -4,15 +4,48 @@ import org.jackhuang.hmcl.task.Task;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class TaskTest {
/**
* TaskExecutor will not catch error and will be thrown to global handler.
*/
@Test
public void expectErrorUncaught() {
AtomicReference<Throwable> throwable = new AtomicReference<>();
Thread.setDefaultUncaughtExceptionHandler((t, e) -> throwable.set(e));
Assert.assertFalse(Task.composeAsync(() -> Task.allOf(
Task.allOf(Task.runAsync(() -> {
throw new Error();
}))
)).whenComplete(Assert::assertNull).test());
Assert.assertTrue(throwable.get() instanceof Error);
}
/**
*
*/
@Test
public void testWhenComplete() {
Task.composeAsync(() -> Task.allOf(
Task.allOf(Task.runAsync(() -> {
throw new Exception();
}))
)).whenComplete((exception -> {
Assert.assertFalse(exception == null);
})).test();
Assert.assertFalse(Task.supplyAsync(() -> {
throw new IllegalStateException();
}).whenComplete(exception -> {
Assert.assertTrue(exception instanceof IllegalStateException);
}).test());
}
}
@Test
public void testWithCompose() {
AtomicBoolean bool = new AtomicBoolean();
boolean success = Task.supplyAsync(() -> {
throw new IllegalStateException();
}).withRun(() -> {
bool.set(true);
}).test();
Assert.assertTrue(success);
Assert.assertTrue(bool.get());
}
}