| 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 #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> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // Helps in setting the current network type and id. | 47 // Helps in setting the current network type and id. |
| 48 class TestNetworkQualityEstimator : public NetworkQualityEstimator { | 48 class TestNetworkQualityEstimator : public NetworkQualityEstimator { |
| 49 public: | 49 public: |
| 50 TestNetworkQualityEstimator( | 50 TestNetworkQualityEstimator( |
| 51 const std::map<std::string, std::string>& variation_params, | 51 const std::map<std::string, std::string>& variation_params, |
| 52 std::unique_ptr<ExternalEstimateProvider> external_estimate_provider) | 52 std::unique_ptr<ExternalEstimateProvider> external_estimate_provider) |
| 53 : NetworkQualityEstimator(std::move(external_estimate_provider), | 53 : NetworkQualityEstimator(std::move(external_estimate_provider), |
| 54 variation_params, | 54 variation_params, |
| 55 true, | 55 true, |
| 56 true), | 56 true), |
| 57 current_network_simulated_(false), |
| 57 url_rtt_set_(false), | 58 url_rtt_set_(false), |
| 58 downlink_throughput_kbps_set_(false) { | 59 downlink_throughput_kbps_set_(false) { |
| 59 // Set up embedded test server. | 60 // Set up embedded test server. |
| 60 embedded_test_server_.ServeFilesFromDirectory( | 61 embedded_test_server_.ServeFilesFromDirectory( |
| 61 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 62 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 62 EXPECT_TRUE(embedded_test_server_.Start()); | 63 EXPECT_TRUE(embedded_test_server_.Start()); |
| 63 embedded_test_server_.RegisterRequestHandler(base::Bind( | 64 embedded_test_server_.RegisterRequestHandler(base::Bind( |
| 64 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); | 65 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); |
| 65 } | 66 } |
| 66 | 67 |
| 67 explicit TestNetworkQualityEstimator( | 68 explicit TestNetworkQualityEstimator( |
| 68 const std::map<std::string, std::string>& variation_params) | 69 const std::map<std::string, std::string>& variation_params) |
| 69 : TestNetworkQualityEstimator( | 70 : TestNetworkQualityEstimator( |
| 70 variation_params, | 71 variation_params, |
| 71 std::unique_ptr<ExternalEstimateProvider>()) {} | 72 std::unique_ptr<ExternalEstimateProvider>()) {} |
| 72 | 73 |
| 73 ~TestNetworkQualityEstimator() override {} | 74 ~TestNetworkQualityEstimator() override {} |
| 74 | 75 |
| 75 // Overrides the current network type and id. | 76 // Overrides the current network type and id. |
| 76 // Notifies network quality estimator of change in connection. | 77 // Notifies network quality estimator of change in connection. |
| 77 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type, | 78 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type, |
| 78 std::string network_id) { | 79 const std::string& network_id) { |
| 80 current_network_simulated_ = true; |
| 79 current_network_type_ = type; | 81 current_network_type_ = type; |
| 80 current_network_id_ = network_id; | 82 current_network_id_ = network_id; |
| 81 OnConnectionTypeChanged(type); | 83 OnConnectionTypeChanged(type); |
| 82 } | 84 } |
| 83 | 85 |
| 84 // Called by embedded server when a HTTP request is received. | 86 // Called by embedded server when a HTTP request is received. |
| 85 std::unique_ptr<test_server::HttpResponse> HandleRequest( | 87 std::unique_ptr<test_server::HttpResponse> HandleRequest( |
| 86 const test_server::HttpRequest& request) { | 88 const test_server::HttpRequest& request) { |
| 87 std::unique_ptr<test_server::BasicHttpResponse> http_response( | 89 std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 88 new test_server::BasicHttpResponse()); | 90 new test_server::BasicHttpResponse()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 *kbps = downlink_throughput_kbps_; | 122 *kbps = downlink_throughput_kbps_; |
| 121 return true; | 123 return true; |
| 122 } | 124 } |
| 123 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); | 125 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); |
| 124 } | 126 } |
| 125 | 127 |
| 126 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 128 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; |
| 127 using NetworkQualityEstimator::OnConnectionTypeChanged; | 129 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 128 | 130 |
| 129 private: | 131 private: |
| 132 // True if the network type and network id are currently simulated. This |
| 133 // ensures that the correctness of the test does not depend on the |
| 134 // actual network type of the device on which the test is running. |
| 135 bool current_network_simulated_; |
| 136 |
| 130 // NetworkQualityEstimator implementation that returns the overridden network | 137 // NetworkQualityEstimator implementation that returns the overridden network |
| 131 // id (instead of invoking platform APIs). | 138 // id (instead of invoking platform APIs). |
| 132 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 139 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
| 140 // GetCurrentNetworkID should be called only if the network type is |
| 141 // currently simulated. |
| 142 EXPECT_TRUE(current_network_simulated_); |
| 143 |
| 133 return NetworkQualityEstimator::NetworkID(current_network_type_, | 144 return NetworkQualityEstimator::NetworkID(current_network_type_, |
| 134 current_network_id_); | 145 current_network_id_); |
| 135 } | 146 } |
| 136 | 147 |
| 137 NetworkChangeNotifier::ConnectionType current_network_type_; | 148 NetworkChangeNotifier::ConnectionType current_network_type_; |
| 138 std::string current_network_id_; | 149 std::string current_network_id_; |
| 139 | 150 |
| 140 bool url_rtt_set_; | 151 bool url_rtt_set_; |
| 141 base::TimeDelta url_rtt_; | 152 base::TimeDelta url_rtt_; |
| 142 | 153 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 421 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 411 } | 422 } |
| 412 | 423 |
| 413 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 424 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
| 414 // no variation params are specified. | 425 // no variation params are specified. |
| 415 TEST(NetworkQualityEstimatorTest, ObtainThresholdsNone) { | 426 TEST(NetworkQualityEstimatorTest, ObtainThresholdsNone) { |
| 416 std::map<std::string, std::string> variation_params; | 427 std::map<std::string, std::string> variation_params; |
| 417 | 428 |
| 418 TestNetworkQualityEstimator estimator(variation_params); | 429 TestNetworkQualityEstimator estimator(variation_params); |
| 419 | 430 |
| 431 // Simulate the connection type as Wi-Fi so that GetEffectiveConnectionType |
| 432 // does not return Offline if the device is offline. |
| 433 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 434 "test"); |
| 435 |
| 420 const struct { | 436 const struct { |
| 421 int32_t rtt_msec; | 437 int32_t rtt_msec; |
| 422 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 438 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 423 } tests[] = { | 439 } tests[] = { |
| 424 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 440 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 425 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 441 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 426 }; | 442 }; |
| 427 | 443 |
| 428 for (const auto& test : tests) { | 444 for (const auto& test : tests) { |
| 429 estimator.set_url_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 445 estimator.set_url_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 430 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 446 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 431 } | 447 } |
| 432 } | 448 } |
| 433 | 449 |
| 450 // Tests that |GetEffectiveConnectionType| returns |
| 451 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline. |
| 452 TEST(NetworkQualityEstimatorTest, Offline) { |
| 453 std::map<std::string, std::string> variation_params; |
| 454 |
| 455 TestNetworkQualityEstimator estimator(variation_params); |
| 456 |
| 457 const struct { |
| 458 NetworkChangeNotifier::ConnectionType connection_type; |
| 459 NetworkQualityEstimator::EffectiveConnectionType expected_connection_type; |
| 460 } tests[] = { |
| 461 {NetworkChangeNotifier::CONNECTION_2G, |
| 462 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN}, |
| 463 {NetworkChangeNotifier::CONNECTION_NONE, |
| 464 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 465 {NetworkChangeNotifier::CONNECTION_3G, |
| 466 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN}, |
| 467 }; |
| 468 |
| 469 for (const auto& test : tests) { |
| 470 estimator.SimulateNetworkChangeTo(test.connection_type, "test"); |
| 471 EXPECT_EQ(test.expected_connection_type, |
| 472 estimator.GetEffectiveConnectionType()); |
| 473 } |
| 474 } |
| 475 |
| 434 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 476 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
| 435 // only RTT thresholds are specified in the variation params. | 477 // only RTT thresholds are specified in the variation params. |
| 436 TEST(NetworkQualityEstimatorTest, ObtainThresholdsOnlyRTT) { | 478 TEST(NetworkQualityEstimatorTest, ObtainThresholdsOnlyRTT) { |
| 437 std::map<std::string, std::string> variation_params; | 479 std::map<std::string, std::string> variation_params; |
| 438 | 480 |
| 439 variation_params["Offline.ThresholdMedianURLRTTMsec"] = "4000"; | 481 variation_params["Offline.ThresholdMedianURLRTTMsec"] = "4000"; |
| 440 variation_params["Slow2G.ThresholdMedianURLRTTMsec"] = "2000"; | 482 variation_params["Slow2G.ThresholdMedianURLRTTMsec"] = "2000"; |
| 441 variation_params["2G.ThresholdMedianURLRTTMsec"] = "1000"; | 483 variation_params["2G.ThresholdMedianURLRTTMsec"] = "1000"; |
| 442 variation_params["3G.ThresholdMedianURLRTTMsec"] = "500"; | 484 variation_params["3G.ThresholdMedianURLRTTMsec"] = "500"; |
| 443 variation_params["4G.ThresholdMedianURLRTTMsec"] = "300"; | 485 variation_params["4G.ThresholdMedianURLRTTMsec"] = "300"; |
| 444 variation_params["Broadband.ThresholdMedianURLRTTMsec"] = "100"; | 486 variation_params["Broadband.ThresholdMedianURLRTTMsec"] = "100"; |
| 445 | 487 |
| 446 TestNetworkQualityEstimator estimator(variation_params); | 488 TestNetworkQualityEstimator estimator(variation_params); |
| 447 | 489 |
| 490 // Simulate the connection type as Wi-Fi so that GetEffectiveConnectionType |
| 491 // does not return Offline if the device is offline. |
| 492 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 493 "test"); |
| 494 |
| 448 const struct { | 495 const struct { |
| 449 int32_t rtt_msec; | 496 int32_t rtt_msec; |
| 450 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 497 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 451 } tests[] = { | 498 } tests[] = { |
| 452 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 499 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 453 {4000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 500 {4000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 454 {3000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 501 {3000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 455 {2000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 502 {2000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 456 {1500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, | 503 {1500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, |
| 457 {1000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, | 504 {1000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 484 | 531 |
| 485 variation_params["Offline.ThresholdMedianKbps"] = "10"; | 532 variation_params["Offline.ThresholdMedianKbps"] = "10"; |
| 486 variation_params["Slow2G.ThresholdMedianKbps"] = "100"; | 533 variation_params["Slow2G.ThresholdMedianKbps"] = "100"; |
| 487 variation_params["2G.ThresholdMedianKbps"] = "300"; | 534 variation_params["2G.ThresholdMedianKbps"] = "300"; |
| 488 variation_params["3G.ThresholdMedianKbps"] = "500"; | 535 variation_params["3G.ThresholdMedianKbps"] = "500"; |
| 489 variation_params["4G.ThresholdMedianKbps"] = "1000"; | 536 variation_params["4G.ThresholdMedianKbps"] = "1000"; |
| 490 variation_params["Broadband.ThresholdMedianKbps"] = "2000"; | 537 variation_params["Broadband.ThresholdMedianKbps"] = "2000"; |
| 491 | 538 |
| 492 TestNetworkQualityEstimator estimator(variation_params); | 539 TestNetworkQualityEstimator estimator(variation_params); |
| 493 | 540 |
| 541 // Simulate the connection type as Wi-Fi so that GetEffectiveConnectionType |
| 542 // does not return Offline if the device is offline. |
| 543 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 544 "test"); |
| 545 |
| 494 const struct { | 546 const struct { |
| 495 int32_t rtt_msec; | 547 int32_t rtt_msec; |
| 496 int32_t downlink_throughput_kbps; | 548 int32_t downlink_throughput_kbps; |
| 497 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 549 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 498 } tests[] = { | 550 } tests[] = { |
| 499 // Set RTT to a very low value to observe the effect of throughput. | 551 // Set RTT to a very low value to observe the effect of throughput. |
| 500 // Throughout is the bottleneck. | 552 // Throughout is the bottleneck. |
| 501 {1, 5, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 553 {1, 5, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 502 {1, 10, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 554 {1, 10, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 503 {1, 50, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 555 {1, 50, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| (...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1200 // At least one notification should be received per socket performance | 1252 // At least one notification should be received per socket performance |
| 1201 // watcher. | 1253 // watcher. |
| 1202 EXPECT_LE(1U, after_count_tcp_rtt_observations - | 1254 EXPECT_LE(1U, after_count_tcp_rtt_observations - |
| 1203 before_count_tcp_rtt_observations) | 1255 before_count_tcp_rtt_observations) |
| 1204 << i; | 1256 << i; |
| 1205 } | 1257 } |
| 1206 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); | 1258 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 1207 } | 1259 } |
| 1208 | 1260 |
| 1209 } // namespace net | 1261 } // namespace net |
| OLD | NEW |