Search in sources :

Example 1 with FailsafeCall

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

the class OkHttpTesting method test.

private <T> void test(FailsafeExecutor<Response> failsafe, Call when, Then<Response> then, int expectedStatus, T expectedResult, Class<? extends Throwable>[] expectedExceptions) {
    AtomicReference<CompletableFuture<Response>> futureRef = new AtomicReference<>();
    AtomicReference<ExecutionCompletedEvent<Response>> completedEventRef = new AtomicReference<>();
    Waiter completionListenerWaiter = new Waiter();
    EventListener<ExecutionCompletedEvent<Response>> 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> 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> assertResult = response -> {
        String result = unwrapExceptions(() -> response.body().string());
        assertEquals(result, expectedResult);
        assertEquals(response.code(), expectedStatus);
    };
    // Run sync test and assert result
    System.out.println("\nRunning sync test");
    FailsafeCall 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> future = failsafeCall.executeAsync();
    futureRef.set(future);
    if (expectedExInner.length == 0) {
        assertResult.accept(unwrapExceptions(future::get));
    } else {
        assertThrowsSup(future::get, expected);
    }
    postTestFn.run();
}
Also used : FailsafeCall(dev.failsafe.okhttp.FailsafeCall) ExecutionCompletedEvent(dev.failsafe.event.ExecutionCompletedEvent) Request(okhttp3.Request) FailsafeExecutor(dev.failsafe.FailsafeExecutor) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) EventListener(dev.failsafe.event.EventListener) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Waiter(net.jodah.concurrentunit.Waiter) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) Response(okhttp3.Response) Call(okhttp3.Call) LinkedList(java.util.LinkedList) 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(okhttp3.Response) CompletableFuture(java.util.concurrent.CompletableFuture) FailsafeCall(dev.failsafe.okhttp.FailsafeCall) 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.okhttp.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 Function (java.util.function.Function)1 Waiter (net.jodah.concurrentunit.Waiter)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 Assert.assertEquals (org.testng.Assert.assertEquals)1 Assert.assertNull (org.testng.Assert.assertNull)1