ScribeBackoffPolicy extends BackoffPolicy { @Override public void backoff(VolleyError volleyError) throws VolleyError { if (!hasAttemptRemaining()) { throw volleyError; } if (volleyError instanceof NoConnectionError) { updateBackoffTime(); return; } NetworkResponse networkResponse = volleyError.networkResponse; if (networkResponse != null && (networkResponse.statusCode == 503 || networkResponse.statusCode == 504)) { updateBackoffTime(); return; } throw volleyError; } ScribeBackoffPolicy(); @VisibleForTesting  ScribeBackoffPolicy(int defaultBackoffTimeMs, int maxRetries, int backoffMultiplier); @Override void backoff(VolleyError volleyError);  }
@Test public void backoff_with503Error_shouldUpdateBackoffTime() throws Exception { NetworkResponse networkResponse = new NetworkResponse(503, null, null, false); VolleyError volleyError = new VolleyError(networkResponse); assertThat(subject.getBackoffMs()).isEqualTo(0); assertThat(subject.getRetryCount()).isEqualTo(0); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(60000); assertThat(subject.getRetryCount()).isEqualTo(1); }
@Test public void backoff_with504Error_shouldUpdateBackoffTime() throws Exception { NetworkResponse networkResponse = new NetworkResponse(504, null, null, false); VolleyError volleyError = new VolleyError(networkResponse); assertThat(subject.getBackoffMs()).isEqualTo(0); assertThat(subject.getRetryCount()).isEqualTo(0); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(60000); assertThat(subject.getRetryCount()).isEqualTo(1); }
@Test public void backoff_withNoConnectionError_shouldUpdateBackoffTime() throws Exception { VolleyError volleyError = new NoConnectionError(); assertThat(subject.getBackoffMs()).isEqualTo(0); assertThat(subject.getRetryCount()).isEqualTo(0); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(60000); assertThat(subject.getRetryCount()).isEqualTo(1); }
@Test(expected = VolleyError.class) public void backoff_withOtherErrorType_shouldRethrowException() throws Exception { NetworkResponse networkResponse = new NetworkResponse(500, null, null, false); VolleyError volleyError = new VolleyError(networkResponse); subject.backoff(volleyError); }
@Test public void backoff_shouldUpdateBackoffTime5TimesMax() throws Exception { VolleyError volleyError = new NoConnectionError(); assertThat(subject.getBackoffMs()).isEqualTo(0); assertThat(subject.getRetryCount()).isEqualTo(0); assertThat(subject.hasAttemptRemaining()).isTrue(); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(60000); assertThat(subject.getRetryCount()).isEqualTo(1); assertThat(subject.hasAttemptRemaining()).isTrue(); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(120000); assertThat(subject.getRetryCount()).isEqualTo(2); assertThat(subject.hasAttemptRemaining()).isTrue(); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(240000); assertThat(subject.getRetryCount()).isEqualTo(3); assertThat(subject.hasAttemptRemaining()).isTrue(); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(480000); assertThat(subject.getRetryCount()).isEqualTo(4); assertThat(subject.hasAttemptRemaining()).isTrue(); subject.backoff(volleyError); assertThat(subject.getBackoffMs()).isEqualTo(960000); assertThat(subject.getRetryCount()).isEqualTo(5); assertThat(subject.hasAttemptRemaining()).isFalse(); }
@Test(expected = NoConnectionError.class) public void backoff_withNoAttemptsRemaining_shouldRethrowVolleyException() throws Exception { VolleyError volleyError = new NoConnectionError(); try { subject.backoff(volleyError); subject.backoff(volleyError); subject.backoff(volleyError); subject.backoff(volleyError); subject.backoff(volleyError); } catch (Exception e) { fail("Exception should not be thrown from above backoffs."); } subject.backoff(volleyError); }