CacheDispatcher extends Thread { private void processRequest() throws InterruptedException { final Request<?> request = mCacheQueue.take(); processRequest(request); } CacheDispatcher(
            BlockingQueue<Request<?>> cacheQueue,
            BlockingQueue<Request<?>> networkQueue,
            Cache cache,
            ResponseDelivery delivery); void quit(); @Override void run();  }
@Test public void processRequestNotifiesListener() throws Exception { RequestQueue.RequestEventListener listener = mock(RequestQueue.RequestEventListener.class); RequestQueue queue = new RequestQueue(mCache, mNetwork, 0, mDelivery); queue.addRequestEventListener(listener); mRequest.setRequestQueue(queue); Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false); when(mCache.get(anyString())).thenReturn(entry); mDispatcher.processRequest(mRequest); InOrder inOrder = inOrder(listener); inOrder.verify(listener) .onRequestEvent(mRequest, RequestQueue.RequestEvent.REQUEST_CACHE_LOOKUP_STARTED); inOrder.verify(listener) .onRequestEvent(mRequest, RequestQueue.RequestEvent.REQUEST_CACHE_LOOKUP_FINISHED); inOrder.verifyNoMoreInteractions(); }
@Test public void cancelledRequest() throws Exception { mRequest.cancel(); mDispatcher.processRequest(mRequest); verify(mCache, never()).get(anyString()); verifyNoResponse(mDelivery); }
@Test public void cacheMiss() throws Exception { mDispatcher.processRequest(mRequest); verifyNoResponse(mDelivery); verify(mNetworkQueue).put(mRequest); assertNull(mRequest.getCacheEntry()); }
@Test public void nonExpiredCacheHit() throws Exception { Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false); when(mCache.get(anyString())).thenReturn(entry); mDispatcher.processRequest(mRequest); verify(mDelivery).postResponse(any(Request.class), any(Response.class)); verify(mDelivery, never()).postError(any(Request.class), any(VolleyError.class)); }
@Test public void expiredCacheHit() throws Exception { Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true); when(mCache.get(anyString())).thenReturn(entry); mDispatcher.processRequest(mRequest); verifyNoResponse(mDelivery); verify(mNetworkQueue).put(mRequest); assertSame(entry, mRequest.getCacheEntry()); }
@Test public void duplicateCacheMiss() throws Exception { StringRequest secondRequest = new StringRequest(Request.Method.GET, "http: mRequest.setSequence(1); secondRequest.setSequence(2); mDispatcher.processRequest(mRequest); mDispatcher.processRequest(secondRequest); verify(mNetworkQueue).put(mRequest); verifyNoResponse(mDelivery); }
@Test public void tripleCacheMiss_networkErrorOnFirst() throws Exception { StringRequest secondRequest = new StringRequest(Request.Method.GET, "http: StringRequest thirdRequest = new StringRequest(Request.Method.GET, "http: mRequest.setSequence(1); secondRequest.setSequence(2); thirdRequest.setSequence(3); mDispatcher.processRequest(mRequest); mDispatcher.processRequest(secondRequest); mDispatcher.processRequest(thirdRequest); verify(mNetworkQueue).put(mRequest); verifyNoResponse(mDelivery); ((Request<?>) mRequest).notifyListenerResponseNotUsable(); verify(mNetworkQueue).put(secondRequest); ((Request<?>) secondRequest).notifyListenerResponseNotUsable(); verify(mNetworkQueue).put(thirdRequest); }