|
21 | 21 | import org.junit.Test;
|
22 | 22 |
|
23 | 23 | import java.time.Duration;
|
| 24 | +import java.util.function.Predicate; |
24 | 25 |
|
25 | 26 | import static org.assertj.core.api.BDDAssertions.then;
|
26 | 27 |
|
27 | 28 | public class CircuitBreakerConfigTest {
|
28 | 29 |
|
| 30 | + public static final Predicate<Throwable> TEST_PREDICATE = e -> "test".equals(e.getMessage()); |
| 31 | + |
29 | 32 | @Test(expected = IllegalArgumentException.class)
|
30 | 33 | public void zeroMaxFailuresShouldFail() {
|
31 | 34 | CircuitBreakerConfig.custom().failureRateThreshold(0).build();
|
@@ -97,9 +100,102 @@ public void shouldSetWaitInterval() {
|
97 | 100 | }
|
98 | 101 |
|
99 | 102 | @Test()
|
100 |
| - public void shouldUseCustomExceptionPredicate() { |
| 103 | + public void shouldUseRecordFailureThrowablePredicate() { |
101 | 104 | CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
|
102 |
| - .recordFailure((Throwable throwable) -> true).build(); |
103 |
| - then(circuitBreakerConfig.getRecordFailurePredicate()).isNotNull(); |
| 105 | + .recordFailure(TEST_PREDICATE).build(); |
| 106 | + then(circuitBreakerConfig.getRecordFailurePredicate().test(new Error("test"))).isEqualTo(true); |
| 107 | + then(circuitBreakerConfig.getRecordFailurePredicate().test(new Error("fail"))).isEqualTo(false); |
| 108 | + then(circuitBreakerConfig.getRecordFailurePredicate().test(new RuntimeException("test"))).isEqualTo(true); |
| 109 | + then(circuitBreakerConfig.getRecordFailurePredicate().test(new Error())).isEqualTo(false); |
| 110 | + then(circuitBreakerConfig.getRecordFailurePredicate().test(new RuntimeException())).isEqualTo(false); |
| 111 | + } |
| 112 | + |
| 113 | + private static class ExtendsException extends Exception { |
| 114 | + public ExtendsException() { } |
| 115 | + public ExtendsException(String message) { super(message); } |
| 116 | + } |
| 117 | + private static class ExtendsRuntimeException extends RuntimeException {} |
| 118 | + private static class ExtendsExtendsException extends ExtendsException {} |
| 119 | + private static class ExtendsException2 extends Exception {}; |
| 120 | + private static class ExtendsError extends Error {}; |
| 121 | + |
| 122 | + @Test() |
| 123 | + public void shouldUseIgnoreExceptionToBuildPredicate() { |
| 124 | + CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() |
| 125 | + .ignoreExceptions(RuntimeException.class, ExtendsExtendsException.class).build(); |
| 126 | + final Predicate<? super Throwable> failurePredicate = circuitBreakerConfig.getRecordFailurePredicate(); |
| 127 | + then(failurePredicate.test(new Exception())).isEqualTo(true); // not explicitly excluded |
| 128 | + then(failurePredicate.test(new ExtendsError())).isEqualTo(true); // not explicitly excluded |
| 129 | + then(failurePredicate.test(new ExtendsException())).isEqualTo(true); // not explicitly excluded |
| 130 | + then(failurePredicate.test(new ExtendsException2())).isEqualTo(true); // not explicitly excluded |
| 131 | + then(failurePredicate.test(new RuntimeException())).isEqualTo(false); // explicitly excluded |
| 132 | + then(failurePredicate.test(new ExtendsRuntimeException())).isEqualTo(false); // inherits excluded from ExtendsException |
| 133 | + then(failurePredicate.test(new ExtendsExtendsException())).isEqualTo(false); // explicitly excluded |
| 134 | + } |
| 135 | + |
| 136 | + @Test() |
| 137 | + public void shouldUseRecordExceptionToBuildPredicate() { |
| 138 | + CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() |
| 139 | + .recordExceptions(RuntimeException.class, ExtendsExtendsException.class).build(); |
| 140 | + final Predicate<? super Throwable> failurePredicate = circuitBreakerConfig.getRecordFailurePredicate(); |
| 141 | + then(failurePredicate.test(new Exception())).isEqualTo(false); // not explicitly included |
| 142 | + then(failurePredicate.test(new ExtendsError())).isEqualTo(false); // not explicitly included |
| 143 | + then(failurePredicate.test(new ExtendsException())).isEqualTo(false); // not explicitly included |
| 144 | + then(failurePredicate.test(new ExtendsException2())).isEqualTo(false); // not explicitly included |
| 145 | + then(failurePredicate.test(new RuntimeException())).isEqualTo(true); // explicitly included |
| 146 | + then(failurePredicate.test(new ExtendsRuntimeException())).isEqualTo(true); // inherits included from ExtendsException |
| 147 | + then(failurePredicate.test(new ExtendsExtendsException())).isEqualTo(true); // explicitly included |
| 148 | + } |
| 149 | + |
| 150 | + @Test() |
| 151 | + public void shouldUseIgnoreExceptionOverRecordToBuildPredicate() { |
| 152 | + CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() |
| 153 | + .recordExceptions(RuntimeException.class, ExtendsExtendsException.class) |
| 154 | + .ignoreExceptions(ExtendsException.class, ExtendsRuntimeException.class) |
| 155 | + .build(); |
| 156 | + final Predicate<? super Throwable> failurePredicate = circuitBreakerConfig.getRecordFailurePredicate(); |
| 157 | + then(failurePredicate.test(new Exception())).isEqualTo(false); // not explicitly included |
| 158 | + then(failurePredicate.test(new ExtendsError())).isEqualTo(false); // not explicitly included |
| 159 | + then(failurePredicate.test(new ExtendsException())).isEqualTo(false); // explicitly excluded |
| 160 | + then(failurePredicate.test(new ExtendsException2())).isEqualTo(false); // not explicitly included |
| 161 | + then(failurePredicate.test(new RuntimeException())).isEqualTo(true); // explicitly included |
| 162 | + then(failurePredicate.test(new ExtendsRuntimeException())).isEqualTo(false); // explicitly excluded |
| 163 | + then(failurePredicate.test(new ExtendsExtendsException())).isEqualTo(false); // inherits excluded from ExtendsException |
| 164 | + } |
| 165 | + |
| 166 | + @Test() |
| 167 | + public void shouldUseBothRecordToBuildPredicate() { |
| 168 | + CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() |
| 169 | + .recordFailure(TEST_PREDICATE) //1 |
| 170 | + .recordExceptions(RuntimeException.class, ExtendsExtendsException.class) //2 |
| 171 | + .ignoreExceptions(ExtendsException.class, ExtendsRuntimeException.class) //3 |
| 172 | + .build(); |
| 173 | + final Predicate<? super Throwable> failurePredicate = circuitBreakerConfig.getRecordFailurePredicate(); |
| 174 | + then(failurePredicate.test(new Exception())).isEqualTo(false); // not explicitly included |
| 175 | + then(failurePredicate.test(new Exception("test"))).isEqualTo(true); // explicitly included by 1 |
| 176 | + then(failurePredicate.test(new ExtendsError())).isEqualTo(false); // ot explicitly included |
| 177 | + then(failurePredicate.test(new ExtendsException())).isEqualTo(false); // explicitly excluded by 3 |
| 178 | + then(failurePredicate.test(new ExtendsException("test"))).isEqualTo(false); // explicitly excluded by 3 even if included by 1 |
| 179 | + then(failurePredicate.test(new ExtendsException2())).isEqualTo(false); // not explicitly included |
| 180 | + then(failurePredicate.test(new RuntimeException())).isEqualTo(true); // explicitly included by 2 |
| 181 | + then(failurePredicate.test(new ExtendsRuntimeException())).isEqualTo(false); // explicitly excluded by 3 |
| 182 | + then(failurePredicate.test(new ExtendsExtendsException())).isEqualTo(false); // inherits excluded from ExtendsException by 3 |
| 183 | + } |
| 184 | + |
| 185 | + @Test() |
| 186 | + public void builderMakePredicateShouldBuildPredicateAcceptingChildClass() { |
| 187 | + final Predicate<Throwable> predicate = CircuitBreakerConfig.Builder.makePredicate(RuntimeException.class); |
| 188 | + then(predicate.test(new RuntimeException())).isEqualTo(true); |
| 189 | + then(predicate.test(new Exception())).isEqualTo(false); |
| 190 | + then(predicate.test(new Throwable())).isEqualTo(false); |
| 191 | + then(predicate.test(new IllegalArgumentException())).isEqualTo(true); |
| 192 | + then(predicate.test(new RuntimeException() { |
| 193 | + })).isEqualTo(true); |
| 194 | + then(predicate.test(new Exception() { |
| 195 | + })).isEqualTo(false); |
| 196 | + |
104 | 197 | }
|
| 198 | + |
| 199 | + |
| 200 | + |
105 | 201 | }
|
0 commit comments