OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
8 | 8 |
9 import org.chromium.base.test.util.Feature; | 9 import org.chromium.base.test.util.Feature; |
10 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; | |
10 | 11 |
11 import java.util.Arrays; | 12 import java.util.Arrays; |
12 import java.util.List; | 13 import java.util.List; |
13 import java.util.concurrent.ExecutorService; | 14 import java.util.concurrent.ExecutorService; |
14 import java.util.concurrent.Executors; | 15 import java.util.concurrent.Executors; |
15 | 16 |
16 /** | 17 /** |
17 * Tests that directly drive {@code CronetUploadDataStream} and | 18 * Tests that directly drive {@code CronetUploadDataStream} and |
18 * {@code UploadDataProvider} to simulate different ordering of reset, init, | 19 * {@code UploadDataProvider} to simulate different ordering of reset, init, |
19 * read, and rewind calls. | 20 * read, and rewind calls. |
21 * | |
pauljensen
2015/12/29 16:35:53
why'd you add an empty line?
Charles
2016/01/05 21:53:56
Done.
| |
20 */ | 22 */ |
21 public class CronetUploadTest extends CronetTestBase { | 23 public class CronetUploadTest extends CronetTestBase { |
22 private TestDrivenDataProvider mDataProvider; | 24 private TestDrivenDataProvider mDataProvider; |
23 private CronetUploadDataStream mUploadDataStream; | 25 private CronetUploadDataStream mUploadDataStream; |
24 private TestUploadDataStreamHandler mHandler; | 26 private TestUploadDataStreamHandler mHandler; |
25 | 27 |
26 @Override | 28 @Override |
27 @SuppressWarnings("PrimitiveArrayPassedToVarargsMethod") | 29 @SuppressWarnings("PrimitiveArrayPassedToVarargsMethod") |
28 protected void setUp() throws Exception { | 30 protected void setUp() throws Exception { |
29 super.setUp(); | 31 super.setUp(); |
(...skipping 12 matching lines...) Expand all Loading... | |
42 mHandler.destroyNativeObjects(); | 44 mHandler.destroyNativeObjects(); |
43 super.tearDown(); | 45 super.tearDown(); |
44 } | 46 } |
45 | 47 |
46 /** | 48 /** |
47 * Tests that after some data is read, init triggers a rewind, and that | 49 * Tests that after some data is read, init triggers a rewind, and that |
48 * before the rewind completes, init blocks. | 50 * before the rewind completes, init blocks. |
49 */ | 51 */ |
50 @SmallTest | 52 @SmallTest |
51 @Feature({"Cronet"}) | 53 @Feature({"Cronet"}) |
52 public void testInitTriggersRewindAndInitBeforeRewindCompletes() | 54 @OnlyRunNativeCronet |
53 throws Exception { | 55 public void testInitTriggersRewindAndInitBeforeRewindCompletes() throws Exce ption { |
54 // Init completes synchronously and read succeeds. | 56 // Init completes synchronously and read succeeds. |
55 assertTrue(mHandler.init()); | 57 assertTrue(mHandler.init()); |
56 mHandler.read(); | 58 mHandler.read(); |
57 mDataProvider.waitForReadRequest(); | 59 mDataProvider.waitForReadRequest(); |
58 mHandler.checkReadCallbackNotInvoked(); | 60 mHandler.checkReadCallbackNotInvoked(); |
59 mDataProvider.onReadSucceeded(mUploadDataStream); | 61 mDataProvider.onReadSucceeded(mUploadDataStream); |
60 mHandler.waitForReadComplete(); | 62 mHandler.waitForReadComplete(); |
61 mDataProvider.assertReadNotPending(); | 63 mDataProvider.assertReadNotPending(); |
62 assertEquals(0, mDataProvider.getNumRewindCalls()); | 64 assertEquals(0, mDataProvider.getNumRewindCalls()); |
63 assertEquals(1, mDataProvider.getNumReadCalls()); | 65 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 27 matching lines...) Expand all Loading... | |
91 assertEquals(2, mDataProvider.getNumReadCalls()); | 93 assertEquals(2, mDataProvider.getNumReadCalls()); |
92 assertEquals("hello", mHandler.getData()); | 94 assertEquals("hello", mHandler.getData()); |
93 } | 95 } |
94 | 96 |
95 /** | 97 /** |
96 * Tests that after some data is read, init triggers a rewind, and that | 98 * Tests that after some data is read, init triggers a rewind, and that |
97 * after the rewind completes, init does not block. | 99 * after the rewind completes, init does not block. |
98 */ | 100 */ |
99 @SmallTest | 101 @SmallTest |
100 @Feature({"Cronet"}) | 102 @Feature({"Cronet"}) |
101 public void testInitTriggersRewindAndInitAfterRewindCompletes() | 103 @OnlyRunNativeCronet |
102 throws Exception { | 104 public void testInitTriggersRewindAndInitAfterRewindCompletes() throws Excep tion { |
103 // Init completes synchronously and read succeeds. | 105 // Init completes synchronously and read succeeds. |
104 assertTrue(mHandler.init()); | 106 assertTrue(mHandler.init()); |
105 mHandler.read(); | 107 mHandler.read(); |
106 mDataProvider.waitForReadRequest(); | 108 mDataProvider.waitForReadRequest(); |
107 mHandler.checkReadCallbackNotInvoked(); | 109 mHandler.checkReadCallbackNotInvoked(); |
108 mDataProvider.onReadSucceeded(mUploadDataStream); | 110 mDataProvider.onReadSucceeded(mUploadDataStream); |
109 mHandler.waitForReadComplete(); | 111 mHandler.waitForReadComplete(); |
110 mDataProvider.assertReadNotPending(); | 112 mDataProvider.assertReadNotPending(); |
111 assertEquals(0, mDataProvider.getNumRewindCalls()); | 113 assertEquals(0, mDataProvider.getNumRewindCalls()); |
112 assertEquals(1, mDataProvider.getNumReadCalls()); | 114 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 26 matching lines...) Expand all Loading... | |
139 assertEquals(2, mDataProvider.getNumReadCalls()); | 141 assertEquals(2, mDataProvider.getNumReadCalls()); |
140 assertEquals("hello", mHandler.getData()); | 142 assertEquals("hello", mHandler.getData()); |
141 } | 143 } |
142 | 144 |
143 /** | 145 /** |
144 * Tests that if init before read completes, a rewind is triggered when | 146 * Tests that if init before read completes, a rewind is triggered when |
145 * read completes. | 147 * read completes. |
146 */ | 148 */ |
147 @SmallTest | 149 @SmallTest |
148 @Feature({"Cronet"}) | 150 @Feature({"Cronet"}) |
151 @OnlyRunNativeCronet | |
149 public void testReadCompleteTriggerRewind() throws Exception { | 152 public void testReadCompleteTriggerRewind() throws Exception { |
150 // Reset and init before read completes. | 153 // Reset and init before read completes. |
151 assertTrue(mHandler.init()); | 154 assertTrue(mHandler.init()); |
152 mHandler.read(); | 155 mHandler.read(); |
153 mDataProvider.waitForReadRequest(); | 156 mDataProvider.waitForReadRequest(); |
154 mHandler.checkReadCallbackNotInvoked(); | 157 mHandler.checkReadCallbackNotInvoked(); |
155 mHandler.reset(); | 158 mHandler.reset(); |
156 // Init should return asynchronously, since there is a pending read. | 159 // Init should return asynchronously, since there is a pending read. |
157 assertFalse(mHandler.init()); | 160 assertFalse(mHandler.init()); |
158 mDataProvider.assertRewindNotPending(); | 161 mDataProvider.assertRewindNotPending(); |
(...skipping 14 matching lines...) Expand all Loading... | |
173 assertEquals("", mHandler.getData()); | 176 assertEquals("", mHandler.getData()); |
174 } | 177 } |
175 | 178 |
176 /** | 179 /** |
177 * Tests that when init again after rewind completes, no additional rewind | 180 * Tests that when init again after rewind completes, no additional rewind |
178 * is triggered. This test is the same as testReadCompleteTriggerRewind | 181 * is triggered. This test is the same as testReadCompleteTriggerRewind |
179 * except that this test invokes reset and init again in the end. | 182 * except that this test invokes reset and init again in the end. |
180 */ | 183 */ |
181 @SmallTest | 184 @SmallTest |
182 @Feature({"Cronet"}) | 185 @Feature({"Cronet"}) |
186 @OnlyRunNativeCronet | |
183 public void testReadCompleteTriggerRewindOnlyOneRewind() throws Exception { | 187 public void testReadCompleteTriggerRewindOnlyOneRewind() throws Exception { |
184 testReadCompleteTriggerRewind(); | 188 testReadCompleteTriggerRewind(); |
185 // Reset and Init again, no rewind should happen. | 189 // Reset and Init again, no rewind should happen. |
186 mHandler.reset(); | 190 mHandler.reset(); |
187 assertTrue(mHandler.init()); | 191 assertTrue(mHandler.init()); |
188 mDataProvider.assertRewindNotPending(); | 192 mDataProvider.assertRewindNotPending(); |
189 assertEquals(1, mDataProvider.getNumRewindCalls()); | 193 assertEquals(1, mDataProvider.getNumRewindCalls()); |
190 assertEquals(1, mDataProvider.getNumReadCalls()); | 194 assertEquals(1, mDataProvider.getNumReadCalls()); |
191 assertEquals("", mHandler.getData()); | 195 assertEquals("", mHandler.getData()); |
192 } | 196 } |
193 | 197 |
194 /** | 198 /** |
195 * Tests that if reset before read completes, no rewind is triggered, and | 199 * Tests that if reset before read completes, no rewind is triggered, and |
196 * that a following init triggers rewind. | 200 * that a following init triggers rewind. |
197 */ | 201 */ |
198 @SmallTest | 202 @SmallTest |
199 @Feature({"Cronet"}) | 203 @Feature({"Cronet"}) |
200 public void testResetBeforeReadCompleteAndInitTriggerRewind() | 204 @OnlyRunNativeCronet |
201 throws Exception { | 205 public void testResetBeforeReadCompleteAndInitTriggerRewind() throws Excepti on { |
202 // Reset before read completes. Rewind is not triggered. | 206 // Reset before read completes. Rewind is not triggered. |
203 assertTrue(mHandler.init()); | 207 assertTrue(mHandler.init()); |
204 mHandler.read(); | 208 mHandler.read(); |
205 mDataProvider.waitForReadRequest(); | 209 mDataProvider.waitForReadRequest(); |
206 mHandler.checkReadCallbackNotInvoked(); | 210 mHandler.checkReadCallbackNotInvoked(); |
207 mHandler.reset(); | 211 mHandler.reset(); |
208 mDataProvider.onReadSucceeded(mUploadDataStream); | 212 mDataProvider.onReadSucceeded(mUploadDataStream); |
209 mDataProvider.assertRewindNotPending(); | 213 mDataProvider.assertRewindNotPending(); |
210 assertEquals(0, mDataProvider.getNumRewindCalls()); | 214 assertEquals(0, mDataProvider.getNumRewindCalls()); |
211 assertEquals(1, mDataProvider.getNumReadCalls()); | 215 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 13 matching lines...) Expand all Loading... | |
225 | 229 |
226 /** | 230 /** |
227 * Tests that there is no crash when native CronetUploadDataStream is | 231 * Tests that there is no crash when native CronetUploadDataStream is |
228 * destroyed while read is pending. The test is racy since the read could | 232 * destroyed while read is pending. The test is racy since the read could |
229 * complete either before or after the Java CronetUploadDataStream's | 233 * complete either before or after the Java CronetUploadDataStream's |
230 * onDestroyUploadDataStream() method is invoked. However, the test should | 234 * onDestroyUploadDataStream() method is invoked. However, the test should |
231 * pass either way, though we are interested in the latter case. | 235 * pass either way, though we are interested in the latter case. |
232 */ | 236 */ |
233 @SmallTest | 237 @SmallTest |
234 @Feature({"Cronet"}) | 238 @Feature({"Cronet"}) |
235 public void testDestroyNativeStreamBeforeReadComplete() | 239 @OnlyRunNativeCronet |
236 throws Exception { | 240 public void testDestroyNativeStreamBeforeReadComplete() throws Exception { |
237 // Start a read and wait for it to be pending. | 241 // Start a read and wait for it to be pending. |
238 assertTrue(mHandler.init()); | 242 assertTrue(mHandler.init()); |
239 mHandler.read(); | 243 mHandler.read(); |
240 mDataProvider.waitForReadRequest(); | 244 mDataProvider.waitForReadRequest(); |
241 mHandler.checkReadCallbackNotInvoked(); | 245 mHandler.checkReadCallbackNotInvoked(); |
242 | 246 |
243 // Destroy the C++ TestUploadDataStreamHandler. The handler will then | 247 // Destroy the C++ TestUploadDataStreamHandler. The handler will then |
244 // destroy the C++ CronetUploadDataStream it owns on the network thread. | 248 // destroy the C++ CronetUploadDataStream it owns on the network thread. |
245 // That will result in calling the Java CronetUploadDataSteam's | 249 // That will result in calling the Java CronetUploadDataSteam's |
246 // onUploadDataStreamDestroyed() method on its executor thread, which | 250 // onUploadDataStreamDestroyed() method on its executor thread, which |
247 // will then destroy the CronetUploadDataStreamAdapter. | 251 // will then destroy the CronetUploadDataStreamAdapter. |
248 mHandler.destroyNativeObjects(); | 252 mHandler.destroyNativeObjects(); |
249 | 253 |
250 // Make the read complete should not encounter a crash. | 254 // Make the read complete should not encounter a crash. |
251 mDataProvider.onReadSucceeded(mUploadDataStream); | 255 mDataProvider.onReadSucceeded(mUploadDataStream); |
252 | 256 |
253 assertEquals(0, mDataProvider.getNumRewindCalls()); | 257 assertEquals(0, mDataProvider.getNumRewindCalls()); |
254 assertEquals(1, mDataProvider.getNumReadCalls()); | 258 assertEquals(1, mDataProvider.getNumReadCalls()); |
255 } | 259 } |
256 | 260 |
257 /** | 261 /** |
258 * Tests that there is no crash when native CronetUploadDataStream is | 262 * Tests that there is no crash when native CronetUploadDataStream is |
259 * destroyed while rewind is pending. The test is racy since rewind could | 263 * destroyed while rewind is pending. The test is racy since rewind could |
260 * complete either before or after the Java CronetUploadDataStream's | 264 * complete either before or after the Java CronetUploadDataStream's |
261 * onDestroyUploadDataStream() method is invoked. However, the test should | 265 * onDestroyUploadDataStream() method is invoked. However, the test should |
262 * pass either way, though we are interested in the latter case. | 266 * pass either way, though we are interested in the latter case. |
263 */ | 267 */ |
264 @SmallTest | 268 @SmallTest |
265 @Feature({"Cronet"}) | 269 @Feature({"Cronet"}) |
266 public void testDestroyNativeStreamBeforeRewindComplete() | 270 @OnlyRunNativeCronet |
267 throws Exception { | 271 public void testDestroyNativeStreamBeforeRewindComplete() throws Exception { |
268 // Start a read and wait for it to complete. | 272 // Start a read and wait for it to complete. |
269 assertTrue(mHandler.init()); | 273 assertTrue(mHandler.init()); |
270 mHandler.read(); | 274 mHandler.read(); |
271 mDataProvider.waitForReadRequest(); | 275 mDataProvider.waitForReadRequest(); |
272 mHandler.checkReadCallbackNotInvoked(); | 276 mHandler.checkReadCallbackNotInvoked(); |
273 mDataProvider.onReadSucceeded(mUploadDataStream); | 277 mDataProvider.onReadSucceeded(mUploadDataStream); |
274 mHandler.waitForReadComplete(); | 278 mHandler.waitForReadComplete(); |
275 mDataProvider.assertReadNotPending(); | 279 mDataProvider.assertReadNotPending(); |
276 assertEquals(0, mDataProvider.getNumRewindCalls()); | 280 assertEquals(0, mDataProvider.getNumRewindCalls()); |
277 assertEquals(1, mDataProvider.getNumReadCalls()); | 281 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 13 matching lines...) Expand all Loading... | |
291 // will then destroy the CronetUploadDataStreamAdapter. | 295 // will then destroy the CronetUploadDataStreamAdapter. |
292 mHandler.destroyNativeObjects(); | 296 mHandler.destroyNativeObjects(); |
293 | 297 |
294 // Signal rewind completes, and wait for init to complete. | 298 // Signal rewind completes, and wait for init to complete. |
295 mDataProvider.onRewindSucceeded(mUploadDataStream); | 299 mDataProvider.onRewindSucceeded(mUploadDataStream); |
296 | 300 |
297 assertEquals(1, mDataProvider.getNumRewindCalls()); | 301 assertEquals(1, mDataProvider.getNumRewindCalls()); |
298 assertEquals(1, mDataProvider.getNumReadCalls()); | 302 assertEquals(1, mDataProvider.getNumReadCalls()); |
299 } | 303 } |
300 } | 304 } |
OLD | NEW |