@Test public void successOnOldAttemptDoesNotResetDelay() { cut.begin().recordFailure(); cut.begin().recordFailure(); Attempt attempt = cut.begin(); cut.begin().recordFailure(); assertThat(cut.begin().delay(), is(approx(70L, 10))); attempt.recordSuccess(); assertThat(cut.begin().delay(), is(approx(70L, 10))); }
@Test public void failureOnOldAttemptDoesNotIncreaseDelay() { for (int i = 0; i < 3; ++i) { cut.begin().recordFailure(); } Attempt attempt = cut.begin(); cut.begin().recordFailure(); assertThat(cut.begin().delay(), is(approx(150L, 10))); attempt.recordFailure(); assertThat(cut.begin().delay(), is(approx(150L, 10))); }
private void retry(final Attempt attempt, final int howLong) throws IOException, InterruptedException { final long start = new Date().getTime(); IOException lastE = null; long timeLeft = howLong; while (timeLeft > 0) { try { attempt.again(); return; } catch (final IOException e) { lastE = e; } final long now = new Date().getTime(); final long timePassed = now - start; timeLeft = howLong - timePassed; final long sleepTime = Math.max(3000, Math.min(timePassed, timeLeft)); Thread.sleep(sleepTime); } if (lastE != null) throw lastE; throw new IOException("failed, and I don't know why"); }
@Test public void initialDelayIsZero() { Attempt attempt = cut.begin(); assertThat(attempt.delay(), is(0L)); }