Search in sources :

Example 6 with NoCache

use of com.android.volley.toolbox.NoCache in project iosched by google.

the class RequestQueueTest method testAdd_dedupeByCacheKey.

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) {
            // req1 must have been parsed.
            assertEquals(1, parsed.get());
            // req1 must have been parsed.
            assertEquals(1, delivered.get());
            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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NoCache(com.android.volley.toolbox.NoCache)

Example 7 with NoCache

use of com.android.volley.toolbox.NoCache in project FastDev4Android by jiangqqlmj.

the class RequestQueueTest method cancelAll_onlyCorrectTag.

@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);
    // A
    queue.add(req1);
    // B
    queue.add(req2);
    // A
    queue.add(req3);
    queue.cancelAll(tagA);
    // A
    queue.add(req4);
    // A cancelled
    verify(req1).cancel();
    // A cancelled
    verify(req3).cancel();
    // B not cancelled
    verify(req2, never()).cancel();
    // A added after cancel not cancelled
    verify(req4, never()).cancel();
}
Also used : NoCache(com.android.volley.toolbox.NoCache) Test(org.junit.Test)

Example 8 with NoCache

use of com.android.volley.toolbox.NoCache in project FastDev4Android by jiangqqlmj.

the class RequestQueueIntegrationTest method add_dedupeByCacheKey.

/**
 * 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();
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockRequest(com.android.volley.mock.MockRequest) MockRequest(com.android.volley.mock.MockRequest) NoCache(com.android.volley.toolbox.NoCache) RequestFinishedListener(com.android.volley.RequestQueue.RequestFinishedListener) Test(org.junit.Test)

Example 9 with NoCache

use of com.android.volley.toolbox.NoCache in project FastDev4Android by jiangqqlmj.

the class RequestQueueIntegrationTest method add_requestFinishedListenerError.

/**
 * Verify RequestFinishedListeners are informed when request errors
 *
 * Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
 */
@Test
public void add_requestFinishedListenerError() throws Exception {
    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Request request = new MockRequest();
    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    when(mMockNetwork.performRequest(request)).thenThrow(new VolleyError());
    queue.addRequestFinishedListener(listener);
    queue.start();
    queue.add(request);
    verify(listener, timeout(100)).onRequestFinished(request);
    queue.stop();
}
Also used : MockRequest(com.android.volley.mock.MockRequest) MockRequest(com.android.volley.mock.MockRequest) NoCache(com.android.volley.toolbox.NoCache) RequestFinishedListener(com.android.volley.RequestQueue.RequestFinishedListener) Test(org.junit.Test)

Example 10 with NoCache

use of com.android.volley.toolbox.NoCache in project TaEmCasa by Dionen.

the class RequestQueueTest method cancelAll_onlyCorrectTag.

@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);
    // A
    queue.add(req1);
    // B
    queue.add(req2);
    // A
    queue.add(req3);
    queue.cancelAll(tagA);
    // A
    queue.add(req4);
    // A cancelled
    verify(req1).cancel();
    // A cancelled
    verify(req3).cancel();
    // B not cancelled
    verify(req2, never()).cancel();
    // A added after cancel not cancelled
    verify(req4, never()).cancel();
}
Also used : NoCache(com.android.volley.toolbox.NoCache) Test(org.junit.Test)

Aggregations

NoCache (com.android.volley.toolbox.NoCache)21 Test (org.junit.Test)18 MockRequest (com.android.volley.mock.MockRequest)16 RequestFinishedListener (com.android.volley.RequestQueue.RequestFinishedListener)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 Answer (org.mockito.stubbing.Answer)9 UiThreadTest (android.test.UiThreadTest)1 MockNetwork (com.android.volley.mock.MockNetwork)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1