/*
 *******************************************************************************
 * Copyright (c) 2016-2018 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package org.eclipse.microprofile.fault.tolerance.tck;

import org.eclipse.microprofile.fault.tolerance.tck.circuitbreaker.clientserver.CircuitBreakerClassLevelClientWithRetry;
import org.eclipse.microprofile.fault.tolerance.tck.circuitbreaker.clientserver.CircuitBreakerClientWithRetry;
import org.eclipse.microprofile.fault.tolerance.tck.circuitbreaker.clientserver.CircuitBreakerClientWithRetryAsync;
import org.eclipse.microprofile.fault.tolerance.tck.config.ConfigAnnotationAsset;
import org.eclipse.microprofile.fault.tolerance.tck.util.DurationMatcher;
import org.eclipse.microprofile.fault.tolerance.tck.util.Exceptions;
import org.eclipse.microprofile.fault.tolerance.tck.util.Packages;
import org.eclipse.microprofile.fault.tolerance.tck.util.TCKConfig;
import org.eclipse.microprofile.fault.tolerance.tck.util.TestException;
import org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException;
import org.hamcrest.MatcherAssert;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.inject.Inject;
import java.sql.Connection;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.lessThan;
import static org.testng.Assert.fail;

/**
 * Test CircuitBreaker Thresholds and delays with Retries.
 *
 * @author <a href="mailto:[email protected]">Neil Young</a>
 * @author <a href="mailto:[email protected]">Andrew Rouse</a>
 */

public class CircuitBreakerRetryTest extends Arquillian {

    private @Inject CircuitBreakerClientWithRetry clientForCBWithRetry;
    private @Inject CircuitBreakerClassLevelClientWithRetry clientForClassLevelCBWithRetry;
    private @Inject CircuitBreakerClientWithRetryAsync clientForCBWithRetryAsync;

    @Deployment
    public static WebArchive deploy() {

        ConfigAnnotationAsset config = new ConfigAnnotationAsset()
                .autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceC")
                .autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceWithRetryOnCbOpen")
                .autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceWithRetryOnTimeout")
                .autoscaleMethod(CircuitBreakerClientWithRetry.class, "serviceWithRetryFailOnCbOpen")
                .autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceC")
                .autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceWithRetryOnCbOpen")
                .autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceWithRetryOnTimeout")
                .autoscaleMethod(CircuitBreakerClientWithRetryAsync.class, "serviceWithRetryFailOnCbOpen");

        JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "ftCircuitBreakerRetry.jar")
                        .addClasses(CircuitBreakerClientWithRetry.class,
                                    CircuitBreakerClassLevelClientWithRetry.class,
                                    CircuitBreakerClientWithRetryAsync.class)
                        .addPackage(Packages.UTILS)
                        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
                        .addAsManifestResource(config, "microprofile-config.properties")
                        .as(JavaArchive.class);

