예제 #1
0
  @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)));
  }
예제 #2
0
  @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)));
  }
예제 #3
0
  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");
  }
예제 #4
0
 @Test
 public void initialDelayIsZero() {
   Attempt attempt = cut.begin();
   assertThat(attempt.delay(), is(0L));
 }