Skip to content

Commit

Permalink
Adding in a helper method for future insist checking that takes in a …
Browse files Browse the repository at this point in the history
…simple duration and the wait is a default one
  • Loading branch information
littleclay committed Sep 18, 2017
1 parent 5920f94 commit a2772f5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To install, you can simply include the dependency from Maven Central:
<dependency>
<groupId>com.redfin</groupId>
<artifactId>insist</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>com.redfin</groupId>
<artifactId>insist</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

Expand Down Expand Up @@ -81,7 +81,7 @@
<dependency>
<groupId>com.redfin</groupId>
<artifactId>patience</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.opentest4j</groupId>
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/redfin/insist/InsistVerifiableFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,6 +38,17 @@
public final class InsistVerifiableFactory<X extends Throwable>
extends AbstractVerifiableFactory<X, InsistVerifiableFactory<X>> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -81,6 +95,25 @@ public InsistCompletableFuture<X> 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<X> 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
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/redfin/insist/InsistVerifiableFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import java.time.Duration;

final class InsistVerifiableFactoryTest {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a2772f5

Please sign in to comment.