JAMES-2545 Move RetryExecutorUtil in the utility project
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/f02def1d Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/f02def1d Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/f02def1d Branch: refs/heads/master Commit: f02def1d12aa44001ee70bb6281cf585bcdfa974 Parents: 3b570bc Author: Antoine Duprat <[email protected]> Authored: Wed Sep 12 14:39:45 2018 +0200 Committer: Benoit Tellier <[email protected]> Committed: Fri Sep 14 10:17:42 2018 +0700 ---------------------------------------------------------------------- server/container/guice/cassandra-guice/pom.xml | 4 + .../mailbox/ElasticSearchMailboxModule.java | 2 +- .../mailbox/ResilientClusterProvider.java | 2 +- server/container/guice/guice-common/pom.xml | 4 - .../apache/james/utils/RetryExecutorUtil.java | 44 ------- .../org/apache/james/utils/FaultyService.java | 24 ---- .../james/utils/RetryExecutorUtilTest.java | 127 ------------------- server/container/util/pom.xml | 9 ++ .../apache/james/util/retry/FaultyService.java | 24 ++++ .../james/util/retry/RetryExecutorUtil.java | 44 +++++++ .../james/util/retry/RetryExecutorUtilTest.java | 127 +++++++++++++++++++ 11 files changed, 210 insertions(+), 201 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/guice/cassandra-guice/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/pom.xml b/server/container/guice/cassandra-guice/pom.xml index 896c2f0..d13b33f 100644 --- a/server/container/guice/cassandra-guice/pom.xml +++ b/server/container/guice/cassandra-guice/pom.xml @@ -215,6 +215,10 @@ <dependency> <groupId>${james.groupId}</groupId> <artifactId>james-server-util</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>james-server-util</artifactId> <type>test-jar</type> <scope>test</scope> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java index 5bfd220..ee6529d 100644 --- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java +++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java @@ -44,8 +44,8 @@ import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; import org.apache.james.mailbox.store.search.MessageSearchIndex; import org.apache.james.quota.search.elasticsearch.QuotaSearchIndexCreationUtil; +import org.apache.james.util.retry.RetryExecutorUtil; import org.apache.james.utils.PropertiesProvider; -import org.apache.james.utils.RetryExecutorUtil; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.NoNodeAvailableException; import org.slf4j.Logger; http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java index fa9a81e..ef2e4a8 100644 --- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java +++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java @@ -30,7 +30,7 @@ import javax.inject.Singleton; import org.apache.james.backends.cassandra.init.ClusterBuilder; import org.apache.james.backends.cassandra.init.ClusterWithKeyspaceCreatedFactory; import org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration; -import org.apache.james.utils.RetryExecutorUtil; +import org.apache.james.util.retry.RetryExecutorUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/guice/guice-common/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/pom.xml b/server/container/guice/guice-common/pom.xml index d2333e7..8e64644 100644 --- a/server/container/guice/guice-common/pom.xml +++ b/server/container/guice/guice-common/pom.xml @@ -171,10 +171,6 @@ <artifactId>guice-multibindings</artifactId> </dependency> <dependency> - <groupId>com.nurkiewicz.asyncretry</groupId> - <artifactId>asyncretry</artifactId> - </dependency> - <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/guice/guice-common/src/main/java/org/apache/james/utils/RetryExecutorUtil.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/RetryExecutorUtil.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/RetryExecutorUtil.java deleted file mode 100644 index b4b218f..0000000 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/RetryExecutorUtil.java +++ /dev/null @@ -1,44 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you 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.apache.james.utils; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.nurkiewicz.asyncretry.AsyncRetryExecutor; - -public class RetryExecutorUtil { - private static final Logger LOG = LoggerFactory.getLogger(RetryExecutorUtil.class); - - private static final int INITIAL_DELAY_MILLIS = 500; - private static final int MULTIPLIER = 2; - - @SafeVarargs - public static AsyncRetryExecutor retryOnExceptions(AsyncRetryExecutor executor, int maxRetries, int minDelay, Class<? extends Throwable>... clazz) { - LOG.info("The action should retry when {} and retry to {} times if needed", clazz, maxRetries); - return executor - .withExponentialBackoff(INITIAL_DELAY_MILLIS, MULTIPLIER) - .withProportionalJitter() - .retryOn(clazz) - .withMaxRetries(maxRetries) - .withMinDelay(minDelay); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FaultyService.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FaultyService.java b/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FaultyService.java deleted file mode 100644 index 12d8f92..0000000 --- a/server/container/guice/guice-common/src/test/java/org/apache/james/utils/FaultyService.java +++ /dev/null @@ -1,24 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you 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.apache.james.utils; - -public interface FaultyService { - String faultyService(); -} http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/guice/guice-common/src/test/java/org/apache/james/utils/RetryExecutorUtilTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/utils/RetryExecutorUtilTest.java b/server/container/guice/guice-common/src/test/java/org/apache/james/utils/RetryExecutorUtilTest.java deleted file mode 100644 index dac929e..0000000 --- a/server/container/guice/guice-common/src/test/java/org/apache/james/utils/RetryExecutorUtilTest.java +++ /dev/null @@ -1,127 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you 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.apache.james.utils; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.BDDMockito.given; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import com.nurkiewicz.asyncretry.AsyncRetryExecutor; -import com.nurkiewicz.asyncretry.RetryExecutor; - -public class RetryExecutorUtilTest { - private static final int MAX_RETRIES = 3; - private static final int MIN_DELAY = 100; - @Mock - protected FaultyService serviceMock; - - private RetryExecutor retryExecutor; - private ScheduledExecutorService scheduledExecutor; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - scheduledExecutor = Executors.newSingleThreadScheduledExecutor(); - } - - @After - public void tearDown() throws Exception { - scheduledExecutor.shutdownNow(); - } - - @Test - public void retryOnExceptionsAndExecuteShouldRethrowWhenScheduledServiceAlwaysThrowException() throws Exception { - given(serviceMock.faultyService()) - .willThrow(IllegalArgumentException.class) - .willThrow(IllegalArgumentException.class) - .willThrow(IllegalArgumentException.class) - .willThrow(IllegalArgumentException.class); - - retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalArgumentException.class); - - assertThatThrownBy(() -> retryExecutor.getWithRetry(serviceMock::faultyService).get()) - .isInstanceOf(ExecutionException.class) - .hasCauseInstanceOf(IllegalArgumentException.class); - } - - @Test - public void retryOnExceptionsAndExecuteShouldRetryWhenMatchExceptionAndSuccess() throws Exception { - given(serviceMock.faultyService()) - .willThrow(IllegalArgumentException.class) - .willReturn("Foo"); - retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalArgumentException.class); - - final CompletableFuture<String> future = retryExecutor.getWithRetry(serviceMock::faultyService); - - assertThat(future.get()).isEqualTo("Foo"); - } - - @Test - public void retryOnExceptionsAndExecuteShouldNotRetryWhenDoesNotMatchException() throws Exception { - given(serviceMock.faultyService()) - .willThrow(IllegalStateException.class) - .willReturn("Foo"); - - retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalArgumentException.class); - - assertThatThrownBy(() -> retryExecutor.getWithRetry(serviceMock::faultyService).get()) - .isInstanceOf(ExecutionException.class) - .hasCauseInstanceOf(IllegalStateException.class); - } - - @Test - @SuppressWarnings("unchecked") - public void retryOnExceptionsAndExecuteShouldRetryWithMaxTimesAndReturnValue() throws Exception { - given(serviceMock.faultyService()) - .willThrow(IllegalStateException.class, IllegalStateException.class, IllegalStateException.class) - .willReturn("Foo"); - - retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalStateException.class); - - CompletableFuture<String> future = retryExecutor.getWithRetry(serviceMock::faultyService); - - assertThat(future.get()).isEqualTo("Foo"); - } - - @Test - @SuppressWarnings("unchecked") - public void retryOnExceptionsAndExecuteShouldFailIfFailMoreThanMaxRetry() throws Exception { - given(serviceMock.faultyService()) - .willThrow(IllegalStateException.class, IllegalStateException.class, IllegalStateException.class, IllegalStateException.class) - .willReturn("Foo"); - - retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalStateException.class); - - assertThatThrownBy(() -> retryExecutor.getWithRetry(serviceMock::faultyService).get()) - .isInstanceOf(ExecutionException.class) - .hasCauseInstanceOf(IllegalStateException.class); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/util/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/util/pom.xml b/server/container/util/pom.xml index 8e367d1..939a670 100644 --- a/server/container/util/pom.xml +++ b/server/container/util/pom.xml @@ -59,6 +59,10 @@ <artifactId>icu4j</artifactId> </dependency> <dependency> + <groupId>com.nurkiewicz.asyncretry</groupId> + <artifactId>asyncretry</artifactId> + </dependency> + <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> </dependency> @@ -100,6 +104,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/util/src/main/java/org/apache/james/util/retry/FaultyService.java ---------------------------------------------------------------------- diff --git a/server/container/util/src/main/java/org/apache/james/util/retry/FaultyService.java b/server/container/util/src/main/java/org/apache/james/util/retry/FaultyService.java new file mode 100644 index 0000000..f8c85e6 --- /dev/null +++ b/server/container/util/src/main/java/org/apache/james/util/retry/FaultyService.java @@ -0,0 +1,24 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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.apache.james.util.retry; + +public interface FaultyService { + String faultyService(); +} http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/util/src/main/java/org/apache/james/util/retry/RetryExecutorUtil.java ---------------------------------------------------------------------- diff --git a/server/container/util/src/main/java/org/apache/james/util/retry/RetryExecutorUtil.java b/server/container/util/src/main/java/org/apache/james/util/retry/RetryExecutorUtil.java new file mode 100644 index 0000000..34bdcf9 --- /dev/null +++ b/server/container/util/src/main/java/org/apache/james/util/retry/RetryExecutorUtil.java @@ -0,0 +1,44 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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.apache.james.util.retry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.nurkiewicz.asyncretry.AsyncRetryExecutor; + +public class RetryExecutorUtil { + private static final Logger LOG = LoggerFactory.getLogger(RetryExecutorUtil.class); + + private static final int INITIAL_DELAY_MILLIS = 500; + private static final int MULTIPLIER = 2; + + @SafeVarargs + public static AsyncRetryExecutor retryOnExceptions(AsyncRetryExecutor executor, int maxRetries, int minDelay, Class<? extends Throwable>... clazz) { + LOG.info("The action should retry when {} and retry to {} times if needed", clazz, maxRetries); + return executor + .withExponentialBackoff(INITIAL_DELAY_MILLIS, MULTIPLIER) + .withProportionalJitter() + .retryOn(clazz) + .withMaxRetries(maxRetries) + .withMinDelay(minDelay); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/f02def1d/server/container/util/src/test/java/org/apache/james/util/retry/RetryExecutorUtilTest.java ---------------------------------------------------------------------- diff --git a/server/container/util/src/test/java/org/apache/james/util/retry/RetryExecutorUtilTest.java b/server/container/util/src/test/java/org/apache/james/util/retry/RetryExecutorUtilTest.java new file mode 100644 index 0000000..81c2f11 --- /dev/null +++ b/server/container/util/src/test/java/org/apache/james/util/retry/RetryExecutorUtilTest.java @@ -0,0 +1,127 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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.apache.james.util.retry; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.BDDMockito.given; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.nurkiewicz.asyncretry.AsyncRetryExecutor; +import com.nurkiewicz.asyncretry.RetryExecutor; + +public class RetryExecutorUtilTest { + private static final int MAX_RETRIES = 3; + private static final int MIN_DELAY = 100; + @Mock + protected FaultyService serviceMock; + + private RetryExecutor retryExecutor; + private ScheduledExecutorService scheduledExecutor; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + scheduledExecutor = Executors.newSingleThreadScheduledExecutor(); + } + + @After + public void tearDown() throws Exception { + scheduledExecutor.shutdownNow(); + } + + @Test + public void retryOnExceptionsAndExecuteShouldRethrowWhenScheduledServiceAlwaysThrowException() throws Exception { + given(serviceMock.faultyService()) + .willThrow(IllegalArgumentException.class) + .willThrow(IllegalArgumentException.class) + .willThrow(IllegalArgumentException.class) + .willThrow(IllegalArgumentException.class); + + retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalArgumentException.class); + + assertThatThrownBy(() -> retryExecutor.getWithRetry(serviceMock::faultyService).get()) + .isInstanceOf(ExecutionException.class) + .hasCauseInstanceOf(IllegalArgumentException.class); + } + + @Test + public void retryOnExceptionsAndExecuteShouldRetryWhenMatchExceptionAndSuccess() throws Exception { + given(serviceMock.faultyService()) + .willThrow(IllegalArgumentException.class) + .willReturn("Foo"); + retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalArgumentException.class); + + final CompletableFuture<String> future = retryExecutor.getWithRetry(serviceMock::faultyService); + + assertThat(future.get()).isEqualTo("Foo"); + } + + @Test + public void retryOnExceptionsAndExecuteShouldNotRetryWhenDoesNotMatchException() throws Exception { + given(serviceMock.faultyService()) + .willThrow(IllegalStateException.class) + .willReturn("Foo"); + + retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalArgumentException.class); + + assertThatThrownBy(() -> retryExecutor.getWithRetry(serviceMock::faultyService).get()) + .isInstanceOf(ExecutionException.class) + .hasCauseInstanceOf(IllegalStateException.class); + } + + @Test + @SuppressWarnings("unchecked") + public void retryOnExceptionsAndExecuteShouldRetryWithMaxTimesAndReturnValue() throws Exception { + given(serviceMock.faultyService()) + .willThrow(IllegalStateException.class, IllegalStateException.class, IllegalStateException.class) + .willReturn("Foo"); + + retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalStateException.class); + + CompletableFuture<String> future = retryExecutor.getWithRetry(serviceMock::faultyService); + + assertThat(future.get()).isEqualTo("Foo"); + } + + @Test + @SuppressWarnings("unchecked") + public void retryOnExceptionsAndExecuteShouldFailIfFailMoreThanMaxRetry() throws Exception { + given(serviceMock.faultyService()) + .willThrow(IllegalStateException.class, IllegalStateException.class, IllegalStateException.class, IllegalStateException.class) + .willReturn("Foo"); + + retryExecutor = RetryExecutorUtil.retryOnExceptions(new AsyncRetryExecutor(scheduledExecutor), MAX_RETRIES, MIN_DELAY, IllegalStateException.class); + + assertThatThrownBy(() -> retryExecutor.getWithRetry(serviceMock::faultyService).get()) + .isInstanceOf(ExecutionException.class) + .hasCauseInstanceOf(IllegalStateException.class); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
