| Index: net/base/network_quality_estimator_unittest.cc
|
| diff --git a/net/base/network_quality_estimator_unittest.cc b/net/base/network_quality_estimator_unittest.cc
|
| index 82e1bbaf3d23e58ce960121170ad5d917e83e6f9..8c198d7abe0c7e8c475ed879b5125dd62b19afe8 100644
|
| --- a/net/base/network_quality_estimator_unittest.cc
|
| +++ b/net/base/network_quality_estimator_unittest.cc
|
| @@ -8,6 +8,7 @@
|
| #include "base/logging.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/run_loop.h"
|
| +#include "base/strings/safe_sprintf.h"
|
| #include "base/test/histogram_tester.h"
|
| #include "base/threading/platform_thread.h"
|
| #include "base/time/time.h"
|
| @@ -19,6 +20,39 @@
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| #include "url/gurl.h"
|
|
|
| +namespace {
|
| +
|
| +// Helps in setting the current network type and id.
|
| +class TestNetworkQualityEstimator : public net::NetworkQualityEstimator {
|
| + public:
|
| + TestNetworkQualityEstimator() {}
|
| +
|
| + ~TestNetworkQualityEstimator() override {}
|
| +
|
| + // Overrides the current network type and id.
|
| + // Notifies network quality estimator of change in connection.
|
| + void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type,
|
| + std::string network_id) {
|
| + current_network_id_ = network_id;
|
| + OnConnectionTypeChanged(type);
|
| + }
|
| +
|
| + using NetworkQualityEstimator::GetCacheSizeForTests;
|
| + using NetworkQualityEstimator::OnConnectionTypeChanged;
|
| + using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
|
| +
|
| + private:
|
| + // NetworkQualityEstimator implementation that returns the overridden network
|
| + // id (instead of invoking platform APIs).
|
| + std::string GetCurrentNetworkName() const override {
|
| + return current_network_id_;
|
| + }
|
| +
|
| + std::string current_network_id_;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| namespace net {
|
|
|
| // SpawnedTestServer not supported on iOS (see http://crbug.com/148666).
|
| @@ -71,7 +105,7 @@ TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
|
| EXPECT_GT(network_quality.fastest_rtt_confidence, 0);
|
| EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0);
|
| EXPECT_GE(network_quality.fastest_rtt, request_duration);
|
| - EXPECT_GT(network_quality.peak_throughput_kbps, uint32_t(0));
|
| + EXPECT_GT(network_quality.peak_throughput_kbps, 0U);
|
| EXPECT_LE(
|
| network_quality.peak_throughput_kbps,
|
| min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds());
|
| @@ -89,7 +123,7 @@ TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
|
| histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1);
|
| {
|
| NetworkQuality network_quality = estimator.GetEstimate();
|
| - EXPECT_EQ(estimator.current_connection_type_,
|
| + EXPECT_EQ(estimator.current_network_id_.type,
|
| NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
|
| EXPECT_EQ(network_quality.fastest_rtt_confidence, 0);
|
| EXPECT_EQ(network_quality.peak_throughput_kbps_confidence, 0);
|
| @@ -97,4 +131,118 @@ TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
|
| }
|
| #endif // !defined(OS_IOS)
|
|
|
| +// Test if the network estimates are cached when network change notification
|
| +// is invoked.
|
| +TEST(NetworkQualityEstimatorTest, TestCaching) {
|
| + TestNetworkQualityEstimator estimator;
|
| + size_t expected_cache_size = 0;
|
| + EXPECT_EQ(expected_cache_size, estimator.GetCacheSizeForTests());
|
| +
|
| + // Cache entry will be added for (NONE, "").
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
|
| + EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests());
|
| +
|
| + // Entry will be added for (2G, "test1").
|
| + // Also, set the network quality for (2G, "test1") so that it is stored in the
|
| + // cache.
|
| + estimator.peak_kbps_since_last_connection_change_ = 1;
|
| + estimator.fastest_RTT_since_last_connection_change_ =
|
| + base::TimeDelta::FromMilliseconds(1000);
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1");
|
| + EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests());
|
| +
|
| + // Entry will be added for (3G, "test1").
|
| + // Also, set the network quality for (3G, "test1") so that it is stored in the
|
| + // cache.
|
| + estimator.peak_kbps_since_last_connection_change_ = 2;
|
| + estimator.fastest_RTT_since_last_connection_change_ =
|
| + base::TimeDelta::FromMilliseconds(500);
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2");
|
| + EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests());
|
| +
|
| + // Entry will be added for (3G, "test2").
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
|
| + EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests());
|
| +
|
| + // Read the network quality for (2G, "test-1").
|
| + EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
|
| + EXPECT_EQ(1U, estimator.peak_kbps_since_last_connection_change_);
|
| + EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000),
|
| + estimator.fastest_RTT_since_last_connection_change_);
|
| + // No new entry should be added for (2G, "test1") since it already exists
|
| + // in the cache.
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1");
|
| + EXPECT_EQ(expected_cache_size, estimator.GetCacheSizeForTests());
|
| +
|
| + // Read the network quality for (3G, "test1").
|
| + EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
|
| + EXPECT_EQ(2U, estimator.peak_kbps_since_last_connection_change_);
|
| + EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
|
| + estimator.fastest_RTT_since_last_connection_change_);
|
| + // No new entry should be added for (3G, "test1") since it already exists
|
| + // in the cache.
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2");
|
| + EXPECT_EQ(expected_cache_size, estimator.GetCacheSizeForTests());
|
| +
|
| + // Reading quality of (3G, "test2") should return true.
|
| + EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
|
| + EXPECT_EQ(0U, estimator.peak_kbps_since_last_connection_change_);
|
| + EXPECT_EQ(base::TimeDelta(),
|
| + estimator.fastest_RTT_since_last_connection_change_);
|
| +
|
| + // Reading quality of (2G, "test-3") should return false.
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3");
|
| + EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate());
|
| +}
|
| +
|
| +// Tests if the cache size remains bounded. Also, ensure that the cache is
|
| +// LRU.
|
| +TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) {
|
| + TestNetworkQualityEstimator estimator;
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string());
|
| + EXPECT_EQ(1U, estimator.GetCacheSizeForTests());
|
| +
|
| + char network_name[20];
|
| + // Add 100 more networks than the maximum size of the cache.
|
| + size_t network_count =
|
| + NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100;
|
| +
|
| + for (size_t i = 1; i <= network_count; ++i) {
|
| + base::strings::SafeSPrintf(network_name, "%d", i);
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name);
|
| + EXPECT_LE(estimator.GetCacheSizeForTests(),
|
| + NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)
|
| + << i;
|
| + }
|
| + // One more call so that the last network is also written to cache.
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name);
|
| +
|
| + EXPECT_EQ(NetworkQualityEstimator::kMaximumNetworkQualityCacheSize,
|
| + estimator.GetCacheSizeForTests());
|
| +
|
| + // Test that the cache is LRU by examining its contents.
|
| + for (size_t i = 1; i <= network_count; ++i) {
|
| + // The first 100 networks should not be present in the cache.
|
| + bool expect_present_in_cache = i >= 101;
|
| + base::strings::SafeSPrintf(network_name, "%d", i);
|
| +
|
| + NetworkQualityEstimator::NetworkID network_id(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name);
|
| +
|
| + bool found = estimator.cached_network_quality_.find(network_id) !=
|
| + estimator.cached_network_quality_.end();
|
| + EXPECT_EQ(expect_present_in_cache, found) << i;
|
| + }
|
| +}
|
| +
|
| } // namespace net
|
|
|