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 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 {100, EFFECTIVE_CONNECTION_TYPE_4G}, | 537 {100, EFFECTIVE_CONNECTION_TYPE_4G}, |
538 {20, EFFECTIVE_CONNECTION_TYPE_4G}, | 538 {20, EFFECTIVE_CONNECTION_TYPE_4G}, |
539 }; | 539 }; |
540 | 540 |
541 for (const auto& test : tests) { | 541 for (const auto& test : tests) { |
542 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 542 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
543 estimator.set_recent_http_rtt( | 543 estimator.set_recent_http_rtt( |
544 base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 544 base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
545 estimator.set_downlink_throughput_kbps(INT32_MAX); | 545 estimator.set_downlink_throughput_kbps(INT32_MAX); |
546 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); | 546 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); |
| 547 // Run one main frame request to force recomputation of effective connection |
| 548 // type. |
| 549 estimator.RunOneRequest(); |
547 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 550 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
548 } | 551 } |
549 } | 552 } |
550 | 553 |
551 // Tests that default transport RTT thresholds for different effective | 554 // Tests that default transport RTT thresholds for different effective |
552 // connection types are correctly set. | 555 // connection types are correctly set. |
553 TEST(NetworkQualityEstimatorTest, DefaultTransportRTTBasedThresholds) { | 556 TEST(NetworkQualityEstimatorTest, DefaultTransportRTTBasedThresholds) { |
554 const struct { | 557 const struct { |
555 bool override_defaults_using_variation_params; | 558 bool override_defaults_using_variation_params; |
556 int32_t transport_rtt_msec; | 559 int32_t transport_rtt_msec; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 // does not return Offline if the device is offline. | 594 // does not return Offline if the device is offline. |
592 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, | 595 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, |
593 "test"); | 596 "test"); |
594 | 597 |
595 estimator.set_transport_rtt( | 598 estimator.set_transport_rtt( |
596 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); | 599 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); |
597 estimator.set_recent_transport_rtt( | 600 estimator.set_recent_transport_rtt( |
598 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); | 601 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); |
599 estimator.set_downlink_throughput_kbps(INT32_MAX); | 602 estimator.set_downlink_throughput_kbps(INT32_MAX); |
600 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); | 603 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); |
| 604 // Run one main frame request to force recomputation of effective connection |
| 605 // type. |
| 606 estimator.RunOneRequest(); |
601 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 607 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
602 } | 608 } |
603 } | 609 } |
604 | 610 |
605 // Tests that default HTTP RTT thresholds for different effective | 611 // Tests that default HTTP RTT thresholds for different effective |
606 // connection types are correctly set. | 612 // connection types are correctly set. |
607 TEST(NetworkQualityEstimatorTest, DefaultHttpRTTBasedThresholds) { | 613 TEST(NetworkQualityEstimatorTest, DefaultHttpRTTBasedThresholds) { |
608 const struct { | 614 const struct { |
609 bool override_defaults_using_variation_params; | 615 bool override_defaults_using_variation_params; |
610 int32_t http_rtt_msec; | 616 int32_t http_rtt_msec; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 // does not return Offline if the device is offline. | 649 // does not return Offline if the device is offline. |
644 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, | 650 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, |
645 "test"); | 651 "test"); |
646 | 652 |
647 estimator.set_http_rtt( | 653 estimator.set_http_rtt( |
648 base::TimeDelta::FromMilliseconds(test.http_rtt_msec)); | 654 base::TimeDelta::FromMilliseconds(test.http_rtt_msec)); |
649 estimator.set_recent_http_rtt( | 655 estimator.set_recent_http_rtt( |
650 base::TimeDelta::FromMilliseconds(test.http_rtt_msec)); | 656 base::TimeDelta::FromMilliseconds(test.http_rtt_msec)); |
651 estimator.set_downlink_throughput_kbps(INT32_MAX); | 657 estimator.set_downlink_throughput_kbps(INT32_MAX); |
652 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); | 658 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); |
| 659 // Run one main frame request to force recomputation of effective connection |
| 660 // type. |
| 661 estimator.RunOneRequest(); |
653 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 662 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
654 } | 663 } |
655 } | 664 } |
656 | 665 |
657 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 666 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
658 // only transport RTT thresholds are specified in the variation params. | 667 // only transport RTT thresholds are specified in the variation params. |
659 TEST(NetworkQualityEstimatorTest, ObtainThresholdsOnlyTransportRTT) { | 668 TEST(NetworkQualityEstimatorTest, ObtainThresholdsOnlyTransportRTT) { |
660 std::map<std::string, std::string> variation_params; | 669 std::map<std::string, std::string> variation_params; |
661 variation_params["effective_connection_type_algorithm"] = | 670 variation_params["effective_connection_type_algorithm"] = |
662 "TransportRTTOrDownstreamThroughput"; | 671 "TransportRTTOrDownstreamThroughput"; |
(...skipping 30 matching lines...) Expand all Loading... |
693 {20, EFFECTIVE_CONNECTION_TYPE_4G}, | 702 {20, EFFECTIVE_CONNECTION_TYPE_4G}, |
694 }; | 703 }; |
695 | 704 |
696 for (const auto& test : tests) { | 705 for (const auto& test : tests) { |
697 estimator.set_transport_rtt( | 706 estimator.set_transport_rtt( |
698 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); | 707 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); |
699 estimator.set_recent_transport_rtt( | 708 estimator.set_recent_transport_rtt( |
700 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); | 709 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); |
701 estimator.set_downlink_throughput_kbps(INT32_MAX); | 710 estimator.set_downlink_throughput_kbps(INT32_MAX); |
702 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); | 711 estimator.set_recent_downlink_throughput_kbps(INT32_MAX); |
| 712 // Run one main frame request to force recomputation of effective connection |
| 713 // type. |
| 714 estimator.RunOneRequest(); |
703 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 715 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
704 } | 716 } |
705 } | 717 } |
706 | 718 |
707 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 719 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
708 // both HTTP RTT and throughput thresholds are specified in the variation | 720 // both HTTP RTT and throughput thresholds are specified in the variation |
709 // params. | 721 // params. |
710 TEST(NetworkQualityEstimatorTest, ObtainThresholdsHttpRTTandThroughput) { | 722 TEST(NetworkQualityEstimatorTest, ObtainThresholdsHttpRTTandThroughput) { |
711 std::map<std::string, std::string> variation_params; | 723 std::map<std::string, std::string> variation_params; |
712 | 724 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 {700, 25000, EFFECTIVE_CONNECTION_TYPE_3G}, | 765 {700, 25000, EFFECTIVE_CONNECTION_TYPE_3G}, |
754 }; | 766 }; |
755 | 767 |
756 for (const auto& test : tests) { | 768 for (const auto& test : tests) { |
757 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 769 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
758 estimator.set_recent_http_rtt( | 770 estimator.set_recent_http_rtt( |
759 base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 771 base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
760 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); | 772 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); |
761 estimator.set_recent_downlink_throughput_kbps( | 773 estimator.set_recent_downlink_throughput_kbps( |
762 test.downlink_throughput_kbps); | 774 test.downlink_throughput_kbps); |
| 775 // Run one main frame request to force recomputation of effective connection |
| 776 // type. |
| 777 estimator.RunOneRequest(); |
763 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 778 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
764 } | 779 } |
765 } | 780 } |
766 | 781 |
767 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 782 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
768 // both transport RTT and throughput thresholds are specified in the variation | 783 // both transport RTT and throughput thresholds are specified in the variation |
769 // params. | 784 // params. |
770 TEST(NetworkQualityEstimatorTest, ObtainThresholdsTransportRTTandThroughput) { | 785 TEST(NetworkQualityEstimatorTest, ObtainThresholdsTransportRTTandThroughput) { |
771 std::map<std::string, std::string> variation_params; | 786 std::map<std::string, std::string> variation_params; |
772 variation_params["effective_connection_type_algorithm"] = | 787 variation_params["effective_connection_type_algorithm"] = |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 }; | 831 }; |
817 | 832 |
818 for (const auto& test : tests) { | 833 for (const auto& test : tests) { |
819 estimator.set_transport_rtt( | 834 estimator.set_transport_rtt( |
820 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); | 835 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); |
821 estimator.set_recent_transport_rtt( | 836 estimator.set_recent_transport_rtt( |
822 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); | 837 base::TimeDelta::FromMilliseconds(test.transport_rtt_msec)); |
823 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); | 838 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); |
824 estimator.set_recent_downlink_throughput_kbps( | 839 estimator.set_recent_downlink_throughput_kbps( |
825 test.downlink_throughput_kbps); | 840 test.downlink_throughput_kbps); |
| 841 // Run one main frame request to force recomputation of effective connection |
| 842 // type. |
| 843 estimator.RunOneRequest(); |
826 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 844 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
827 } | 845 } |
828 } | 846 } |
829 | 847 |
830 // Tests if |weight_multiplier_per_second_| is set to correct value for various | 848 // Tests if |weight_multiplier_per_second_| is set to correct value for various |
831 // values of half life parameter. | 849 // values of half life parameter. |
832 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 850 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
833 std::map<std::string, std::string> variation_params; | 851 std::map<std::string, std::string> variation_params; |
834 | 852 |
835 const struct { | 853 const struct { |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1275 estimator.AddEffectiveConnectionTypeObserver(&observer); | 1293 estimator.AddEffectiveConnectionTypeObserver(&observer); |
1276 estimator.SetTickClockForTesting(std::move(tick_clock)); | 1294 estimator.SetTickClockForTesting(std::move(tick_clock)); |
1277 | 1295 |
1278 TestDelegate test_delegate; | 1296 TestDelegate test_delegate; |
1279 TestURLRequestContext context(true); | 1297 TestURLRequestContext context(true); |
1280 context.set_network_quality_estimator(&estimator); | 1298 context.set_network_quality_estimator(&estimator); |
1281 context.Init(); | 1299 context.Init(); |
1282 | 1300 |
1283 EXPECT_EQ(0U, observer.effective_connection_types().size()); | 1301 EXPECT_EQ(0U, observer.effective_connection_types().size()); |
1284 | 1302 |
1285 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); | 1303 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
1286 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); | 1304 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); |
1287 | 1305 |
1288 std::unique_ptr<URLRequest> request(context.CreateRequest( | 1306 std::unique_ptr<URLRequest> request(context.CreateRequest( |
1289 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1307 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
1290 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); | 1308 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); |
1291 request->Start(); | 1309 request->Start(); |
1292 base::RunLoop().Run(); | 1310 base::RunLoop().Run(); |
1293 EXPECT_EQ(1U, observer.effective_connection_types().size()); | 1311 EXPECT_EQ(1U, observer.effective_connection_types().size()); |
1294 histogram_tester.ExpectUniqueSample( | 1312 histogram_tester.ExpectUniqueSample( |
1295 "NQE.MainFrame.EffectiveConnectionType.Unknown", | 1313 "NQE.MainFrame.EffectiveConnectionType.Unknown", |
(...skipping 10 matching lines...) Expand all Loading... |
1306 | 1324 |
1307 // Change in connection type should send out notification to the observers. | 1325 // Change in connection type should send out notification to the observers. |
1308 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 1326 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
1309 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, | 1327 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, |
1310 "test"); | 1328 "test"); |
1311 EXPECT_EQ(2U, observer.effective_connection_types().size()); | 1329 EXPECT_EQ(2U, observer.effective_connection_types().size()); |
1312 | 1330 |
1313 // A change in effective connection type does not trigger notification to the | 1331 // A change in effective connection type does not trigger notification to the |
1314 // observers, since it is not accompanied by any new observation or a network | 1332 // observers, since it is not accompanied by any new observation or a network |
1315 // change event. | 1333 // change event. |
1316 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 1334 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
1317 EXPECT_EQ(2U, observer.effective_connection_types().size()); | 1335 EXPECT_EQ(2U, observer.effective_connection_types().size()); |
1318 } | 1336 } |
1319 | 1337 |
1320 // Tests that the effective connection type is computed on every RTT | 1338 // Tests that the effective connection type is computed on every RTT |
1321 // observation if the last computed effective connection type was unknown. | 1339 // observation if the last computed effective connection type was unknown. |
1322 TEST(NetworkQualityEstimatorTest, UnknownEffectiveConnectionType) { | 1340 TEST(NetworkQualityEstimatorTest, UnknownEffectiveConnectionType) { |
1323 std::unique_ptr<base::SimpleTestTickClock> tick_clock( | 1341 std::unique_ptr<base::SimpleTestTickClock> tick_clock( |
1324 new base::SimpleTestTickClock()); | 1342 new base::SimpleTestTickClock()); |
1325 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); | 1343 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); |
1326 | 1344 |
1327 TestEffectiveConnectionTypeObserver observer; | 1345 TestEffectiveConnectionTypeObserver observer; |
1328 std::map<std::string, std::string> variation_params; | 1346 std::map<std::string, std::string> variation_params; |
1329 TestNetworkQualityEstimator estimator(variation_params); | 1347 TestNetworkQualityEstimator estimator(variation_params); |
1330 estimator.SetTickClockForTesting(std::move(tick_clock)); | 1348 estimator.SetTickClockForTesting(std::move(tick_clock)); |
1331 estimator.AddEffectiveConnectionTypeObserver(&observer); | 1349 estimator.AddEffectiveConnectionTypeObserver(&observer); |
1332 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); | 1350 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); |
1333 | 1351 |
1334 size_t expected_effective_connection_type_notifications = 0; | 1352 size_t expected_effective_connection_type_notifications = 0; |
1335 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_UNKNOWN); | 1353 estimator.set_recent_effective_connection_type( |
| 1354 EFFECTIVE_CONNECTION_TYPE_UNKNOWN); |
| 1355 // Run one main frame request to force recomputation of effective connection |
| 1356 // type. |
| 1357 estimator.RunOneRequest(); |
1336 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, | 1358 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, |
1337 "test"); | 1359 "test"); |
1338 | 1360 |
1339 NetworkQualityEstimator::RttObservation rtt_observation( | 1361 NetworkQualityEstimator::RttObservation rtt_observation( |
1340 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), | 1362 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), |
1341 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST); | 1363 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST); |
1342 | 1364 |
1343 for (size_t i = 0; i < 10; ++i) { | 1365 for (size_t i = 0; i < 10; ++i) { |
1344 estimator.NotifyObserversOfRTT(rtt_observation); | 1366 estimator.NotifyObserversOfRTT(rtt_observation); |
1345 EXPECT_EQ(expected_effective_connection_type_notifications, | 1367 EXPECT_EQ(expected_effective_connection_type_notifications, |
1346 observer.effective_connection_types().size()); | 1368 observer.effective_connection_types().size()); |
1347 } | 1369 } |
1348 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 1370 estimator.set_recent_effective_connection_type( |
| 1371 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
1349 // Even though there are 10 RTT samples already available, the addition of one | 1372 // Even though there are 10 RTT samples already available, the addition of one |
1350 // more RTT sample should trigger recomputation of the effective connection | 1373 // more RTT sample should trigger recomputation of the effective connection |
1351 // type since the last computed effective connection type was unknown. | 1374 // type since the last computed effective connection type was unknown. |
1352 estimator.NotifyObserversOfRTT(NetworkQualityEstimator::RttObservation( | 1375 estimator.NotifyObserversOfRTT(NetworkQualityEstimator::RttObservation( |
1353 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), | 1376 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), |
1354 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); | 1377 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
1355 ++expected_effective_connection_type_notifications; | 1378 ++expected_effective_connection_type_notifications; |
1356 EXPECT_EQ(expected_effective_connection_type_notifications, | 1379 EXPECT_EQ(expected_effective_connection_type_notifications, |
1357 observer.effective_connection_types().size()); | 1380 observer.effective_connection_types().size()); |
1358 } | 1381 } |
(...skipping 15 matching lines...) Expand all Loading... |
1374 "test"); | 1397 "test"); |
1375 estimator.AddEffectiveConnectionTypeObserver(&observer); | 1398 estimator.AddEffectiveConnectionTypeObserver(&observer); |
1376 | 1399 |
1377 TestDelegate test_delegate; | 1400 TestDelegate test_delegate; |
1378 TestURLRequestContext context(true); | 1401 TestURLRequestContext context(true); |
1379 context.set_network_quality_estimator(&estimator); | 1402 context.set_network_quality_estimator(&estimator); |
1380 context.Init(); | 1403 context.Init(); |
1381 | 1404 |
1382 EXPECT_EQ(0U, observer.effective_connection_types().size()); | 1405 EXPECT_EQ(0U, observer.effective_connection_types().size()); |
1383 | 1406 |
1384 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); | 1407 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
1385 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); | 1408 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); |
1386 | 1409 |
1387 std::unique_ptr<URLRequest> request(context.CreateRequest( | 1410 std::unique_ptr<URLRequest> request(context.CreateRequest( |
1388 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1411 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
1389 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); | 1412 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); |
1390 request->Start(); | 1413 request->Start(); |
1391 base::RunLoop().Run(); | 1414 base::RunLoop().Run(); |
1392 EXPECT_EQ(1U, observer.effective_connection_types().size()); | 1415 EXPECT_EQ(1U, observer.effective_connection_types().size()); |
1393 histogram_tester.ExpectUniqueSample( | 1416 histogram_tester.ExpectUniqueSample( |
1394 "NQE.MainFrame.EffectiveConnectionType.WiFi", | 1417 "NQE.MainFrame.EffectiveConnectionType.WiFi", |
1395 EFFECTIVE_CONNECTION_TYPE_2G, 1); | 1418 EFFECTIVE_CONNECTION_TYPE_2G, 1); |
1396 | 1419 |
1397 size_t expected_effective_connection_type_notifications = 1; | 1420 size_t expected_effective_connection_type_notifications = 1; |
1398 EXPECT_EQ(expected_effective_connection_type_notifications, | 1421 EXPECT_EQ(expected_effective_connection_type_notifications, |
1399 observer.effective_connection_types().size()); | 1422 observer.effective_connection_types().size()); |
1400 | 1423 |
1401 EXPECT_EQ(expected_effective_connection_type_notifications, | 1424 EXPECT_EQ(expected_effective_connection_type_notifications, |
1402 estimator.rtt_observations_.Size()); | 1425 estimator.rtt_observations_.Size()); |
1403 | 1426 |
1404 // Increase the number of RTT observations. Every time the number of RTT | 1427 // Increase the number of RTT observations. Every time the number of RTT |
1405 // observations is more than doubled, effective connection type must be | 1428 // observations is more than doubled, effective connection type must be |
1406 // recomputed and notified to observers. | 1429 // recomputed and notified to observers. |
1407 for (size_t repetition = 0; repetition < 2; ++repetition) { | 1430 for (size_t repetition = 0; repetition < 2; ++repetition) { |
1408 // Change the effective connection type so that the observers are | 1431 // Change the effective connection type so that the observers are |
1409 // notified when the effective connection type is recomputed. | 1432 // notified when the effective connection type is recomputed. |
1410 if (repetition % 2 == 0) { | 1433 if (repetition % 2 == 0) { |
1411 estimator.set_effective_connection_type( | 1434 estimator.set_recent_effective_connection_type( |
1412 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 1435 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
1413 } else { | 1436 } else { |
1414 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 1437 estimator.set_recent_effective_connection_type( |
| 1438 EFFECTIVE_CONNECTION_TYPE_3G); |
1415 } | 1439 } |
1416 size_t rtt_observations_count = estimator.rtt_observations_.Size(); | 1440 size_t rtt_observations_count = estimator.rtt_observations_.Size() * 0.5; |
1417 // Increase the number of RTT observations to more than twice the number | 1441 // Increase the number of RTT observations to more than twice the number |
1418 // of current observations. This should trigger recomputation of | 1442 // of current observations. This should trigger recomputation of |
1419 // effective connection type. | 1443 // effective connection type. |
1420 for (size_t i = 0; i < rtt_observations_count + 1; ++i) { | 1444 for (size_t i = 0; i < rtt_observations_count + 1; ++i) { |
1421 estimator.rtt_observations_.AddObservation( | 1445 estimator.rtt_observations_.AddObservation( |
1422 NetworkQualityEstimator::RttObservation( | 1446 NetworkQualityEstimator::RttObservation( |
1423 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), | 1447 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), |
1424 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); | 1448 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
1425 | 1449 |
1426 estimator.NotifyObserversOfRTT(NetworkQualityEstimator::RttObservation( | 1450 estimator.NotifyObserversOfRTT(NetworkQualityEstimator::RttObservation( |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1975 | 1999 |
1976 TEST(NetworkQualityEstimatorTest, CacheObserver) { | 2000 TEST(NetworkQualityEstimatorTest, CacheObserver) { |
1977 TestNetworkQualitiesCacheObserver observer; | 2001 TestNetworkQualitiesCacheObserver observer; |
1978 std::map<std::string, std::string> variation_params; | 2002 std::map<std::string, std::string> variation_params; |
1979 TestNetworkQualityEstimator estimator(variation_params); | 2003 TestNetworkQualityEstimator estimator(variation_params); |
1980 | 2004 |
1981 // Add |observer| as a persistent caching observer. | 2005 // Add |observer| as a persistent caching observer. |
1982 estimator.NetworkQualityStoreForTesting()->AddNetworkQualitiesCacheObserver( | 2006 estimator.NetworkQualityStoreForTesting()->AddNetworkQualitiesCacheObserver( |
1983 &observer); | 2007 &observer); |
1984 | 2008 |
1985 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 2009 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
1986 estimator.SimulateNetworkChange( | 2010 estimator.SimulateNetworkChange( |
1987 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test3g"); | 2011 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test3g"); |
1988 estimator.RunOneRequest(); | 2012 estimator.RunOneRequest(); |
1989 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); | 2013 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); |
1990 EXPECT_EQ("test3g", observer.network_id().id); | 2014 EXPECT_EQ("test3g", observer.network_id().id); |
1991 | 2015 |
1992 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); | 2016 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
1993 estimator.SimulateNetworkChange( | 2017 estimator.SimulateNetworkChange( |
1994 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test2g"); | 2018 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test2g"); |
1995 // One notification should be received for the previous network | 2019 // One notification should be received for the previous network |
1996 // ("test3g") right before the connection change event. The second | 2020 // ("test3g") right before the connection change event. The second |
1997 // notification should be received for the second network ("test2g"). | 2021 // notification should be received for the second network ("test2g"). |
| 2022 base::RunLoop().RunUntilIdle(); |
1998 EXPECT_EQ(2u, observer.get_notification_received_and_reset()); | 2023 EXPECT_EQ(2u, observer.get_notification_received_and_reset()); |
1999 estimator.RunOneRequest(); | 2024 estimator.RunOneRequest(); |
2000 EXPECT_EQ("test2g", observer.network_id().id); | 2025 EXPECT_EQ("test2g", observer.network_id().id); |
2001 | 2026 |
2002 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_4G); | 2027 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_4G); |
2003 // Start multiple requests, but there should be only one notification | 2028 // Start multiple requests, but there should be only one notification |
2004 // received, since the effective connection type does not change. | 2029 // received, since the effective connection type does not change. |
2005 estimator.RunOneRequest(); | 2030 estimator.RunOneRequest(); |
2006 estimator.RunOneRequest(); | 2031 estimator.RunOneRequest(); |
2007 estimator.RunOneRequest(); | 2032 estimator.RunOneRequest(); |
2008 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); | 2033 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); |
2009 | 2034 |
2010 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); | 2035 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
2011 estimator.RunOneRequest(); | 2036 estimator.RunOneRequest(); |
2012 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); | 2037 EXPECT_EQ(1u, observer.get_notification_received_and_reset()); |
2013 | 2038 |
2014 // Remove |observer|, and it should not receive any notifications. | 2039 // Remove |observer|, and it should not receive any notifications. |
2015 estimator.NetworkQualityStoreForTesting() | 2040 estimator.NetworkQualityStoreForTesting() |
2016 ->RemoveNetworkQualitiesCacheObserver(&observer); | 2041 ->RemoveNetworkQualitiesCacheObserver(&observer); |
2017 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 2042 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
2018 estimator.SimulateNetworkChange( | 2043 estimator.SimulateNetworkChange( |
2019 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test2g"); | 2044 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test2g"); |
2020 EXPECT_EQ(0u, observer.get_notification_received_and_reset()); | 2045 EXPECT_EQ(0u, observer.get_notification_received_and_reset()); |
2021 estimator.RunOneRequest(); | 2046 estimator.RunOneRequest(); |
2022 EXPECT_EQ(0u, observer.get_notification_received_and_reset()); | 2047 EXPECT_EQ(0u, observer.get_notification_received_and_reset()); |
2023 } | 2048 } |
2024 | 2049 |
2025 // Tests that the value of the effective connection type can be forced through | 2050 // Tests that the value of the effective connection type can be forced through |
2026 // field trial parameters. | 2051 // field trial parameters. |
2027 TEST(NetworkQualityEstimatorTest, | 2052 TEST(NetworkQualityEstimatorTest, |
2028 ForceEffectiveConnectionTypeThroughFieldTrial) { | 2053 ForceEffectiveConnectionTypeThroughFieldTrial) { |
2029 for (int i = 0; i < EFFECTIVE_CONNECTION_TYPE_LAST; ++i) { | 2054 for (int i = 0; i < EFFECTIVE_CONNECTION_TYPE_LAST; ++i) { |
2030 std::map<std::string, std::string> variation_params; | 2055 std::map<std::string, std::string> variation_params; |
2031 variation_params["force_effective_connection_type"] = | 2056 variation_params["force_effective_connection_type"] = |
2032 GetNameForEffectiveConnectionType( | 2057 GetNameForEffectiveConnectionType( |
2033 static_cast<EffectiveConnectionType>(i)); | 2058 static_cast<EffectiveConnectionType>(i)); |
2034 TestNetworkQualityEstimator estimator(variation_params); | 2059 TestNetworkQualityEstimator estimator(variation_params); |
2035 EXPECT_EQ(i, estimator.GetEffectiveConnectionType()); | |
2036 | 2060 |
2037 TestEffectiveConnectionTypeObserver observer; | 2061 TestEffectiveConnectionTypeObserver observer; |
2038 estimator.AddEffectiveConnectionTypeObserver(&observer); | 2062 estimator.AddEffectiveConnectionTypeObserver(&observer); |
2039 | 2063 |
2040 TestDelegate test_delegate; | 2064 TestDelegate test_delegate; |
2041 TestURLRequestContext context(true); | 2065 TestURLRequestContext context(true); |
2042 context.set_network_quality_estimator(&estimator); | 2066 context.set_network_quality_estimator(&estimator); |
2043 context.Init(); | 2067 context.Init(); |
2044 | 2068 |
2045 EXPECT_EQ(0U, observer.effective_connection_types().size()); | 2069 EXPECT_EQ(0U, observer.effective_connection_types().size()); |
2046 | 2070 |
2047 std::unique_ptr<URLRequest> request(context.CreateRequest( | 2071 std::unique_ptr<URLRequest> request(context.CreateRequest( |
2048 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 2072 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
2049 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); | 2073 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); |
2050 request->Start(); | 2074 request->Start(); |
2051 base::RunLoop().Run(); | 2075 base::RunLoop().Run(); |
2052 | 2076 |
| 2077 EXPECT_EQ(i, estimator.GetEffectiveConnectionType()); |
| 2078 |
2053 size_t expected_count = static_cast<EffectiveConnectionType>(i) == | 2079 size_t expected_count = static_cast<EffectiveConnectionType>(i) == |
2054 EFFECTIVE_CONNECTION_TYPE_UNKNOWN | 2080 EFFECTIVE_CONNECTION_TYPE_UNKNOWN |
2055 ? 0 | 2081 ? 0 |
2056 : 1; | 2082 : 1; |
2057 ASSERT_EQ(expected_count, observer.effective_connection_types().size()); | 2083 ASSERT_EQ(expected_count, observer.effective_connection_types().size()); |
2058 if (expected_count == 1) { | 2084 if (expected_count == 1) { |
2059 EffectiveConnectionType last_notified_type = | 2085 EffectiveConnectionType last_notified_type = |
2060 observer.effective_connection_types().at( | 2086 observer.effective_connection_types().at( |
2061 observer.effective_connection_types().size() - 1); | 2087 observer.effective_connection_types().size() - 1); |
2062 EXPECT_EQ(i, last_notified_type); | 2088 EXPECT_EQ(i, last_notified_type); |
2063 } | 2089 } |
2064 } | 2090 } |
2065 } | 2091 } |
2066 | 2092 |
2067 } // namespace net | 2093 } // namespace net |
OLD | NEW |