com.android.volley.toolbox.NoCache Java Examples

The following examples show how to use com.android.volley.toolbox.NoCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestQueueTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@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
}
 
Example #2
Source File: RequestQueueTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@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
}
 
Example #3
Source File: RequestQueueIntegrationTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #4
Source File: RequestQueueIntegrationTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #5
Source File: RequestQueueTest.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: RequestQueueTest.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #7
Source File: RequestQueueTest.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
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
}
 
Example #8
Source File: RequestQueueTest.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #9
Source File: RequestQueueTest.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #10
Source File: RequestQueueTest.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
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
}
 
Example #11
Source File: RequestQueueTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void cancelAll_onlyCorrectTag() throws Exception {
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    Object tagA = new Object();
    Object tagB = new Object();
    StringRequest req1 = mock(StringRequest.class);
    when(req1.getTag()).thenReturn(tagA);
    StringRequest req2 = mock(StringRequest.class);
    when(req2.getTag()).thenReturn(tagB);
    StringRequest req3 = mock(StringRequest.class);
    when(req3.getTag()).thenReturn(tagA);
    StringRequest req4 = mock(StringRequest.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
}
 
Example #12
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #13
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #14
Source File: RequestQueueTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@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
}
 
Example #15
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #16
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #17
Source File: RequestQueueTest.java    From CrossBow with Apache License 2.0 6 votes vote down vote up
@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
}
 
Example #18
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #19
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #20
Source File: RequestQueueTest.java    From device-database with Apache License 2.0 6 votes vote down vote up
@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
}
 
Example #21
Source File: NetworkDispatcherTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void exceptionNotifiesListener() throws Exception {
    RequestQueue.RequestEventListener listener = mock(RequestQueue.RequestEventListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mNetwork, 0, mDelivery);
    queue.addRequestEventListener(listener);
    mRequest.setRequestQueue(queue);

    when(mNetwork.performRequest(any(Request.class))).thenThrow(new ServerError());

    mDispatcher.processRequest(mRequest);

    InOrder inOrder = inOrder(listener);
    inOrder.verify(listener)
            .onRequestEvent(
                    mRequest, RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_STARTED);
    inOrder.verify(listener)
            .onRequestEvent(
                    mRequest, RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_FINISHED);
    inOrder.verifyNoMoreInteractions();
}
 
Example #22
Source File: NetworkDispatcherTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void successNotifiesListener() throws Exception {
    RequestQueue.RequestEventListener listener = mock(RequestQueue.RequestEventListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mNetwork, 0, mDelivery);
    queue.addRequestEventListener(listener);
    mRequest.setRequestQueue(queue);

    when(mNetwork.performRequest(any(Request.class)))
            .thenReturn(new NetworkResponse(CANNED_DATA));

    mDispatcher.processRequest(mRequest);

    InOrder inOrder = inOrder(listener);
    inOrder.verify(listener)
            .onRequestEvent(
                    mRequest, RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_STARTED);
    inOrder.verify(listener)
            .onRequestEvent(
                    mRequest, RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_FINISHED);
    inOrder.verifyNoMoreInteractions();
}
 
Example #23
Source File: RequestQueueIntegrationTest.java    From volley with Apache License 2.0 6 votes vote down vote up
/** Verify RequestFinishedListeners are informed when requests are canceled. */
@Test
public void add_requestFinishedListenerCanceled() throws Exception {
    MockRequest 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(mMockListener);
    queue.start();
    queue.add(request);

    request.cancel();
    verify(mMockListener, timeout(10000)).onRequestFinished(request);
    queue.stop();
}
 
Example #24
Source File: RequestQueueIntegrationTest.java    From volley with Apache License 2.0 6 votes vote down vote up
/** Verify RequestFinishedListeners are informed when requests are successfully delivered. */
@Test
public void add_requestFinishedListenerSuccess() throws Exception {
    MockRequest request = new MockRequest();
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);

    queue.addRequestFinishedListener(mMockListener);
    queue.addRequestFinishedListener(mMockListener2);
    queue.start();
    queue.add(request);

    verify(mMockListener, timeout(10000)).onRequestFinished(request);
    verify(mMockListener2, timeout(10000)).onRequestFinished(request);

    queue.stop();
}
 
Example #25
Source File: RequestQueueIntegrationTest.java    From device-database with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #26
Source File: RequestQueueIntegrationTest.java    From device-database with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #27
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that requests with same cache key are processed in order.
 *
 * Needs to be an integration test because relies on complex interations between various queues
 */
@Test public void add_dedupeByCacheKey() throws Exception {
    // Enqueue 2 requests with the same cache key. The first request takes 20ms. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    Request req1 = new MockRequest();
    Request req2 = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for first
    when(mMockNetwork.performRequest(req1)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(req2)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 3, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(req1);
    queue.add(req2);
    queue.start();

    // you cannot do strict order verification with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, then verify req1 goes through
    verifyNoMoreInteractions(listener);
    verify(listener, timeout(100)).onRequestFinished(req1);
    verify(listener, timeout(10)).onRequestFinished(req2);
    queue.stop();
}
 
Example #28
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@Test public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);

    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();

    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
 
Example #29
Source File: RequestQueueTest.java    From volley with Apache License 2.0 5 votes vote down vote up
@Test
public void finish_notifiesListener() throws Exception {
    RequestQueue.RequestEventListener listener = mock(RequestQueue.RequestEventListener.class);
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
    queue.addRequestEventListener(listener);
    StringRequest req = mock(StringRequest.class);

    queue.finish(req);

    verify(listener).onRequestEvent(req, RequestQueue.RequestEvent.REQUEST_FINISHED);
    verifyNoMoreInteractions(listener);
}
 
Example #30
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that requests with same cache key are processed in order.
 *
 * Needs to be an integration test because relies on complex interations between various queues
 */
@Test public void add_dedupeByCacheKey() throws Exception {
    // Enqueue 2 requests with the same cache key. The first request takes 20ms. Assert that the
    // second request is only handled after the first one has been parsed and delivered.
    Request req1 = new MockRequest();
    Request req2 = new MockRequest();
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for first
    when(mMockNetwork.performRequest(req1)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(req2)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 3, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(req1);
    queue.add(req2);
    queue.start();

    // you cannot do strict order verification with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, then verify req1 goes through
    verifyNoMoreInteractions(listener);
    verify(listener, timeout(100)).onRequestFinished(req1);
    verify(listener, timeout(10)).onRequestFinished(req2);
    queue.stop();
}