Skip to content

Commit b1ebfff

Browse files
committed
Improve test utils
1 parent 7319dc4 commit b1ebfff

File tree

4 files changed

+81
-42
lines changed

4 files changed

+81
-42
lines changed

src/test/java/net/jodah/failsafe/Testing.java

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -285,82 +285,121 @@ public static CircuitBreakerInternals getInternals(CircuitBreaker circuitBreaker
285285
}
286286
}
287287

288+
public static <T> void testSyncAndAsyncSuccess(FailsafeExecutor<T> failsafe, Runnable given, CheckedRunnable when,
289+
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
290+
CheckedSupplier<T> supplier = () -> {
291+
when.run();
292+
return null;
293+
};
294+
testSyncAndAsyncInternal(failsafe, given, supplier, then, expectedResult);
295+
}
296+
297+
public static <T> void testSyncAndAsyncSuccess(FailsafeExecutor<T> failsafe, Runnable given, CheckedSupplier<T> when,
298+
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
299+
testSyncAndAsyncInternal(failsafe, given, when, then, expectedResult);
300+
}
301+
302+
@SafeVarargs
303+
public static <T> void testSyncAndAsyncFailure(FailsafeExecutor<T> failsafe, Runnable given, CheckedRunnable when,
304+
Consumer<ExecutionCompletedEvent<T>> then, Class<? extends Throwable>... expectedExceptions) {
305+
CheckedSupplier<T> supplier = () -> {
306+
when.run();
307+
return null;
308+
};
309+
testSyncAndAsyncInternal(failsafe, given, supplier, then, null, expectedExceptions);
310+
}
311+
312+
@SafeVarargs
313+
public static <T> void testSyncAndAsyncFailure(FailsafeExecutor<T> failsafe, Runnable given, CheckedSupplier<T> when,
314+
Consumer<ExecutionCompletedEvent<T>> then, Class<? extends Throwable>... expectedExceptions) {
315+
testSyncAndAsyncInternal(failsafe, given, when, then, null, expectedExceptions);
316+
}
317+
288318
/**
289319
* Does a .run and .runAsync against the failsafe, performing pre-test setup and post-test assertion checks. {@code
290320
* expectedExceptions} are verified against thrown exceptions _and_ the ExecutionCompletedEvent's failure.
291321
* <p>
292322
* This method helps ensure behavior is identical between sync and async executions.
293323
*/
294324
@SafeVarargs
295-
public static void testSyncAndAsync(FailsafeExecutor<?> failsafe, Runnable given, CheckedRunnable when,
296-
Consumer<ExecutionCompletedEvent<?>> then, Class<? extends Throwable>... expectedExceptions) {
297-
AtomicReference<ExecutionCompletedEvent<?>> completedEventRef = new AtomicReference<>();
298-
CheckedConsumer<ExecutionCompletedEvent<?>> setCompletedEventFn = completedEventRef::set;
325+
private static <T> void testSyncAndAsyncInternal(FailsafeExecutor<T> failsafe, Runnable given,
326+
CheckedSupplier<T> when, Consumer<ExecutionCompletedEvent<T>> then, T expectedResult,
327+
Class<? extends Throwable>... expectedExceptions) {
328+
AtomicReference<ExecutionCompletedEvent<T>> completedEventRef = new AtomicReference<>();
329+
CheckedConsumer<ExecutionCompletedEvent<T>> setCompletedEventFn = completedEventRef::set;
299330
List<Class<? extends Throwable>> expected = new LinkedList<>();
300331
Collections.addAll(expected, expectedExceptions);
301332

302333
Runnable postTestFn = () -> {
334+
ExecutionCompletedEvent<T> completedEvent = completedEventRef.get();
303335
if (expectedExceptions.length > 0)
304-
Asserts.assertMatches(completedEventRef.get().getFailure(), Arrays.asList(expectedExceptions));
336+
Asserts.assertMatches(completedEvent.getFailure(), Arrays.asList(expectedExceptions));
337+
else
338+
assertEquals(completedEvent.getResult(), expectedResult);
305339
then.accept(completedEventRef.get());
306340
};
307341

308342
// Sync test
309343
System.out.println("\nRunning sync test");
310344
given.run();
311-
if (expectedExceptions.length == 0)
312-
Testing.unwrapRunnableExceptions(() -> failsafe.onComplete(setCompletedEventFn::accept).run(when));
313-
else
314-
Asserts.assertThrows(() -> failsafe.onComplete(setCompletedEventFn::accept).run(when), expectedExceptions);
345+
if (expectedExceptions.length == 0) {
346+
T result = Testing.unwrapExceptions(() -> failsafe.onComplete(setCompletedEventFn::accept).get(when));
347+
assertEquals(result, expectedResult);
348+
} else
349+
Asserts.assertThrows(() -> failsafe.onComplete(setCompletedEventFn::accept).get(when), expectedExceptions);
315350
postTestFn.run();
316351

317352
// Async test
318353
System.out.println("\nRunning async test");
319354
given.run();
320355
if (expectedExceptions.length == 0) {
321-
Testing.unwrapExceptions(() -> failsafe.onComplete(setCompletedEventFn::accept).runAsync(when).get());
356+
T result = Testing.unwrapExceptions(() -> failsafe.onComplete(setCompletedEventFn::accept).getAsync(when).get());
357+
assertEquals(result, expectedResult);
322358
} else {
323359
expected.add(0, ExecutionException.class);
324-
Asserts.assertThrows(() -> failsafe.onComplete(setCompletedEventFn::accept).runAsync(when).get(), expected);
360+
Asserts.assertThrows(() -> failsafe.onComplete(setCompletedEventFn::accept).getAsync(when).get(), expected);
325361
}
326362
postTestFn.run();
327363
}
328364

329-
@SafeVarargs
330-
public static void testAsyncExecution(FailsafeExecutor<?> failsafe, AsyncRunnable when,
331-
Consumer<ExecutionCompletedEvent<?>> then, Class<? extends Throwable>... expectedExceptions) {
365+
public static <T> void testAsyncExecutionSuccess(FailsafeExecutor<T> failsafe, AsyncRunnable when,
366+
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
332367
AsyncSupplier supplier = ex -> {
333368
when.run(ex);
334369
return null;
335370
};
336-
testAsyncExecution(failsafe, supplier, then, null, expectedExceptions);
371+
testAsyncExecutionInternal(failsafe, supplier, then, expectedResult);
372+
}
373+
374+
public static <T> void testAsyncExecutionSuccess(FailsafeExecutor<T> failsafe, AsyncSupplier<T> when,
375+
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
376+
testAsyncExecutionInternal(failsafe, when, then, expectedResult);
337377
}
338378

339379
@SafeVarargs
340-
public static void testAsyncExecution(FailsafeExecutor<?> failsafe, AsyncRunnable when,
341-
Consumer<ExecutionCompletedEvent<?>> then, Object expectedResult,
342-
Class<? extends Throwable>... expectedExceptions) {
380+
public static <T> void testAsyncExecutionFailure(FailsafeExecutor<T> failsafe, AsyncRunnable when,
381+
Consumer<ExecutionCompletedEvent<T>> then, Class<? extends Throwable>... expectedExceptions) {
343382
AsyncSupplier supplier = ex -> {
344383
when.run(ex);
345384
return null;
346385
};
347-
testAsyncExecution(failsafe, supplier, then, expectedResult, expectedExceptions);
386+
testAsyncExecutionInternal(failsafe, supplier, then, null, expectedExceptions);
348387
}
349388

350389
@SafeVarargs
351-
public static <T> void testAsyncExecution(FailsafeExecutor<T> failsafe, AsyncSupplier<T> when,
352-
Consumer<ExecutionCompletedEvent<?>> then, Class<? extends Throwable>... expectedExceptions) {
353-
testAsyncExecution(failsafe, when, then, null, expectedExceptions);
390+
public static <T> void testAsyncExecutionFailure(FailsafeExecutor<T> failsafe, AsyncSupplier<T> when,
391+
Consumer<ExecutionCompletedEvent<T>> then, Class<? extends Throwable>... expectedExceptions) {
392+
testAsyncExecutionInternal(failsafe, when, then, null, expectedExceptions);
354393
}
355394

356395
@SafeVarargs
357-
public static <T> void testAsyncExecution(FailsafeExecutor<T> failsafe, AsyncSupplier<T> when,
358-
Consumer<ExecutionCompletedEvent<?>> then, Object expectedResult,
359-
Class<? extends Throwable>... expectedExceptions) {
360-
AtomicReference<ExecutionCompletedEvent<?>> completedEventRef = new AtomicReference<>();
361-
CheckedConsumer<ExecutionCompletedEvent<?>> setCompletedEventFn = completedEventRef::set;
396+
private static <T> void testAsyncExecutionInternal(FailsafeExecutor<T> failsafe, AsyncSupplier<T> when,
397+
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult, Class<? extends Throwable>... expectedExceptions) {
398+
399+
AtomicReference<ExecutionCompletedEvent<T>> completedEventRef = new AtomicReference<>();
400+
CheckedConsumer<ExecutionCompletedEvent<T>> setCompletedEventFn = completedEventRef::set;
362401
Runnable postTestFn = () -> {
363-
ExecutionCompletedEvent<?> completedEvent = completedEventRef.get();
402+
ExecutionCompletedEvent<T> completedEvent = completedEventRef.get();
364403
if (expectedExceptions.length > 0)
365404
Asserts.assertMatches(completedEvent.getFailure(), Arrays.asList(expectedExceptions));
366405
else
@@ -371,7 +410,7 @@ public static <T> void testAsyncExecution(FailsafeExecutor<T> failsafe, AsyncSup
371410
// Async test
372411
System.out.println("\nRunning async execution test");
373412
if (expectedExceptions.length == 0) {
374-
Object result = Testing.unwrapExceptions(
413+
T result = Testing.unwrapExceptions(
375414
() -> failsafe.onComplete(setCompletedEventFn::accept).getAsyncExecution(when).get());
376415
assertEquals(result, expectedResult);
377416
} else {

src/test/java/net/jodah/failsafe/functional/AlternativeResultTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void testRejectedSyncAndAsync() {
3434
RetryPolicy<Object> rp = withStats(new RetryPolicy<>().withMaxAttempts(7), rpStats, true);
3535
CircuitBreaker<Object> cb = withStats(new CircuitBreaker<>().withFailureThreshold(3), cbStats, true);
3636

37-
testSyncAndAsync(Failsafe.with(rp, cb), () -> {
37+
testSyncAndAsyncFailure(Failsafe.with(rp, cb), () -> {
3838
rpStats.reset();
3939
cbStats.reset();
4040
cb.close();
@@ -58,7 +58,7 @@ public void testRejectedAsyncExecutionWithRetry() {
5858
CircuitBreaker<Object> cb = withStats(new CircuitBreaker<>().withFailureThreshold(3), cbStats, true);
5959

6060
// Test with retryOn()
61-
testAsyncExecution(Failsafe.with(rp, cb), ex -> {
61+
testAsyncExecutionFailure(Failsafe.with(rp, cb), ex -> {
6262
runAsync(() -> {
6363
System.out.println("Executing");
6464
ex.retryOn(new IllegalStateException());
@@ -84,7 +84,7 @@ public void testRejectedAsyncExecutionWithCompleteAndRetry() {
8484
rpStats.reset();
8585
cbStats.reset();
8686
cb.close();
87-
testAsyncExecution(Failsafe.with(rp, cb), ex -> {
87+
testAsyncExecutionFailure(Failsafe.with(rp, cb), ex -> {
8888
runAsync(() -> {
8989
System.out.println("Executing");
9090
if (!ex.complete(null, new IllegalStateException())) {

src/test/java/net/jodah/failsafe/functional/AsyncExecutionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testAsyncExecWithPassthroughPolicies() {
3939
Timeout<Object> timeout = Timeout.of(Duration.ofMinutes(1));
4040
AtomicInteger counter = new AtomicInteger();
4141

42-
Consumer<FailsafeExecutor<Object>> test = failsafe -> testAsyncExecution(failsafe, ex -> {
42+
Consumer<FailsafeExecutor<Object>> test = failsafe -> testAsyncExecutionSuccess(failsafe, ex -> {
4343
runAsync(() -> {
4444
System.out.println("Executing");
4545
if (counter.getAndIncrement() < 3)

src/test/java/net/jodah/failsafe/functional/TimeoutTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.time.Duration;
2222
import java.util.concurrent.atomic.AtomicInteger;
2323

24-
import static net.jodah.failsafe.Testing.testSyncAndAsync;
24+
import static net.jodah.failsafe.Testing.testSyncAndAsyncFailure;
2525
import static org.testng.Assert.assertEquals;
2626

2727
/**
@@ -45,7 +45,7 @@ public void testTimeoutThenRetryWithBlockedSupplier() {
4545
System.out.println("Retrying");
4646
});
4747

48-
Runnable test = () -> testSyncAndAsync(Failsafe.with(retryPolicy, timeout), () -> {
48+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(retryPolicy, timeout), () -> {
4949
timeoutCounter.set(0);
5050
retryPolicyCounter.set(0);
5151
}, () -> {
@@ -85,7 +85,7 @@ public void testTimeoutThenRetryWithPendingRetry() {
8585
System.out.println("Retrying");
8686
});
8787

88-
Runnable test = () -> testSyncAndAsync(Failsafe.with(retryPolicy, timeout), () -> {
88+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(retryPolicy, timeout), () -> {
8989
executionCounter.set(0);
9090
timeoutSuccessCounter.set(0);
9191
}, () -> {
@@ -125,7 +125,7 @@ public void testRetryThenTimeoutWithBlockedSupplier() {
125125
System.out.println("Retrying");
126126
});
127127

128-
Runnable test = () -> testSyncAndAsync(Failsafe.with(timeout, retryPolicy), () -> {
128+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(timeout, retryPolicy), () -> {
129129
executionCounter.set(0);
130130
timeoutCounter.set(0);
131131
}, () -> {
@@ -170,7 +170,7 @@ public void testRetryThenTimeoutWithPendingRetry() {
170170
System.out.println("Retrying");
171171
});
172172

173-
Runnable test = () -> testSyncAndAsync(Failsafe.with(timeout, retryPolicy), () -> {
173+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(timeout, retryPolicy), () -> {
174174
executionCounter.set(0);
175175
timeoutCounter.set(0);
176176
failedAttemptCounter.set(0);
@@ -211,7 +211,7 @@ public void testTimeoutThenFallbackWithBlockedSupplier() {
211211
throw new IllegalStateException();
212212
});
213213

214-
Runnable test = () -> testSyncAndAsync(Failsafe.with(fallback, timeout), () -> {
214+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(fallback, timeout), () -> {
215215
timeoutCounter.set(0);
216216
fallbackCounter.set(0);
217217
}, () -> {
@@ -251,7 +251,7 @@ public void testTimeoutThenFallback() {
251251
throw new IllegalStateException();
252252
});
253253

254-
Runnable test = () -> testSyncAndAsync(Failsafe.with(fallback, timeout), () -> {
254+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(fallback, timeout), () -> {
255255
fallbackCounter.set(0);
256256
}, () -> {
257257
System.out.println("Executing");
@@ -288,7 +288,7 @@ public void testFallbackThenTimeoutWithBlockedSupplier() {
288288
throw new IllegalStateException();
289289
});
290290

291-
Runnable test = () -> testSyncAndAsync(Failsafe.with(timeout, fallback), () -> {
291+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(timeout, fallback), () -> {
292292
timeoutCounter.set(0);
293293
fallbackCounter.set(0);
294294
}, () -> {
@@ -328,7 +328,7 @@ public void testFallbackThenTimeoutWithBlockedFallback() {
328328
throw new IllegalStateException();
329329
});
330330

331-
Runnable test = () -> testSyncAndAsync(Failsafe.with(timeout, fallback), () -> {
331+
Runnable test = () -> testSyncAndAsyncFailure(Failsafe.with(timeout, fallback), () -> {
332332
timeoutCounter.set(0);
333333
fallbackCounter.set(0);
334334
}, () -> {

0 commit comments

Comments
 (0)