network_quality_estimator_params_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/nqe/network_quality_estimator_params.h"
  5. #include <map>
  6. #include <string>
  7. #include "net/base/network_change_notifier.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace net::nqe::internal {
  10. namespace {
  11. // Tests if |weight_multiplier_per_second()| returns correct value for various
  12. // values of half life parameter.
  13. TEST(NetworkQualityEstimatorParamsTest, HalfLifeParam) {
  14. std::map<std::string, std::string> variation_params;
  15. const struct {
  16. std::string description;
  17. std::string variation_params_value;
  18. double expected_weight_multiplier;
  19. } tests[] = {
  20. {"Half life parameter is not set, default value should be used",
  21. std::string(), 0.988},
  22. {"Half life parameter is set to negative, default value should be used",
  23. "-100", 0.988},
  24. {"Half life parameter is set to zero, default value should be used", "0",
  25. 0.988},
  26. {"Half life parameter is set correctly", "10", 0.933},
  27. };
  28. for (const auto& test : tests) {
  29. variation_params["HalfLifeSeconds"] = test.variation_params_value;
  30. NetworkQualityEstimatorParams params(variation_params);
  31. EXPECT_NEAR(test.expected_weight_multiplier,
  32. params.weight_multiplier_per_second(), 0.001)
  33. << test.description;
  34. }
  35. }
  36. // Test that the typical network qualities are set correctly.
  37. TEST(NetworkQualityEstimatorParamsTest, TypicalNetworkQualities) {
  38. std::map<std::string, std::string> variation_params;
  39. NetworkQualityEstimatorParams params(variation_params);
  40. // Typical network quality should not be set for Unknown and Offline.
  41. for (size_t i = EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
  42. i <= EFFECTIVE_CONNECTION_TYPE_OFFLINE; ++i) {
  43. EffectiveConnectionType ect = static_cast<EffectiveConnectionType>(i);
  44. EXPECT_EQ(nqe::internal::InvalidRTT(),
  45. params.TypicalNetworkQuality(ect).http_rtt());
  46. EXPECT_EQ(nqe::internal::InvalidRTT(),
  47. params.TypicalNetworkQuality(ect).transport_rtt());
  48. }
  49. // Typical network quality should be set for other effective connection
  50. // types.
  51. for (size_t i = EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
  52. i <= EFFECTIVE_CONNECTION_TYPE_3G; ++i) {
  53. EffectiveConnectionType ect = static_cast<EffectiveConnectionType>(i);
  54. // The typical RTT for an effective connection type should be at least as
  55. // much as the threshold RTT.
  56. EXPECT_NE(nqe::internal::InvalidRTT(),
  57. params.TypicalNetworkQuality(ect).http_rtt());
  58. EXPECT_GT(params.TypicalNetworkQuality(ect).http_rtt(),
  59. params.ConnectionThreshold(ect).http_rtt());
  60. EXPECT_NE(nqe::internal::InvalidRTT(),
  61. params.TypicalNetworkQuality(ect).transport_rtt());
  62. EXPECT_EQ(nqe::internal::InvalidRTT(),
  63. params.ConnectionThreshold(ect).transport_rtt());
  64. EXPECT_NE(nqe::internal::INVALID_RTT_THROUGHPUT,
  65. params.TypicalNetworkQuality(ect).downstream_throughput_kbps());
  66. EXPECT_EQ(nqe::internal::INVALID_RTT_THROUGHPUT,
  67. params.ConnectionThreshold(ect).downstream_throughput_kbps());
  68. EXPECT_EQ(params.TypicalNetworkQuality(ect).http_rtt(),
  69. NetworkQualityEstimatorParams::GetDefaultTypicalHttpRtt(ect));
  70. EXPECT_EQ(
  71. params.TypicalNetworkQuality(ect).downstream_throughput_kbps(),
  72. NetworkQualityEstimatorParams::GetDefaultTypicalDownlinkKbps(ect));
  73. }
  74. // The typical network quality of 4G connection should be at least as fast
  75. // as the threshold for 3G connection.
  76. EXPECT_LT(
  77. params.TypicalNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G).http_rtt(),
  78. params.ConnectionThreshold(EFFECTIVE_CONNECTION_TYPE_3G).http_rtt());
  79. EXPECT_NE(nqe::internal::InvalidRTT(),
  80. params.TypicalNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G)
  81. .transport_rtt());
  82. EXPECT_EQ(
  83. nqe::internal::InvalidRTT(),
  84. params.ConnectionThreshold(EFFECTIVE_CONNECTION_TYPE_4G).transport_rtt());
  85. EXPECT_NE(nqe::internal::INVALID_RTT_THROUGHPUT,
  86. params.TypicalNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G)
  87. .downstream_throughput_kbps());
  88. EXPECT_EQ(nqe::internal::INVALID_RTT_THROUGHPUT,
  89. params.ConnectionThreshold(EFFECTIVE_CONNECTION_TYPE_4G)
  90. .downstream_throughput_kbps());
  91. }
  92. // Verify ECT when forced ECT is Slow-2G-On-Cellular.
  93. TEST(NetworkQualityEstimatorParamsTest, GetForcedECTCellularOnly) {
  94. std::map<std::string, std::string> variation_params;
  95. // Set force-effective-connection-type to Slow-2G-On-Cellular.
  96. variation_params[kForceEffectiveConnectionType] =
  97. kEffectiveConnectionTypeSlow2GOnCellular;
  98. NetworkQualityEstimatorParams params(variation_params);
  99. for (size_t i = 0; i < NetworkChangeNotifier::ConnectionType::CONNECTION_LAST;
  100. ++i) {
  101. NetworkChangeNotifier::ConnectionType connection_type =
  102. static_cast<NetworkChangeNotifier::ConnectionType>(i);
  103. absl::optional<EffectiveConnectionType> ect =
  104. params.GetForcedEffectiveConnectionType(connection_type);
  105. if (net::NetworkChangeNotifier::IsConnectionCellular(connection_type)) {
  106. // Test for cellular connection types. Make sure that ECT is Slow-2G.
  107. EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_SLOW_2G, ect);
  108. } else {
  109. // Test for non-cellular connection types. Make sure that there is no
  110. // forced ect.
  111. EXPECT_EQ(absl::nullopt, ect);
  112. }
  113. }
  114. }
  115. } // namespace
  116. } // namespace net::nqe::internal