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

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

Issue 2892013002: [Cronet] Clean up tests (Closed)
Patch Set: address comments Created 3 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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.os.StrictMode; 7 import android.os.StrictMode;
8 import android.support.test.filters.SmallTest; 8 import android.support.test.filters.SmallTest;
9 9
10 import org.chromium.base.Log; 10 import org.chromium.base.Log;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 } 62 }
63 } 63 }
64 }); 64 });
65 return mNetworkQualityThread; 65 return mNetworkQualityThread;
66 } 66 }
67 } 67 }
68 68
69 @SmallTest 69 @SmallTest
70 @Feature({"Cronet"}) 70 @Feature({"Cronet"})
71 public void testNotEnabled() throws Exception { 71 public void testNotEnabled() throws Exception {
72 ExperimentalCronetEngine.Builder mCronetEngineBuilder = 72 ExperimentalCronetEngine.Builder cronetEngineBuilder =
73 new ExperimentalCronetEngine.Builder(getContext()); 73 new ExperimentalCronetEngine.Builder(getContext());
74 final CronetTestFramework testFramework = 74 final ExperimentalCronetEngine cronetEngine = cronetEngineBuilder.build( );
75 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder);
76 Executor networkQualityExecutor = Executors.newSingleThreadExecutor(); 75 Executor networkQualityExecutor = Executors.newSingleThreadExecutor();
77 TestNetworkQualityRttListener rttListener = 76 TestNetworkQualityRttListener rttListener =
78 new TestNetworkQualityRttListener(networkQualityExecutor); 77 new TestNetworkQualityRttListener(networkQualityExecutor);
79 TestNetworkQualityThroughputListener throughputListener = 78 TestNetworkQualityThroughputListener throughputListener =
80 new TestNetworkQualityThroughputListener(networkQualityExecutor) ; 79 new TestNetworkQualityThroughputListener(networkQualityExecutor) ;
81 try { 80 try {
82 testFramework.mCronetEngine.addRttListener(rttListener); 81 cronetEngine.addRttListener(rttListener);
83 fail("Should throw an exception."); 82 fail("Should throw an exception.");
84 } catch (IllegalStateException e) { 83 } catch (IllegalStateException e) {
85 } 84 }
86 try { 85 try {
87 testFramework.mCronetEngine.addThroughputListener(throughputListener ); 86 cronetEngine.addThroughputListener(throughputListener);
88 fail("Should throw an exception."); 87 fail("Should throw an exception.");
89 } catch (IllegalStateException e) { 88 } catch (IllegalStateException e) {
90 } 89 }
91 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 90 TestUrlRequestCallback callback = new TestUrlRequestCallback();
92 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu ilder( 91 UrlRequest.Builder builder =
93 mUrl, callback, callback.getExecutor()); 92 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx ecutor());
94 UrlRequest urlRequest = builder.build(); 93 UrlRequest urlRequest = builder.build();
95 94
96 urlRequest.start(); 95 urlRequest.start();
97 callback.blockForDone(); 96 callback.blockForDone();
98 assertEquals(0, rttListener.rttObservationCount()); 97 assertEquals(0, rttListener.rttObservationCount());
99 assertEquals(0, throughputListener.throughputObservationCount()); 98 assertEquals(0, throughputListener.throughputObservationCount());
100 testFramework.mCronetEngine.shutdown(); 99 cronetEngine.shutdown();
101 } 100 }
102 101
103 @SmallTest 102 @SmallTest
104 @Feature({"Cronet"}) 103 @Feature({"Cronet"})
105 public void testListenerRemoved() throws Exception { 104 public void testListenerRemoved() throws Exception {
106 ExperimentalCronetEngine.Builder mCronetEngineBuilder = 105 ExperimentalCronetEngine.Builder cronetEngineBuilder =
107 new ExperimentalCronetEngine.Builder(getContext()); 106 new ExperimentalCronetEngine.Builder(getContext());
108 TestExecutor networkQualityExecutor = new TestExecutor(); 107 TestExecutor networkQualityExecutor = new TestExecutor();
109 TestNetworkQualityRttListener rttListener = 108 TestNetworkQualityRttListener rttListener =
110 new TestNetworkQualityRttListener(networkQualityExecutor); 109 new TestNetworkQualityRttListener(networkQualityExecutor);
111 mCronetEngineBuilder.enableNetworkQualityEstimator(true); 110 cronetEngineBuilder.enableNetworkQualityEstimator(true);
112 final CronetTestFramework testFramework = 111 final ExperimentalCronetEngine cronetEngine = cronetEngineBuilder.build( );
113 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder); 112 cronetEngine.configureNetworkQualityEstimatorForTesting(true, true, fals e);
114 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t rue, true, false);
115 113
116 testFramework.mCronetEngine.addRttListener(rttListener); 114 cronetEngine.addRttListener(rttListener);
117 testFramework.mCronetEngine.removeRttListener(rttListener); 115 cronetEngine.removeRttListener(rttListener);
118 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 116 TestUrlRequestCallback callback = new TestUrlRequestCallback();
119 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu ilder( 117 UrlRequest.Builder builder =
120 mUrl, callback, callback.getExecutor()); 118 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx ecutor());
121 UrlRequest urlRequest = builder.build(); 119 UrlRequest urlRequest = builder.build();
122 urlRequest.start(); 120 urlRequest.start();
123 callback.blockForDone(); 121 callback.blockForDone();
124 networkQualityExecutor.runAllTasks(); 122 networkQualityExecutor.runAllTasks();
125 assertEquals(0, rttListener.rttObservationCount()); 123 assertEquals(0, rttListener.rttObservationCount());
126 testFramework.mCronetEngine.shutdown(); 124 cronetEngine.shutdown();
127 } 125 }
128 126
129 // Returns whether a file contains a particular string. 127 // Returns whether a file contains a particular string.
130 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") 128 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE")
131 private boolean fileContainsString(String filename, String content) throws I OException { 129 private boolean fileContainsString(String filename, String content) throws I OException {
132 File file = 130 File file = new File(getTestStorage(getContext()) + "/prefs/" + filename );
133 new File(CronetTestFramework.getTestStorage(getContext()) + "/pr efs/" + filename);
134 FileInputStream fileInputStream = new FileInputStream(file); 131 FileInputStream fileInputStream = new FileInputStream(file);
135 byte[] data = new byte[(int) file.length()]; 132 byte[] data = new byte[(int) file.length()];
136 fileInputStream.read(data); 133 fileInputStream.read(data);
137 fileInputStream.close(); 134 fileInputStream.close();
138 return new String(data, "UTF-8").contains(content); 135 return new String(data, "UTF-8").contains(content);
139 } 136 }
140 137
141 @SmallTest 138 @SmallTest
142 @Feature({"Cronet"}) 139 @Feature({"Cronet"})
143 @DisabledTest(message = "Disabled due to flaky assert. See crbug.com/710626" ) 140 @DisabledTest(message = "Disabled due to flaky assert. See crbug.com/710626" )
144 public void testQuicDisabled() throws Exception { 141 public void testQuicDisabled() throws Exception {
145 ExperimentalCronetEngine.Builder mCronetEngineBuilder = 142 ExperimentalCronetEngine.Builder cronetEngineBuilder =
146 new ExperimentalCronetEngine.Builder(getContext()); 143 new ExperimentalCronetEngine.Builder(getContext());
147 assert RttThroughputValues.INVALID_RTT_THROUGHPUT < 0; 144 assert RttThroughputValues.INVALID_RTT_THROUGHPUT < 0;
148 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu torThreadFactory()); 145 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu torThreadFactory());
149 TestNetworkQualityRttListener rttListener = 146 TestNetworkQualityRttListener rttListener =
150 new TestNetworkQualityRttListener(listenersExecutor); 147 new TestNetworkQualityRttListener(listenersExecutor);
151 TestNetworkQualityThroughputListener throughputListener = 148 TestNetworkQualityThroughputListener throughputListener =
152 new TestNetworkQualityThroughputListener(listenersExecutor); 149 new TestNetworkQualityThroughputListener(listenersExecutor);
153 mCronetEngineBuilder.enableNetworkQualityEstimator(true).enableHttp2(tru e).enableQuic( 150 cronetEngineBuilder.enableNetworkQualityEstimator(true).enableHttp2(true ).enableQuic(false);
154 false); 151 cronetEngineBuilder.setStoragePath(getTestStorage(getContext()));
155 mCronetEngineBuilder.setStoragePath(CronetTestFramework.getTestStorage(g etContext())); 152 final ExperimentalCronetEngine cronetEngine = cronetEngineBuilder.build( );
156 final CronetTestFramework testFramework = 153 cronetEngine.configureNetworkQualityEstimatorForTesting(true, true, true );
157 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder);
158 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t rue, true, true);
159 154
160 testFramework.mCronetEngine.addRttListener(rttListener); 155 cronetEngine.addRttListener(rttListener);
161 testFramework.mCronetEngine.addThroughputListener(throughputListener); 156 cronetEngine.addThroughputListener(throughputListener);
162 157
163 HistogramDelta writeCountHistogram = new HistogramDelta("NQE.Prefs.Write Count", 1); 158 HistogramDelta writeCountHistogram = new HistogramDelta("NQE.Prefs.Write Count", 1);
164 assertEquals(0, writeCountHistogram.getDelta()); // Sanity check. 159 assertEquals(0, writeCountHistogram.getDelta()); // Sanity check.
165 160
166 HistogramDelta readCountHistogram = new HistogramDelta("NQE.Prefs.ReadCo unt", 1); 161 HistogramDelta readCountHistogram = new HistogramDelta("NQE.Prefs.ReadCo unt", 1);
167 assertEquals(0, readCountHistogram.getDelta()); // Sanity check. 162 assertEquals(0, readCountHistogram.getDelta()); // Sanity check.
168 163
169 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 164 TestUrlRequestCallback callback = new TestUrlRequestCallback();
170 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu ilder( 165 UrlRequest.Builder builder =
171 mUrl, callback, callback.getExecutor()); 166 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx ecutor());
172 UrlRequest urlRequest = builder.build(); 167 UrlRequest urlRequest = builder.build();
173 urlRequest.start(); 168 urlRequest.start();
174 callback.blockForDone(); 169 callback.blockForDone();
175 170
176 // Throughput observation is posted to the network quality estimator on the network thread 171 // Throughput observation is posted to the network quality estimator on the network thread
177 // after the UrlRequest is completed. The observations are then eventual ly posted to 172 // after the UrlRequest is completed. The observations are then eventual ly posted to
178 // throughput listeners on the executor provided to network quality. 173 // throughput listeners on the executor provided to network quality.
179 throughputListener.waitUntilFirstThroughputObservationReceived(); 174 throughputListener.waitUntilFirstThroughputObservationReceived();
180 175
181 // Wait for RTT observation (at the URL request layer) to be posted. 176 // Wait for RTT observation (at the URL request layer) to be posted.
(...skipping 16 matching lines...) Expand all
198 193
199 // NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC 194 // NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC
200 assertEquals(0, rttListener.rttObservationCount(2)); 195 assertEquals(0, rttListener.rttObservationCount(2));
201 196
202 // Verify that the listeners were notified on the expected thread. 197 // Verify that the listeners were notified on the expected thread.
203 assertEquals(mNetworkQualityThread, rttListener.getThread()); 198 assertEquals(mNetworkQualityThread, rttListener.getThread());
204 assertEquals(mNetworkQualityThread, throughputListener.getThread()); 199 assertEquals(mNetworkQualityThread, throughputListener.getThread());
205 200
206 // Verify that effective connection type callback is received and 201 // Verify that effective connection type callback is received and
207 // effective connection type is correctly set. 202 // effective connection type is correctly set.
208 assertTrue(testFramework.mCronetEngine.getEffectiveConnectionType() 203 assertTrue(
209 != EffectiveConnectionType.TYPE_UNKNOWN); 204 cronetEngine.getEffectiveConnectionType() != EffectiveConnection Type.TYPE_UNKNOWN);
210 205
211 // Verify that the HTTP RTT, transport RTT and downstream throughput 206 // Verify that the HTTP RTT, transport RTT and downstream throughput
212 // estimates are available. 207 // estimates are available.
213 assertTrue(testFramework.mCronetEngine.getHttpRttMs() >= 0); 208 assertTrue(cronetEngine.getHttpRttMs() >= 0);
214 assertTrue(testFramework.mCronetEngine.getTransportRttMs() >= 0); 209 assertTrue(cronetEngine.getTransportRttMs() >= 0);
215 assertTrue(testFramework.mCronetEngine.getDownstreamThroughputKbps() >= 0); 210 assertTrue(cronetEngine.getDownstreamThroughputKbps() >= 0);
216 211
217 // Verify that the cached estimates were written to the prefs. 212 // Verify that the cached estimates were written to the prefs.
218 while (true) { 213 while (true) {
219 Log.i(TAG, "Still waiting for pref file update....."); 214 Log.i(TAG, "Still waiting for pref file update.....");
220 Thread.sleep(12000); 215 Thread.sleep(12000);
221 try { 216 try {
222 if (fileContainsString("local_prefs.json", "network_qualities")) { 217 if (fileContainsString("local_prefs.json", "network_qualities")) {
223 break; 218 break;
224 } 219 }
225 } catch (FileNotFoundException e) { 220 } catch (FileNotFoundException e) {
226 // Ignored this exception since the file will only be created wh en updates are 221 // Ignored this exception since the file will only be created wh en updates are
227 // flushed to the disk. 222 // flushed to the disk.
228 } 223 }
229 } 224 }
230 assertTrue(fileContainsString("local_prefs.json", "network_qualities")); 225 assertTrue(fileContainsString("local_prefs.json", "network_qualities"));
231 226
232 testFramework.mCronetEngine.shutdown(); 227 cronetEngine.shutdown();
233 assertTrue(writeCountHistogram.getDelta() > 0); 228 assertTrue(writeCountHistogram.getDelta() > 0);
234 } 229 }
235 } 230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698