|
| 1 | +package io.temporal.internal.testservice; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import com.google.protobuf.Timestamp; |
| 6 | +import com.google.protobuf.util.Durations; |
| 7 | +import com.google.protobuf.util.Timestamps; |
| 8 | +import io.temporal.api.common.v1.RetryPolicy; |
| 9 | +import io.temporal.api.enums.v1.RetryState; |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.Optional; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class TestServiceRetryStateTest { |
| 15 | + |
| 16 | + private static final RetryPolicy RETRY_POLICY = |
| 17 | + RetryPolicy.newBuilder() |
| 18 | + .setInitialInterval(Durations.fromSeconds(10)) |
| 19 | + .setBackoffCoefficient(1.0) |
| 20 | + .build(); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void expirationOnWholeSecondTimesOut() { |
| 24 | + // A deadline with a zero nanos field must still be honored. |
| 25 | + Timestamp now = Timestamps.fromSeconds(1_000); |
| 26 | + Timestamp expiration = Timestamps.fromSeconds(1_005); |
| 27 | + TestServiceRetryState state = new TestServiceRetryState(RETRY_POLICY, expiration); |
| 28 | + |
| 29 | + TestServiceRetryState.BackoffInterval backoff = |
| 30 | + state.getBackoffIntervalInSeconds(Optional.empty(), now, Optional.empty()); |
| 31 | + |
| 32 | + assertEquals(RetryState.RETRY_STATE_TIMEOUT, backoff.getRetryState()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void expirationWithSubsecondTimesOut() { |
| 37 | + Timestamp now = Timestamps.fromMillis(1_000_500); |
| 38 | + Timestamp expiration = Timestamps.fromMillis(1_005_500); |
| 39 | + TestServiceRetryState state = new TestServiceRetryState(RETRY_POLICY, expiration); |
| 40 | + |
| 41 | + TestServiceRetryState.BackoffInterval backoff = |
| 42 | + state.getBackoffIntervalInSeconds(Optional.empty(), now, Optional.empty()); |
| 43 | + |
| 44 | + assertEquals(RetryState.RETRY_STATE_TIMEOUT, backoff.getRetryState()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void unsetExpirationNeverTimesOut() { |
| 49 | + Timestamp now = Timestamps.fromSeconds(1_000); |
| 50 | + TestServiceRetryState state = |
| 51 | + new TestServiceRetryState(RETRY_POLICY, Timestamp.getDefaultInstance()); |
| 52 | + |
| 53 | + TestServiceRetryState.BackoffInterval backoff = |
| 54 | + state.getBackoffIntervalInSeconds(Optional.empty(), now, Optional.empty()); |
| 55 | + |
| 56 | + assertEquals(RetryState.RETRY_STATE_IN_PROGRESS, backoff.getRetryState()); |
| 57 | + assertEquals(Duration.ofSeconds(10), backoff.getInterval()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void retryWithinExpirationProceeds() { |
| 62 | + Timestamp now = Timestamps.fromSeconds(1_000); |
| 63 | + Timestamp expiration = Timestamps.fromSeconds(1_060); |
| 64 | + TestServiceRetryState state = new TestServiceRetryState(RETRY_POLICY, expiration); |
| 65 | + |
| 66 | + TestServiceRetryState.BackoffInterval backoff = |
| 67 | + state.getBackoffIntervalInSeconds(Optional.empty(), now, Optional.empty()); |
| 68 | + |
| 69 | + assertEquals(RetryState.RETRY_STATE_IN_PROGRESS, backoff.getRetryState()); |
| 70 | + assertEquals(Duration.ofSeconds(10), backoff.getInterval()); |
| 71 | + } |
| 72 | +} |
0 commit comments