        WebArchive war = ShrinkWrap.create(WebArchive.class, "ftCircuitBreakerRetry.war")
                        .addAsLibrary(testJar);
        return war;
    }

    /**
     * A test to exercise Circuit Breaker thresholds with sufficient retries to open the
     * Circuit and result in a CircuitBreakerOpenException.
     */
    @Test
    public void testCircuitOpenWithMoreRetries() {
        int invokeCounter = 0;
        try {
            clientForCBWithRetry.serviceA();

            // serviceA should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
            Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
                            + invokeCounter);
        }
        catch (CircuitBreakerOpenException cboe) {
            // Expected on iteration 4
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
            if (invokeCounter < 4) {
                Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
            Assert.fail("serviceA should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMoreRetries on iteration "
                            + invokeCounter);
        }

        invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceA();
        Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
    }

    /**
     * A test to exercise Circuit Breaker thresholds with insufficient retries to open the
     * Circuit so that the Circuit remains closed and a RuntimeException is caught.
     */
    @Test
    public void testCircuitOpenWithFewRetries() {
        int invokeCounter = 0;
        try {
            clientForCBWithRetry.serviceB();

            // serviceB should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);
        }
        catch (CircuitBreakerOpenException cboe) {
            // Not Expected
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry or throw a RuntimeException (not a CBOE) in testCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);

        }
        catch (RuntimeException ex) {
            // Expected on iteration 3
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
            if (invokeCounter < 3) {
                Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry or throw a RuntimeException in testCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);
        }

        invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceB();
        Assert.assertEquals(invokeCounter, 3, "The number of executions should be 3");
    }

    /**
     * Analogous to testCircuitOpenWithMoreRetries with Class level @CircuitBreaker and @Retry annotations
     * that are inherited by serviceA
     */
    @Test
    public void testClassLevelCircuitOpenWithMoreRetries() {
        int invokeCounter = 0;
        try {
            clientForClassLevelCBWithRetry.serviceA();

            // serviceA should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
            Assert.fail("serviceA should retry in testClassLevelCircuitOpenWithMoreRetries on iteration "
                            + invokeCounter);
        }
        catch (CircuitBreakerOpenException cboe) {
            // Expected on iteration 4
            invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
            if (invokeCounter < 4) {
                Assert.fail("serviceA should retry in testClassLevelCircuitOpenWithMoreRetries on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
            Assert.fail("serviceA should retry or throw a CircuitBreakerOpenException in testClassLevelCircuitOpenWithMoreRetries on iteration "
                            + invokeCounter);
        }

        invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceA();
        Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
    }

    /**
     * Analogous to testCircuitOpenWithFewRetries with Class level @CircuitBreaker and @Retry annotations
     * that are overridden by serviceB.
     */
    @Test
    public void testClassLevelCircuitOpenWithFewRetries() {
        int invokeCounter = 0;
        try {
            clientForClassLevelCBWithRetry.serviceB();

            // serviceB should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry in testClassLevelCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);
        }
        catch (CircuitBreakerOpenException cboe) {
            // Not Expected
            invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry or throw a RuntimeException (not a CBOE) in testClassLevelCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);

        }
        catch (RuntimeException ex) {
            // Expected on iteration 3
            invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
            if (invokeCounter < 3) {
                Assert.fail("serviceB should retry in testClassLevelCircuitOpenWithFewRetries on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry or throw a RuntimeException in testClassLevelCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);
        }

        invokeCounter = clientForClassLevelCBWithRetry.getCounterForInvokingServiceB();
        Assert.assertEquals(invokeCounter, 3, "The number of executions should be 3");
    }

    /**
     * Analogous to testCircuitOpenWithMoreRetries but execution failures are caused by timeouts.
     */
    @Test
    public void testCircuitOpenWithMultiTimeouts() {
        int invokeCounter = 0;
        try {
            clientForCBWithRetry.serviceC();

            // serviceC should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
            Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
                                + invokeCounter);

        }
        catch (CircuitBreakerOpenException cboe) {
            // Expected on iteration 4
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
            if (invokeCounter < 4) {
                Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
            Assert.fail("serviceC should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMultiTimeouts on iteration "
                            + invokeCounter + ", caught exception: " + ex);
        }

        invokeCounter = clientForCBWithRetry.getCounterForInvokingServiceC();
        Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
    }

    /**
     * Test that we retry around an open circuit breaker
     * <p>
     * Test that when retries are configured with sufficient delay, a call to an
     * open circuit can retry until the circuit half-closes, allowing the call to
     * succeed.
     */
    @Test
    public void testRetriesSucceedWhenCircuitCloses() {
        // Open the circuit by submitting four failures
        for (int i = 0; i < 4; i++) {
            try {
                clientForCBWithRetry.serviceWithRetryOnCbOpen(true);
            }
            catch (TestException e) {
                // Expected
            }
        }

        // Circuit is now open for 1 second
        // Our next call should cause a CircuitBreakerOpenException which should then be retried
        // The retry delay is 100ms, maxRetries is 20 so we expect a successful call after around 1 second when the circuit closes
        long startTime = System.nanoTime();
        clientForCBWithRetry.serviceWithRetryOnCbOpen(false);
        long endTime = System.nanoTime();

        // Check that the call took the expected time
        // Allow a margin of 250ms since there's a retry delay of 100ms
        MatcherAssert.assertThat("Call was successful but did not take the expected time",
            Duration.ofNanos(endTime - startTime),
            DurationMatcher.closeTo(TCKConfig.getConfig().getTimeoutInDuration(1000),
                TCKConfig.getConfig().getTimeoutInDuration(250)));
    }

    /**
     * Test that we don't retry around an open circuit breaker if
     * CircuitBreakerOpenException is not included in the retryOn attribute of the
     * Retry annotation
     * <p>
     * This test calls a method which only retries on TimeoutException
     */
    public void testNoRetriesIfNotRetryOn() {
        // Open the circuit by submitting four failures
        for (int i = 0; i < 4; i++) {
            try {
                clientForCBWithRetry.serviceWithRetryOnTimeout(true);
            }
            catch (TestException e) {
                // Expected
            }
        }

        // Circuit is now open for 1 second
        // Our next call should cause a CircuitBreakerOpenException which should not be retried
        // We expect a response almost immediately
        long startTime = System.nanoTime();
        Exceptions.expectCbOpen(() -> clientForCBWithRetry.serviceWithRetryOnTimeout(false));
        long endTime = System.nanoTime();

        // Check that the call took less than 200ms (i.e. we didn't delay and retry)
        MatcherAssert.assertThat("Call was successful but did not take the expected time",
                                 Duration.ofNanos(endTime - startTime),
                                 lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
    }

    /**
     * Test that we don't retry around an open circuit breaker if
     * CircuitBreakerOpenException is included in the abortOn attribute of the
     * Retry annotation
     */
    public void testNoRetriesIfAbortOn() {
        // Open the circuit by submitting four failures
        for (int i = 0; i < 4; i++) {
            try {
                clientForCBWithRetry.serviceWithRetryFailOnCbOpen(true);
            }
            catch (TestException e) {
                // Expected
            }
        }

        // Circuit is now open for 1 second
        // Our next call should cause a CircuitBreakerOpenException which should not be retried
        // We expect a response almost immediately
        long startTime = System.nanoTime();
        Exceptions.expectCbOpen(() -> clientForCBWithRetry.serviceWithRetryFailOnCbOpen(false));
        long endTime = System.nanoTime();

        // Check that the call took less than 200ms (i.e. we didn't delay and retry)
        MatcherAssert.assertThat("Call was successful but did not take the expected time",
                                 Duration.ofNanos(endTime - startTime),
                                 lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
    }


    /**
     * A test to exercise Circuit Breaker thresholds with sufficient retries to open the
     * Circuit and result in a CircuitBreakerOpenException using an Asynchronous call.
     */
    @Test
    public void testCircuitOpenWithMoreRetriesAsync() {
        int invokeCounter = 0;
        Future<Connection> result = clientForCBWithRetryAsync.serviceA();
        try {
            result.get(TCKConfig.getConfig().getTimeoutInMillis(5000), TimeUnit.MILLISECONDS);

            // serviceA should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
            Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
                            + invokeCounter);
        }
        catch (ExecutionException executionException) {
            // Expected execution exception wrapping CircuitBreakerOpenException
            MatcherAssert.assertThat("Thrown exception is the wrong type",
                                     executionException.getCause(),
                                     instanceOf(CircuitBreakerOpenException.class));

            // Expected on iteration 4
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
            if (invokeCounter < 4) {
                Assert.fail("serviceA should retry in testCircuitOpenWithMoreRetries on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
            Assert.fail("serviceA should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMoreRetries on iteration "
                            + invokeCounter);
        }

        invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceA();
        Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
    }

    /**
     * A test to exercise Circuit Breaker thresholds with insufficient retries to open the
     * Circuit so that the Circuit remains closed and a RuntimeException is caught when
     * using an Asynchronous call.
     */
    @Test
    public void testCircuitOpenWithFewRetriesAsync() {
        int invokeCounter = 0;
        Future<Connection> result = clientForCBWithRetryAsync.serviceB();
        try {
            result.get(TCKConfig.getConfig().getTimeoutInMillis(5000), TimeUnit.MILLISECONDS);

            // serviceB should retry until the Retry maxRetries is reached. At that point a TestException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);
        }
        catch (ExecutionException ex) {
            // Expected execution exception wrapping TestException
            MatcherAssert.assertThat("Thrown exception is the wrong type",
                                     ex.getCause(),
                                     instanceOf(TestException.class));

            // Expected on iteration 3
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
            if (invokeCounter < 3) {
                Assert.fail("serviceB should retry in testCircuitOpenWithFewRetries on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
            Assert.fail("serviceB should retry or throw a RuntimeException in testCircuitOpenWithFewRetries on iteration "
                            + invokeCounter);
        }

        invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceB();
        Assert.assertEquals(invokeCounter, 3, "The number of executions should be 3");
    }

    /**
     * Analogous to testCircuitOpenWithMoreRetriesAsync but execution failures are caused by timeouts.
     */
    @Test
    public void testCircuitOpenWithMultiTimeoutsAsync() {
        int invokeCounter = 0;
        Future<Connection> result = clientForCBWithRetryAsync.serviceC();
        try {
            result.get(TCKConfig.getConfig().getTimeoutInMillis(10000), TimeUnit.MILLISECONDS); // Expected to finish after < 1 second

            // serviceC should retry until the CB threshold is reached. At that point a CircuitBreakerOpenException
            // should be thrown. Assert if this does not happen.
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
            Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
                                + invokeCounter);

        }
        catch (ExecutionException executionException) {
            // Expected execution exception wrapping CircuitBreakerOpenException
            MatcherAssert.assertThat("Thrown exception is the wrong type",
                                     executionException.getCause(),
                                     instanceOf(CircuitBreakerOpenException.class));

            // Expected on iteration 4
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
            if (invokeCounter < 4) {
                Assert.fail("serviceC should retry in testCircuitOpenWithMultiTimeouts on iteration "
                                + invokeCounter);
            }
        }
        catch (Exception ex) {
            // Not Expected
            invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
            Assert.fail("serviceC should retry or throw a CircuitBreakerOpenException in testCircuitOpenWithMultiTimeouts on iteration "
                            + invokeCounter + ", caught exception: " + ex);
        }

        invokeCounter = clientForCBWithRetryAsync.getCounterForInvokingServiceC();
        Assert.assertEquals(invokeCounter, 4, "The number of executions should be 4");
    }

    /**
     * Test that we retry around an open circuit breaker
     * <p>
     * Test that when retries are configured with sufficient delay, a call to an
     * open circuit can retry until the circuit half-closes, allowing the call to
     * succeed.
     */
    @Test
    public void testRetriesSucceedWhenCircuitClosesAsync() {
        // Open the circuit by submitting four failures
        for (int i = 0; i < 4; i++) {
            Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnCbOpen(true);
            Exceptions.expect(TestException.class, result);
        }

        // Circuit is now open for 1 second
        // Our next call should cause a CircuitBreakerOpenException which should then be retried
        // The retry delay is 100ms, maxRetries is 20 so we expect a successful call after around 1 second when the circuit closes
        long startTime = System.nanoTime();
        Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnCbOpen(false);
        try {
            result.get(TCKConfig.getConfig().getTimeoutInMillis(10000), TimeUnit.MILLISECONDS);
        }
        catch (TimeoutException e) {
            fail("Call to serviceWithRetryOnCbOpen did not succeed within 10 seconds");
        }
        catch (ExecutionException e) {
            fail("Call to serviceWithRetryOnCbOpen failed with exception", e);
        }
        catch (InterruptedException e) {
            fail("Call to serviceWithRetryOnCbOpen was interrupted", e);
        }
        long endTime = System.nanoTime();

        // Check that the call took the expected time
        // Allow a margin of 250ms since there's a retry delay of 100ms
        MatcherAssert.assertThat("Call was successful but did not take the expected time",
            Duration.ofNanos(endTime - startTime),
            DurationMatcher.closeTo(TCKConfig.getConfig().getTimeoutInDuration(1000),
                TCKConfig.getConfig().getTimeoutInDuration(250)));
    }

    /**
     * Test that we don't retry around an open circuit breaker if
     * CircuitBreakerOpenException is not included in the retryOn attribute of the
     * Retry annotation
     * <p>
     * This test calls a method which only retries on TimeoutException
     */
    @Test
    public void testNoRetriesIfNotRetryOnAsync() {
        // Open the circuit by submitting four failures
        for (int i = 0; i < 4; i++) {
            Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnTimeout(true);
            Exceptions.expect(TestException.class, result);
        }

        // Circuit is now open for 1 second
        // Our next call should cause a CircuitBreakerOpenException which should not be retried
        long startTime = System.nanoTime();
        Future<String> result = clientForCBWithRetryAsync.serviceWithRetryOnTimeout(false);
        Exceptions.expect(CircuitBreakerOpenException.class, result);
        long endTime = System.nanoTime();

        // Check that the call took less than 200ms (i.e. we didn't delay and retry)
        MatcherAssert.assertThat("Call was successful but did not take the expected time",
                                 Duration.ofNanos(endTime - startTime),
                                 lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
    }

    /**
     * Test that we don't retry around an open circuit breaker if
     * CircuitBreakerOpenException is included in the abortOn attribute of the
     * Retry annotation
     */
    @Test
    public void testNoRetriesIfAbortOnAsync() {
        // Open the circuit by submitting four failures
        for (int i = 0; i < 4; i++) {
            Future<String> result = clientForCBWithRetryAsync.serviceWithRetryFailOnCbOpen(true);
            Exceptions.expect(TestException.class, result);
        }

        // Circuit is now open for 1 second
        // Our next call should cause a CircuitBreakerOpenException which should not be retried
        long startTime = System.nanoTime();
        Future<String> result = clientForCBWithRetryAsync.serviceWithRetryFailOnCbOpen(false);
        Exceptions.expect(CircuitBreakerOpenException.class, result);
        long endTime = System.nanoTime();

        // Check that the call took less than 200ms (i.e. we didn't delay and retry)
        MatcherAssert.assertThat("Call was successful but did not take the expected time",
                                 Duration.ofNanos(endTime - startTime),
                                 lessThan(TCKConfig.getConfig().getTimeoutInDuration(200)));
    }


}