|
1 | 1 | package com.linkedin.parseq; |
2 | 2 |
|
3 | | -import static org.testng.Assert.assertEquals; |
4 | | -import static org.testng.Assert.assertFalse; |
5 | | -import static org.testng.Assert.assertNull; |
6 | | -import static org.testng.Assert.assertSame; |
7 | | -import static org.testng.Assert.assertTrue; |
8 | | -import static org.testng.Assert.fail; |
9 | | - |
10 | 3 | import java.util.concurrent.Callable; |
11 | 4 | import java.util.concurrent.TimeUnit; |
12 | 5 | import java.util.concurrent.atomic.AtomicReference; |
13 | 6 |
|
| 7 | +import com.linkedin.parseq.trace.Trace; |
14 | 8 | import org.testng.annotations.Test; |
15 | 9 |
|
16 | 10 | import com.linkedin.parseq.function.Failure; |
17 | 11 | import com.linkedin.parseq.function.Function1; |
18 | 12 | import com.linkedin.parseq.function.Success; |
19 | 13 | import com.linkedin.parseq.function.Try; |
20 | 14 |
|
| 15 | +import static org.testng.Assert.*; |
| 16 | + |
21 | 17 |
|
22 | 18 | /** |
23 | 19 | * @author Jaroslaw Odzga (jodzga@linkedin.com) |
@@ -98,6 +94,56 @@ public void testRecover(int expectedNumberOfTasks) { |
98 | 94 | assertEquals(countTasks(failure.getTrace()), expectedNumberOfTasks); |
99 | 95 | } |
100 | 96 |
|
| 97 | + public void testRecoverWithExceptionFilter(int expectedNumberOfTasks) { |
| 98 | + Task<Integer> recoverd = getFailureTask() |
| 99 | + .map("strlen", String::length) |
| 100 | + .recover("", RuntimeException.class, e -> -1); |
| 101 | + runAndWait("AbstractTaskTest.recoverd", recoverd); |
| 102 | + assertEquals((int) recoverd.get(), -1); |
| 103 | + assertEquals(countTasks(recoverd.getTrace()), expectedNumberOfTasks); |
| 104 | + |
| 105 | + Task<Integer> notRecovered = getFailureTask() |
| 106 | + .map("strlen", String::length) |
| 107 | + .recover("", ArithmeticException.class, e -> -1); |
| 108 | + runAndWaitException("AbstractTaskTest.testRecoverWithExceptionFilterFailure", notRecovered, RuntimeException.class); |
| 109 | + assertTrue(notRecovered.isFailed()); |
| 110 | + assertEquals(countTasks(notRecovered.getTrace()), expectedNumberOfTasks); |
| 111 | + |
| 112 | + Task<String> recoveredExceptionSubclassesForward = getFailureTask() // Failed with RuntimeError |
| 113 | + .recover("", Throwable.class, e -> "expected-value") // expected to recover, as RuntimeError is subclass of Throwable |
| 114 | + .recover("", RuntimeException.class, e -> "unexpected-value"); // no action, as already recovered at previous step |
| 115 | + runAndWait("AbstractTaskTest.recoveredExceptionSubclassesForward", recoveredExceptionSubclassesForward); |
| 116 | + assertEquals(recoveredExceptionSubclassesForward.get(), "expected-value"); |
| 117 | + assertEquals(countTasks(recoveredExceptionSubclassesForward.getTrace()), expectedNumberOfTasks); |
| 118 | + |
| 119 | + Task<String> recoveredExceptionSubclassesBackwards = getFailureTask() // Failed with RuntimeError |
| 120 | + .recover("", RuntimeException.class, e -> "expected-value") // expected to recover, as exception class matches |
| 121 | + .recover("", Throwable.class, e -> "unexpected-value"); // no action, as already recovered at previous step |
| 122 | + runAndWait("AbstractTaskTest.recoveredExceptionSubclassesBackwards", recoveredExceptionSubclassesBackwards); |
| 123 | + assertEquals(recoveredExceptionSubclassesBackwards.get(), "expected-value"); // recovered with expected value |
| 124 | + assertEquals(countTasks(recoveredExceptionSubclassesBackwards.getTrace()), expectedNumberOfTasks); |
| 125 | + |
| 126 | + Task<String> recoveredUnrelatedExceptionFirst = getFailureTask() // Failed with RuntimeError |
| 127 | + .recover("", Error.class, e -> "unexpected-value") // no action, as the failed state is not of type Error |
| 128 | + .recover("", Throwable.class, e -> "expected-value"); // expected to recover, as exception class matches |
| 129 | + runAndWait("AbstractTaskTest.recoveredUnrelatedExceptionFirst", recoveredUnrelatedExceptionFirst); |
| 130 | + assertEquals(recoveredUnrelatedExceptionFirst.get(), "expected-value"); // recovered with expected value |
| 131 | + assertEquals(countTasks(recoveredUnrelatedExceptionFirst.getTrace()), expectedNumberOfTasks); |
| 132 | + |
| 133 | + Task<String> recoveredRecoveryThrows = getFailureTask() // Failed with RuntimeError |
| 134 | + .recover("", RuntimeException.class, e -> {throw new IllegalArgumentException();}) // exception class matches, but recovery throws another exception |
| 135 | + .recover("", IllegalArgumentException.class, e -> "expected-value"); // expected to recover, as exception class matches (see above) |
| 136 | + runAndWait("AbstractTaskTest.recoveredRecoveryThrows", recoveredRecoveryThrows); |
| 137 | + assertEquals(recoveredRecoveryThrows.get(), "expected-value"); // recovered with expected value |
| 138 | + assertEquals(countTasks(recoveredRecoveryThrows.getTrace()), expectedNumberOfTasks); |
| 139 | + |
| 140 | + Task<String> failedAfterRecoveryThrows = getFailureTask() // Failed with RuntimeError |
| 141 | + .recover("", RuntimeException.class, e -> {throw new IllegalArgumentException();}); // throws another exception while recovering |
| 142 | + IllegalArgumentException ex = runAndWaitException(failedAfterRecoveryThrows, IllegalArgumentException.class); |
| 143 | + assertNotNull(ex); |
| 144 | + assertEquals(countTasks(recoveredRecoveryThrows.getTrace()), expectedNumberOfTasks); |
| 145 | + } |
| 146 | + |
101 | 147 | public void testCancelledRecover(int expectedNumberOfTasks) { |
102 | 148 | Task<Integer> cancelled = getCancelledTask().map("strlen", String::length).recover(e -> -1); |
103 | 149 | try { |
|
0 commit comments