PendingJobManager() { this( BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT .withMaxRetries(Integer.MAX_VALUE) .withInitialBackoff(Duration.standardSeconds(1)) .withMaxBackoff(Duration.standardMinutes(1)) .backoff())); }
@Override public Job pollJob(JobReference jobRef, int maxAttempts) throws InterruptedException { BackOff backoff = BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT .withMaxRetries(maxAttempts) .withInitialBackoff(INITIAL_JOB_STATUS_POLL_BACKOFF) .withMaxBackoff(Duration.standardMinutes(1)) .backoff()); return pollJob(jobRef, Sleeper.DEFAULT, backoff); }
@Test public void testInvalidMaxBackoff() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("maxBackoff PT0S must be at least 1 millisecond"); defaultBackoff.withMaxBackoff(Duration.ZERO); }
FluentBackoff.DEFAULT .withInitialBackoff(Duration.standardSeconds(1)) .withMaxBackoff(Duration.standardMinutes(2));
/** Tests with bounded interval, custom exponent, limited cumulative time, and a reset. */ @Test public void testBoundedIntervalAndCumTimeWithReset() throws Exception { BackOff backOff = FluentBackoff.DEFAULT .withInitialBackoff(Duration.millis(500)) .withMaxBackoff(Duration.standardSeconds(1)) .withMaxCumulativeBackoff(Duration.standardMinutes(1)) .backoff(); assertThat(countMaximumBackoff(backOff), equalTo(Duration.standardMinutes(1).getMillis())); backOff.reset(); assertThat(countMaximumBackoff(backOff), equalTo(Duration.standardMinutes(1).getMillis())); // sanity check: should get 0 if we don't reset assertThat(countMaximumBackoff(backOff), equalTo(0L)); backOff.reset(); assertThat(countMaximumBackoff(backOff), equalTo(Duration.standardMinutes(1).getMillis())); }
/** Tests with bounded interval, custom exponent, limited cumulative time and retries. */ @Test public void testBoundedIntervalAndCumTimeAndRetriesWithReset() throws Exception { BackOff backOff = FluentBackoff.DEFAULT .withInitialBackoff(Duration.millis(500)) .withMaxBackoff(Duration.standardSeconds(1)) .withMaxCumulativeBackoff(Duration.standardMinutes(1)) .backoff(); long cumulativeBackoffMillis = 0; long currentBackoffMillis = backOff.nextBackOffMillis(); while (currentBackoffMillis != BackOff.STOP) { cumulativeBackoffMillis += currentBackoffMillis; currentBackoffMillis = backOff.nextBackOffMillis(); } assertThat(cumulativeBackoffMillis, equalTo(Duration.standardMinutes(1).getMillis())); }
@Test public void testBackoffImplToString() throws IOException { FluentBackoff config = FluentBackoff.DEFAULT .withExponent(3.4) .withMaxRetries(4) .withInitialBackoff(Duration.standardSeconds(3)) .withMaxBackoff(Duration.standardHours(1)) .withMaxCumulativeBackoff(Duration.standardDays(1)); BackOff backOff = config.backoff(); assertEquals( "BackoffImpl{backoffConfig=" + config.toString() + "," + " currentRetry=0, currentCumulativeBackoff=PT0S}", backOff.toString()); // backoff once, ignoring result backOff.nextBackOffMillis(); // currentRetry is exact, we can test it. assertThat(backOff.toString(), containsString("currentRetry=1")); // currentCumulativeBackoff is not exact; we cannot even check that it's non-zero (randomness). } }
@Override public Job pollJob(JobReference jobRef, int maxAttempts) throws InterruptedException { BackOff backoff = BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT .withMaxRetries(maxAttempts) .withInitialBackoff(Duration.millis(10)) .withMaxBackoff(Duration.standardSeconds(1)) .backoff()); Sleeper sleeper = Sleeper.DEFAULT; try { do { Job job = getJob(jobRef); if (job != null) { JobStatus status = job.getStatus(); if (status != null && ("DONE".equals(status.getState()) || "FAILED".equals(status.getState()))) { return job; } } } while (BackOffUtils.next(sleeper, backoff)); } catch (IOException e) { return null; } return null; }
@Test public void testFluentBackoffToString() throws IOException { FluentBackoff config = FluentBackoff.DEFAULT .withExponent(3.4) .withMaxRetries(4) .withInitialBackoff(Duration.standardSeconds(3)) .withMaxBackoff(Duration.standardHours(1)) .withMaxCumulativeBackoff(Duration.standardDays(1)); assertEquals( "FluentBackoff{exponent=3.4, initialBackoff=PT3S, maxBackoff=PT3600S," + " maxRetries=4, maxCumulativeBackoff=PT86400S}", config.toString()); }
/** Tests with bounded interval, custom exponent, and unlimited retries. */ @Test public void testBoundedIntervalWithReset() throws Exception { BackOff backOff = FluentBackoff.DEFAULT .withInitialBackoff(Duration.millis(500)) .withMaxBackoff(Duration.standardSeconds(1)) .backoff(); assertThat(backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(249L), lessThan(751L))); assertThat(backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(374L), lessThan(1126L))); assertThat( backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(500L), lessThanOrEqualTo(1500L))); assertThat( backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(500L), lessThanOrEqualTo(1500L))); assertThat( backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(500L), lessThanOrEqualTo(1500L))); assertThat( backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(500L), lessThanOrEqualTo(1500L))); assertThat( backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(500L), lessThanOrEqualTo(1500L))); // Reset, should go back to short times. backOff.reset(); assertThat(backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(249L), lessThan(751L))); assertThat(backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(374L), lessThan(1126L))); assertThat( backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(500L), lessThanOrEqualTo(1500L))); assertThat( backOff.nextBackOffMillis(), allOf(greaterThanOrEqualTo(500L), lessThanOrEqualTo(1500L))); }
.withMaxRetries(Integer.MAX_VALUE) .withInitialBackoff(Duration.millis(10)) .withMaxBackoff(Duration.millis(10)) .backoff()));