Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CircuitBreaker + Predicate + Failure interaction. (#131)
* Failing test with CircuitBreaker + Predicate + Failure interaction. * Tests are only run if they end in *Test.java! http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html * Went ahead and implemented fixes. * Fill in test coverage for missing branches. * Remove PR discussion comments. Ready to merge. * Minor comment fix. * Add RetryPolicy Predicate exception-handling.
- Loading branch information
1 parent
59afa14
commit e3c37eb
Showing
6 changed files
with
176 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License | ||
*/ | ||
package net.jodah.failsafe; | ||
|
||
import net.jodah.failsafe.function.BiPredicate; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
@Test | ||
public class PredicatesTest { | ||
public void testResultPredicateOnlyHandlesResults() { | ||
BiPredicate<Object, Throwable> resultPredicate = Predicates.resultPredicateFor(result -> true); | ||
Assert.assertTrue(resultPredicate.test("result", null)); | ||
Assert.assertFalse(resultPredicate.test(null, new RuntimeException())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License | ||
*/ | ||
package net.jodah.failsafe.issues; | ||
|
||
import net.jodah.concurrentunit.Waiter; | ||
import net.jodah.failsafe.*; | ||
import net.jodah.failsafe.function.Predicate; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.*; | ||
|
||
import static org.testng.Assert.assertFalse; | ||
|
||
@Test | ||
public class Issue131Test { | ||
|
||
/** | ||
* This predicate is invoked in failure scenarios with an arg of null, | ||
* producing a {@link NullPointerException} yielding surpising results. | ||
*/ | ||
private static Predicate<String> failIfEqualsIgnoreCaseFoo = new Predicate<String>() { | ||
@Override | ||
public boolean test(String s) { | ||
return s.equalsIgnoreCase("foo"); // produces NPE when invoked in failing scenarios. | ||
} | ||
}; | ||
|
||
/** | ||
* Simple synchronous case throwing a {@link NullPointerException} | ||
* instead of the expected {@link FailsafeException}. | ||
*/ | ||
@Test(expectedExceptions = FailsafeException.class) | ||
public void syncShouldThrowTheUnderlyingIOException() throws Throwable { | ||
CircuitBreaker circuitBreaker = new CircuitBreaker().failIf(failIfEqualsIgnoreCaseFoo); | ||
SyncFailsafe<String> failsafe = Failsafe.<String>with(circuitBreaker); | ||
|
||
// I expect this get() to throw IOException, not NPE. | ||
failsafe.get(new Callable<String>() { | ||
@Override | ||
public String call() throws Exception { | ||
throw new IOException("let's blame it on network error"); | ||
} | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* More alarming async case where the Future is not even completed | ||
* since Failsafe does not recover from the {@link NullPointerException} thrown by the predicate. | ||
*/ | ||
public void asyncShouldCompleteTheFuture() throws Throwable { | ||
CircuitBreaker circuitBreaker = new CircuitBreaker().failIf(failIfEqualsIgnoreCaseFoo); | ||
AsyncFailsafe<String> failsafe = Failsafe.<String>with(circuitBreaker).with(Executors.newSingleThreadScheduledExecutor()); | ||
|
||
Waiter waiter = new Waiter(); | ||
|
||
failsafe | ||
.future(new Callable<CompletionStage<String>>() { | ||
@Override | ||
public CompletionStage<String> call() throws Exception { | ||
CompletableFuture<String> future = new CompletableFuture<String>(); | ||
future.completeExceptionally(new IOException("let's blame it on network error")); | ||
return future; | ||
} | ||
}) | ||
.whenComplete((s, t) -> waiter.resume()); // Never invoked! | ||
|
||
waiter.await(1000); | ||
} | ||
} |