| 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 3e18657197eaea55b45cfa074d4b9e5c7f2f4aa7..4d9a03c8742acd069c1d2666a5bb71dd18d1f116 100644
|
| --- a/net/base/network_quality_estimator_unittest.cc
|
| +++ b/net/base/network_quality_estimator_unittest.cc
|
| @@ -10,6 +10,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/time/time.h"
|
| #include "build/build_config.h"
|
| @@ -20,6 +21,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::GetNetworkQualityCacheSizeForTests;
|
| + using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
|
| + using NetworkQualityEstimator::OnConnectionTypeChanged;
|
| +
|
| + 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 {
|
|
|
| TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
|
| @@ -66,7 +100,7 @@ TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
|
| histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1);
|
| {
|
| NetworkQuality network_quality = estimator.GetPeakEstimate();
|
| - EXPECT_EQ(estimator.current_connection_type_,
|
| + EXPECT_EQ(estimator.current_network_id_.type,
|
| NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
|
| EXPECT_EQ(network_quality.rtt(), base::TimeDelta::Max());
|
| EXPECT_EQ(network_quality.downstream_throughput_kbps(), 0);
|
| @@ -119,4 +153,129 @@ TEST(NetworkQualityEstimatorTest, StoreObservations) {
|
| }
|
| }
|
|
|
| +// 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.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // Cache entry will be added for (NONE, "").
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
|
| + EXPECT_EQ(++expected_cache_size,
|
| + estimator.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // 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.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // 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.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // Entry will be added for (3G, "test2").
|
| + estimator.SimulateNetworkChangeTo(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
|
| + EXPECT_EQ(++expected_cache_size,
|
| + estimator.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // Read the network quality for (2G, "test-1").
|
| + EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
|
| + EXPECT_EQ(1, 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.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // Read the network quality for (3G, "test1").
|
| + EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
|
| + EXPECT_EQ(2, 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.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // Reading quality of (3G, "test2") should return true.
|
| + EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
|
| + EXPECT_EQ(0, estimator.peak_kbps_since_last_connection_change_);
|
| + EXPECT_EQ(base::TimeDelta::Max(),
|
| + 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(
|
| + net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
|
| + std::string());
|
| + EXPECT_EQ(1U, estimator.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + 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 = 0; i < network_count; ++i) {
|
| + base::strings::SafeSPrintf(network_name, "%d", i);
|
| + estimator.SimulateNetworkChangeTo(
|
| + net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
|
| + network_name);
|
| + EXPECT_LE(estimator.GetNetworkQualityCacheSizeForTests(),
|
| + NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)
|
| + << i;
|
| + }
|
| + // One more call so that the last network is also written to cache.
|
| + estimator.SimulateNetworkChangeTo(
|
| + net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
|
| + network_name);
|
| +
|
| + EXPECT_EQ(NetworkQualityEstimator::kMaximumNetworkQualityCacheSize,
|
| + estimator.GetNetworkQualityCacheSizeForTests());
|
| +
|
| + // Test that the cache is LRU by examining its contents.
|
| + for (size_t i = 0; i < network_count; ++i) {
|
| + // The first 100 networks should not be present in the cache.
|
| + bool expect_present_in_cache = i >= 100;
|
| + base::strings::SafeSPrintf(network_name, "%d", i);
|
| +
|
| + NetworkQualityEstimator::NetworkID network_id(
|
| + NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name);
|
| +
|
| + bool found = estimator.cached_network_qualities_.find(network_id) !=
|
| + estimator.cached_network_qualities_.end();
|
| + EXPECT_EQ(expect_present_in_cache, found) << i;
|
| + }
|
| +}
|
| +
|
| } // namespace net
|
|
|