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. |
(...skipping 22 matching lines...) Expand all Loading... |
42 mHandler.destroyNativeObjects(); | 43 mHandler.destroyNativeObjects(); |
43 super.tearDown(); | 44 super.tearDown(); |
44 } | 45 } |
45 | 46 |
46 /** | 47 /** |
47 * Tests that after some data is read, init triggers a rewind, and that | 48 * Tests that after some data is read, init triggers a rewind, and that |
48 * before the rewind completes, init blocks. | 49 * before the rewind completes, init blocks. |
49 */ | 50 */ |
50 @SmallTest | 51 @SmallTest |
51 @Feature({"Cronet"}) | 52 @Feature({"Cronet"}) |
52 public void testInitTriggersRewindAndInitBeforeRewindCompletes() | 53 @OnlyRunNativeCronet |
53 throws Exception { | 54 public void testInitTriggersRewindAndInitBeforeRewindCompletes() throws Exce
ption { |
54 // Init completes synchronously and read succeeds. | 55 // Init completes synchronously and read succeeds. |
55 assertTrue(mHandler.init()); | 56 assertTrue(mHandler.init()); |
56 mHandler.read(); | 57 mHandler.read(); |
57 mDataProvider.waitForReadRequest(); | 58 mDataProvider.waitForReadRequest(); |
58 mHandler.checkReadCallbackNotInvoked(); | 59 mHandler.checkReadCallbackNotInvoked(); |
59 mDataProvider.onReadSucceeded(mUploadDataStream); | 60 mDataProvider.onReadSucceeded(mUploadDataStream); |
60 mHandler.waitForReadComplete(); | 61 mHandler.waitForReadComplete(); |
61 mDataProvider.assertReadNotPending(); | 62 mDataProvider.assertReadNotPending(); |
62 assertEquals(0, mDataProvider.getNumRewindCalls()); | 63 assertEquals(0, mDataProvider.getNumRewindCalls()); |
63 assertEquals(1, mDataProvider.getNumReadCalls()); | 64 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 27 matching lines...) Expand all Loading... |
91 assertEquals(2, mDataProvider.getNumReadCalls()); | 92 assertEquals(2, mDataProvider.getNumReadCalls()); |
92 assertEquals("hello", mHandler.getData()); | 93 assertEquals("hello", mHandler.getData()); |
93 } | 94 } |
94 | 95 |
95 /** | 96 /** |
96 * Tests that after some data is read, init triggers a rewind, and that | 97 * Tests that after some data is read, init triggers a rewind, and that |
97 * after the rewind completes, init does not block. | 98 * after the rewind completes, init does not block. |
98 */ | 99 */ |
99 @SmallTest | 100 @SmallTest |
100 @Feature({"Cronet"}) | 101 @Feature({"Cronet"}) |
101 public void testInitTriggersRewindAndInitAfterRewindCompletes() | 102 @OnlyRunNativeCronet |
102 throws Exception { | 103 public void testInitTriggersRewindAndInitAfterRewindCompletes() throws Excep
tion { |
103 // Init completes synchronously and read succeeds. | 104 // Init completes synchronously and read succeeds. |
104 assertTrue(mHandler.init()); | 105 assertTrue(mHandler.init()); |
105 mHandler.read(); | 106 mHandler.read(); |
106 mDataProvider.waitForReadRequest(); | 107 mDataProvider.waitForReadRequest(); |
107 mHandler.checkReadCallbackNotInvoked(); | 108 mHandler.checkReadCallbackNotInvoked(); |
108 mDataProvider.onReadSucceeded(mUploadDataStream); | 109 mDataProvider.onReadSucceeded(mUploadDataStream); |
109 mHandler.waitForReadComplete(); | 110 mHandler.waitForReadComplete(); |
110 mDataProvider.assertReadNotPending(); | 111 mDataProvider.assertReadNotPending(); |
111 assertEquals(0, mDataProvider.getNumRewindCalls()); | 112 assertEquals(0, mDataProvider.getNumRewindCalls()); |
112 assertEquals(1, mDataProvider.getNumReadCalls()); | 113 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 26 matching lines...) Expand all Loading... |
139 assertEquals(2, mDataProvider.getNumReadCalls()); | 140 assertEquals(2, mDataProvider.getNumReadCalls()); |
140 assertEquals("hello", mHandler.getData()); | 141 assertEquals("hello", mHandler.getData()); |
141 } | 142 } |
142 | 143 |
143 /** | 144 /** |
144 * Tests that if init before read completes, a rewind is triggered when | 145 * Tests that if init before read completes, a rewind is triggered when |
145 * read completes. | 146 * read completes. |
146 */ | 147 */ |
147 @SmallTest | 148 @SmallTest |
148 @Feature({"Cronet"}) | 149 @Feature({"Cronet"}) |
| 150 @OnlyRunNativeCronet |
149 public void testReadCompleteTriggerRewind() throws Exception { | 151 public void testReadCompleteTriggerRewind() throws Exception { |
150 // Reset and init before read completes. | 152 // Reset and init before read completes. |
151 assertTrue(mHandler.init()); | 153 assertTrue(mHandler.init()); |
152 mHandler.read(); | 154 mHandler.read(); |
153 mDataProvider.waitForReadRequest(); | 155 mDataProvider.waitForReadRequest(); |
154 mHandler.checkReadCallbackNotInvoked(); | 156 mHandler.checkReadCallbackNotInvoked(); |
155 mHandler.reset(); | 157 mHandler.reset(); |
156 // Init should return asynchronously, since there is a pending read. | 158 // Init should return asynchronously, since there is a pending read. |
157 assertFalse(mHandler.init()); | 159 assertFalse(mHandler.init()); |
158 mDataProvider.assertRewindNotPending(); | 160 mDataProvider.assertRewindNotPending(); |
(...skipping 14 matching lines...) Expand all Loading... |
173 assertEquals("", mHandler.getData()); | 175 assertEquals("", mHandler.getData()); |
174 } | 176 } |
175 | 177 |
176 /** | 178 /** |
177 * Tests that when init again after rewind completes, no additional rewind | 179 * Tests that when init again after rewind completes, no additional rewind |
178 * is triggered. This test is the same as testReadCompleteTriggerRewind | 180 * is triggered. This test is the same as testReadCompleteTriggerRewind |
179 * except that this test invokes reset and init again in the end. | 181 * except that this test invokes reset and init again in the end. |
180 */ | 182 */ |
181 @SmallTest | 183 @SmallTest |
182 @Feature({"Cronet"}) | 184 @Feature({"Cronet"}) |
| 185 @OnlyRunNativeCronet |
183 public void testReadCompleteTriggerRewindOnlyOneRewind() throws Exception { | 186 public void testReadCompleteTriggerRewindOnlyOneRewind() throws Exception { |
184 testReadCompleteTriggerRewind(); | 187 testReadCompleteTriggerRewind(); |
185 // Reset and Init again, no rewind should happen. | 188 // Reset and Init again, no rewind should happen. |
186 mHandler.reset(); | 189 mHandler.reset(); |
187 assertTrue(mHandler.init()); | 190 assertTrue(mHandler.init()); |
188 mDataProvider.assertRewindNotPending(); | 191 mDataProvider.assertRewindNotPending(); |
189 assertEquals(1, mDataProvider.getNumRewindCalls()); | 192 assertEquals(1, mDataProvider.getNumRewindCalls()); |
190 assertEquals(1, mDataProvider.getNumReadCalls()); | 193 assertEquals(1, mDataProvider.getNumReadCalls()); |
191 assertEquals("", mHandler.getData()); | 194 assertEquals("", mHandler.getData()); |
192 } | 195 } |
193 | 196 |
194 /** | 197 /** |
195 * Tests that if reset before read completes, no rewind is triggered, and | 198 * Tests that if reset before read completes, no rewind is triggered, and |
196 * that a following init triggers rewind. | 199 * that a following init triggers rewind. |
197 */ | 200 */ |
198 @SmallTest | 201 @SmallTest |
199 @Feature({"Cronet"}) | 202 @Feature({"Cronet"}) |
200 public void testResetBeforeReadCompleteAndInitTriggerRewind() | 203 @OnlyRunNativeCronet |
201 throws Exception { | 204 public void testResetBeforeReadCompleteAndInitTriggerRewind() throws Excepti
on { |
202 // Reset before read completes. Rewind is not triggered. | 205 // Reset before read completes. Rewind is not triggered. |
203 assertTrue(mHandler.init()); | 206 assertTrue(mHandler.init()); |
204 mHandler.read(); | 207 mHandler.read(); |
205 mDataProvider.waitForReadRequest(); | 208 mDataProvider.waitForReadRequest(); |
206 mHandler.checkReadCallbackNotInvoked(); | 209 mHandler.checkReadCallbackNotInvoked(); |
207 mHandler.reset(); | 210 mHandler.reset(); |
208 mDataProvider.onReadSucceeded(mUploadDataStream); | 211 mDataProvider.onReadSucceeded(mUploadDataStream); |
209 mDataProvider.assertRewindNotPending(); | 212 mDataProvider.assertRewindNotPending(); |
210 assertEquals(0, mDataProvider.getNumRewindCalls()); | 213 assertEquals(0, mDataProvider.getNumRewindCalls()); |
211 assertEquals(1, mDataProvider.getNumReadCalls()); | 214 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 13 matching lines...) Expand all Loading... |
225 | 228 |
226 /** | 229 /** |
227 * Tests that there is no crash when native CronetUploadDataStream is | 230 * Tests that there is no crash when native CronetUploadDataStream is |
228 * destroyed while read is pending. The test is racy since the read could | 231 * destroyed while read is pending. The test is racy since the read could |
229 * complete either before or after the Java CronetUploadDataStream's | 232 * complete either before or after the Java CronetUploadDataStream's |
230 * onDestroyUploadDataStream() method is invoked. However, the test should | 233 * onDestroyUploadDataStream() method is invoked. However, the test should |
231 * pass either way, though we are interested in the latter case. | 234 * pass either way, though we are interested in the latter case. |
232 */ | 235 */ |
233 @SmallTest | 236 @SmallTest |
234 @Feature({"Cronet"}) | 237 @Feature({"Cronet"}) |
235 public void testDestroyNativeStreamBeforeReadComplete() | 238 @OnlyRunNativeCronet |
236 throws Exception { | 239 public void testDestroyNativeStreamBeforeReadComplete() throws Exception { |
237 // Start a read and wait for it to be pending. | 240 // Start a read and wait for it to be pending. |
238 assertTrue(mHandler.init()); | 241 assertTrue(mHandler.init()); |
239 mHandler.read(); | 242 mHandler.read(); |
240 mDataProvider.waitForReadRequest(); | 243 mDataProvider.waitForReadRequest(); |
241 mHandler.checkReadCallbackNotInvoked(); | 244 mHandler.checkReadCallbackNotInvoked(); |
242 | 245 |
243 // Destroy the C++ TestUploadDataStreamHandler. The handler will then | 246 // Destroy the C++ TestUploadDataStreamHandler. The handler will then |
244 // destroy the C++ CronetUploadDataStream it owns on the network thread. | 247 // destroy the C++ CronetUploadDataStream it owns on the network thread. |
245 // That will result in calling the Java CronetUploadDataSteam's | 248 // That will result in calling the Java CronetUploadDataSteam's |
246 // onUploadDataStreamDestroyed() method on its executor thread, which | 249 // onUploadDataStreamDestroyed() method on its executor thread, which |
247 // will then destroy the CronetUploadDataStreamAdapter. | 250 // will then destroy the CronetUploadDataStreamAdapter. |
248 mHandler.destroyNativeObjects(); | 251 mHandler.destroyNativeObjects(); |
249 | 252 |
250 // Make the read complete should not encounter a crash. | 253 // Make the read complete should not encounter a crash. |
251 mDataProvider.onReadSucceeded(mUploadDataStream); | 254 mDataProvider.onReadSucceeded(mUploadDataStream); |
252 | 255 |
253 assertEquals(0, mDataProvider.getNumRewindCalls()); | 256 assertEquals(0, mDataProvider.getNumRewindCalls()); |
254 assertEquals(1, mDataProvider.getNumReadCalls()); | 257 assertEquals(1, mDataProvider.getNumReadCalls()); |
255 } | 258 } |
256 | 259 |
257 /** | 260 /** |
258 * Tests that there is no crash when native CronetUploadDataStream is | 261 * Tests that there is no crash when native CronetUploadDataStream is |
259 * destroyed while rewind is pending. The test is racy since rewind could | 262 * destroyed while rewind is pending. The test is racy since rewind could |
260 * complete either before or after the Java CronetUploadDataStream's | 263 * complete either before or after the Java CronetUploadDataStream's |
261 * onDestroyUploadDataStream() method is invoked. However, the test should | 264 * onDestroyUploadDataStream() method is invoked. However, the test should |
262 * pass either way, though we are interested in the latter case. | 265 * pass either way, though we are interested in the latter case. |
263 */ | 266 */ |
264 @SmallTest | 267 @SmallTest |
265 @Feature({"Cronet"}) | 268 @Feature({"Cronet"}) |
266 public void testDestroyNativeStreamBeforeRewindComplete() | 269 @OnlyRunNativeCronet |
267 throws Exception { | 270 public void testDestroyNativeStreamBeforeRewindComplete() throws Exception { |
268 // Start a read and wait for it to complete. | 271 // Start a read and wait for it to complete. |
269 assertTrue(mHandler.init()); | 272 assertTrue(mHandler.init()); |
270 mHandler.read(); | 273 mHandler.read(); |
271 mDataProvider.waitForReadRequest(); | 274 mDataProvider.waitForReadRequest(); |
272 mHandler.checkReadCallbackNotInvoked(); | 275 mHandler.checkReadCallbackNotInvoked(); |
273 mDataProvider.onReadSucceeded(mUploadDataStream); | 276 mDataProvider.onReadSucceeded(mUploadDataStream); |
274 mHandler.waitForReadComplete(); | 277 mHandler.waitForReadComplete(); |
275 mDataProvider.assertReadNotPending(); | 278 mDataProvider.assertReadNotPending(); |
276 assertEquals(0, mDataProvider.getNumRewindCalls()); | 279 assertEquals(0, mDataProvider.getNumRewindCalls()); |
277 assertEquals(1, mDataProvider.getNumReadCalls()); | 280 assertEquals(1, mDataProvider.getNumReadCalls()); |
(...skipping 13 matching lines...) Expand all Loading... |
291 // will then destroy the CronetUploadDataStreamAdapter. | 294 // will then destroy the CronetUploadDataStreamAdapter. |
292 mHandler.destroyNativeObjects(); | 295 mHandler.destroyNativeObjects(); |
293 | 296 |
294 // Signal rewind completes, and wait for init to complete. | 297 // Signal rewind completes, and wait for init to complete. |
295 mDataProvider.onRewindSucceeded(mUploadDataStream); | 298 mDataProvider.onRewindSucceeded(mUploadDataStream); |
296 | 299 |
297 assertEquals(1, mDataProvider.getNumRewindCalls()); | 300 assertEquals(1, mDataProvider.getNumRewindCalls()); |
298 assertEquals(1, mDataProvider.getNumReadCalls()); | 301 assertEquals(1, mDataProvider.getNumReadCalls()); |
299 } | 302 } |
300 } | 303 } |
OLD | NEW |