Skip to content

Commit

Permalink
Fix an issue when the first request returns an error. (google#98)
Browse files Browse the repository at this point in the history
* Fix an issue when the first request returns an error and the next waiting request is sent to the network.

* Add CacheDispatcher test for cases when next waiting request is sent to network.
  • Loading branch information
uhager authored and jpd236 committed Sep 26, 2017
1 parent 5fb28f6 commit f07e05e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/android/volley/CacheDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public synchronized void onNoUsableResponseReceived(Request<?> request) {
}
Request<?> nextInLine = waitingRequests.remove(0);
mWaitingRequests.put(cacheKey, waitingRequests);
nextInLine.setNetworkRequestCompleteListener(this);
try {
mCacheDispatcher.mNetworkQueue.put(nextInLine);
} catch (InterruptedException iex) {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/android/volley/CacheDispatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ public class CacheDispatcherTest {
assertFalse(mDelivery.postResponse_called);
}

@Test public void tripleCacheMiss_networkErrorOnFirst() throws Exception {
MockRequest secondRequest = new MockRequest();
MockRequest thirdRequest = new MockRequest();
mRequest.setSequence(1);
secondRequest.setSequence(2);
thirdRequest.setSequence(3);
mCacheQueue.add(mRequest);
mCacheQueue.add(secondRequest);
mCacheQueue.add(thirdRequest);
mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);

assertTrue(mNetworkQueue.size() == 1);
assertFalse(mDelivery.postResponse_called);

Request request = mNetworkQueue.take();
request.notifyListenerResponseNotUsable();
// Second request should now be in network queue.
assertTrue(mNetworkQueue.size() == 1);
request = mNetworkQueue.take();
assertTrue(request.equals(secondRequest));
// Another unusable response, third request should now be added.
request.notifyListenerResponseNotUsable();
assertTrue(mNetworkQueue.size() == 1);
request = mNetworkQueue.take();
assertTrue(request.equals(thirdRequest));
}

@Test public void duplicateSoftExpiredCacheHit_failedRequest() throws Exception {
Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
mCache.setEntryToReturn(entry);
Expand Down

0 comments on commit f07e05e

Please sign in to comment.