diff --git a/README.md b/README.md index de90058..924325b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ To install, you can simply include the dependency from Maven Central: com.redfin insist - 1.1.0 + 1.2.0 ``` diff --git a/pom.xml b/pom.xml index c3aeb57..db37fff 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ com.redfin insist - 1.1.0 + 1.2.0 4.0.0 jar @@ -81,7 +81,7 @@ com.redfin patience - 2.1.0 + 2.2.0 org.opentest4j diff --git a/src/main/java/com/redfin/insist/InsistVerifiableFactory.java b/src/main/java/com/redfin/insist/InsistVerifiableFactory.java index ce37a58..f6803f5 100644 --- a/src/main/java/com/redfin/insist/InsistVerifiableFactory.java +++ b/src/main/java/com/redfin/insist/InsistVerifiableFactory.java @@ -16,10 +16,13 @@ package com.redfin.insist; +import com.redfin.patience.PatientExecutionHandlers; +import com.redfin.patience.PatientRetryStrategies; import com.redfin.patience.PatientWait; import com.redfin.validity.AbstractVerifiableFactory; import com.redfin.validity.FailedValidationExecutor; +import java.time.Duration; import java.util.Objects; import java.util.function.BiFunction; @@ -35,6 +38,17 @@ public final class InsistVerifiableFactory extends AbstractVerifiableFactory> { + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Constants + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + private static final PatientWait DEFAULT_WAIT = PatientWait.builder() + .withInitialDelay(Duration.ZERO) + .withDefaultTimeout(Duration.ZERO) + .withExecutionHandler(PatientExecutionHandlers.ignoringAll()) + .withRetryStrategy(PatientRetryStrategies.withFixedDelay(Duration.ofMillis(500))) + .build(); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Instance Fields & Methods // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -81,6 +95,25 @@ public InsistCompletableFuture withWait(PatientWait wait) { return new InsistCompletableFutureImpl<>(throwableFunction, getMessage(), wait); } + /** + * Like calling {@link #withWait(PatientWait)} with a wait object that retries + * repeatedly up to the set tryingFor maximum with a short delay between + * attempts. Any throwable thrown during the execution will be ignored. + * + * @param tryingFor the {@link Duration} object to be used as the maximum + * time to wait. + * May not be null or negative. + * + * @return an InsistCompletableFuture instance initialized with the default wait object + * and the given timeout. + * + * @throws IllegalArgumentException if tryingFor is null or negative. + */ + public InsistFuture within(Duration tryingFor) { + validate().that(tryingFor).isAtLeast(Duration.ZERO); + return withWait(DEFAULT_WAIT).within(tryingFor); + } + /** * Call the executable, If the executable throws a throwable of type T, * then exit normally. If a different type of throwable is thrown or no diff --git a/src/test/java/com/redfin/insist/InsistVerifiableFactoryTest.java b/src/test/java/com/redfin/insist/InsistVerifiableFactoryTest.java index 9ffdbd3..bf925b4 100644 --- a/src/test/java/com/redfin/insist/InsistVerifiableFactoryTest.java +++ b/src/test/java/com/redfin/insist/InsistVerifiableFactoryTest.java @@ -22,6 +22,8 @@ import org.junit.jupiter.api.Test; import org.opentest4j.AssertionFailedError; +import java.time.Duration; + final class InsistVerifiableFactoryTest { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -100,6 +102,26 @@ void testWithWaitThrowsExceptionForNullWait() { "InsistVerifiableFactory withWait should throw exception for null wait."); } + @Test + void testWithinReturnsANonNullInstance() { + Assertions.assertNotNull(getInstance("message").within(Duration.ofMillis(100)), + "InsistVerifiableFactory within should return a non-null instance."); + } + + @Test + void testWithinThrowsExceptionForNullTryingFor() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> getInstance("message").within(null), + "InsistVerifiableFactory within should throw exception for null tryingFor."); + } + + @Test + void testWithinThrowsExceptionForNegativeTryingFor() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> getInstance("message").within(Duration.ofMillis(100).negated()), + "InsistVerifiableFactory within should throw exception for negative tryingFor."); + } + @Test void testThatThrowsExceptionForNullExpectedClass() { Assertions.assertThrows(IllegalArgumentException.class,