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

Side by Side Diff: net/nqe/network_quality_estimator_unittest.cc

Issue 2007713002: Expose NQE::OnEffectiveConnectionTypeChanged API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased 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 | « net/nqe/network_quality_estimator.cc ('k') | 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 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 #include "net/nqe/network_quality_estimator.h" 5 #include "net/nqe/network_quality_estimator.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <string> 13 #include <string>
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/metrics/histogram_samples.h" 20 #include "base/metrics/histogram_samples.h"
21 #include "base/run_loop.h" 21 #include "base/run_loop.h"
22 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
23 #include "base/test/histogram_tester.h" 23 #include "base/test/histogram_tester.h"
24 #include "base/test/simple_test_tick_clock.h"
24 #include "base/time/time.h" 25 #include "base/time/time.h"
25 #include "build/build_config.h" 26 #include "build/build_config.h"
26 #include "net/base/load_flags.h" 27 #include "net/base/load_flags.h"
27 #include "net/base/network_change_notifier.h" 28 #include "net/base/network_change_notifier.h"
28 #include "net/http/http_status_code.h" 29 #include "net/http/http_status_code.h"
29 #include "net/nqe/external_estimate_provider.h" 30 #include "net/nqe/external_estimate_provider.h"
30 #include "net/nqe/network_quality_observation.h" 31 #include "net/nqe/network_quality_observation.h"
31 #include "net/nqe/network_quality_observation_source.h" 32 #include "net/nqe/network_quality_observation_source.h"
32 #include "net/nqe/observation_buffer.h" 33 #include "net/nqe/observation_buffer.h"
33 #include "net/socket/socket_performance_watcher.h" 34 #include "net/socket/socket_performance_watcher.h"
(...skipping 23 matching lines...) Expand all
57 58
58 TestNetworkQualityEstimator( 59 TestNetworkQualityEstimator(
59 std::unique_ptr<net::ExternalEstimateProvider> external_estimate_provider, 60 std::unique_ptr<net::ExternalEstimateProvider> external_estimate_provider,
60 const std::map<std::string, std::string>& variation_params, 61 const std::map<std::string, std::string>& variation_params,
61 bool allow_local_host_requests_for_tests, 62 bool allow_local_host_requests_for_tests,
62 bool allow_smaller_responses_for_tests) 63 bool allow_smaller_responses_for_tests)
63 : NetworkQualityEstimator(std::move(external_estimate_provider), 64 : NetworkQualityEstimator(std::move(external_estimate_provider),
64 variation_params, 65 variation_params,
65 allow_local_host_requests_for_tests, 66 allow_local_host_requests_for_tests,
66 allow_smaller_responses_for_tests), 67 allow_smaller_responses_for_tests),
67 current_network_simulated_(false), 68 effective_connection_type_set_(false),
69 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
70 current_network_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN),
68 http_rtt_set_(false), 71 http_rtt_set_(false),
69 downlink_throughput_kbps_set_(false) { 72 downlink_throughput_kbps_set_(false) {
70 // Set up embedded test server. 73 // Set up embedded test server.
71 embedded_test_server_.ServeFilesFromDirectory( 74 embedded_test_server_.ServeFilesFromDirectory(
72 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 75 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
73 EXPECT_TRUE(embedded_test_server_.Start()); 76 EXPECT_TRUE(embedded_test_server_.Start());
74 embedded_test_server_.RegisterRequestHandler(base::Bind( 77 embedded_test_server_.RegisterRequestHandler(base::Bind(
75 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); 78 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this)));
76 } 79 }
77 80
78 explicit TestNetworkQualityEstimator( 81 explicit TestNetworkQualityEstimator(
79 const std::map<std::string, std::string>& variation_params) 82 const std::map<std::string, std::string>& variation_params)
80 : TestNetworkQualityEstimator( 83 : TestNetworkQualityEstimator(
81 variation_params, 84 variation_params,
82 std::unique_ptr<ExternalEstimateProvider>()) {} 85 std::unique_ptr<ExternalEstimateProvider>()) {}
83 86
84 ~TestNetworkQualityEstimator() override {} 87 ~TestNetworkQualityEstimator() override {}
85 88
86 // Overrides the current network type and id. 89 // Overrides the current network type and id.
87 // Notifies network quality estimator of change in connection. 90 // Notifies network quality estimator of change in connection.
88 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type, 91 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type,
89 const std::string& network_id) { 92 const std::string& network_id) {
90 current_network_simulated_ = true;
91 current_network_type_ = type; 93 current_network_type_ = type;
92 current_network_id_ = network_id; 94 current_network_id_ = network_id;
93 OnConnectionTypeChanged(type); 95 OnConnectionTypeChanged(type);
94 } 96 }
95 97
96 // Called by embedded server when a HTTP request is received. 98 // Called by embedded server when a HTTP request is received.
97 std::unique_ptr<test_server::HttpResponse> HandleRequest( 99 std::unique_ptr<test_server::HttpResponse> HandleRequest(
98 const test_server::HttpRequest& request) { 100 const test_server::HttpRequest& request) {
99 std::unique_ptr<test_server::BasicHttpResponse> http_response( 101 std::unique_ptr<test_server::BasicHttpResponse> http_response(
100 new test_server::BasicHttpResponse()); 102 new test_server::BasicHttpResponse());
101 http_response->set_code(HTTP_OK); 103 http_response->set_code(HTTP_OK);
102 http_response->set_content("hello"); 104 http_response->set_content("hello");
103 http_response->set_content_type("text/plain"); 105 http_response->set_content_type("text/plain");
104 return std::move(http_response); 106 return std::move(http_response);
105 } 107 }
106 108
107 // Returns a GURL hosted at embedded test server. 109 // Returns a GURL hosted at embedded test server.
108 const GURL GetEchoURL() const { 110 const GURL GetEchoURL() const {
109 return embedded_test_server_.GetURL("/echo.html"); 111 return embedded_test_server_.GetURL("/echo.html");
110 } 112 }
111 113
112 void set_http_rtt(const base::TimeDelta& http_rtt) { 114 void set_http_rtt(const base::TimeDelta& http_rtt) {
113 http_rtt_set_ = true; 115 http_rtt_set_ = true;
114 http_rtt_ = http_rtt; 116 http_rtt_ = http_rtt;
115 } 117 }
116 118
119 void set_effective_connection_type(EffectiveConnectionType type) {
120 effective_connection_type_set_ = true;
121 effective_connection_type_ = type;
122 }
123
124 EffectiveConnectionType GetEffectiveConnectionType() const override {
125 if (effective_connection_type_set_)
126 return effective_connection_type_;
127 return NetworkQualityEstimator::GetEffectiveConnectionType();
128 }
129
117 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override { 130 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override {
118 if (http_rtt_set_) { 131 if (http_rtt_set_) {
119 *rtt = http_rtt_; 132 *rtt = http_rtt_;
120 return true; 133 return true;
121 } 134 }
122 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt); 135 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt);
123 } 136 }
124 137
125 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time, 138 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time,
126 base::TimeDelta* rtt) const override { 139 base::TimeDelta* rtt) const override {
(...skipping 20 matching lines...) Expand all
147 bool GetRecentMedianDownlinkThroughputKbps(const base::TimeTicks& start_time, 160 bool GetRecentMedianDownlinkThroughputKbps(const base::TimeTicks& start_time,
148 int32_t* kbps) const override { 161 int32_t* kbps) const override {
149 if (downlink_throughput_kbps_set_) { 162 if (downlink_throughput_kbps_set_) {
150 *kbps = downlink_throughput_kbps_; 163 *kbps = downlink_throughput_kbps_;
151 return true; 164 return true;
152 } 165 }
153 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( 166 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps(
154 start_time, kbps); 167 start_time, kbps);
155 } 168 }
156 169
170 using NetworkQualityEstimator::SetTickClockForTesting;
157 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; 171 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
158 using NetworkQualityEstimator::OnConnectionTypeChanged; 172 using NetworkQualityEstimator::OnConnectionTypeChanged;
159 173
160 private: 174 private:
161 // True if the network type and network id are currently simulated. This
162 // ensures that the correctness of the test does not depend on the
163 // actual network type of the device on which the test is running.
164 bool current_network_simulated_;
165
166 // NetworkQualityEstimator implementation that returns the overridden network 175 // NetworkQualityEstimator implementation that returns the overridden network
167 // id (instead of invoking platform APIs). 176 // id (instead of invoking platform APIs).
168 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { 177 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override {
169 // GetCurrentNetworkID should be called only if the network type is
170 // currently simulated.
171 EXPECT_TRUE(current_network_simulated_);
172
173 return NetworkQualityEstimator::NetworkID(current_network_type_, 178 return NetworkQualityEstimator::NetworkID(current_network_type_,
174 current_network_id_); 179 current_network_id_);
175 } 180 }
176 181
182 bool effective_connection_type_set_;
183 EffectiveConnectionType effective_connection_type_;
184
177 NetworkChangeNotifier::ConnectionType current_network_type_; 185 NetworkChangeNotifier::ConnectionType current_network_type_;
178 std::string current_network_id_; 186 std::string current_network_id_;
179 187
180 bool http_rtt_set_; 188 bool http_rtt_set_;
181 base::TimeDelta http_rtt_; 189 base::TimeDelta http_rtt_;
182 190
183 bool downlink_throughput_kbps_set_; 191 bool downlink_throughput_kbps_set_;
184 int32_t downlink_throughput_kbps_; 192 int32_t downlink_throughput_kbps_;
185 193
186 // Embedded server used for testing. 194 // Embedded server used for testing.
187 EmbeddedTestServer embedded_test_server_; 195 EmbeddedTestServer embedded_test_server_;
188 196
189 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 197 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
190 }; 198 };
191 199
200 class TestEffectiveConnectionTypeObserver
201 : public NetworkQualityEstimator::EffectiveConnectionTypeObserver {
202 public:
203 std::vector<NetworkQualityEstimator::EffectiveConnectionType>&
204 effective_connection_types() {
205 return effective_connection_types_;
206 }
207
208 // EffectiveConnectionTypeObserver implementation:
209 void OnEffectiveConnectionTypeChanged(
210 NetworkQualityEstimator::EffectiveConnectionType type) override {
211 effective_connection_types_.push_back(type);
212 }
213
214 private:
215 std::vector<NetworkQualityEstimator::EffectiveConnectionType>
216 effective_connection_types_;
217 };
218
192 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver { 219 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver {
193 public: 220 public:
194 struct Observation { 221 struct Observation {
195 Observation(int32_t ms, 222 Observation(int32_t ms,
196 const base::TimeTicks& ts, 223 const base::TimeTicks& ts,
197 NetworkQualityObservationSource src) 224 NetworkQualityObservationSource src)
198 : rtt_ms(ms), timestamp(ts), source(src) {} 225 : rtt_ms(ms), timestamp(ts), source(src) {}
199 int32_t rtt_ms; 226 int32_t rtt_ms;
200 base::TimeTicks timestamp; 227 base::TimeTicks timestamp;
201 NetworkQualityObservationSource source; 228 NetworkQualityObservationSource source;
(...skipping 1008 matching lines...) Expand 10 before | Expand all | Expand 10 after
1210 request->Start(); 1237 request->Start();
1211 base::RunLoop().Run(); 1238 base::RunLoop().Run();
1212 1239
1213 EXPECT_EQ(test.allow_small_localhost_requests, 1240 EXPECT_EQ(test.allow_small_localhost_requests,
1214 estimator.GetHttpRTTEstimate(&rtt)); 1241 estimator.GetHttpRTTEstimate(&rtt));
1215 EXPECT_EQ(test.allow_small_localhost_requests, 1242 EXPECT_EQ(test.allow_small_localhost_requests,
1216 estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); 1243 estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
1217 } 1244 }
1218 } 1245 }
1219 1246
1220 TEST(NetworkQualityEstimatorTest, TestObservers) { 1247 // Tests that the effective connection type is computed at the specified
1248 // interval, and that the observers are notified of any change.
1249 TEST(NetworkQualityEstimatorTest, TestEffectiveConnectionTypeObserver) {
1250 std::unique_ptr<base::SimpleTestTickClock> tick_clock(
1251 new base::SimpleTestTickClock());
1252 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get();
1253
1254 TestEffectiveConnectionTypeObserver observer;
1255 std::map<std::string, std::string> variation_params;
1256 TestNetworkQualityEstimator estimator(variation_params);
1257 estimator.AddEffectiveConnectionTypeObserver(&observer);
1258 estimator.SetTickClockForTesting(std::move(tick_clock));
1259
1260 TestDelegate test_delegate;
1261 TestURLRequestContext context(true);
1262 context.set_network_quality_estimator(&estimator);
1263 context.Init();
1264
1265 EXPECT_EQ(0U, observer.effective_connection_types().size());
1266
1267 estimator.set_effective_connection_type(
1268 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G);
1269 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60));
1270
1271 std::unique_ptr<URLRequest> request(context.CreateRequest(
1272 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
1273 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME);
1274 request->Start();
1275 base::RunLoop().Run();
1276 EXPECT_EQ(1U, observer.effective_connection_types().size());
1277
1278 // Next request should not trigger recomputation of effective connection type
1279 // since there has been no change in the clock.
1280 std::unique_ptr<URLRequest> request2(context.CreateRequest(
1281 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
1282 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME);
1283 request2->Start();
1284 base::RunLoop().Run();
1285 EXPECT_EQ(1U, observer.effective_connection_types().size());
1286
1287 // Change in connection type should send out notification to the observers.
1288 estimator.set_effective_connection_type(
1289 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G);
1290 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI,
1291 "test");
1292 EXPECT_EQ(2U, observer.effective_connection_types().size());
1293
1294 // A change in effective connection type does not trigger notification to the
1295 // observers, since it is not accompanied by any new observation or a network
1296 // change event.
1297 estimator.set_effective_connection_type(
1298 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G);
1299 EXPECT_EQ(2U, observer.effective_connection_types().size());
1300 }
1301
1302 TEST(NetworkQualityEstimatorTest, TestRttThroughputObservers) {
1221 TestRTTObserver rtt_observer; 1303 TestRTTObserver rtt_observer;
1222 TestThroughputObserver throughput_observer; 1304 TestThroughputObserver throughput_observer;
1223 std::map<std::string, std::string> variation_params; 1305 std::map<std::string, std::string> variation_params;
1224 TestNetworkQualityEstimator estimator(variation_params); 1306 TestNetworkQualityEstimator estimator(variation_params);
1225 estimator.AddRTTObserver(&rtt_observer); 1307 estimator.AddRTTObserver(&rtt_observer);
1226 estimator.AddThroughputObserver(&throughput_observer); 1308 estimator.AddThroughputObserver(&throughput_observer);
1227 1309
1228 TestDelegate test_delegate; 1310 TestDelegate test_delegate;
1229 TestURLRequestContext context(true); 1311 TestURLRequestContext context(true);
1230 context.set_network_quality_estimator(&estimator); 1312 context.set_network_quality_estimator(&estimator);
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
1372 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); 1454 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1);
1373 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", 1455 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown",
1374 1); 1456 1);
1375 1457
1376 // Verify that metrics are logged correctly on main-frame requests. 1458 // Verify that metrics are logged correctly on main-frame requests.
1377 histogram_tester.ExpectTotalCount( 1459 histogram_tester.ExpectTotalCount(
1378 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests); 1460 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests);
1379 } 1461 }
1380 1462
1381 } // namespace net 1463 } // namespace net
OLDNEW
« no previous file with comments | « net/nqe/network_quality_estimator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698