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 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 @Override | 26 @Override |
27 protected void setUp() throws Exception { | 27 protected void setUp() throws Exception { |
28 super.setUp(); | 28 super.setUp(); |
29 launchCronetTestApp(); | 29 launchCronetTestApp(); |
30 ExecutorService executor = Executors.newSingleThreadExecutor(); | 30 ExecutorService executor = Executors.newSingleThreadExecutor(); |
31 List<byte[]> reads = Arrays.asList("hello".getBytes()); | 31 List<byte[]> reads = Arrays.asList("hello".getBytes()); |
32 mDataProvider = new TestDrivenDataProvider(executor, reads); | 32 mDataProvider = new TestDrivenDataProvider(executor, reads); |
33 mUploadDataStream = new CronetUploadDataStream(mDataProvider, executor); | 33 mUploadDataStream = new CronetUploadDataStream(mDataProvider, executor); |
34 mHandler = new TestUploadDataStreamHandler( | 34 mHandler = new TestUploadDataStreamHandler( |
35 mUploadDataStream.createAdapterForTesting()); | 35 mUploadDataStream.createUploadDataStreamForTesting()); |
36 } | 36 } |
37 | 37 |
38 @Override | 38 @Override |
39 protected void tearDown() throws Exception { | 39 protected void tearDown() throws Exception { |
40 // Destroy handler's native objects. | 40 // Destroy handler's native objects. |
41 mHandler.destroyNativeObjects(); | 41 mHandler.destroyNativeObjects(); |
42 super.tearDown(); | 42 super.tearDown(); |
43 } | 43 } |
44 | 44 |
45 /** | 45 /** |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 mHandler.checkInitCallbackNotInvoked(); | 216 mHandler.checkInitCallbackNotInvoked(); |
217 mDataProvider.onRewindSucceeded(mUploadDataStream); | 217 mDataProvider.onRewindSucceeded(mUploadDataStream); |
218 mHandler.waitForInitComplete(); | 218 mHandler.waitForInitComplete(); |
219 mDataProvider.assertRewindNotPending(); | 219 mDataProvider.assertRewindNotPending(); |
220 assertEquals(1, mDataProvider.getNumRewindCalls()); | 220 assertEquals(1, mDataProvider.getNumRewindCalls()); |
221 assertEquals(1, mDataProvider.getNumReadCalls()); | 221 assertEquals(1, mDataProvider.getNumReadCalls()); |
222 assertEquals("", mHandler.getData()); | 222 assertEquals("", mHandler.getData()); |
223 } | 223 } |
224 | 224 |
225 /** | 225 /** |
226 * Tests that there is no crash when native CronetUploadDataStreamAdapter is | 226 * Tests that there is no crash when native CronetUploadDataStream is |
227 * destroyed while read is pending. The test is racy since read could | 227 * destroyed while read is pending. The test is racy since the read could |
228 * complete either before or after onDestroyAdapter() is called in | 228 * complete either before or after the Java CronetUploadDataStream's |
229 * CronetUploadDataStream. However, the test should pass either way, though | 229 * onDestroyUploadDataStream() method is invoked. However, the test should |
230 * we are interested in the latter case. | 230 * pass either way, though we are interested in the latter case. |
231 */ | 231 */ |
232 @SmallTest | 232 @SmallTest |
233 @Feature({"Cronet"}) | 233 @Feature({"Cronet"}) |
234 public void testDestroyAdapterBeforeReadComplete() | 234 public void testDestroyNativeStreamBeforeReadComplete() |
235 throws Exception { | 235 throws Exception { |
236 // Start a read and wait for it to be pending. | 236 // Start a read and wait for it to be pending. |
237 assertTrue(mHandler.init()); | 237 assertTrue(mHandler.init()); |
238 mHandler.read(); | 238 mHandler.read(); |
239 mDataProvider.waitForReadRequest(); | 239 mDataProvider.waitForReadRequest(); |
240 mHandler.checkReadCallbackNotInvoked(); | 240 mHandler.checkReadCallbackNotInvoked(); |
241 | 241 |
242 // Destroy the C++ TestUploadDataStreamHandler. The handler owns the | 242 // Destroy the C++ TestUploadDataStreamHandler. The handler will then |
243 // CronetUploadDataStreamAdapter, which this will cause it to destroy on | 243 // destroy the C++ CronetUploadDataStream it owns on the network thread. |
244 // the network thread. Destroying the adapter will result in calling | 244 // That will result in calling the Java CronetUploadDataSteam's |
245 // the CronetUploadDataSteam's onAdapterDestroyed() method on its | 245 // onCanceled() method on its executor thread, which will then destroy |
246 // executor thread, which will then destroy the | 246 // the CronetUploadDataStreamDelegate. |
247 // CronetUploadDataStreamDelegate. | |
248 mHandler.destroyNativeObjects(); | 247 mHandler.destroyNativeObjects(); |
249 | 248 |
250 // Make the read complete should not encounter a crash. | 249 // Make the read complete should not encounter a crash. |
251 mDataProvider.onReadSucceeded(mUploadDataStream); | 250 mDataProvider.onReadSucceeded(mUploadDataStream); |
252 | 251 |
253 assertEquals(0, mDataProvider.getNumRewindCalls()); | 252 assertEquals(0, mDataProvider.getNumRewindCalls()); |
254 assertEquals(1, mDataProvider.getNumReadCalls()); | 253 assertEquals(1, mDataProvider.getNumReadCalls()); |
255 } | 254 } |
256 | 255 |
257 /** | 256 /** |
258 * Tests that there is no crash when native CronetUploadDataStreamAdapter is | 257 * Tests that there is no crash when native CronetUploadDataStream is |
259 * destroyed while rewind is pending. The test is racy since rewind could | 258 * destroyed while rewind is pending. The test is racy since rewind could |
260 * complete either before or after onDestroyAdapter() is called in | 259 * complete either before or after the Java CronetUploadDataStream's |
261 * CronetUploadDataStream. However, the test should pass either way, though | 260 * onDestroyUploadDataStream() method is invoked. However, the test should |
262 * we are interested in the latter case. | 261 * pass either way, though we are interested in the latter case. |
263 */ | 262 */ |
264 @SmallTest | 263 @SmallTest |
265 @Feature({"Cronet"}) | 264 @Feature({"Cronet"}) |
266 public void testDestroyAdapterBeforeRewindComplete() | 265 public void testDestroyNativeStreamBeforeRewindComplete() |
267 throws Exception { | 266 throws Exception { |
268 // Start a read and wait for it to complete. | 267 // Start a read and wait for it to complete. |
269 assertTrue(mHandler.init()); | 268 assertTrue(mHandler.init()); |
270 mHandler.read(); | 269 mHandler.read(); |
271 mDataProvider.waitForReadRequest(); | 270 mDataProvider.waitForReadRequest(); |
272 mHandler.checkReadCallbackNotInvoked(); | 271 mHandler.checkReadCallbackNotInvoked(); |
273 mDataProvider.onReadSucceeded(mUploadDataStream); | 272 mDataProvider.onReadSucceeded(mUploadDataStream); |
274 mHandler.waitForReadComplete(); | 273 mHandler.waitForReadComplete(); |
275 mDataProvider.assertReadNotPending(); | 274 mDataProvider.assertReadNotPending(); |
276 assertEquals(0, mDataProvider.getNumRewindCalls()); | 275 assertEquals(0, mDataProvider.getNumRewindCalls()); |
277 assertEquals(1, mDataProvider.getNumReadCalls()); | 276 assertEquals(1, mDataProvider.getNumReadCalls()); |
278 assertEquals("hello", mHandler.getData()); | 277 assertEquals("hello", mHandler.getData()); |
279 | 278 |
280 // Reset and then init, which should trigger a rewind. | 279 // Reset and then init, which should trigger a rewind. |
281 mHandler.reset(); | 280 mHandler.reset(); |
282 assertEquals("", mHandler.getData()); | 281 assertEquals("", mHandler.getData()); |
283 assertFalse(mHandler.init()); | 282 assertFalse(mHandler.init()); |
284 mDataProvider.waitForRewindRequest(); | 283 mDataProvider.waitForRewindRequest(); |
285 mHandler.checkInitCallbackNotInvoked(); | 284 mHandler.checkInitCallbackNotInvoked(); |
286 | 285 |
287 // Destroy the C++ TestUploadDataStreamHandler. The handler owns the | 286 // Destroy the C++ TestUploadDataStreamHandler. The handler will then |
288 // CronetUploadDataStreamAdapter, which this will cause it to destroy on | 287 // destroy the C++ CronetUploadDataStream it owns on the network thread. |
289 // the network thread. Destroying the adapter will result in calling | 288 // That will result in calling the Java CronetUploadDataSteam's |
290 // the CronetUploadDataSteam's onAdapterDestroyed() method on its | 289 // onCanceled() method on its executor thread, which will then destroy |
291 // executor thread, which will then destroy the | 290 // the CronetUploadDataStreamDelegate. |
292 // CronetUploadDataStreamDelegate. | |
293 mHandler.destroyNativeObjects(); | 291 mHandler.destroyNativeObjects(); |
294 | 292 |
295 // Signal rewind completes, and wait for init to complete. | 293 // Signal rewind completes, and wait for init to complete. |
296 mDataProvider.onRewindSucceeded(mUploadDataStream); | 294 mDataProvider.onRewindSucceeded(mUploadDataStream); |
297 | 295 |
298 assertEquals(1, mDataProvider.getNumRewindCalls()); | 296 assertEquals(1, mDataProvider.getNumRewindCalls()); |
299 assertEquals(1, mDataProvider.getNumReadCalls()); | 297 assertEquals(1, mDataProvider.getNumReadCalls()); |
300 } | 298 } |
301 } | 299 } |
OLD | NEW |