DelaysAndMaxRetry { public static DelaysAndMaxRetry from(int intendedMaxRetries, String delaysAsString) throws MessagingException { List<Delay> delayTimesList = createDelayList(delaysAsString); int totalAttempts = computeTotalAttempts(delayTimesList); return getDelaysAndMaxRetry(intendedMaxRetries, totalAttempts, delayTimesList); } @VisibleForTesting  DelaysAndMaxRetry(int maxRetries, List<Delay> delays); static DelaysAndMaxRetry defaults(); static DelaysAndMaxRetry from(int intendedMaxRetries, String delaysAsString); int getMaxRetries(); ImmutableList<Duration> getExpandedDelays(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test void fromShouldParseSingleDelay() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(1, "1s"); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(1, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)))); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldParseTwoDelays() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(2, "1s,2s"); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(2, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(1, Duration.ofSeconds(2)))); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldAdaptMaxRetriesWhenUnderAttempts() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(1, "1s,2*2s"); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(3, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(2, Duration.ofSeconds(2)))); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldAdaptDelaysWhenUnderMaxRetries() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(4, "1s,2*2s"); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(4, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(3, Duration.ofSeconds(2)))); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldHandleNullValuesForDelayAsString() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(1, null); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(1, ImmutableList.of(new Delay(Delay.DEFAULT_ATTEMPTS, Delay.DEFAULT_DELAY_TIME))); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldIgnoreEmptyDelay() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(1, "1s,,2s"); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(2, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(1, Duration.ofSeconds(2)))); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldHandleParsingFailures() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(3, "1s,invalid,2s"); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(3, ImmutableList.of(new Delay(3, Duration.ofSeconds(1)))); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldHandleEmptyStringWithZeroMaxRetries() throws Exception { DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(0, ""); DelaysAndMaxRetry expected = new DelaysAndMaxRetry(0, ImmutableList.<Delay>of()); assertThat(actual).isEqualTo(expected); }
@Test void fromShouldThrowOnEmptyStringWithNonZeroMaxRetry() { assertThatThrownBy(() -> DelaysAndMaxRetry.from(2, "")) .isInstanceOf(MessagingException.class); }