Skip to content

Commit

Permalink
Merge pull request square#1563 from dlew/dlew/fix-offline-retries
Browse files Browse the repository at this point in the history
Retry requests even when there is no connectivity
  • Loading branch information
JakeWharton authored Jan 4, 2017
2 parents 289ed30 + 7b7c750 commit 8f892b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
31 changes: 8 additions & 23 deletions picasso/src/main/java/com/squareup/picasso/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,22 +331,7 @@ void performRetry(BitmapHunter hunter) {
networkInfo = connectivityManager.getActiveNetworkInfo();
}

boolean hasConnectivity = networkInfo != null && networkInfo.isConnected();
boolean shouldRetryHunter = hunter.shouldRetry(airplaneMode, networkInfo);
boolean supportsReplay = hunter.supportsReplay();

if (!shouldRetryHunter) {
// Mark for replay only if we observe network info changes and support replay.
boolean willReplay = scansNetworkChanges && supportsReplay;
performError(hunter, willReplay);
if (willReplay) {
markForReplay(hunter);
}
return;
}

// If we don't scan for network changes (missing permission) or if we have connectivity, retry.
if (!scansNetworkChanges || hasConnectivity) {
if (hunter.shouldRetry(airplaneMode, networkInfo)) {
if (hunter.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_RETRYING, getLogIdsForHunter(hunter));
}
Expand All @@ -355,13 +340,13 @@ void performRetry(BitmapHunter hunter) {
hunter.networkPolicy |= NetworkPolicy.NO_CACHE.index;
}
hunter.future = service.submit(hunter);
return;
}

performError(hunter, supportsReplay);

if (supportsReplay) {
markForReplay(hunter);
} else {
// Mark for replay only if we observe network info changes and support replay.
boolean willReplay = scansNetworkChanges && hunter.supportsReplay();
performError(hunter, willReplay);
if (willReplay) {
markForReplay(hunter);
}
}
}

Expand Down
10 changes: 3 additions & 7 deletions picasso/src/test/java/com/squareup/picasso/DispatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,25 +360,21 @@ public class DispatcherTest {
verify(service).submit(hunter);
}

@Test public void performRetryMarksForReplayIfSupportsReplayAndNoConnectivity() {
NetworkInfo networkInfo = mockNetworkInfo(false);
@Test public void performRetryMarksForReplayIfSupportsReplayAndShouldNotRetry() {
Action action = mockAction(URI_KEY_1, URI_1, mockTarget());
BitmapHunter hunter = mockHunter(URI_KEY_1, bitmap1, false, action);
when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(true);
when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(false);
when(hunter.supportsReplay()).thenReturn(true);
when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);
dispatcher.performRetry(hunter);
assertThat(dispatcher.hunterMap).isEmpty();
assertThat(dispatcher.failedActions).hasSize(1);
verify(service, never()).submit(hunter);
}

@Test public void performRetryRetriesIfHasConnectivity() {
NetworkInfo networkInfo = mockNetworkInfo(true);
@Test public void performRetryRetriesIfShouldRetry() {
Action action = mockAction(URI_KEY_1, URI_1, mockTarget());
BitmapHunter hunter = mockHunter(URI_KEY_1, bitmap1, false, action);
when(hunter.shouldRetry(anyBoolean(), any(NetworkInfo.class))).thenReturn(true);
when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);
dispatcher.performRetry(hunter);
assertThat(dispatcher.hunterMap).isEmpty();
assertThat(dispatcher.failedActions).isEmpty();
Expand Down

0 comments on commit 8f892b1

Please sign in to comment.