Skip to content

Commit 61e82fe

Browse files
committed
Add more testing utils
1 parent 273188d commit 61e82fe

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

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

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.LinkedList;
2727
import java.util.List;
28-
import java.util.concurrent.CompletableFuture;
29-
import java.util.concurrent.ExecutionException;
30-
import java.util.concurrent.ScheduledExecutorService;
31-
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.*;
3229
import java.util.concurrent.atomic.AtomicReference;
3330
import java.util.function.Consumer;
3431

@@ -41,6 +38,13 @@ public class Testing {
4138
public static class ConnectException extends RuntimeException {
4239
}
4340

41+
public static class SyncExecutor implements Executor {
42+
@Override
43+
public void execute(Runnable command) {
44+
command.run();
45+
}
46+
}
47+
4448
public static class Stats {
4549
// Common
4650
public volatile int failureCount;
@@ -285,18 +289,48 @@ public static CircuitBreakerInternals getInternals(CircuitBreaker circuitBreaker
285289
}
286290
}
287291

292+
public static <T> void testAsyncSuccess(FailsafeExecutor<T> failsafe, CheckedRunnable when,
293+
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
294+
CheckedSupplier<T> supplier = () -> {
295+
when.run();
296+
return null;
297+
};
298+
testSyncAndAsyncInternal(false, failsafe, null, supplier, then, expectedResult);
299+
}
300+
301+
public static <T> void testAsyncSuccess(FailsafeExecutor<T> failsafe, CheckedSupplier<T> when,
302+
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
303+
testSyncAndAsyncInternal(false, failsafe, null, when, then, expectedResult);
304+
}
305+
306+
@SafeVarargs
307+
public static <T> void testAsyncFailure(FailsafeExecutor<T> failsafe, CheckedRunnable when,
308+
Consumer<ExecutionCompletedEvent<T>> then, Class<? extends Throwable>... expectedExceptions) {
309+
CheckedSupplier<T> supplier = () -> {
310+
when.run();
311+
return null;
312+
};
313+
testSyncAndAsyncInternal(false, failsafe, null, supplier, then, null, expectedExceptions);
314+
}
315+
316+
@SafeVarargs
317+
public static <T> void testAsyncFailure(FailsafeExecutor<T> failsafe, CheckedSupplier<T> when,
318+
Consumer<ExecutionCompletedEvent<T>> then, Class<? extends Throwable>... expectedExceptions) {
319+
testSyncAndAsyncInternal(false, failsafe, null, when, then, null, expectedExceptions);
320+
}
321+
288322
public static <T> void testSyncAndAsyncSuccess(FailsafeExecutor<T> failsafe, Runnable given, CheckedRunnable when,
289323
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
290324
CheckedSupplier<T> supplier = () -> {
291325
when.run();
292326
return null;
293327
};
294-
testSyncAndAsyncInternal(failsafe, given, supplier, then, expectedResult);
328+
testSyncAndAsyncInternal(true, failsafe, given, supplier, then, expectedResult);
295329
}
296330

297331
public static <T> void testSyncAndAsyncSuccess(FailsafeExecutor<T> failsafe, Runnable given, CheckedSupplier<T> when,
298332
Consumer<ExecutionCompletedEvent<T>> then, T expectedResult) {
299-
testSyncAndAsyncInternal(failsafe, given, when, then, expectedResult);
333+
testSyncAndAsyncInternal(true, failsafe, given, when, then, expectedResult);
300334
}
301335

302336
@SafeVarargs
@@ -306,13 +340,13 @@ public static <T> void testSyncAndAsyncFailure(FailsafeExecutor<T> failsafe, Run
306340
when.run();
307341
return null;
308342
};
309-
testSyncAndAsyncInternal(failsafe, given, supplier, then, null, expectedExceptions);
343+
testSyncAndAsyncInternal(true, failsafe, given, supplier, then, null, expectedExceptions);
310344
}
311345

312346
@SafeVarargs
313347
public static <T> void testSyncAndAsyncFailure(FailsafeExecutor<T> failsafe, Runnable given, CheckedSupplier<T> when,
314348
Consumer<ExecutionCompletedEvent<T>> then, Class<? extends Throwable>... expectedExceptions) {
315-
testSyncAndAsyncInternal(failsafe, given, when, then, null, expectedExceptions);
349+
testSyncAndAsyncInternal(true, failsafe, given, when, then, null, expectedExceptions);
316350
}
317351

318352
/**
@@ -322,7 +356,7 @@ public static <T> void testSyncAndAsyncFailure(FailsafeExecutor<T> failsafe, Run
322356
* This method helps ensure behavior is identical between sync and async executions.
323357
*/
324358
@SafeVarargs
325-
private static <T> void testSyncAndAsyncInternal(FailsafeExecutor<T> failsafe, Runnable given,
359+
private static <T> void testSyncAndAsyncInternal(boolean testSync, FailsafeExecutor<T> failsafe, Runnable given,
326360
CheckedSupplier<T> when, Consumer<ExecutionCompletedEvent<T>> then, T expectedResult,
327361
Class<? extends Throwable>... expectedExceptions) {
328362
AtomicReference<ExecutionCompletedEvent<T>> completedEventRef = new AtomicReference<>();
@@ -340,18 +374,22 @@ private static <T> void testSyncAndAsyncInternal(FailsafeExecutor<T> failsafe, R
340374
};
341375

342376
// Sync test
343-
System.out.println("\nRunning sync test");
344-
given.run();
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);
350-
postTestFn.run();
377+
if (testSync) {
378+
System.out.println("\nRunning sync test");
379+
if (given != null)
380+
given.run();
381+
if (expectedExceptions.length == 0) {
382+
T result = Testing.unwrapExceptions(() -> failsafe.onComplete(setCompletedEventFn::accept).get(when));
383+
assertEquals(result, expectedResult);
384+
} else
385+
Asserts.assertThrows(() -> failsafe.onComplete(setCompletedEventFn::accept).get(when), expectedExceptions);
386+
postTestFn.run();
387+
}
351388

352389
// Async test
353390
System.out.println("\nRunning async test");
354-
given.run();
391+
if (given != null)
392+
given.run();
355393
if (expectedExceptions.length == 0) {
356394
T result = Testing.unwrapExceptions(() -> failsafe.onComplete(setCompletedEventFn::accept).getAsync(when).get());
357395
assertEquals(result, expectedResult);

0 commit comments

Comments
 (0)