Search in sources :

Example 1 with FailsafeCall

use of dev.failsafe.retrofit.FailsafeCall in project failsafe by jhalterman.

the class RetrofitTesting method test.

private <T> void test(FailsafeExecutor<Response<T>> failsafe, Call<T> when, Then<Response<T>> then, int expectedStatus, T expectedResult, Class<? extends Throwable>[] expectedExceptions) {
    AtomicReference<CompletableFuture<Response<T>>> futureRef = new AtomicReference<>();
    AtomicReference<ExecutionCompletedEvent<Response<T>>> completedEventRef = new AtomicReference<>();
    Waiter completionListenerWaiter = new Waiter();
    EventListener<ExecutionCompletedEvent<Response<T>>> setCompletedEventFn = e -> {
        completedEventRef.set(e);
        completionListenerWaiter.resume();
    };
    List<Class<? extends Throwable>> expected = new LinkedList<>();
    Class<? extends Throwable>[] expectedExInner = expectedExceptions == null ? new Class[] {} : expectedExceptions;
    Collections.addAll(expected, expectedExInner);
    failsafe.onComplete(setCompletedEventFn);
    Runnable postTestFn = () -> {
        ignoreExceptions(() -> completionListenerWaiter.await(5000));
        ExecutionCompletedEvent<Response<T>> completedEvent = completedEventRef.get();
        if (expectedExceptions == null) {
            assertEquals(completedEvent.getResult().code(), expectedStatus);
            assertNull(completedEvent.getException());
        } else {
            assertNull(completedEvent.getResult());
            assertMatches(completedEvent.getException(), expectedExceptions);
        }
        if (then != null)
            then.accept(futureRef.get(), completedEvent);
    };
    Consumer<Response<T>> assertResult = response -> {
        T result = unwrapExceptions(response::body);
        assertEquals(result, expectedResult);
        assertEquals(response.code(), expectedStatus);
    };
    // Run sync test and assert result
    System.out.println("\nRunning sync test");
    FailsafeCall<T> failsafeCall = FailsafeCall.with(failsafe).compose(when);
    if (expectedExceptions == null) {
        assertResult.accept(unwrapExceptions(failsafeCall::execute));
    } else {
        assertThrows(failsafeCall::execute, expectedExceptions);
    }
    postTestFn.run();
    if (expectedExInner.length > 0)
        expected.add(0, ExecutionException.class);
    // Run async test and assert result
    System.out.println("\nRunning async test");
    failsafeCall = failsafeCall.clone();
    CompletableFuture<Response<T>> future = failsafeCall.executeAsync();
    futureRef.set(future);
    if (expectedExInner.length == 0) {
        assertResult.accept(unwrapExceptions(future::get));
    } else {
        assertThrowsSup(future::get, expected);
    }
    postTestFn.run();
}
Also used : ExecutionCompletedEvent(dev.failsafe.event.ExecutionCompletedEvent) FailsafeExecutor(dev.failsafe.FailsafeExecutor) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) CompletableFuture(java.util.concurrent.CompletableFuture) Response(retrofit2.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventListener(dev.failsafe.event.EventListener) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) FailsafeCall(dev.failsafe.retrofit.FailsafeCall) Waiter(net.jodah.concurrentunit.Waiter) List(java.util.List) LinkedList(java.util.LinkedList) Call(retrofit2.Call) Collections(java.util.Collections) Testing(dev.failsafe.testing.Testing) AtomicReference(java.util.concurrent.atomic.AtomicReference) LinkedList(java.util.LinkedList) ExecutionCompletedEvent(dev.failsafe.event.ExecutionCompletedEvent) Response(retrofit2.Response) CompletableFuture(java.util.concurrent.CompletableFuture) Waiter(net.jodah.concurrentunit.Waiter) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

FailsafeExecutor (dev.failsafe.FailsafeExecutor)1 EventListener (dev.failsafe.event.EventListener)1 ExecutionCompletedEvent (dev.failsafe.event.ExecutionCompletedEvent)1 FailsafeCall (dev.failsafe.retrofit.FailsafeCall)1 Testing (dev.failsafe.testing.Testing)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1 Waiter (net.jodah.concurrentunit.Waiter)1 Assert.assertEquals (org.testng.Assert.assertEquals)1 Assert.assertNull (org.testng.Assert.assertNull)1 Call (retrofit2.Call)1 Response (retrofit2.Response)1