Skip to content

Commit

Permalink
Send request to network when cache entry parsing fails (google#321)
Browse files Browse the repository at this point in the history
* Send request to network if parsing a cache entry returns an error.

Also invalidate the corrupted cache entry and set the request's entry to null.

* Add request marker when cache entry parsing fails.
  • Loading branch information
uhager authored Feb 26, 2020
1 parent 514eac8 commit 8a3a7ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/android/volley/CacheDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ void processRequest(final Request<?> request) throws InterruptedException {
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");

if (!response.isSuccess()) {
request.addMarker("cache-parsing-failed");
mCache.invalidate(request.getCacheKey(), true);
request.setCacheEntry(null);
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
mNetworkQueue.put(request);
}
return;
}
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
mDelivery.postResponse(request, response);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/android/volley/CacheDispatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ public void expiredCacheHit() throws Exception {
assertSame(entry, mRequest.getCacheEntry());
}

// An fresh cache hit with parse error, does not post a response and queues to the network.
@Test
public void freshCacheHit_parseError() throws Exception {
Request request = mock(Request.class);
when(request.parseNetworkResponse(any(NetworkResponse.class)))
.thenReturn(Response.error(new ParseError()));
when(request.getCacheKey()).thenReturn("cache/key");
Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
when(mCache.get(anyString())).thenReturn(entry);

mDispatcher.processRequest(request);

verifyNoResponse(mDelivery);
verify(mNetworkQueue).put(request);
assertNull(request.getCacheEntry());
verify(mCache).invalidate("cache/key", true);
verify(request).addMarker("cache-parsing-failed");
}

@Test
public void duplicateCacheMiss() throws Exception {
StringRequest secondRequest =
Expand Down

0 comments on commit 8a3a7ba

Please sign in to comment.