@Test public void cancelAll_onlyCorrectTag() throws Exception { RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery); Object tagA = new Object(); Object tagB = new Object(); Request req1 = mock(Request.class); when(req1.getTag()).thenReturn(tagA); Request req2 = mock(Request.class); when(req2.getTag()).thenReturn(tagB); Request req3 = mock(Request.class); when(req3.getTag()).thenReturn(tagA); Request req4 = mock(Request.class); when(req4.getTag()).thenReturn(tagA); queue.add(req1); // A queue.add(req2); // B queue.add(req3); // A queue.cancelAll(tagA); queue.add(req4); // A verify(req1).cancel(); // A cancelled verify(req3).cancel(); // A cancelled verify(req2, never()).cancel(); // B not cancelled verify(req4, never()).cancel(); // A added after cancel not cancelled }
/** * Verify RequestFinishedListeners are informed when requests are canceled * * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction */ @Test public void add_requestFinishedListenerCanceled() throws Exception { RequestFinishedListener listener = mock(RequestFinishedListener.class); Request request = new MockRequest(); Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() { @Override public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable { Thread.sleep(200); return mock(NetworkResponse.class); } }; RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery); when(mMockNetwork.performRequest(request)).thenAnswer(delayAnswer); queue.addRequestFinishedListener(listener); queue.start(); queue.add(request); request.cancel(); verify(listener, timeout(100)).onRequestFinished(request); queue.stop(); }
/** * Verify RequestFinishedListeners are informed when requests are successfully delivered * * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction */ @Test public void add_requestFinishedListenerSuccess() throws Exception { NetworkResponse response = mock(NetworkResponse.class); Request request = new MockRequest(); RequestFinishedListener listener = mock(RequestFinishedListener.class); RequestFinishedListener listener2 = mock(RequestFinishedListener.class); RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery); queue.addRequestFinishedListener(listener); queue.addRequestFinishedListener(listener2); queue.start(); queue.add(request); verify(listener, timeout(100)).onRequestFinished(request); verify(listener2, timeout(100)).onRequestFinished(request); queue.stop(); }
@UiThreadTest public void testAdd_requestProcessedInCorrectOrder() throws Exception { int requestsToMake = 100; OrderCheckingNetwork network = new OrderCheckingNetwork(); RequestQueue queue = new RequestQueue(new NoCache(), network, 1, mDelivery); for (Request<?> request : makeRequests(requestsToMake)) { queue.add(request); } network.setExpectedRequests(requestsToMake); queue.start(); network.waitUntilExpectedDone(2000); // 2 seconds queue.stop(); }
public void testAdd_dedupeByCacheKey() throws Exception { OrderCheckingNetwork network = new OrderCheckingNetwork(); final AtomicInteger parsed = new AtomicInteger(); final AtomicInteger delivered = new AtomicInteger(); // Enqueue 2 requests with the same cache key. The first request takes 1.5s. Assert that the // second request is only handled after the first one has been parsed and delivered. DelayedRequest req1 = new DelayedRequest(1500, parsed, delivered); DelayedRequest req2 = new DelayedRequest(0, parsed, delivered) { @Override protected Response<Object> parseNetworkResponse(NetworkResponse response) { assertEquals(1, parsed.get()); // req1 must have been parsed. assertEquals(1, delivered.get()); // req1 must have been parsed. return super.parseNetworkResponse(response); } }; network.setExpectedRequests(2); RequestQueue queue = new RequestQueue(new NoCache(), network, 3, mDelivery); queue.add(req1); queue.add(req2); queue.start(); network.waitUntilExpectedDone(2000); queue.stop(); }
public void testCancelAll_onlyCorrectTag() throws Exception { MockNetwork network = new MockNetwork(); RequestQueue queue = new RequestQueue(new NoCache(), network, 3, mDelivery); Object tagA = new Object(); Object tagB = new Object(); MockRequest req1 = new MockRequest(); req1.setTag(tagA); MockRequest req2 = new MockRequest(); req2.setTag(tagB); MockRequest req3 = new MockRequest(); req3.setTag(tagA); MockRequest req4 = new MockRequest(); req4.setTag(tagA); queue.add(req1); // A queue.add(req2); // B queue.add(req3); // A queue.cancelAll(tagA); queue.add(req4); // A assertTrue(req1.cancel_called); // A cancelled assertFalse(req2.cancel_called); // B not cancelled assertTrue(req3.cancel_called); // A cancelled assertFalse(req4.cancel_called); // A added after cancel not cancelled }
public RequestQueue getRequestQueue() { if (requestQueue == null) { // getApplicationContext() is key, it keeps you from leaking the // Activity or BroadcastReceiver if someone passes one in. if (ctx == null) { requestQueue = new RequestQueue(new NoCache(), new BasicNetwork(new OkHttp3Stack())); } else { if (StringUtils.isBlank(trustedHostname)) { requestQueue = Volley.newRequestQueue(ctx.getApplicationContext(), new OkHttp3Stack()); } else { requestQueue = Volley.newRequestQueue(ctx.getApplicationContext(), new OkHttp3Stack(ClientUtils.newCustomSocketFactory(trustedHostname))); } } } return requestQueue; }