Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java

Issue 1999303002: Fix flaky NQE cronet test (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add locks so that counts can be read concurrently Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 static org.chromium.base.CollectionUtil.newHashSet; 7 import static org.chromium.base.CollectionUtil.newHashSet;
8 8
9 import android.content.Context; 9 import android.content.Context;
10 import android.content.ContextWrapper; 10 import android.content.ContextWrapper;
(...skipping 11 matching lines...) Expand all
22 import org.chromium.net.test.EmbeddedTestServer; 22 import org.chromium.net.test.EmbeddedTestServer;
23 23
24 import java.io.BufferedReader; 24 import java.io.BufferedReader;
25 import java.io.File; 25 import java.io.File;
26 import java.io.FileReader; 26 import java.io.FileReader;
27 import java.util.Arrays; 27 import java.util.Arrays;
28 import java.util.HashSet; 28 import java.util.HashSet;
29 import java.util.LinkedList; 29 import java.util.LinkedList;
30 import java.util.NoSuchElementException; 30 import java.util.NoSuchElementException;
31 import java.util.concurrent.Executor; 31 import java.util.concurrent.Executor;
32 import java.util.concurrent.Executors;
32 33
33 /** 34 /**
34 * Test CronetEngine. 35 * Test CronetEngine.
35 */ 36 */
36 @JNINamespace("cronet") 37 @JNINamespace("cronet")
37 public class CronetUrlRequestContextTest extends CronetTestBase { 38 public class CronetUrlRequestContextTest extends CronetTestBase {
38 // URLs used for tests. 39 // URLs used for tests.
39 private static final String MOCK_CRONET_TEST_FAILED_URL = 40 private static final String MOCK_CRONET_TEST_FAILED_URL =
40 "http://mock.failed.request/-2"; 41 "http://mock.failed.request/-2";
41 private static final String MOCK_CRONET_TEST_SUCCESS_URL = 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL =
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 while (mTaskQueue.size() > 0) { 120 while (mTaskQueue.size() > 0) {
120 mTaskQueue.remove().run(); 121 mTaskQueue.remove().run();
121 } 122 }
122 } catch (NoSuchElementException e) { 123 } catch (NoSuchElementException e) {
123 } 124 }
124 } 125 }
125 } 126 }
126 127
127 static class TestNetworkQualityListener 128 static class TestNetworkQualityListener
128 implements NetworkQualityRttListener, NetworkQualityThroughputListen er { 129 implements NetworkQualityRttListener, NetworkQualityThroughputListen er {
129 int mRttObservationCount; 130 // Lock to ensure that observation counts can be updated and read by dif ferent threads.
130 int mThroughputObservationCount; 131 private final Object mLock = new Object();
132 private final ConditionVariable mWaitForThroughput;
133 private int mRttObservationCount;
134 private int mThroughputObservationCount;
135
136 TestNetworkQualityListener(ConditionVariable waitForThroughput) {
137 mWaitForThroughput = waitForThroughput;
138 }
131 139
132 @Override 140 @Override
133 public void onRttObservation(int rttMs, long when, int source) { 141 public void onRttObservation(int rttMs, long when, int source) {
134 mRttObservationCount++; 142 synchronized (mLock) {
143 mRttObservationCount++;
144 }
135 } 145 }
136 146
137 @Override 147 @Override
138 public void onThroughputObservation(int throughputKbps, long when, int s ource) { 148 public void onThroughputObservation(int throughputKbps, long when, int s ource) {
139 mThroughputObservationCount++; 149 synchronized (mLock) {
150 if (mWaitForThroughput != null) {
151 mWaitForThroughput.open();
152 }
153 mThroughputObservationCount++;
154 }
140 } 155 }
141 156
142 public int rttObservationCount() { 157 public int rttObservationCount() {
143 return mRttObservationCount; 158 synchronized (mLock) {
159 return mRttObservationCount;
160 }
144 } 161 }
145 162
146 public int throughputObservationCount() { 163 public int throughputObservationCount() {
147 return mThroughputObservationCount; 164 synchronized (mLock) {
165 return mThroughputObservationCount;
166 }
148 } 167 }
149 } 168 }
150 169
151 @SmallTest 170 @SmallTest
152 @Feature({"Cronet"}) 171 @Feature({"Cronet"})
153 public void testConfigUserAgent() throws Exception { 172 public void testConfigUserAgent() throws Exception {
154 String userAgentName = "User-Agent"; 173 String userAgentName = "User-Agent";
155 String userAgentValue = "User-Agent-Value"; 174 String userAgentValue = "User-Agent-Value";
156 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC ontext()); 175 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC ontext());
157 if (testingJavaImpl()) { 176 if (testingJavaImpl()) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 // Verify that the request is successful and that the Data Reduction 232 // Verify that the request is successful and that the Data Reduction
214 // Proxy logic configured to use the test server as its proxy. 233 // Proxy logic configured to use the test server as its proxy.
215 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); 234 assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
216 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer()); 235 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer());
217 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess. txt", 236 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess. txt",
218 callback.mResponseInfo.getUrl()); 237 callback.mResponseInfo.getUrl());
219 } 238 }
220 239
221 @SmallTest 240 @SmallTest
222 @Feature({"Cronet"}) 241 @Feature({"Cronet"})
223 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed.
224 @SuppressWarnings("deprecation")
225 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except ion { 242 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except ion {
226 mTestFramework = startCronetTestFramework(); 243 mTestFramework = startCronetTestFramework();
227 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(); 244 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(null);
228 try { 245 try {
229 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); 246 mTestFramework.mCronetEngine.addRttListener(networkQualityListener);
230 fail("Should throw an exception."); 247 fail("Should throw an exception.");
231 } catch (IllegalStateException e) { 248 } catch (IllegalStateException e) {
232 } 249 }
233 try { 250 try {
234 mTestFramework.mCronetEngine.addThroughputListener(networkQualityLis tener); 251 mTestFramework.mCronetEngine.addThroughputListener(networkQualityLis tener);
235 fail("Should throw an exception."); 252 fail("Should throw an exception.");
236 } catch (IllegalStateException e) { 253 } catch (IllegalStateException e) {
237 } 254 }
238 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 255 TestUrlRequestCallback callback = new TestUrlRequestCallback();
239 UrlRequest urlRequest = 256 UrlRequest urlRequest =
240 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); 257 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor());
241 urlRequest.start(); 258 urlRequest.start();
242 callback.blockForDone(); 259 callback.blockForDone();
243 assertEquals(0, networkQualityListener.rttObservationCount()); 260 assertEquals(0, networkQualityListener.rttObservationCount());
244 assertEquals(0, networkQualityListener.throughputObservationCount()); 261 assertEquals(0, networkQualityListener.throughputObservationCount());
245 mTestFramework.mCronetEngine.shutdown(); 262 mTestFramework.mCronetEngine.shutdown();
246 } 263 }
247 264
248 @SmallTest 265 @SmallTest
249 @Feature({"Cronet"}) 266 @Feature({"Cronet"})
250 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed.
251 @SuppressWarnings("deprecation")
252 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E xception { 267 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E xception {
253 mTestFramework = startCronetTestFramework(); 268 mTestFramework = startCronetTestFramework();
254 TestExecutor testExecutor = new TestExecutor(); 269 TestExecutor testExecutor = new TestExecutor();
255 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(); 270 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(null);
256 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( 271 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting(
257 true, true, testExecutor); 272 true, true, testExecutor);
258 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); 273 mTestFramework.mCronetEngine.addRttListener(networkQualityListener);
259 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r); 274 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r);
260 mTestFramework.mCronetEngine.removeRttListener(networkQualityListener); 275 mTestFramework.mCronetEngine.removeRttListener(networkQualityListener);
261 mTestFramework.mCronetEngine.removeThroughputListener(networkQualityList ener); 276 mTestFramework.mCronetEngine.removeThroughputListener(networkQualityList ener);
262 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 277 TestUrlRequestCallback callback = new TestUrlRequestCallback();
263 UrlRequest urlRequest = 278 UrlRequest urlRequest =
264 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); 279 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor());
265 urlRequest.start(); 280 urlRequest.start();
266 callback.blockForDone(); 281 callback.blockForDone();
267 testExecutor.runAllTasks(); 282 testExecutor.runAllTasks();
268 assertEquals(0, networkQualityListener.rttObservationCount()); 283 assertEquals(0, networkQualityListener.rttObservationCount());
269 assertEquals(0, networkQualityListener.throughputObservationCount()); 284 assertEquals(0, networkQualityListener.throughputObservationCount());
270 mTestFramework.mCronetEngine.shutdown(); 285 mTestFramework.mCronetEngine.shutdown();
271 } 286 }
272 287
273 /*
274 @SmallTest 288 @SmallTest
275 @Feature({"Cronet"}) 289 @Feature({"Cronet"})
276 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed.
277 @SuppressWarnings("deprecation")
278 */
279 @FlakyTest(message = "http://crbug.com/614227")
280 public void testRealTimeNetworkQualityObservations() throws Exception { 290 public void testRealTimeNetworkQualityObservations() throws Exception {
281 mTestFramework = startCronetTestFramework(); 291 mTestFramework = startCronetTestFramework();
282 TestExecutor testExecutor = new TestExecutor(); 292 Executor executor = Executors.newSingleThreadExecutor();
283 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(); 293 ConditionVariable waitForThroughput = new ConditionVariable();
284 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( 294 TestNetworkQualityListener networkQualityListener =
285 true, true, testExecutor); 295 new TestNetworkQualityListener(waitForThroughput);
296 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting(tru e, true, executor);
286 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); 297 mTestFramework.mCronetEngine.addRttListener(networkQualityListener);
287 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r); 298 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r);
288 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 299 TestUrlRequestCallback callback = new TestUrlRequestCallback();
289 UrlRequest urlRequest = 300 UrlRequest urlRequest =
290 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); 301 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor());
291 urlRequest.start(); 302 urlRequest.start();
292 callback.blockForDone(); 303 callback.blockForDone();
293 testExecutor.runAllTasks(); 304
305 // Throughput observation is posted to the network quality estimator on the network thread
306 // after the UrlRequest is completed. The observations are then eventual ly posted to
307 // throughput listeners on the executor provided to network quality.
308 waitForThroughput.block();
309 assertTrue(networkQualityListener.throughputObservationCount() > 0);
310
311 // Check RTT observation count after throughput observation has been rec eived. This ensures
312 // that executor has finished posting the RTT observation to the RTT lis teners.
294 assertTrue(networkQualityListener.rttObservationCount() > 0); 313 assertTrue(networkQualityListener.rttObservationCount() > 0);
295 assertTrue(networkQualityListener.throughputObservationCount() > 0); 314
296 mTestFramework.mCronetEngine.shutdown(); 315 mTestFramework.mCronetEngine.shutdown();
297 } 316 }
298 317
299 private static class TestRequestFinishedListener 318 private static class TestRequestFinishedListener
300 implements CronetEngine.RequestFinishedListener { 319 implements CronetEngine.RequestFinishedListener {
301 private UrlRequestInfo mRequestInfo = null; 320 private UrlRequestInfo mRequestInfo = null;
302 321
303 @Override 322 @Override
304 public void onRequestFinished(UrlRequestInfo requestInfo) { 323 public void onRequestFinished(UrlRequestInfo requestInfo) {
305 assertNull("onRequestFinished called repeatedly", mRequestInfo); 324 assertNull("onRequestFinished called repeatedly", mRequestInfo);
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 } 1168 }
1150 }.start(); 1169 }.start();
1151 otherThreadDone.block(); 1170 otherThreadDone.block();
1152 builder.build().shutdown(); 1171 builder.build().shutdown();
1153 uiThreadDone.open(); 1172 uiThreadDone.open();
1154 } 1173 }
1155 }); 1174 });
1156 assertTrue(uiThreadDone.block(1000)); 1175 assertTrue(uiThreadDone.block(1000));
1157 } 1176 }
1158 } 1177 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698