| 23410 |
tejbeer |
1 |
/*
|
|
|
2 |
* Copyright (C) 2015 The Android Open Source Project
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.android.volley;
|
|
|
18 |
|
|
|
19 |
import com.android.volley.Request.Priority;
|
|
|
20 |
import com.android.volley.RequestQueue.RequestFinishedListener;
|
|
|
21 |
import com.android.volley.mock.MockRequest;
|
|
|
22 |
import com.android.volley.mock.ShadowSystemClock;
|
|
|
23 |
import com.android.volley.toolbox.NoCache;
|
|
|
24 |
import com.android.volley.utils.ImmediateResponseDelivery;
|
|
|
25 |
|
|
|
26 |
import org.junit.Before;
|
|
|
27 |
import org.junit.Test;
|
|
|
28 |
import org.junit.runner.RunWith;
|
|
|
29 |
import org.mockito.Mock;
|
|
|
30 |
import org.mockito.invocation.InvocationOnMock;
|
|
|
31 |
import org.mockito.stubbing.Answer;
|
|
|
32 |
import org.robolectric.RobolectricTestRunner;
|
|
|
33 |
import org.robolectric.annotation.Config;
|
|
|
34 |
|
|
|
35 |
import static org.mockito.Mockito.mock;
|
|
|
36 |
import static org.mockito.Mockito.timeout;
|
|
|
37 |
import static org.mockito.Mockito.verify;
|
|
|
38 |
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
|
39 |
import static org.mockito.Mockito.when;
|
|
|
40 |
import static org.mockito.MockitoAnnotations.initMocks;
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Integration tests for {@link RequestQueue}, that verify its behavior in conjunction with real dispatcher, queues and
|
|
|
45 |
* Requests. Network is mocked out
|
|
|
46 |
*/
|
|
|
47 |
@RunWith(RobolectricTestRunner.class)
|
|
|
48 |
@Config(shadows = {ShadowSystemClock.class})
|
|
|
49 |
public class RequestQueueIntegrationTest {
|
|
|
50 |
|
|
|
51 |
private ResponseDelivery mDelivery;
|
|
|
52 |
@Mock private Network mMockNetwork;
|
|
|
53 |
|
|
|
54 |
@Before public void setUp() throws Exception {
|
|
|
55 |
mDelivery = new ImmediateResponseDelivery();
|
|
|
56 |
initMocks(this);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
@Test public void add_requestProcessedInCorrectOrder() throws Exception {
|
|
|
60 |
// Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
|
|
|
61 |
// takes 20ms.
|
|
|
62 |
// Assert that first request is only handled after the first one has been parsed and delivered.
|
|
|
63 |
MockRequest lowerPriorityReq = new MockRequest();
|
|
|
64 |
MockRequest higherPriorityReq = new MockRequest();
|
|
|
65 |
lowerPriorityReq.setCacheKey("1");
|
|
|
66 |
higherPriorityReq.setCacheKey("2");
|
|
|
67 |
lowerPriorityReq.setPriority(Priority.LOW);
|
|
|
68 |
higherPriorityReq.setPriority(Priority.HIGH);
|
|
|
69 |
|
|
|
70 |
RequestFinishedListener listener = mock(RequestFinishedListener.class);
|
|
|
71 |
Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
|
|
|
72 |
@Override
|
|
|
73 |
public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
|
|
|
74 |
Thread.sleep(20);
|
|
|
75 |
return mock(NetworkResponse.class);
|
|
|
76 |
}
|
|
|
77 |
};
|
|
|
78 |
//delay only for higher request
|
|
|
79 |
when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
|
|
|
80 |
when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));
|
|
|
81 |
|
|
|
82 |
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
|
|
|
83 |
queue.addRequestFinishedListener(listener);
|
|
|
84 |
queue.add(lowerPriorityReq);
|
|
|
85 |
queue.add(higherPriorityReq);
|
|
|
86 |
queue.start();
|
|
|
87 |
|
|
|
88 |
// you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
|
|
|
89 |
// as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
|
|
|
90 |
verifyNoMoreInteractions(listener);
|
|
|
91 |
// verify higherPriorityReq goes through first
|
|
|
92 |
verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
|
|
|
93 |
// verify lowerPriorityReq goes last
|
|
|
94 |
verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
|
|
|
95 |
queue.stop();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Asserts that requests with same cache key are processed in order.
|
|
|
100 |
*
|
|
|
101 |
* Needs to be an integration test because relies on complex interations between various queues
|
|
|
102 |
*/
|
|
|
103 |
@Test public void add_dedupeByCacheKey() throws Exception {
|
|
|
104 |
// Enqueue 2 requests with the same cache key. The first request takes 20ms. Assert that the
|
|
|
105 |
// second request is only handled after the first one has been parsed and delivered.
|
|
|
106 |
Request req1 = new MockRequest();
|
|
|
107 |
Request req2 = new MockRequest();
|
|
|
108 |
RequestFinishedListener listener = mock(RequestFinishedListener.class);
|
|
|
109 |
Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
|
|
|
110 |
@Override
|
|
|
111 |
public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
|
|
|
112 |
Thread.sleep(20);
|
|
|
113 |
return mock(NetworkResponse.class);
|
|
|
114 |
}
|
|
|
115 |
};
|
|
|
116 |
//delay only for first
|
|
|
117 |
when(mMockNetwork.performRequest(req1)).thenAnswer(delayAnswer);
|
|
|
118 |
when(mMockNetwork.performRequest(req2)).thenReturn(mock(NetworkResponse.class));
|
|
|
119 |
|
|
|
120 |
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 3, mDelivery);
|
|
|
121 |
queue.addRequestFinishedListener(listener);
|
|
|
122 |
queue.add(req1);
|
|
|
123 |
queue.add(req2);
|
|
|
124 |
queue.start();
|
|
|
125 |
|
|
|
126 |
// you cannot do strict order verification with mockito 1.9.5 :(
|
|
|
127 |
// as an alternative, first verify no requests have finished, then verify req1 goes through
|
|
|
128 |
verifyNoMoreInteractions(listener);
|
|
|
129 |
verify(listener, timeout(100)).onRequestFinished(req1);
|
|
|
130 |
verify(listener, timeout(10)).onRequestFinished(req2);
|
|
|
131 |
queue.stop();
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Verify RequestFinishedListeners are informed when requests are canceled
|
|
|
136 |
*
|
|
|
137 |
* Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
|
|
|
138 |
*/
|
|
|
139 |
@Test public void add_requestFinishedListenerCanceled() throws Exception {
|
|
|
140 |
RequestFinishedListener listener = mock(RequestFinishedListener.class);
|
|
|
141 |
Request request = new MockRequest();
|
|
|
142 |
Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
|
|
|
143 |
@Override
|
|
|
144 |
public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
|
|
|
145 |
Thread.sleep(200);
|
|
|
146 |
return mock(NetworkResponse.class);
|
|
|
147 |
}
|
|
|
148 |
};
|
|
|
149 |
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
|
|
|
150 |
|
|
|
151 |
when(mMockNetwork.performRequest(request)).thenAnswer(delayAnswer);
|
|
|
152 |
|
|
|
153 |
queue.addRequestFinishedListener(listener);
|
|
|
154 |
queue.start();
|
|
|
155 |
queue.add(request);
|
|
|
156 |
|
|
|
157 |
request.cancel();
|
|
|
158 |
verify(listener, timeout(100)).onRequestFinished(request);
|
|
|
159 |
queue.stop();
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Verify RequestFinishedListeners are informed when requests are successfully delivered
|
|
|
164 |
*
|
|
|
165 |
* Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
|
|
|
166 |
*/
|
|
|
167 |
@Test public void add_requestFinishedListenerSuccess() throws Exception {
|
|
|
168 |
NetworkResponse response = mock(NetworkResponse.class);
|
|
|
169 |
Request request = new MockRequest();
|
|
|
170 |
RequestFinishedListener listener = mock(RequestFinishedListener.class);
|
|
|
171 |
RequestFinishedListener listener2 = mock(RequestFinishedListener.class);
|
|
|
172 |
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
|
|
|
173 |
|
|
|
174 |
queue.addRequestFinishedListener(listener);
|
|
|
175 |
queue.addRequestFinishedListener(listener2);
|
|
|
176 |
queue.start();
|
|
|
177 |
queue.add(request);
|
|
|
178 |
|
|
|
179 |
verify(listener, timeout(100)).onRequestFinished(request);
|
|
|
180 |
verify(listener2, timeout(100)).onRequestFinished(request);
|
|
|
181 |
|
|
|
182 |
queue.stop();
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Verify RequestFinishedListeners are informed when request errors
|
|
|
187 |
*
|
|
|
188 |
* Needs to be an integration test because relies on Request -> dispatcher -> RequestQueue interaction
|
|
|
189 |
*/
|
|
|
190 |
@Test public void add_requestFinishedListenerError() throws Exception {
|
|
|
191 |
RequestFinishedListener listener = mock(RequestFinishedListener.class);
|
|
|
192 |
Request request = new MockRequest();
|
|
|
193 |
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
|
|
|
194 |
|
|
|
195 |
when(mMockNetwork.performRequest(request)).thenThrow(new VolleyError());
|
|
|
196 |
|
|
|
197 |
queue.addRequestFinishedListener(listener);
|
|
|
198 |
queue.start();
|
|
|
199 |
queue.add(request);
|
|
|
200 |
|
|
|
201 |
verify(listener, timeout(100)).onRequestFinished(request);
|
|
|
202 |
queue.stop();
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
}
|