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/base/network_quality_estimator.h" | 5 #include "net/base/network_quality_estimator.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <map> | 10 #include <map> |
(...skipping 14 matching lines...) Expand all Loading... | |
25 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 // Helps in setting the current network type and id. | 31 // Helps in setting the current network type and id. |
32 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { | 32 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { |
33 public: | 33 public: |
34 TestNetworkQualityEstimator( | 34 TestNetworkQualityEstimator( |
35 const std::map<std::string, std::string>& variation_params) | 35 const std::map<std::string, std::string>& variation_params, |
36 : NetworkQualityEstimator(scoped_ptr<net::ExternalEstimateProvider>(), | 36 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider) |
37 : NetworkQualityEstimator(external_estimate_provider.Pass(), | |
37 variation_params, | 38 variation_params, |
38 true, | 39 true, |
39 true) {} | 40 true) {} |
40 | 41 |
42 explicit TestNetworkQualityEstimator( | |
43 const std::map<std::string, std::string>& variation_params) | |
44 : TestNetworkQualityEstimator( | |
45 variation_params, | |
46 scoped_ptr<net::ExternalEstimateProvider>()) {} | |
47 | |
41 ~TestNetworkQualityEstimator() override {} | 48 ~TestNetworkQualityEstimator() override {} |
42 | 49 |
43 // Overrides the current network type and id. | 50 // Overrides the current network type and id. |
44 // Notifies network quality estimator of change in connection. | 51 // Notifies network quality estimator of change in connection. |
45 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, | 52 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, |
46 std::string network_id) { | 53 std::string network_id) { |
47 current_network_type_ = type; | 54 current_network_type_ = type; |
48 current_network_id_ = network_id; | 55 current_network_id_ = network_id; |
49 OnConnectionTypeChanged(type); | 56 OnConnectionTypeChanged(type); |
50 } | 57 } |
51 | 58 |
52 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 59 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; |
53 using NetworkQualityEstimator::OnConnectionTypeChanged; | 60 using NetworkQualityEstimator::OnConnectionTypeChanged; |
54 | 61 |
55 private: | 62 private: |
56 // NetworkQualityEstimator implementation that returns the overridden network | 63 // NetworkQualityEstimator implementation that returns the overridden network |
57 // id (instead of invoking platform APIs). | 64 // id (instead of invoking platform APIs). |
58 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 65 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
59 return NetworkQualityEstimator::NetworkID(current_network_type_, | 66 return NetworkQualityEstimator::NetworkID(current_network_type_, |
60 current_network_id_); | 67 current_network_id_); |
61 } | 68 } |
62 | 69 |
63 net::NetworkChangeNotifier::ConnectionType current_network_type_; | 70 net::NetworkChangeNotifier::ConnectionType current_network_type_; |
64 std::string current_network_id_; | 71 std::string current_network_id_; |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | |
65 }; | 74 }; |
66 | 75 |
67 } // namespace | 76 } // namespace |
68 | 77 |
69 namespace net { | 78 namespace net { |
70 | 79 |
71 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 80 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
72 net::test_server::EmbeddedTestServer embedded_test_server; | 81 net::test_server::EmbeddedTestServer embedded_test_server; |
73 embedded_test_server.ServeFilesFromDirectory( | 82 embedded_test_server.ServeFilesFromDirectory( |
74 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 83 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
647 EXPECT_EQ(100, rtt.InMilliseconds()); | 656 EXPECT_EQ(100, rtt.InMilliseconds()); |
648 | 657 |
649 int32_t downstream_throughput_kbps; | 658 int32_t downstream_throughput_kbps; |
650 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( | 659 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( |
651 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); | 660 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); |
652 EXPECT_TRUE(estimator.GetRecentMedianDownlinkThroughputKbps( | 661 EXPECT_TRUE(estimator.GetRecentMedianDownlinkThroughputKbps( |
653 now, &downstream_throughput_kbps)); | 662 now, &downstream_throughput_kbps)); |
654 EXPECT_EQ(100, downstream_throughput_kbps); | 663 EXPECT_EQ(100, downstream_throughput_kbps); |
655 } | 664 } |
656 | 665 |
666 class TestExternalEstimateProvider : public ExternalEstimateProvider { | |
667 public: | |
668 TestExternalEstimateProvider() | |
669 : time_since_last_update_(base::TimeDelta::FromSeconds(1)), | |
670 time_since_last_update_count_(0), | |
671 rtt_count_(0), | |
672 downstream_throughput_kbps_count_(0), | |
673 update_count_(0) {} | |
674 ~TestExternalEstimateProvider() override {} | |
675 | |
676 // ExternalEstimateProvider implementation: | |
677 bool GetRTT(base::TimeDelta* rtt) const override { | |
678 *rtt = base::TimeDelta::FromMilliseconds(1); | |
679 rtt_count_++; | |
680 return true; | |
681 } | |
682 | |
683 // ExternalEstimateProvider implementation: | |
684 bool GetDownstreamThroughputKbps( | |
685 int32_t* downstream_throughput_kbps) const override { | |
686 *downstream_throughput_kbps = 100; | |
687 downstream_throughput_kbps_count_++; | |
688 return true; | |
mmenke
2015/09/15 20:09:09
Maybe also have another test with an ExternalEstim
tbansal1
2015/09/15 22:33:07
Done.
| |
689 } | |
690 | |
691 // ExternalEstimateProvider implementation: | |
692 bool GetUpstreamThroughputKbps( | |
693 int32_t* upstream_throughput_kbps) const override { | |
694 // NetworkQualityEstimator does not support upstream throughput. | |
695 ADD_FAILURE(); | |
696 return false; | |
697 } | |
698 | |
699 // ExternalEstimateProvider implementation: | |
700 bool GetTimeSinceLastUpdate( | |
701 base::TimeDelta* time_since_last_update) const override { | |
702 *time_since_last_update = time_since_last_update_; | |
703 time_since_last_update_count_++; | |
704 return true; | |
705 } | |
706 | |
707 // ExternalEstimateProvider implementation: | |
708 void SetUpdatedEstimateDelegate(UpdatedEstimateDelegate* delegate) override {} | |
709 | |
710 // ExternalEstimateProvider implementation: | |
711 void Update() const override { update_count_++; } | |
712 | |
713 void set_time_since_last_update(base::TimeDelta time_since_last_update) { | |
714 time_since_last_update_ = time_since_last_update; | |
715 } | |
716 | |
717 size_t get_time_since_last_update_count() const { | |
mmenke
2015/09/15 20:09:09
-get. The get in get_rtt_count is because it's th
tbansal1
2015/09/15 22:33:08
I am confused.
This returns the number of times *G
| |
718 return time_since_last_update_count_; | |
719 } | |
720 size_t get_rtt_count() const { return rtt_count_; } | |
mmenke
2015/09/15 20:09:09
nit: Rename rtt_count_, too.
tbansal1
2015/09/15 22:33:07
Done.
| |
721 size_t get_downstream_throughput_kbps_count() const { | |
mmenke
2015/09/15 20:09:09
-get
tbansal1
2015/09/15 22:33:07
same here. this is counting number of times GetDow
| |
722 return downstream_throughput_kbps_count_; | |
723 } | |
724 size_t update_count() const { return update_count_; } | |
725 | |
726 private: | |
727 base::TimeDelta time_since_last_update_; | |
728 | |
729 mutable size_t time_since_last_update_count_; | |
730 mutable size_t rtt_count_; | |
731 mutable size_t downstream_throughput_kbps_count_; | |
732 mutable size_t update_count_; | |
733 | |
734 DISALLOW_COPY_AND_ASSIGN(TestExternalEstimateProvider); | |
735 }; | |
736 | |
737 // Tests if the external estimate provider is called in the constructor and | |
738 // on network change notification. | |
739 TEST(NetworkQualityEstimatorTest, TestExternalEstimateProvider) { | |
740 TestExternalEstimateProvider* test_external_estimate_provider = | |
741 new TestExternalEstimateProvider(); | |
742 scoped_ptr<ExternalEstimateProvider> external_estimate_provider( | |
743 test_external_estimate_provider); | |
744 std::map<std::string, std::string> variation_params; | |
745 TestNetworkQualityEstimator estimator(variation_params, | |
746 external_estimate_provider.Pass()); | |
747 | |
748 base::TimeDelta rtt; | |
749 int32_t kbps; | |
750 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | |
751 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
752 | |
753 EXPECT_EQ( | |
754 1U, test_external_estimate_provider->get_time_since_last_update_count()); | |
755 EXPECT_EQ(1U, test_external_estimate_provider->get_rtt_count()); | |
756 EXPECT_EQ( | |
757 1U, | |
758 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
759 | |
760 // Change network type to WiFi. Number of queries to External estimate | |
761 // provider must increment. | |
762 estimator.SimulateNetworkChangeTo( | |
763 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); | |
764 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | |
765 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
766 EXPECT_EQ( | |
767 2U, test_external_estimate_provider->get_time_since_last_update_count()); | |
768 EXPECT_EQ(2U, test_external_estimate_provider->get_rtt_count()); | |
769 EXPECT_EQ( | |
770 2U, | |
771 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
772 | |
773 // Change network type to 2G. Number of queries to External estimate provider | |
774 // must increment. | |
775 estimator.SimulateNetworkChangeTo( | |
776 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
777 EXPECT_EQ( | |
778 3U, test_external_estimate_provider->get_time_since_last_update_count()); | |
779 EXPECT_EQ(3U, test_external_estimate_provider->get_rtt_count()); | |
780 EXPECT_EQ( | |
781 3U, | |
782 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
783 | |
784 // Set the external estimate as old. Network Quality estimator should request | |
785 // an update on connection type change. | |
786 EXPECT_EQ(0U, test_external_estimate_provider->update_count()); | |
787 test_external_estimate_provider->set_time_since_last_update( | |
788 base::TimeDelta::Max()); | |
789 | |
790 estimator.SimulateNetworkChangeTo( | |
791 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); | |
792 EXPECT_EQ( | |
793 4U, test_external_estimate_provider->get_time_since_last_update_count()); | |
794 EXPECT_EQ(3U, test_external_estimate_provider->get_rtt_count()); | |
795 EXPECT_EQ( | |
796 3U, | |
797 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
798 EXPECT_EQ(1U, test_external_estimate_provider->update_count()); | |
799 | |
800 // Estimates are unavailable because external estimate provider never | |
801 // notifies network quality estimator of the updated estimates. | |
802 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | |
803 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
804 } | |
805 | |
806 // Tests if the estimate from the external estimate provider is merged with the | |
807 // observations collected from the HTTP requests. | |
808 TEST(NetworkQualityEstimatorTest, TestExternalEstimateProviderMergeEstimates) { | |
809 TestExternalEstimateProvider* test_external_estimate_provider = | |
810 new TestExternalEstimateProvider(); | |
811 scoped_ptr<ExternalEstimateProvider> external_estimate_provider( | |
812 test_external_estimate_provider); | |
813 std::map<std::string, std::string> variation_params; | |
814 TestNetworkQualityEstimator estimator(variation_params, | |
815 external_estimate_provider.Pass()); | |
816 | |
817 base::TimeDelta rtt; | |
818 int32_t kbps; | |
819 // Estimate provided by network quality estimator should match the estimate | |
820 // provided by external estimate provider. | |
821 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | |
822 base::TimeDelta external_estimate_provider_rtt; | |
mmenke
2015/09/15 20:09:09
Think it's much cleaner to make this a constant (1
tbansal1
2015/09/15 22:33:08
Done.
| |
823 EXPECT_TRUE( | |
824 test_external_estimate_provider->GetRTT(&external_estimate_provider_rtt)); | |
825 EXPECT_EQ(external_estimate_provider_rtt, rtt); | |
826 | |
827 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
828 int32_t external_estimate_provider_downstream_throughput; | |
829 EXPECT_TRUE(test_external_estimate_provider->GetDownstreamThroughputKbps( | |
830 &external_estimate_provider_downstream_throughput)); | |
831 EXPECT_EQ(external_estimate_provider_downstream_throughput, kbps); | |
mmenke
2015/09/15 20:09:09
As above, should be comparing these with constants
tbansal1
2015/09/15 22:33:08
Done.
| |
832 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | |
833 | |
834 // Send a HTTP request. | |
835 net::test_server::EmbeddedTestServer embedded_test_server; | |
836 embedded_test_server.ServeFilesFromDirectory( | |
837 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
mmenke
2015/09/15 20:09:09
include FilePath header.
tbansal1
2015/09/15 22:33:08
Done.
| |
838 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | |
839 | |
840 TestDelegate test_delegate; | |
841 TestURLRequestContext context(false); | |
842 | |
843 scoped_ptr<URLRequest> request( | |
844 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | |
845 DEFAULT_PRIORITY, &test_delegate)); | |
846 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | |
mmenke
2015/09/15 20:09:09
I don't think we care about the load flags? Can j
tbansal1
2015/09/15 22:33:08
Done.
| |
847 request->Start(); | |
848 base::RunLoop().Run(); | |
849 | |
850 // Both RTT and downstream throughput should be updated. | |
851 estimator.NotifyHeadersReceived(*request); | |
852 estimator.NotifyRequestCompleted(*request); | |
mmenke
2015/09/15 20:09:09
Why are we doing this manually? Can't we just set
tbansal1
2015/09/15 22:33:08
Done. That's what I was doing in URLRequestTestHTT
| |
853 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); | |
854 } | |
855 | |
657 } // namespace net | 856 } // namespace net |
OLD | NEW |