network_quality_estimator_test_util.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2016 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. #ifndef NET_NQE_NETWORK_QUALITY_ESTIMATOR_TEST_UTIL_H_
  5. #define NET_NQE_NETWORK_QUALITY_ESTIMATOR_TEST_UTIL_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/files/file_path.h"
  13. #include "base/time/time.h"
  14. #include "net/base/network_change_notifier.h"
  15. #include "net/log/net_log.h"
  16. #include "net/log/test_net_log.h"
  17. #include "net/nqe/effective_connection_type.h"
  18. #include "net/nqe/network_quality_estimator.h"
  19. #include "net/test/embedded_test_server/embedded_test_server.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. #include "url/gurl.h"
  23. namespace net {
  24. // Helps in setting the current network type and id.
  25. class TestNetworkQualityEstimator : public NetworkQualityEstimator {
  26. public:
  27. TestNetworkQualityEstimator();
  28. explicit TestNetworkQualityEstimator(
  29. const std::map<std::string, std::string>& variation_params);
  30. TestNetworkQualityEstimator(
  31. const std::map<std::string, std::string>& variation_params,
  32. bool allow_local_host_requests_for_tests,
  33. bool allow_smaller_responses_for_tests);
  34. TestNetworkQualityEstimator(
  35. const std::map<std::string, std::string>& variation_params,
  36. bool allow_local_host_requests_for_tests,
  37. bool allow_smaller_responses_for_tests,
  38. bool suppress_notifications_for_testing);
  39. explicit TestNetworkQualityEstimator(
  40. std::unique_ptr<NetworkQualityEstimatorParams> params);
  41. TestNetworkQualityEstimator(const TestNetworkQualityEstimator&) = delete;
  42. TestNetworkQualityEstimator& operator=(const TestNetworkQualityEstimator&) =
  43. delete;
  44. ~TestNetworkQualityEstimator() override;
  45. // Runs one URL request to completion.
  46. void RunOneRequest();
  47. // Overrides the current network type and id.
  48. // Notifies network quality estimator of a change in connection.
  49. void SimulateNetworkChange(
  50. NetworkChangeNotifier::ConnectionType new_connection_type,
  51. const std::string& network_id);
  52. // Returns a GURL hosted at the embedded test server.
  53. const GURL GetEchoURL();
  54. // Returns a GURL hosted at the embedded test server which contains redirect
  55. // to another HTTPS URL.
  56. const GURL GetRedirectURL();
  57. void set_effective_connection_type(EffectiveConnectionType type) {
  58. effective_connection_type_ = type;
  59. }
  60. // Returns the effective connection type that was set using
  61. // |set_effective_connection_type|. If the connection type has not been set,
  62. // then the base implementation is called.
  63. EffectiveConnectionType GetEffectiveConnectionType() const override;
  64. void set_recent_effective_connection_type(EffectiveConnectionType type) {
  65. // Callers should not set effective connection type along with the
  66. // lower-layer metrics.
  67. DCHECK(!start_time_null_http_rtt_ && !recent_http_rtt_ &&
  68. !start_time_null_transport_rtt_ && !recent_transport_rtt_ &&
  69. !start_time_null_downlink_throughput_kbps_ &&
  70. !recent_downlink_throughput_kbps_);
  71. recent_effective_connection_type_ = type;
  72. }
  73. // Returns the effective connection type that was set using
  74. // |set_effective_connection_type|. If the connection type has not been set,
  75. // then the base implementation is called. |http_rtt|, |transport_rtt| and
  76. // |downstream_throughput_kbps| are set to the values that were previously
  77. // set by calling set_recent_http_rtt(), set_recent_transport_rtt()
  78. // and set_recent_transport_rtt() methods, respectively.
  79. EffectiveConnectionType GetRecentEffectiveConnectionTypeUsingMetrics(
  80. base::TimeDelta* http_rtt,
  81. base::TimeDelta* transport_rtt,
  82. base::TimeDelta* end_to_end_rtt,
  83. int32_t* downstream_throughput_kbps,
  84. size_t* observations_count,
  85. size_t* end_to_end_rtt_observation_count) const override;
  86. void NotifyObserversOfRTTOrThroughputComputed() const override;
  87. void NotifyRTTAndThroughputEstimatesObserverIfPresent(
  88. RTTAndThroughputEstimatesObserver* observer) const override;
  89. // Force set the HTTP RTT estimate.
  90. void SetStartTimeNullHttpRtt(const base::TimeDelta http_rtt);
  91. void set_recent_http_rtt(const base::TimeDelta& recent_http_rtt) {
  92. // Callers should not set effective connection type along with the
  93. // lower-layer metrics.
  94. DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
  95. recent_http_rtt_ = recent_http_rtt;
  96. }
  97. // Returns the recent RTT that was set using set_recent_http_rtt() or
  98. // set_recent_transport_rtt(). If the recent RTT has not been set, then the
  99. // base implementation is called.
  100. bool GetRecentRTT(nqe::internal::ObservationCategory observation_category,
  101. const base::TimeTicks& start_time,
  102. base::TimeDelta* rtt,
  103. size_t* observations_count) const override;
  104. // Force set the transport RTT estimate.
  105. void SetStartTimeNullTransportRtt(const base::TimeDelta transport_rtt);
  106. void set_recent_transport_rtt(const base::TimeDelta& recent_transport_rtt) {
  107. // Callers should not set effective connection type along with the
  108. // lower-layer metrics.
  109. DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
  110. recent_transport_rtt_ = recent_transport_rtt;
  111. }
  112. absl::optional<base::TimeDelta> GetTransportRTT() const override;
  113. void set_start_time_null_downlink_throughput_kbps(
  114. int32_t downlink_throughput_kbps) {
  115. start_time_null_downlink_throughput_kbps_ = downlink_throughput_kbps;
  116. }
  117. void set_recent_downlink_throughput_kbps(
  118. int32_t recent_downlink_throughput_kbps) {
  119. // Callers should not set effective connection type along with the
  120. // lower-layer metrics.
  121. DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
  122. recent_downlink_throughput_kbps_ = recent_downlink_throughput_kbps;
  123. }
  124. // Returns the downlink throughput that was set using
  125. // |set_recent_downlink_throughput_kbps|. If the downlink throughput has not
  126. // been set, then the base implementation is called.
  127. bool GetRecentDownlinkThroughputKbps(const base::TimeTicks& start_time,
  128. int32_t* kbps) const override;
  129. // Returns the recent HTTP RTT value that was set using
  130. // |set_rtt_estimate_internal|. If it has not been set, then the base
  131. // implementation is called.
  132. base::TimeDelta GetRTTEstimateInternal(
  133. base::TimeTicks start_time,
  134. nqe::internal::ObservationCategory observation_category,
  135. int percentile,
  136. size_t* observations_count) const override;
  137. void set_rtt_estimate_internal(base::TimeDelta value) {
  138. rtt_estimate_internal_ = value;
  139. }
  140. void set_start_time_null_end_to_end_rtt(const base::TimeDelta rtt) {
  141. // Callers should not set effective connection type along with the
  142. // lower-layer metrics.
  143. DCHECK(!effective_connection_type_ && !recent_effective_connection_type_);
  144. start_time_null_end_to_end_rtt_ = rtt;
  145. }
  146. void set_start_time_null_end_to_end_rtt_observation_count(size_t count) {
  147. end_to_end_rtt_observation_count_at_last_ect_computation_ = count;
  148. }
  149. // Returns the number of entries in |net_log_| that have type set to |type|.
  150. int GetEntriesCount(NetLogEventType type) const;
  151. // Returns the value of the parameter with name |key| from the last net log
  152. // entry that has type set to |type|. Different methods are provided for
  153. // values of different types.
  154. std::string GetNetLogLastStringValue(NetLogEventType type,
  155. const std::string& key) const;
  156. int GetNetLogLastIntegerValue(NetLogEventType type,
  157. const std::string& key) const;
  158. // Notifies the registered observers that the network quality estimate has
  159. // changed to |network_quality|.
  160. void NotifyObserversOfRTTOrThroughputEstimatesComputed(
  161. const net::nqe::internal::NetworkQuality& network_quality);
  162. // Updates the computed effective connection type to |type| and notifies the
  163. // registered observers that the effective connection type has changed to
  164. // |type|.
  165. void SetAndNotifyObserversOfEffectiveConnectionType(
  166. EffectiveConnectionType type);
  167. // Updates the count of active P2P connections to |count| and notifies the
  168. // registered observers that the active P2P connection counts has changed to
  169. // |count|.
  170. void SetAndNotifyObserversOfP2PActiveConnectionsCountChange(uint32_t count);
  171. void SetTransportRTTAtastECTSampleCount(size_t count) {
  172. transport_rtt_observation_count_last_ect_computation_ = count;
  173. }
  174. // Returns count of ping RTTs received from H2/spdy connections.
  175. size_t ping_rtt_received_count() const { return ping_rtt_received_count_; }
  176. const NetworkQualityEstimatorParams* params() const;
  177. void RecordSpdyPingLatency(const HostPortPair& host_port_pair,
  178. base::TimeDelta rtt) override;
  179. using NetworkQualityEstimator::SetTickClockForTesting;
  180. using NetworkQualityEstimator::OnConnectionTypeChanged;
  181. using NetworkQualityEstimator::OnUpdatedTransportRTTAvailable;
  182. using NetworkQualityEstimator::AddAndNotifyObserversOfRTT;
  183. using NetworkQualityEstimator::AddAndNotifyObserversOfThroughput;
  184. using NetworkQualityEstimator::IsHangingRequest;
  185. private:
  186. class LocalHttpTestServer : public EmbeddedTestServer {
  187. public:
  188. explicit LocalHttpTestServer(const base::FilePath& document_root);
  189. };
  190. // NetworkQualityEstimator implementation that returns the overridden
  191. // network id and signal strength (instead of invoking platform APIs).
  192. nqe::internal::NetworkID GetCurrentNetworkID() const override;
  193. absl::optional<net::EffectiveConnectionType> GetOverrideECT() const override;
  194. // Net log observer used to test correctness of NetLog entries.
  195. net::RecordingNetLogObserver net_log_observer_;
  196. // If set, GetEffectiveConnectionType() and GetRecentEffectiveConnectionType()
  197. // would return the set values, respectively.
  198. absl::optional<EffectiveConnectionType> effective_connection_type_;
  199. absl::optional<EffectiveConnectionType> recent_effective_connection_type_;
  200. NetworkChangeNotifier::ConnectionType current_network_type_ =
  201. NetworkChangeNotifier::CONNECTION_UNKNOWN;
  202. std::string current_network_id_;
  203. // If set, GetRecentHttpRTT() would return one of the set values.
  204. // |start_time_null_http_rtt_| is returned if the |start_time| is null.
  205. // Otherwise, |recent_http_rtt_| is returned.
  206. absl::optional<base::TimeDelta> start_time_null_http_rtt_;
  207. absl::optional<base::TimeDelta> recent_http_rtt_;
  208. // If set, GetRecentTransportRTT() would return one of the set values.
  209. // |start_time_null_transport_rtt_| is returned if the |start_time| is null.
  210. // Otherwise, |recent_transport_rtt_| is returned.
  211. absl::optional<base::TimeDelta> start_time_null_transport_rtt_;
  212. absl::optional<base::TimeDelta> recent_transport_rtt_;
  213. // If set, GetRecentDownlinkThroughputKbps() would return one of the set
  214. // values. |start_time_null_downlink_throughput_kbps_| is returned if the
  215. // |start_time| is null. Otherwise, |recent_downlink_throughput_kbps_| is
  216. // returned.
  217. absl::optional<int32_t> start_time_null_downlink_throughput_kbps_;
  218. absl::optional<int32_t> recent_downlink_throughput_kbps_;
  219. // If set, GetRTTEstimateInternal() would return the set value.
  220. absl::optional<base::TimeDelta> rtt_estimate_internal_;
  221. // If set, GetRTTEstimateInternal() would return the set value.
  222. absl::optional<base::TimeDelta> start_time_null_end_to_end_rtt_;
  223. LocalHttpTestServer embedded_test_server_;
  224. // If true, notifications are not sent to any of the observers.
  225. const bool suppress_notifications_for_testing_;
  226. size_t ping_rtt_received_count_ = 0;
  227. absl::optional<size_t> transport_rtt_observation_count_last_ect_computation_;
  228. };
  229. } // namespace net
  230. #endif // NET_NQE_NETWORK_QUALITY_ESTIMATOR_TEST_UTIL_H_