Skip to content

Commit

Permalink
Ensure exception Attempt has non-null Exception (#221)
Browse files Browse the repository at this point in the history
* Add argument check to ensure an Attempt resulting
  from an Exception does not allow a null Exception
* Add a complete test for Attempt
  • Loading branch information
sleberknight authored Jun 4, 2024
1 parent f359f85 commit 7585b5c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/kiwiproject/retry/Attempt.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kiwiproject.retry;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

/**
* An attempt of a call, which resulted either in a result returned by the call,
Expand All @@ -24,7 +25,12 @@ private Attempt(T result, int attemptNumber, long delaySinceFirstAttempt) {
}

private Attempt(Exception exception, int attemptNumber, long delaySinceFirstAttempt) {
this(null, exception, attemptNumber, delaySinceFirstAttempt);
this(
null,
requireNonNull(exception, "exception must not be null"),
attemptNumber,
delaySinceFirstAttempt
);
}

private Attempt(T result, Exception exception, int attemptNumber, long delaySinceFirstAttempt) {
Expand All @@ -37,7 +43,7 @@ private Attempt(T result, Exception exception, int attemptNumber, long delaySinc
/**
* Create a new {@link Attempt} that has a result.
*
* @param result the result of the attempt
* @param result the result of the attempt, may be null
* @param attemptNumber the number of this attempt
* @param delaySinceFirstAttempt the delay in milliseconds since the first attempt was made
* @param <T> the type of result
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/org/kiwiproject/retry/AttemptTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.kiwiproject.retry;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.junit.jupiter.api.Assertions.assertAll;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;

@DisplayName("Attempt")
class AttemptTest {

private ThreadLocalRandom rand;
int attemptNumber;
long delaySinceFirstAttempt;

@BeforeEach
void setUp() {
rand = ThreadLocalRandom.current();
attemptNumber = rand.nextInt(1, 5);
delaySinceFirstAttempt = rand.nextLong(1_000, 5_000);
}

@Test
void shouldCreateWithResult() {
var result = rand.nextInt(0, 101);
var attempt = Attempt.newResultAttempt(result, attemptNumber, delaySinceFirstAttempt);

assertAll(
() -> assertThat(attempt.hasResult()).isTrue(),
() -> assertThat(attempt.hasException()).isFalse(),
() -> assertThat(attempt.getResult()).isEqualTo(result),
() -> assertThatIllegalStateException().isThrownBy(() -> attempt.getException()),
() -> assertThat(attempt.getAttemptNumber()).isEqualTo(attemptNumber),
() -> assertThat(attempt.getDelaySinceFirstAttempt()).isEqualTo(delaySinceFirstAttempt)
);
}

@Test
void shouldAllowNullResult() {
var attempt = Attempt.<Integer>newResultAttempt(null, attemptNumber, delaySinceFirstAttempt);

assertAll(
() -> assertThat(attempt.hasResult()).isTrue(),
() -> assertThat(attempt.hasException()).isFalse(),
() -> assertThat(attempt.getResult()).isNull(),
() -> assertThatIllegalStateException().isThrownBy(() -> attempt.getException()),
() -> assertThat(attempt.getAttemptNumber()).isEqualTo(attemptNumber),
() -> assertThat(attempt.getDelaySinceFirstAttempt()).isEqualTo(delaySinceFirstAttempt)
);
}

@Test
void shouldCreateWithException() {
var exception = new IOException("I/O error - device not ready");
var attempt = Attempt.newExceptionAttempt(exception, attemptNumber, delaySinceFirstAttempt);

assertAll(
() -> assertThat(attempt.hasResult()).isFalse(),
() -> assertThat(attempt.hasException()).isTrue(),
() -> assertThatIllegalStateException().isThrownBy(() -> attempt.getResult()),
() -> assertThat(attempt.getException()).isSameAs(exception),
() -> assertThat(attempt.getAttemptNumber()).isEqualTo(attemptNumber),
() -> assertThat(attempt.getDelaySinceFirstAttempt()).isEqualTo(delaySinceFirstAttempt)
);
}

@Test
void shouldNotPermitExceptionAttempt_WithNullException() {
assertThatNullPointerException()
.isThrownBy(() -> Attempt.newExceptionAttempt(null, attemptNumber, delaySinceFirstAttempt))
.withMessage("exception must not be null");
}
}

0 comments on commit 7585b5c

Please sign in to comment.