@Test(enabled=false) // not a test, but picked up as such because public public static void assertCallTimes(List<Long> actual, Integer... expected) { Assert.assertEquals(actual.size(), expected.length); for (int i = 0; i < expected.length; i++) { long callTime = actual.get(i); assertOrdered(expected[i]-EARLY_RETURN_GRACE, callTime, expected[i]+SLOW_BUILD_SERVER_GRACE); } }
@Test(enabled = false) // not a test, but picked up as such because public public static void assertCallTimes(List<Long> actual, Integer... expected) { Assert.assertEquals(actual.size(), expected.length, "actual=" + actual); for (int i = 0; i < expected.length; i++) { long callTime = actual.get(i); assertOrdered(expected[i] - EARLY_RETURN_GRACE, callTime, expected[i] + SLOW_BUILD_SERVER_GRACE); } }
@Test void testAlwaysTrue() { // will call once immediately Predicate<String> predicate = retry(Predicates.<String> alwaysTrue(), 3, 1, SECONDS); stopwatch.start(); predicate.apply(""); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(duration, SLOW_BUILD_SERVER_GRACE); }
@Test void testAlwaysFalseMillis() { // maxWait=3; period=1; maxPeriod defaults to 1*10 // will call at 0, 1, 1+(1*1.5), 3 Predicate<String> predicate = retry(Predicates.<String> alwaysFalse(), 3, 1, SECONDS); stopwatch.start(); predicate.apply(""); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(3000-EARLY_RETURN_GRACE, duration, 3000+SLOW_BUILD_SERVER_GRACE); }
private void ensureImmediateReturnFor(final Exception ex) { Predicate<Supplier<String>> predicate = retry( new Predicate<Supplier<String>>() { public boolean apply(Supplier<String> input) { return "goo".equals(input.get()); } }, 3, 1, SECONDS); stopwatch.start(); assert !predicate.apply(new Supplier<String>() { @Override public String get() { throw new RuntimeException(ex); } }); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(duration, SLOW_BUILD_SERVER_GRACE); }
private void ensureImmediateReturnFor(final Exception ex) { Predicate<Supplier<String>> predicate = retry( new Predicate<Supplier<String>>() { public boolean apply(Supplier<String> input) { return "goo".equals(input.get()); } }, 3, 1, SECONDS); stopwatch.start(); assertFalse(predicate.apply(new Supplier<String>() { @Override public String get() { throw new RuntimeException(ex); } })); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(duration, SLOW_BUILD_SERVER_GRACE); }
@Test void testRetryFirstTimeTrue() { RepeatedAttemptsPredicate rawPredicate = new RepeatedAttemptsPredicate(1); Predicate<String> predicate = retry(rawPredicate, 4, 1, SECONDS); stopwatch.start(); assertTrue(predicate.apply("")); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(0, duration, 0 + SLOW_BUILD_SERVER_GRACE); assertCallTimes(rawPredicate.callTimes, 0); }
@Test void testThirdTimeTrue() { // maxWait=4; period=1; maxPeriod defaults to 1*10 // will call at 0, 1, 1+(1*1.5) RepeatedAttemptsPredicate rawPredicate = new RepeatedAttemptsPredicate(2); Predicate<String> predicate = retry(rawPredicate, 4, 1, SECONDS); stopwatch.start(); predicate.apply(""); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(2500-EARLY_RETURN_GRACE, duration, 2500+SLOW_BUILD_SERVER_GRACE); assertCallTimes(rawPredicate.callTimes, 0, 1000, 1000+1500); }
@Test void testThirdTimeTrueLimitedMaxInterval() { // maxWait=3; period=1; maxPeriod=1 // will call at 0, 1, 1+1 RepeatedAttemptsPredicate rawPredicate = new RepeatedAttemptsPredicate(2); Predicate<String> predicate = retry(rawPredicate, 3, 1, 1, SECONDS); stopwatch.start(); predicate.apply(""); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(2000-EARLY_RETURN_GRACE, duration, 2000+SLOW_BUILD_SERVER_GRACE); assertCallTimes(rawPredicate.callTimes, 0, 1000, 2000); }
@Test void testRetryAlwaysFalseMillis() { // maxWait=3; period=1; maxPeriod defaults to 1*10 // will call at 0, 1, 1+(1*1.5), 3 RepeatedAttemptsPredicate rawPredicate = new RepeatedAttemptsPredicate(Integer.MAX_VALUE); Predicate<String> predicate = retry(rawPredicate, 3, 1, SECONDS); stopwatch.start(); assertFalse(predicate.apply("")); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(3000 - EARLY_RETURN_GRACE, duration, 3000 + SLOW_BUILD_SERVER_GRACE); assertCallTimes(rawPredicate.callTimes, 0, 1000, 1000 + 1500, 3000); }
@Test void testRetryWillRunOnceOnNegativeTimeout() { RepeatedAttemptsPredicate rawPredicate = new RepeatedAttemptsPredicate(1); Predicate<String> predicate = retry(rawPredicate, -1, 1, SECONDS); stopwatch.start(); assertTrue(predicate.apply("")); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(0, duration, 0 + SLOW_BUILD_SERVER_GRACE); assertCallTimes(rawPredicate.callTimes, 0); }
@Test void testRetryThirdTimeTrue() { // maxWait=4; period=1; maxPeriod defaults to 1*10 // will call at 0, 1, 1+(1*1.5) RepeatedAttemptsPredicate rawPredicate = new RepeatedAttemptsPredicate(3); Predicate<String> predicate = retry(rawPredicate, 4, 1, SECONDS); stopwatch.start(); assertTrue(predicate.apply("")); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(2500 - EARLY_RETURN_GRACE, duration, 2500 + SLOW_BUILD_SERVER_GRACE); assertCallTimes(rawPredicate.callTimes, 0, 1000, 1000 + 1500); }
@Test void testRetryThirdTimeTrueLimitedMaxInterval() { // maxWait=3; period=1; maxPeriod=1 // will call at 0, 1, 1+1 RepeatedAttemptsPredicate rawPredicate = new RepeatedAttemptsPredicate(3); Predicate<String> predicate = retry(rawPredicate, 3, 1, 1, SECONDS); stopwatch.start(); assertTrue(predicate.apply("")); long duration = stopwatch.elapsed(MILLISECONDS); assertOrdered(2000 - EARLY_RETURN_GRACE, duration, 2000 + SLOW_BUILD_SERVER_GRACE); assertCallTimes(rawPredicate.callTimes, 0, 1000, 2000); }