Skip to content

Commit 201062d

Browse files
committed
Add support for no policies via Failsafe.none
Closes #252
1 parent 7a92886 commit 201062d

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

src/main/java/net/jodah/failsafe/Failsafe.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public static <R, P extends Policy<R>> FailsafeExecutor<R> with(P outerPolicy, P
8484
*/
8585
@SuppressWarnings("unchecked")
8686
public static <R> FailsafeExecutor<R> with(List<? extends Policy<R>> policies) {
87-
return new FailsafeExecutor<>((List<Policy<R>>) Assert.notNull(policies, "policies"));
87+
Assert.notNull(policies, "policies");
88+
Assert.isTrue(!policies.isEmpty(), "At least one policy must be supplied");
89+
return new FailsafeExecutor<>((List<Policy<R>>) policies);
90+
}
91+
92+
/**
93+
* Creates and returns a noop {@link FailsafeExecutor} instance that treats any exception as a failure for the
94+
* purposes of calling event listeners, and provides no additional failure handling.
95+
*
96+
* @param <R> result type
97+
* @throws NullPointerException if {@code policies} is null
98+
* @throws IllegalArgumentException if {@code policies} is empty
99+
*/
100+
public static <R> FailsafeExecutor<R> none() {
101+
return new FailsafeExecutor<>(Collections.emptyList());
88102
}
89103
}

src/main/java/net/jodah/failsafe/FailsafeExecutor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class FailsafeExecutor<R> extends PolicyListeners<FailsafeExecutor<R>, R>
5151
* @throws IllegalArgumentException if {@code policies} is empty
5252
*/
5353
FailsafeExecutor(List<Policy<R>> policies) {
54-
Assert.isTrue(!policies.isEmpty(), "At least one policy must be supplied");
5554
this.policies = policies;
5655
}
5756

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.jodah.failsafe.functional;
2+
3+
import net.jodah.failsafe.Failsafe;
4+
import net.jodah.failsafe.FailsafeExecutor;
5+
import org.testng.annotations.Test;
6+
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
import static net.jodah.failsafe.Testing.testSyncAndAsyncFailure;
10+
import static net.jodah.failsafe.Testing.testSyncAndAsyncSuccess;
11+
import static org.testng.Assert.assertEquals;
12+
13+
@Test
14+
public class NoPolicyTest {
15+
public void testWithNoPolicy() {
16+
AtomicInteger successCounter = new AtomicInteger();
17+
AtomicInteger failureCounter = new AtomicInteger();
18+
FailsafeExecutor<Object> executor = Failsafe.none().onFailure(e -> {
19+
failureCounter.incrementAndGet();
20+
}).onSuccess(e -> {
21+
successCounter.incrementAndGet();
22+
});
23+
24+
// Test success
25+
testSyncAndAsyncSuccess(executor, () -> {
26+
successCounter.set(0);
27+
failureCounter.set(0);
28+
}, () -> "success", e -> {
29+
assertEquals(e.getAttemptCount(), 1);
30+
assertEquals(e.getExecutionCount(), 1);
31+
assertEquals(successCounter.get(), 1);
32+
assertEquals(failureCounter.get(), 0);
33+
}, "success");
34+
35+
// Test failure
36+
testSyncAndAsyncFailure(executor, () -> {
37+
successCounter.set(0);
38+
failureCounter.set(0);
39+
}, () -> {
40+
throw new IllegalStateException();
41+
}, e -> {
42+
assertEquals(e.getAttemptCount(), 1);
43+
assertEquals(e.getExecutionCount(), 1);
44+
assertEquals(successCounter.get(), 0);
45+
assertEquals(failureCounter.get(), 1);
46+
}, IllegalStateException.class);
47+
}
48+
}

0 commit comments

Comments
 (0)