Fix exception handling
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user