1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/nqe/network_quality_estimator_test_util.h"
#include "base/files/file_path.h"
#include "base/run_loop.h"
#include "net/base/load_flags.h"
#include "net/log/net_log.h"
#include "net/log/net_log_with_source.h"
#include "net/log/test_net_log_util.h"
#include "net/nqe/network_quality_estimator_params.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_test_util.h"
namespace {
const base::FilePath::CharType kTestFilePath[] =
FILE_PATH_LITERAL("net/data/url_request_unittest");
} // namespace
namespace net {
TestNetworkQualityEstimator::TestNetworkQualityEstimator()
: TestNetworkQualityEstimator(std::map<std::string, std::string>()) {}
TestNetworkQualityEstimator::TestNetworkQualityEstimator(
const std::map<std::string, std::string>& variation_params)
: TestNetworkQualityEstimator(variation_params, true, true) {}
TestNetworkQualityEstimator::TestNetworkQualityEstimator(
const std::map<std::string, std::string>& variation_params,
bool allow_local_host_requests_for_tests,
bool allow_smaller_responses_for_tests)
: TestNetworkQualityEstimator(variation_params,
allow_local_host_requests_for_tests,
allow_smaller_responses_for_tests,
false) {}
TestNetworkQualityEstimator::TestNetworkQualityEstimator(
const std::map<std::string, std::string>& variation_params,
bool allow_local_host_requests_for_tests,
bool allow_smaller_responses_for_tests,
bool suppress_notifications_for_testing)
: NetworkQualityEstimator(
std::make_unique<NetworkQualityEstimatorParams>(variation_params),
NetLog::Get()),
suppress_notifications_for_testing_(suppress_notifications_for_testing),
embedded_test_server_(base::FilePath(kTestFilePath)) {
SetUseLocalHostRequestsForTesting(allow_local_host_requests_for_tests);
SetUseSmallResponsesForTesting(allow_smaller_responses_for_tests);
}
TestNetworkQualityEstimator::TestNetworkQualityEstimator(
std::unique_ptr<NetworkQualityEstimatorParams> params)
: NetworkQualityEstimator(std::move(params), NetLog::Get()),
suppress_notifications_for_testing_(false),
embedded_test_server_(base::FilePath(kTestFilePath)) {}
TestNetworkQualityEstimator::~TestNetworkQualityEstimator() = default;
void TestNetworkQualityEstimator::RunOneRequest() {
// Set up the embedded test server.
if (!embedded_test_server_.Started()) {
EXPECT_TRUE(embedded_test_server_.Start());
}
TestDelegate test_delegate;
auto builder = CreateTestURLRequestContextBuilder();
builder->set_network_quality_estimator(this);
auto context = builder->Build();
std::unique_ptr<URLRequest> request(
context->CreateRequest(GetEchoURL(), DEFAULT_PRIORITY, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS));
request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED);
request->Start();
test_delegate.RunUntilComplete();
}
void TestNetworkQualityEstimator::SimulateNetworkChange(
NetworkChangeNotifier::ConnectionType new_connection_type,
const std::string& network_id) {
current_network_type_ = new_connection_type;
current_network_id_ = network_id;
OnConnectionTypeChanged(new_connection_type);
}
const GURL TestNetworkQualityEstimator::GetEchoURL() {
// Set up the embedded test server.
if (!embedded_test_server_.Started()) {
EXPECT_TRUE(embedded_test_server_.Start());
}
return embedded_test_server_.GetURL("/simple.html");
}
const GURL TestNetworkQualityEstimator::GetRedirectURL() {
// Set up the embedded test server.
if (!embedded_test_server_.Started()) {
EXPECT_TRUE(embedded_test_server_.Start());
}
return embedded_test_server_.GetURL("/redirect302-to-https");
}
EffectiveConnectionType
TestNetworkQualityEstimator::GetEffectiveConnectionType() const {
if (effective_connection_type_)
return effective_connection_type_.value();
return NetworkQualityEstimator::GetEffectiveConnectionType();
}
EffectiveConnectionType
TestNetworkQualityEstimator::GetRecentEffectiveConnectionTypeUsingMetrics(
base::TimeDelta* http_rtt,
base::TimeDelta* transport_rtt,
base::TimeDelta* end_to_end_rtt,
int32_t* downstream_throughput_kbps,
size_t* observations_count,
size_t* end_to_end_rtt_observation_count) const {
if (recent_effective_connection_type_) {
GetRecentRTT(nqe::internal::OBSERVATION_CATEGORY_HTTP, base::TimeTicks(),
http_rtt, nullptr);
GetRecentRTT(nqe::internal::OBSERVATION_CATEGORY_TRANSPORT,
base::TimeTicks(), transport_rtt, observations_count);
GetRecentDownlinkThroughputKbps(base::TimeTicks(),
downstream_throughput_kbps);
return recent_effective_connection_type_.value();
}
return NetworkQualityEstimator::GetRecentEffectiveConnectionTypeUsingMetrics(
http_rtt, transport_rtt, end_to_end_rtt, downstream_throughput_kbps,
observations_count, end_to_end_rtt_observation_count);
}
bool TestNetworkQualityEstimator::GetRecentRTT(
nqe::internal::ObservationCategory observation_category,
const base::TimeTicks& start_time,
base::TimeDelta* rtt,
size_t* observations_count) const {
switch (observation_category) {
case nqe::internal::OBSERVATION_CATEGORY_HTTP:
if (start_time.is_null()) {
if (start_time_null_http_rtt_) {
*rtt = start_time_null_http_rtt_.value();
return true;
}
return NetworkQualityEstimator::GetRecentRTT(
observation_category, start_time, rtt, observations_count);
}
if (recent_http_rtt_) {
*rtt = recent_http_rtt_.value();
return true;
}
break;
case nqe::internal::OBSERVATION_CATEGORY_TRANSPORT:
if (start_time.is_null()) {
if (start_time_null_transport_rtt_) {
*rtt = start_time_null_transport_rtt_.value();
if (transport_rtt_observation_count_last_ect_computation_) {
*observations_count =
transport_rtt_observation_count_last_ect_computation_.value();
}
return true;
}
return NetworkQualityEstimator::GetRecentRTT(
observation_category, start_time, rtt, observations_count);
}
if (recent_transport_rtt_) {
*rtt = recent_transport_rtt_.value();
return true;
}
break;
case nqe::internal::OBSERVATION_CATEGORY_END_TO_END:
if (start_time_null_end_to_end_rtt_) {
*rtt = start_time_null_end_to_end_rtt_.value();
return true;
}
break;
case nqe::internal::OBSERVATION_CATEGORY_COUNT:
NOTREACHED();
}
return NetworkQualityEstimator::GetRecentRTT(observation_category, start_time,
rtt, observations_count);
}
std::optional<base::TimeDelta> TestNetworkQualityEstimator::GetTransportRTT()
const {
if (start_time_null_transport_rtt_)
return start_time_null_transport_rtt_;
return NetworkQualityEstimator::GetTransportRTT();
}
bool TestNetworkQualityEstimator::GetRecentDownlinkThroughputKbps(
const base::TimeTicks& start_time,
int32_t* kbps) const {
if (start_time.is_null()) {
if (start_time_null_downlink_throughput_kbps_) {
*kbps = start_time_null_downlink_throughput_kbps_.value();
return true;
}
return NetworkQualityEstimator::GetRecentDownlinkThroughputKbps(start_time,
kbps);
}
if (recent_downlink_throughput_kbps_) {
*kbps = recent_downlink_throughput_kbps_.value();
return true;
}
return NetworkQualityEstimator::GetRecentDownlinkThroughputKbps(start_time,
kbps);
}
base::TimeDelta TestNetworkQualityEstimator::GetRTTEstimateInternal(
base::TimeTicks start_time,
nqe::internal::ObservationCategory observation_category,
int percentile,
size_t* observations_count) const {
if (rtt_estimate_internal_)
return rtt_estimate_internal_.value();
return NetworkQualityEstimator::GetRTTEstimateInternal(
start_time, observation_category, percentile, observations_count);
}
int TestNetworkQualityEstimator::GetEntriesCount(NetLogEventType type) const {
return net_log_observer_.GetEntriesWithType(type).size();
}
std::string TestNetworkQualityEstimator::GetNetLogLastStringValue(
NetLogEventType type,
const std::string& key) const {
auto entries = net_log_observer_.GetEntries();
for (int i = entries.size() - 1; i >= 0; --i) {
if (entries[i].type == type) {
auto value = GetOptionalStringValueFromParams(entries[i], key);
if (value)
return *value;
}
}
return std::string();
}
int TestNetworkQualityEstimator::GetNetLogLastIntegerValue(
NetLogEventType type,
const std::string& key) const {
auto entries = net_log_observer_.GetEntries();
for (int i = entries.size() - 1; i >= 0; --i) {
if (entries[i].type == type) {
auto value = GetOptionalIntegerValueFromParams(entries[i], key);
if (value)
return *value;
}
}
return 0;
}
void TestNetworkQualityEstimator::
NotifyObserversOfRTTOrThroughputEstimatesComputed(
const net::nqe::internal::NetworkQuality& network_quality) {
for (auto& observer : rtt_and_throughput_estimates_observer_list_) {
observer.OnRTTOrThroughputEstimatesComputed(
network_quality.http_rtt(), network_quality.transport_rtt(),
network_quality.downstream_throughput_kbps());
}
}
void TestNetworkQualityEstimator::
SetAndNotifyObserversOfEffectiveConnectionType(
EffectiveConnectionType type) {
set_effective_connection_type(type);
for (auto& observer : effective_connection_type_observer_list_)
observer.OnEffectiveConnectionTypeChanged(type);
}
std::optional<net::EffectiveConnectionType>
TestNetworkQualityEstimator::GetOverrideECT() const {
return effective_connection_type_;
}
void TestNetworkQualityEstimator::
SetAndNotifyObserversOfP2PActiveConnectionsCountChange(uint32_t count) {
p2p_connections_count_ = count;
for (auto& observer : peer_to_peer_type_observer_list_)
observer.OnPeerToPeerConnectionsCountChange(count);
}
void TestNetworkQualityEstimator::RecordSpdyPingLatency(
const HostPortPair& host_port_pair,
base::TimeDelta rtt) {
++ping_rtt_received_count_;
NetworkQualityEstimator::RecordSpdyPingLatency(host_port_pair, rtt);
}
const NetworkQualityEstimatorParams* TestNetworkQualityEstimator::params()
const {
return params_.get();
}
nqe::internal::NetworkID TestNetworkQualityEstimator::GetCurrentNetworkID()
const {
return nqe::internal::NetworkID(current_network_type_, current_network_id_,
INT32_MIN);
}
TestNetworkQualityEstimator::LocalHttpTestServer::LocalHttpTestServer(
const base::FilePath& document_root) {
AddDefaultHandlers(document_root);
}
void TestNetworkQualityEstimator::NotifyObserversOfRTTOrThroughputComputed()
const {
if (suppress_notifications_for_testing_)
return;
NetworkQualityEstimator::NotifyObserversOfRTTOrThroughputComputed();
}
void TestNetworkQualityEstimator::
NotifyRTTAndThroughputEstimatesObserverIfPresent(
RTTAndThroughputEstimatesObserver* observer) const {
if (suppress_notifications_for_testing_)
return;
NetworkQualityEstimator::NotifyRTTAndThroughputEstimatesObserverIfPresent(
observer);
}
void TestNetworkQualityEstimator::SetStartTimeNullHttpRtt(
const base::TimeDelta http_rtt) {
start_time_null_http_rtt_ = http_rtt;
// Force compute effective connection type so that the new RTT value is
// immediately picked up. This ensures that the next call to
// GetEffectiveConnectionType() returns the effective connnection type
// that was computed based on |http_rtt|.
ComputeEffectiveConnectionType();
}
void TestNetworkQualityEstimator::SetStartTimeNullTransportRtt(
const base::TimeDelta transport_rtt) {
start_time_null_transport_rtt_ = transport_rtt;
// Force compute effective connection type so that the new RTT value is
// immediately picked up. This ensures that the next call to
// GetEffectiveConnectionType() returns the effective connnection type
// that was computed based on |transport_rtt|.
ComputeEffectiveConnectionType();
}
} // namespace net
|