@@ -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 ("\n Running 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 ("\n Running 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 ("\n Running 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 {
0 commit comments