#include "services/network/resource_scheduler/resource_scheduler.h"
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "net/base/features.h"
#include "net/base/host_port_pair.h"
#include "net/base/isolation_info.h"
#include "net/base/load_timing_info.h"
#include "net/base/request_priority.h"
#include "net/nqe/network_quality_estimator_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.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"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/resource_scheduler/resource_scheduler_params_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/scheme_host_port.h"
using std::string;
namespace network {
namespace {
class TestRequestFactory;
using ClientId = ResourceScheduler::ClientId;
const ClientId kClientId1 = ClientId::CreateForTest(30);
const ClientId kClientId2 = ClientId::CreateForTest(60);
const ClientId kTrustedClientId = ClientId::CreateForTest(120);
const ClientId kBackgroundClientId = ClientId::CreateForTest(150);
const size_t kMaxNumDelayableRequestsPerHostPerClient = 6;
class TestRequest {
public:
TestRequest(std::unique_ptr<net::URLRequest> url_request,
std::unique_ptr<ResourceScheduler::ScheduledResourceRequest>
scheduled_request,
ResourceScheduler* scheduler)
: started_(false),
url_request_(std::move(url_request)),
scheduled_request_(std::move(scheduled_request)),
scheduler_(scheduler) {
scheduled_request_->set_resume_callback(
base::BindOnce(&TestRequest::Resume, base::Unretained(this)));
}
virtual ~TestRequest() {
scheduled_request_.reset();
}
bool started() const { return started_; }
void Start() {
bool deferred = false;
scheduled_request_->WillStartRequest(&deferred);
started_ = !deferred;
}
void ChangePriority(net::RequestPriority new_priority, int intra_priority) {
scheduler_->ReprioritizeRequest(url_request_.get(), new_priority,
intra_priority);
}
const net::URLRequest* url_request() const { return url_request_.get(); }
virtual void Resume() { started_ = true; }
private:
bool started_;
std::unique_ptr<net::URLRequest> url_request_;
std::unique_ptr<ResourceScheduler::ScheduledResourceRequest>
scheduled_request_;
raw_ptr<ResourceScheduler> scheduler_;
};
class CancelingTestRequest : public TestRequest {
public:
CancelingTestRequest(
std::unique_ptr<net::URLRequest> url_request,
std::unique_ptr<ResourceScheduler::ScheduledResourceRequest>
scheduled_request,
ResourceScheduler* scheduler)
: TestRequest(std::move(url_request),
std::move(scheduled_request),
scheduler) {}
void set_request_to_cancel(std::unique_ptr<TestRequest> request_to_cancel) {
request_to_cancel_ = std::move(request_to_cancel);
}
private:
void Resume() override {
TestRequest::Resume();
request_to_cancel_.reset();
}
std::unique_ptr<TestRequest> request_to_cancel_;
};
class ResourceSchedulerTest : public testing::Test {
protected:
ResourceSchedulerTest() {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(
net::features::kPartitionConnectionsByNetworkIsolationKey);
auto context_builder = net::CreateTestURLRequestContextBuilder();
context_builder->set_network_quality_estimator(&network_quality_estimator_);
context_ = context_builder->Build();
InitializeScheduler();
}
~ResourceSchedulerTest() override { CleanupScheduler(); }
void InitializeScheduler() {
CleanupScheduler();
scheduler_ = std::make_unique<ResourceScheduler>(&tick_clock_);
scheduler()->SetResourceSchedulerParamsManagerForTests(
resource_scheduler_params_manager_);
scheduler_->OnClientCreated(kClientId1, IsBrowserInitiated(false),
&network_quality_estimator_);
scheduler_->OnClientCreated(kBackgroundClientId, IsBrowserInitiated(false),
&network_quality_estimator_);
scheduler_->OnClientCreated(kTrustedClientId, IsBrowserInitiated(true),
&network_quality_estimator_);
}
ResourceSchedulerParamsManager FixedParamsManager(
size_t max_delayable_requests) const {
ResourceSchedulerParamsManager::ParamsForNetworkQualityContainer c;
for (int i = 0; i != net::EFFECTIVE_CONNECTION_TYPE_LAST; ++i) {
auto type = static_cast<net::EffectiveConnectionType>(i);
c[type] = ResourceSchedulerParamsManager::ParamsForNetworkQuality(
max_delayable_requests, 0.0, false, std::nullopt);
}
return ResourceSchedulerParamsManager(std::move(c));
}
void SetMaxDelayableRequests(size_t max_delayable_requests) {
scheduler()->SetResourceSchedulerParamsManagerForTests(
ResourceSchedulerParamsManager(
FixedParamsManager(max_delayable_requests)));
}
void CleanupScheduler() {
if (scheduler_) {
scheduler_->OnClientDeleted(kClientId1);
scheduler_->OnClientDeleted(kBackgroundClientId);
scheduler_->OnClientDeleted(kTrustedClientId);
}
}
std::unique_ptr<net::URLRequest> NewURLRequest(
const char* url,
net::RequestPriority priority,
const net::NetworkTrafficAnnotationTag& traffic_annotation) {
std::unique_ptr<net::URLRequest> url_request(context_->CreateRequest(
GURL(url), priority, nullptr, traffic_annotation));
return url_request;
}
std::unique_ptr<net::URLRequest> NewURLRequest(
const char* url,
net::RequestPriority priority) {
return NewURLRequest(url, priority, TRAFFIC_ANNOTATION_FOR_TESTS);
}
std::unique_ptr<TestRequest> NewRequestWithClientId(
const char* url,
net::RequestPriority priority,
ClientId client_id) {
return GetNewTestRequest(url, priority, TRAFFIC_ANNOTATION_FOR_TESTS,
client_id, true, net::IsolationInfo());
}
std::unique_ptr<TestRequest> NewRequest(const char* url,
net::RequestPriority priority) {
return NewRequestWithClientId(url, priority, kClientId1);
}
std::unique_ptr<TestRequest> NewBackgroundRequest(
const char* url,
net::RequestPriority priority) {
return NewRequestWithClientId(url, priority, kBackgroundClientId);
}
std::unique_ptr<TestRequest> NewTrustedRequest(
const char* url,
net::RequestPriority priority) {
return NewRequestWithClientId(url, priority, kTrustedClientId);
}
std::unique_ptr<TestRequest> NewTrustedRequest(
const char* url,
net::RequestPriority priority,
const net::NetworkTrafficAnnotationTag& traffic_annotation) {
return GetNewTestRequest(url, priority, traffic_annotation,
kTrustedClientId, true, net::IsolationInfo());
}
std::unique_ptr<TestRequest> NewSyncRequest(const char* url,
net::RequestPriority priority) {
return NewSyncRequestWithClientId(url, priority, kClientId1);
}
std::unique_ptr<TestRequest> NewBackgroundSyncRequest(
const char* url,
net::RequestPriority priority) {
return NewSyncRequestWithClientId(url, priority, kBackgroundClientId);
}
std::unique_ptr<TestRequest> NewSyncRequestWithClientId(
const char* url,
net::RequestPriority priority,
ClientId client_id) {
return GetNewTestRequest(url, priority, TRAFFIC_ANNOTATION_FOR_TESTS,
client_id, false, net::IsolationInfo());
}
std::unique_ptr<TestRequest> NewRequestWithIsolationInfo(
const char* url,
net::RequestPriority priority,
const net::IsolationInfo& isolation_info) {
return GetNewTestRequest(url, priority, TRAFFIC_ANNOTATION_FOR_TESTS,
kClientId1, true, isolation_info);
}
std::unique_ptr<TestRequest> GetNewTestRequest(
const char* url,
net::RequestPriority priority,
const net::NetworkTrafficAnnotationTag& traffic_annotation,
ClientId client_id,
bool is_async,
const net::IsolationInfo& isolation_info) {
std::unique_ptr<net::URLRequest> url_request(
NewURLRequest(url, priority, traffic_annotation));
url_request->set_isolation_info(isolation_info);
auto scheduled_request =
scheduler_->ScheduleRequest(client_id, is_async, url_request.get());
auto request = std::make_unique<TestRequest>(
std::move(url_request), std::move(scheduled_request), scheduler());
request->Start();
return request;
}
void ChangeRequestPriority(TestRequest* request,
net::RequestPriority new_priority,
int intra_priority = 0) {
request->ChangePriority(new_priority, intra_priority);
}
void RequestLimitOverrideConfigTestHelper(bool experiment_status) {
InitializeThrottleDelayableExperiment(experiment_status, 0.0);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> high2(
NewRequest("http://host/high2", net::HIGHEST));
EXPECT_TRUE(high2->started());
const int kOverriddenNumRequests = 2;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < kOverriddenNumRequests; ++i) {
std::string url = "http://host/low" + base::NumberToString(i);
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows_singlehost[i]->started());
}
std::unique_ptr<TestRequest> second_last_singlehost(
NewRequest("http://host/s_last", net::LOWEST));
std::unique_ptr<TestRequest> last_singlehost(
NewRequest("http://host/last", net::LOWEST));
if (experiment_status) {
EXPECT_FALSE(second_last_singlehost->started());
lows_singlehost.erase(lows_singlehost.begin());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(second_last_singlehost->started());
EXPECT_FALSE(last_singlehost->started());
lows_singlehost.erase(lows_singlehost.begin());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(last_singlehost->started());
} else {
EXPECT_TRUE(second_last_singlehost->started());
EXPECT_TRUE(last_singlehost->started());
}
}
void ConfigureDelayRequestsOnMultiplexedConnectionsFieldTrial() {
std::map<net::EffectiveConnectionType,
ResourceSchedulerParamsManager::ParamsForNetworkQuality>
params_for_network_quality_container;
ResourceSchedulerParamsManager::ParamsForNetworkQuality params_slow_2g(
8, 3.0, true, std::nullopt);
ResourceSchedulerParamsManager::ParamsForNetworkQuality params_2g(
8, 3.0, true, std::nullopt);
params_for_network_quality_container
[net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G] = params_slow_2g;
params_for_network_quality_container[net::EFFECTIVE_CONNECTION_TYPE_2G] =
params_2g;
resource_scheduler_params_manager_.Reset(
params_for_network_quality_container);
}
void InitializeThrottleDelayableExperiment(bool lower_delayable_count_enabled,
double non_delayable_weight) {
std::map<net::EffectiveConnectionType,
ResourceSchedulerParamsManager::ParamsForNetworkQuality>
params_for_network_quality_container;
ResourceSchedulerParamsManager::ParamsForNetworkQuality params_slow_2g(
8, 3.0, false, std::nullopt);
ResourceSchedulerParamsManager::ParamsForNetworkQuality params_3g(
10, 0.0, false, std::nullopt);
if (lower_delayable_count_enabled) {
params_slow_2g.max_delayable_requests = 2;
params_slow_2g.non_delayable_weight = 0.0;
params_3g.max_delayable_requests = 4;
params_3g.non_delayable_weight = 0.0;
}
if (non_delayable_weight > 0.0) {
if (!lower_delayable_count_enabled)
params_slow_2g.max_delayable_requests = 8;
params_slow_2g.non_delayable_weight = non_delayable_weight;
}
params_for_network_quality_container
[net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G] = params_slow_2g;
params_for_network_quality_container[net::EFFECTIVE_CONNECTION_TYPE_3G] =
params_3g;
resource_scheduler_params_manager_.Reset(
params_for_network_quality_container);
}
void InitializeMaxQueuingDelayExperiment(base::TimeDelta max_queuing_time) {
std::map<net::EffectiveConnectionType,
ResourceSchedulerParamsManager::ParamsForNetworkQuality>
params_for_network_quality_container;
ResourceSchedulerParamsManager::ParamsForNetworkQuality params_slow_2g(
8, 3.0, true, std::nullopt);
params_slow_2g.max_queuing_time = max_queuing_time;
params_for_network_quality_container
[net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G] = params_slow_2g;
resource_scheduler_params_manager_.Reset(
params_for_network_quality_container);
}
void NonDelayableThrottlesDelayableHelper(double non_delayable_weight) {
const int kDefaultMaxNumDelayableRequestsPerClient = 8;
InitializeThrottleDelayableExperiment(false, non_delayable_weight);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> non_delayable_request(
NewRequest("http://host/medium", net::MEDIUM));
std::vector<std::unique_ptr<TestRequest>> delayable_requests;
for (int i = 0;
i < kDefaultMaxNumDelayableRequestsPerClient - non_delayable_weight;
++i) {
delayable_requests.push_back(NewRequest(
base::StringPrintf("http://host%d/low", i).c_str(), net::LOWEST));
EXPECT_TRUE(delayable_requests.back()->started());
}
std::unique_ptr<TestRequest> last_low(
NewRequest("http://lasthost/low", net::LOWEST));
EXPECT_FALSE(last_low->started());
}
void ConfigureProactiveThrottlingExperimentFor2G(
double http_rtt_multiplier_for_proactive_throttling) {
std::map<net::EffectiveConnectionType,
ResourceSchedulerParamsManager::ParamsForNetworkQuality>
params_for_network_quality_container;
ResourceSchedulerParamsManager::ParamsForNetworkQuality params_2g;
params_2g.http_rtt_multiplier_for_proactive_throttling =
http_rtt_multiplier_for_proactive_throttling;
params_for_network_quality_container[net::EFFECTIVE_CONNECTION_TYPE_2G] =
params_2g;
resource_scheduler_params_manager_.Reset(
params_for_network_quality_container);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
base::RunLoop().RunUntilIdle();
InitializeScheduler();
}
ResourceScheduler* scheduler() { return scheduler_.get(); }
base::test::TaskEnvironment task_environment_;
std::unique_ptr<ResourceScheduler> scheduler_;
net::TestNetworkQualityEstimator network_quality_estimator_;
std::unique_ptr<net::URLRequestContext> context_;
ResourceSchedulerParamsManager resource_scheduler_params_manager_;
base::SimpleTestTickClock tick_clock_;
};
TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) {
std::unique_ptr<TestRequest> request(
NewRequest("http://host/1", net::LOWEST));
EXPECT_TRUE(request->started());
}
TEST_F(ResourceSchedulerTest, OneLowLoadsUntilCriticalComplete) {
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_4G);
InitializeScheduler();
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
std::unique_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
EXPECT_TRUE(high->started());
EXPECT_TRUE(low->started());
EXPECT_FALSE(low2->started());
SetMaxDelayableRequests(10);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(low2->started());
high.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(low2->started());
}
TEST_F(ResourceSchedulerTest, MaxRequestsPerHostForSpdyWhenNotDelayable) {
InitializeScheduler();
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("https", "spdyhost", 443),
net::NetworkAnonymizationKey(), true);
std::vector<std::unique_ptr<TestRequest>> requests;
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i)
requests.push_back(NewRequest("https://spdyhost/low", net::LOWEST));
for (const auto& request : requests)
EXPECT_TRUE(request->started());
}
TEST_F(ResourceSchedulerTest,
MaxRequestsPerHostForSpdyWhenNotDelayableWithIsolationInfo) {
const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/"));
const net::IsolationInfo kIsolationInfo1 =
net::IsolationInfo::CreateForInternalRequest(kOrigin1);
const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/"));
const net::IsolationInfo kIsolationInfo2 =
net::IsolationInfo::CreateForInternalRequest(kOrigin2);
InitializeScheduler();
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("https", "spdyhost", 443),
kIsolationInfo1.network_anonymization_key(), true);
std::vector<std::unique_ptr<TestRequest>> requests;
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i) {
requests.push_back(NewRequestWithIsolationInfo(
"https://spdyhost/low", net::LOWEST, kIsolationInfo1));
EXPECT_TRUE(requests.back()->started());
}
requests.clear();
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i) {
requests.push_back(NewRequestWithIsolationInfo(
"https://spdyhost/low", net::LOWEST, net::IsolationInfo()));
EXPECT_EQ(i < kMaxNumDelayableRequestsPerHostPerClient,
requests.back()->started());
}
requests.clear();
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i) {
requests.push_back(NewRequestWithIsolationInfo(
"https://spdyhost/low", net::LOWEST, kIsolationInfo2));
EXPECT_EQ(i < kMaxNumDelayableRequestsPerHostPerClient,
requests.back()->started());
}
requests.clear();
}
TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) {
std::unique_ptr<TestRequest> request(
NewBackgroundRequest("http://host/1", net::LOWEST));
EXPECT_TRUE(request->started());
}
TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low1(
NewRequest("http://host/low1", net::LOWEST));
std::unique_ptr<net::URLRequest> url_request(
NewURLRequest("http://host/low2", net::LOWEST));
auto scheduled_request =
scheduler()->ScheduleRequest(kClientId1, true, url_request.get());
std::unique_ptr<CancelingTestRequest> low2(new CancelingTestRequest(
std::move(url_request), std::move(scheduled_request), scheduler()));
low2->Start();
std::unique_ptr<TestRequest> low3(
NewRequest("http://host/low3", net::LOWEST));
low2->set_request_to_cancel(std::move(low3));
std::unique_ptr<TestRequest> low4(
NewRequest("http://host/low4", net::LOWEST));
EXPECT_TRUE(high->started());
EXPECT_FALSE(low2->started());
SetMaxDelayableRequests(10);
high.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(low1->started());
EXPECT_TRUE(low2->started());
EXPECT_TRUE(low4->started());
}
TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) {
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int kDefaultMaxNumDelayableRequestsPerClient =
10;
const int kMaxNumDelayableRequestsPerHost = 6;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < kMaxNumDelayableRequestsPerHost - 1; ++i) {
string url = "http://host/low" + base::NumberToString(i);
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows_singlehost[i]->started());
}
std::unique_ptr<TestRequest> second_last_singlehost(
NewRequest("http://host/last", net::LOWEST));
std::unique_ptr<TestRequest> last_singlehost(
NewRequest("http://host/s_last", net::LOWEST));
EXPECT_FALSE(second_last_singlehost->started());
high.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(second_last_singlehost->started());
EXPECT_FALSE(last_singlehost->started());
lows_singlehost.erase(lows_singlehost.begin());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(last_singlehost->started());
int expected_slots_left = kDefaultMaxNumDelayableRequestsPerClient -
kMaxNumDelayableRequestsPerHost;
EXPECT_GT(expected_slots_left, 0);
std::vector<std::unique_ptr<TestRequest>> lows_different_host;
base::RunLoop().RunUntilIdle();
for (int i = 0; i < expected_slots_left; ++i) {
string url = "http://host" + base::NumberToString(i) + "/low";
lows_different_host.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows_different_host[i]->started());
}
std::unique_ptr<TestRequest> last_different_host(
NewRequest("http://host_new/last", net::LOWEST));
EXPECT_FALSE(last_different_host->started());
}
TEST_F(ResourceSchedulerTest, RaisePriorityAndStart) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::LOWEST));
EXPECT_FALSE(request->started());
ChangeRequestPriority(request.get(), net::HIGHEST);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(request->started());
}
TEST_F(ResourceSchedulerTest, RaisePriorityInQueue) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::IDLE));
std::unique_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
ChangeRequestPriority(request.get(), net::LOWEST);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
const int kDefaultMaxNumDelayableRequestsPerClient = 10;
std::vector<std::unique_ptr<TestRequest>> lows;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient - 1; ++i) {
string url = "http://host/low" + base::NumberToString(i);
lows.push_back(NewRequest(url.c_str(), net::LOWEST));
}
SetMaxDelayableRequests(kDefaultMaxNumDelayableRequestsPerClient);
high.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(request->started());
EXPECT_FALSE(idle->started());
}
TEST_F(ResourceSchedulerTest, LowerPriority) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::LOWEST));
std::unique_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
ChangeRequestPriority(request.get(), net::IDLE);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
const int kDefaultMaxNumDelayableRequestsPerClient =
10;
const int kNumFillerRequests = kDefaultMaxNumDelayableRequestsPerClient - 2;
std::vector<std::unique_ptr<TestRequest>> lows;
for (int i = 0; i < kNumFillerRequests; ++i) {
string url = "http://host" + base::NumberToString(i) + "/low";
lows.push_back(NewRequest(url.c_str(), net::LOWEST));
}
SetMaxDelayableRequests(10);
high.reset();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(request->started());
EXPECT_TRUE(idle->started());
}
TEST_F(ResourceSchedulerTest, LowerPriorityBrowserRequestsNotThrottled) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewTrustedRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(
NewTrustedRequest("http://host/low", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewTrustedRequest("http://host/req", net::LOWEST));
std::unique_ptr<TestRequest> idle(
NewTrustedRequest("http://host/idle", net::IDLE));
EXPECT_TRUE(request->started());
EXPECT_TRUE(idle->started());
const int kDefaultMaxNumDelayableRequestsPerClient =
10;
std::vector<std::unique_ptr<TestRequest>> lows;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient + 1; ++i) {
string url = "http://host" + base::NumberToString(i) + "/low";
lows.push_back(NewTrustedRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows.back()->started());
}
}
TEST_F(ResourceSchedulerTest,
LowerPriorityBrowserRequestsThrottleP2PConnections) {
const struct {
std::string test_case;
size_t p2p_active_connections;
net::EffectiveConnectionType effective_connection_type;
bool enable_pausing_behavior;
bool set_field_trial_param;
bool expected_browser_initiated_traffic_started;
} tests[] = {
{
"Field trial set",
1u,
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
true,
true,
false,
},
{
"Field trial not set",
1u,
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
false,
true,
true,
},
{
"Network fast",
1u,
net::EFFECTIVE_CONNECTION_TYPE_4G,
true,
true,
true,
},
{
"No active p2p connections",
0u,
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
true,
true,
true,
},
{
"Field trial param not set, default params used",
1u,
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
true,
false,
false,
},
};
for (const auto& test : tests) {
base::test::ScopedFeatureList scoped_feature_list;
if (test.enable_pausing_behavior) {
base::FieldTrialParams field_trial_params;
if (test.set_field_trial_param) {
field_trial_params["throttled_traffic_annotation_tags"] = "727528";
}
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kPauseBrowserInitiatedHeavyTrafficForP2P,
field_trial_params);
} else {
scoped_feature_list.InitAndDisableFeature(
features::kPauseBrowserInitiatedHeavyTrafficForP2P);
}
InitializeScheduler();
network_quality_estimator_
.SetAndNotifyObserversOfP2PActiveConnectionsCountChange(
test.p2p_active_connections);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
test.effective_connection_type);
std::string url = "http://host/browser-initiatited";
net::NetworkTrafficAnnotationTag tag = net::DefineNetworkTrafficAnnotation(
"metrics_report_uma",
"Traffic annotation for unit, browser and other tests");
std::unique_ptr<TestRequest> lows = (NewTrustedRequest(
url.c_str(), net::LOWEST, tag));
EXPECT_EQ(test.expected_browser_initiated_traffic_started, lows->started())
<< " test_case=" << test.test_case;
}
}
TEST_F(ResourceSchedulerTest, P2PConnectionWentAway) {
const struct {
int seconds_to_pause_requests_after_end_of_p2p_connections;
bool expect_lows_started;
} tests[] = {
{
0, true},
{60, false},
};
for (const auto& test : tests) {
base::test::ScopedFeatureList scoped_feature_list;
base::FieldTrialParams field_trial_params;
field_trial_params["throttled_traffic_annotation_tags"] = "727528";
field_trial_params
["seconds_to_pause_requests_after_end_of_p2p_connections"] =
base::NumberToString(
test.seconds_to_pause_requests_after_end_of_p2p_connections);
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kPauseBrowserInitiatedHeavyTrafficForP2P, field_trial_params);
InitializeScheduler();
network_quality_estimator_
.SetAndNotifyObserversOfP2PActiveConnectionsCountChange(1u);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
std::string url = "http://host/browser-initiatited";
net::NetworkTrafficAnnotationTag tag = net::DefineNetworkTrafficAnnotation(
"metrics_report_uma",
"Traffic annotation for unit, browser and other tests");
std::unique_ptr<TestRequest> lows = (NewTrustedRequest(
url.c_str(), net::LOWEST, tag));
EXPECT_FALSE(lows->started());
network_quality_estimator_
.SetAndNotifyObserversOfP2PActiveConnectionsCountChange(2u);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(lows->started());
network_quality_estimator_
.SetAndNotifyObserversOfP2PActiveConnectionsCountChange(0u);
EXPECT_FALSE(lows->started());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(test.expect_lows_started, lows->started());
}
}
TEST_F(ResourceSchedulerTest,
RequestThrottleOnlyOnSlowConnectionsWithP2PRequests) {
base::test::ScopedFeatureList scoped_feature_list;
base::FieldTrialParams field_trial_params;
field_trial_params["throttled_traffic_annotation_tags"] = "727528";
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kPauseBrowserInitiatedHeavyTrafficForP2P, field_trial_params);
InitializeScheduler();
network_quality_estimator_
.SetAndNotifyObserversOfP2PActiveConnectionsCountChange(1u);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
std::string url = "http://host/browser-initiatited";
net::NetworkTrafficAnnotationTag tag = net::DefineNetworkTrafficAnnotation(
"metrics_report_uma",
"Traffic annotation for unit, browser and other tests");
std::unique_ptr<TestRequest> lows = (NewTrustedRequest(
url.c_str(), net::LOWEST, tag));
EXPECT_FALSE(lows->started());
network_quality_estimator_
.SetAndNotifyObserversOfP2PActiveConnectionsCountChange(2u);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(lows->started());
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_4G);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(lows->started());
}
TEST_F(ResourceSchedulerTest, ReprioritizedRequestGoesToBackOfQueue) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::LOWEST));
std::unique_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
const int kDefaultMaxNumDelayableRequestsPerClient = 0;
std::vector<std::unique_ptr<TestRequest>> lows;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient; ++i) {
string url = "http://host/low" + base::NumberToString(i);
lows.push_back(NewRequest(url.c_str(), net::LOWEST));
}
SetMaxDelayableRequests(kDefaultMaxNumDelayableRequestsPerClient);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
ChangeRequestPriority(request.get(), net::LOWEST);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(request->started());
EXPECT_FALSE(idle->started());
}
TEST_F(ResourceSchedulerTest, HigherIntraPriorityGoesToFrontOfQueue) {
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
const int kDefaultMaxNumDelayableRequestsPerClient =
10;
std::vector<std::unique_ptr<TestRequest>> lows;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient; ++i) {
string url = "http://host/low" + base::NumberToString(i);
lows.push_back(NewRequest(url.c_str(), net::IDLE));
}
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::IDLE));
EXPECT_FALSE(request->started());
ChangeRequestPriority(request.get(), net::IDLE, 1);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(request->started());
high.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(request->started());
}
TEST_F(ResourceSchedulerTest, NonHTTPSchedulesImmediately) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
std::unique_ptr<TestRequest> low2(
NewRequest("http://host/low2", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("chrome-extension://req", net::LOWEST));
EXPECT_TRUE(low->started());
EXPECT_FALSE(low2->started());
EXPECT_TRUE(request->started());
}
TEST_F(ResourceSchedulerTest, SpdyProxySchedulesImmediately) {
InitializeScheduler();
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::IDLE));
EXPECT_FALSE(request->started());
}
TEST_F(ResourceSchedulerTest, NewSpdyHostInDelayableRequests) {
base::test::ScopedFeatureList scoped_feature_list;
InitializeScheduler();
const int kDefaultMaxNumDelayableRequestsPerClient =
10;
std::unique_ptr<TestRequest> low1_spdy(
NewRequest("http://spdyhost1:8080/low", net::LOWEST));
std::vector<std::unique_ptr<TestRequest>> lows;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient - 1; ++i) {
string url = "http://host" + base::NumberToString(i) + "/low";
lows.push_back(NewRequest(url.c_str(), net::LOWEST));
}
std::unique_ptr<TestRequest> low1(NewRequest("http://host/low", net::LOWEST));
EXPECT_FALSE(low1->started());
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("http", "spdyhost1", 8080),
net::NetworkAnonymizationKey(), true);
low1_spdy.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(low1->started());
low1.reset();
base::RunLoop().RunUntilIdle();
std::unique_ptr<TestRequest> low2_spdy(
NewRequest("http://spdyhost2:8080/low", net::IDLE));
EXPECT_TRUE(low2_spdy->started());
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("http", "spdyhost2", 8080),
net::NetworkAnonymizationKey(), true);
ChangeRequestPriority(low2_spdy.get(), net::LOWEST);
base::RunLoop().RunUntilIdle();
std::unique_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
EXPECT_TRUE(low2->started());
}
TEST_F(ResourceSchedulerTest,
NewDelayableSpdyHostInDelayableRequestsSlowConnection) {
ConfigureDelayRequestsOnMultiplexedConnectionsFieldTrial();
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
InitializeScheduler();
const int max_delayable_requests_per_client_ect_2g = 8;
std::unique_ptr<TestRequest> low1_spdy(
NewRequest("http://spdyhost1:8080/low", net::LOWEST));
EXPECT_TRUE(low1_spdy->started());
std::vector<std::unique_ptr<TestRequest>> lows;
for (int i = 0; i < max_delayable_requests_per_client_ect_2g - 1; ++i) {
string url = "http://host" + base::NumberToString(i) + "/low";
lows.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows.back()->started());
}
std::unique_ptr<TestRequest> low1(NewRequest("http://host/low", net::LOWEST));
EXPECT_FALSE(low1->started());
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("http", "spdyhost1", 8080),
net::NetworkAnonymizationKey(), true);
low1_spdy.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(low1->started());
low1.reset();
base::RunLoop().RunUntilIdle();
std::unique_ptr<TestRequest> low2_spdy(
NewRequest("http://spdyhost2:8080/low", net::IDLE));
EXPECT_TRUE(low2_spdy->started());
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("http", "spdyhost2", 8080),
net::NetworkAnonymizationKey(), true);
ChangeRequestPriority(low2_spdy.get(), net::LOWEST);
base::RunLoop().RunUntilIdle();
std::unique_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
EXPECT_FALSE(low2->started());
std::unique_ptr<TestRequest> low3_spdy(
NewRequest("http://spdyhost1:8080/low", net::LOWEST));
EXPECT_FALSE(low3_spdy->started());
}
TEST_F(ResourceSchedulerTest, RequestStartedAfterClientDeleted) {
SetMaxDelayableRequests(1);
scheduler_->OnClientCreated(kClientId2, IsBrowserInitiated(false),
&network_quality_estimator_);
std::unique_ptr<TestRequest> high(
NewRequestWithClientId("http://host/high", net::HIGHEST, kClientId2));
std::unique_ptr<TestRequest> lowest1(
NewRequestWithClientId("http://host/lowest", net::LOWEST, kClientId2));
std::unique_ptr<TestRequest> lowest2(
NewRequestWithClientId("http://host/lowest", net::LOWEST, kClientId2));
EXPECT_FALSE(lowest2->started());
scheduler_->OnClientDeleted(kClientId2);
high.reset();
lowest1.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(lowest2->started());
}
TEST_F(ResourceSchedulerTest, RequestStartedAfterClientDeletedManyDelayable) {
scheduler_->OnClientCreated(kClientId2, IsBrowserInitiated(false),
&network_quality_estimator_);
std::unique_ptr<TestRequest> high(
NewRequestWithClientId("http://host/high", net::HIGHEST, kClientId2));
const int kDefaultMaxNumDelayableRequestsPerClient = 10;
std::vector<std::unique_ptr<TestRequest>> delayable_requests;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient + 1; ++i) {
delayable_requests.push_back(
NewRequestWithClientId("http://host/lowest", net::LOWEST, kClientId2));
}
std::unique_ptr<TestRequest> lowest(
NewRequestWithClientId("http://host/lowest", net::LOWEST, kClientId2));
EXPECT_FALSE(lowest->started());
scheduler_->OnClientDeleted(kClientId2);
high.reset();
delayable_requests.clear();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(lowest->started());
}
TEST_F(ResourceSchedulerTest, RequestLimitOverrideEnabled) {
RequestLimitOverrideConfigTestHelper(true);
}
TEST_F(ResourceSchedulerTest, RequestLimitOverrideDisabled) {
RequestLimitOverrideConfigTestHelper(false);
}
TEST_F(ResourceSchedulerTest, RequestLimitOverrideOutsideECTRange) {
base::test::ScopedFeatureList scoped_feature_list;
InitializeThrottleDelayableExperiment(true, 0.0);
InitializeScheduler();
for (net::EffectiveConnectionType ect :
{net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
net::EFFECTIVE_CONNECTION_TYPE_OFFLINE,
net::EFFECTIVE_CONNECTION_TYPE_4G}) {
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
ect);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int kDefaultMaxNumDelayableRequestsPerClient = 10;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows_singlehost[i]->started());
}
std::unique_ptr<TestRequest> last_singlehost(
NewRequest("http://host/last", net::LOWEST));
EXPECT_FALSE(last_singlehost->started());
}
}
TEST_F(ResourceSchedulerTest, RequestLimitOverrideNotFixedForPageLoad) {
InitializeThrottleDelayableExperiment(true, 0.0);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int kOverriddenNumRequests = 2;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < kOverriddenNumRequests; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows_singlehost[i]->started());
}
std::unique_ptr<TestRequest> second_last_singlehost(
NewRequest("http://host/slast", net::LOWEST));
EXPECT_FALSE(second_last_singlehost->started());
lows_singlehost.erase(lows_singlehost.begin());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(second_last_singlehost->started());
std::unique_ptr<TestRequest> last_singlehost_before_ect_change(
NewRequest("http://host/last_singlehost_before_ect_change", net::LOWEST));
EXPECT_FALSE(last_singlehost_before_ect_change->started());
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_4G);
base::RunLoop().RunUntilIdle();
std::unique_ptr<TestRequest> last_singlehost_after_ect_change(
NewRequest("http://host/last_singlehost_after_ect_change", net::LOWEST));
EXPECT_TRUE(last_singlehost_before_ect_change->started());
EXPECT_TRUE(last_singlehost_after_ect_change->started());
}
TEST_F(ResourceSchedulerTest, RequestLimitReducedAcrossPageLoads) {
InitializeThrottleDelayableExperiment(true, 0.0);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_3G);
InitializeScheduler();
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int kNumDelayableHigh = 4;
const int kNumDelayableLow = 2;
std::vector<std::unique_ptr<TestRequest>> delayable_first_page;
for (int i = 0; i < kNumDelayableHigh; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low1";
delayable_first_page.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(delayable_first_page[i]->started());
}
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
std::unique_ptr<TestRequest> high2(
NewRequest("http://host/high2", net::HIGHEST));
EXPECT_TRUE(high->started());
std::vector<std::unique_ptr<TestRequest>> delayable_second_page;
for (int i = 0; i < kNumDelayableLow; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low2";
delayable_second_page.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_FALSE(delayable_second_page[i]->started());
}
for (int i = 0; i < kNumDelayableHigh - kNumDelayableLow; ++i) {
delayable_first_page.pop_back();
}
base::RunLoop().RunUntilIdle();
for (int i = 0; i < kNumDelayableLow; ++i) {
EXPECT_FALSE(delayable_second_page[i]->started());
}
delayable_first_page.clear();
base::RunLoop().RunUntilIdle();
for (int i = 0; i < kNumDelayableLow; ++i) {
EXPECT_TRUE(delayable_second_page[i]->started());
}
std::string url =
"http://host" + base::NumberToString(kNumDelayableLow) + "/low3";
delayable_second_page.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_FALSE(delayable_second_page.back()->started());
}
TEST_F(ResourceSchedulerTest, ThrottleDelayableDisabled) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(features::kThrottleDelayable);
InitializeScheduler();
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
std::unique_ptr<TestRequest> medium(
NewRequest("http://host/medium", net::MEDIUM));
ASSERT_TRUE(medium->started());
std::vector<std::unique_ptr<TestRequest>> delayable_requests;
for (int i = 0; i < 5; ++i) {
delayable_requests.push_back(NewRequest(
base::StringPrintf("http://host%d/low", i).c_str(), net::LOWEST));
EXPECT_TRUE(delayable_requests.back()->started());
}
delayable_requests.push_back(
NewRequest("http://host/low-blocked", net::LOWEST));
EXPECT_FALSE(delayable_requests.back()->started());
}
TEST_F(ResourceSchedulerTest, NonDelayableThrottlesDelayableOutsideECT) {
const double kNonDelayableWeight = 2.0;
const int kDefaultMaxNumDelayableRequestsPerClient =
10;
InitializeThrottleDelayableExperiment(false, kNonDelayableWeight);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_3G);
InitializeScheduler();
std::unique_ptr<TestRequest> medium(
NewRequest("http://host/medium", net::MEDIUM));
ASSERT_TRUE(medium->started());
std::vector<std::unique_ptr<TestRequest>> delayable_requests;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient; ++i) {
delayable_requests.push_back(NewRequest(
base::StringPrintf("http://host%d/low", i).c_str(), net::LOWEST));
EXPECT_TRUE(delayable_requests.back()->started());
}
}
TEST_F(ResourceSchedulerTest, NonDelayableThrottlesDelayableVaryNonDelayable) {
const double kNonDelayableWeight = 2.0;
const int kDefaultMaxNumDelayableRequestsPerClient =
8;
InitializeThrottleDelayableExperiment(false, kNonDelayableWeight);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
for (int num_non_delayable = 0; num_non_delayable < 10; ++num_non_delayable) {
base::RunLoop().RunUntilIdle();
std::vector<std::unique_ptr<TestRequest>> non_delayable_requests;
for (int i = 0; i < num_non_delayable; ++i) {
non_delayable_requests.push_back(NewRequest(
base::StringPrintf("http://host%d/medium", i).c_str(), net::MEDIUM));
ASSERT_TRUE(non_delayable_requests.back()->started());
}
std::vector<std::unique_ptr<TestRequest>> delayable_requests;
for (int i = 0; i < kDefaultMaxNumDelayableRequestsPerClient -
num_non_delayable * kNonDelayableWeight;
++i) {
delayable_requests.push_back(NewRequest(
base::StringPrintf("http://host%d/low", i).c_str(), net::LOWEST));
EXPECT_TRUE(delayable_requests.back()->started());
}
std::unique_ptr<TestRequest> last_low(
NewRequest("http://lasthost/low", net::LOWEST));
EXPECT_FALSE(last_low->started());
}
}
TEST_F(ResourceSchedulerTest, NonDelayableThrottlesDelayableWeight1) {
NonDelayableThrottlesDelayableHelper(1.0);
}
TEST_F(ResourceSchedulerTest, NonDelayableThrottlesDelayableWeight3) {
NonDelayableThrottlesDelayableHelper(3.0);
}
TEST_F(ResourceSchedulerTest, Simple) {
SetMaxDelayableRequests(1);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::LOWEST));
EXPECT_FALSE(request->started());
}
TEST_F(ResourceSchedulerTest, MultipleInstances_1) {
SetMaxDelayableRequests(1);
ResourceScheduler another_scheduler(base::DefaultTickClock::GetInstance());
another_scheduler.SetResourceSchedulerParamsManagerForTests(
ResourceSchedulerParamsManager(FixedParamsManager(99)));
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequest("http://host/req", net::LOWEST));
EXPECT_FALSE(request->started());
}
TEST_F(ResourceSchedulerTest, MultipleInstances_2) {
SetMaxDelayableRequests(1);
ResourceScheduler another_scheduler(base::DefaultTickClock::GetInstance());
another_scheduler.OnClientCreated(kClientId1, IsBrowserInitiated(false),
&network_quality_estimator_);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
std::unique_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
std::unique_ptr<TestRequest> request(
NewRequestWithClientId("http://host/req", net::LOWEST, kClientId1));
EXPECT_FALSE(request->started());
{
another_scheduler.SetResourceSchedulerParamsManagerForTests(
FixedParamsManager(1));
std::unique_ptr<net::URLRequest> url_request(NewURLRequest(
"http://host/another", net::LOWEST, TRAFFIC_ANNOTATION_FOR_TESTS));
auto scheduled_request =
another_scheduler.ScheduleRequest(kClientId1, true, url_request.get());
auto another_request = std::make_unique<TestRequest>(
std::move(url_request), std::move(scheduled_request),
&another_scheduler);
another_request->Start();
EXPECT_TRUE(another_request->started());
}
another_scheduler.OnClientDeleted(kClientId1);
}
TEST_F(ResourceSchedulerTest,
MaxRequestsPerHostForSpdyWhenDelayableSlowConnections) {
ConfigureDelayRequestsOnMultiplexedConnectionsFieldTrial();
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
InitializeScheduler();
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("https", "spdyhost", 443),
net::NetworkAnonymizationKey(), true);
const size_t kDefaultMaxNumDelayableRequestsPerClient = 8;
ASSERT_LT(kMaxNumDelayableRequestsPerHostPerClient,
kDefaultMaxNumDelayableRequestsPerClient);
std::vector<std::unique_ptr<TestRequest>> requests;
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i) {
requests.push_back(NewRequest("https://spdyhost/low", net::LOWEST));
EXPECT_TRUE(requests[i]->started());
}
for (size_t i = kMaxNumDelayableRequestsPerHostPerClient + 1;
i < kDefaultMaxNumDelayableRequestsPerClient + 1; i++) {
EXPECT_EQ(i, requests.size());
requests.push_back(NewRequest("https://spdyhost/low", net::LOWEST));
EXPECT_EQ(i < kDefaultMaxNumDelayableRequestsPerClient,
requests[i]->started());
}
}
TEST_F(ResourceSchedulerTest,
MaxRequestsPerHostForSpdyWhenDelayableSlowConnectionsWithIsolationInfo) {
const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/"));
const net::IsolationInfo kIsolationInfo1 =
net::IsolationInfo::CreateForInternalRequest(kOrigin1);
const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/"));
const net::IsolationInfo kIsolationInfo2 =
net::IsolationInfo::CreateForInternalRequest(kOrigin2);
ConfigureDelayRequestsOnMultiplexedConnectionsFieldTrial();
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
InitializeScheduler();
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("https", "spdyhost", 443),
kIsolationInfo1.network_anonymization_key(), true);
const size_t kDefaultMaxNumDelayableRequestsPerClient = 8;
ASSERT_LT(kMaxNumDelayableRequestsPerHostPerClient,
kDefaultMaxNumDelayableRequestsPerClient);
std::vector<std::unique_ptr<TestRequest>> requests;
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i) {
requests.push_back(NewRequestWithIsolationInfo(
"https://spdyhost/low", net::LOWEST, kIsolationInfo1));
EXPECT_TRUE(requests[i]->started());
}
for (size_t i = kMaxNumDelayableRequestsPerHostPerClient + 1;
i < kDefaultMaxNumDelayableRequestsPerClient + 1; i++) {
EXPECT_EQ(i, requests.size());
requests.push_back(NewRequestWithIsolationInfo(
"https://spdyhost/low", net::LOWEST, kIsolationInfo1));
EXPECT_EQ(i < kDefaultMaxNumDelayableRequestsPerClient,
requests[i]->started());
}
requests.clear();
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i) {
requests.push_back(NewRequestWithIsolationInfo(
"https://spdyhost/low", net::LOWEST, net::IsolationInfo()));
EXPECT_EQ(i < kMaxNumDelayableRequestsPerHostPerClient,
requests[i]->started());
}
requests.clear();
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i) {
requests.push_back(NewRequestWithIsolationInfo(
"https://spdyhost/low", net::LOWEST, kIsolationInfo2));
EXPECT_EQ(i < kMaxNumDelayableRequestsPerHostPerClient,
requests[i]->started());
}
}
TEST_F(ResourceSchedulerTest,
MaxRequestsPerHostForSpdyWhenDelayableFastConnections) {
ConfigureDelayRequestsOnMultiplexedConnectionsFieldTrial();
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_4G);
InitializeScheduler();
context_->http_server_properties()->SetSupportsSpdy(
url::SchemeHostPort("https", "spdyhost", 443),
net::NetworkAnonymizationKey(), true);
const size_t kDefaultMaxNumDelayableRequestsPerClient = 10;
ASSERT_LT(kMaxNumDelayableRequestsPerHostPerClient,
kDefaultMaxNumDelayableRequestsPerClient);
std::vector<std::unique_ptr<TestRequest>> requests;
for (size_t i = 0; i < kDefaultMaxNumDelayableRequestsPerClient + 1; ++i) {
requests.push_back(NewRequest("https://spdyhost/low", net::LOWEST));
EXPECT_TRUE(requests[i]->started());
}
}
TEST_F(ResourceSchedulerTest,
MaxRequestsPerHostForNonSpdyWhenDelayableSlowConnections) {
ConfigureDelayRequestsOnMultiplexedConnectionsFieldTrial();
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
InitializeScheduler();
std::vector<std::unique_ptr<TestRequest>> requests;
for (size_t i = 0; i < kMaxNumDelayableRequestsPerHostPerClient + 1; ++i)
requests.push_back(NewRequest("https://non_spdyhost/low", net::LOWEST));
for (size_t i = 0; i < requests.size(); ++i) {
EXPECT_EQ(i < kMaxNumDelayableRequestsPerHostPerClient,
requests[i]->started());
}
}
TEST_F(ResourceSchedulerTest,
DelayableRequestLimitSpdyDelayableSlowConnections) {
ConfigureDelayRequestsOnMultiplexedConnectionsFieldTrial();
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int max_low_priority_requests_allowed = 5;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < max_low_priority_requests_allowed; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_TRUE(lows_singlehost[i]->started()) << i;
}
std::unique_ptr<TestRequest> last_singlehost(
NewRequest("http://host/last", net::LOWEST));
EXPECT_FALSE(last_singlehost->started());
}
TEST_F(ResourceSchedulerTest, MaxQueuingDelaySet) {
base::TimeDelta max_queuing_time = base::Seconds(15);
InitializeMaxQueuingDelayExperiment(max_queuing_time);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int max_low_priority_requests_allowed = 5;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_EQ(i < max_low_priority_requests_allowed,
lows_singlehost[i]->started());
}
tick_clock_.SetNowTicks(base::DefaultTickClock::GetInstance()->NowTicks() +
max_queuing_time + base::Seconds(1));
lows_singlehost[0].reset();
base::RunLoop().RunUntilIdle();
for (int i = 1; i < max_low_priority_requests_allowed + 10; ++i) {
EXPECT_TRUE(lows_singlehost[i]->started());
}
}
TEST_F(ResourceSchedulerTest, MaxQueuingDelayNotSet) {
base::TimeDelta max_queuing_time = base::Seconds(15);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int max_low_priority_requests_allowed = 5;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_EQ(i < max_low_priority_requests_allowed,
lows_singlehost[i]->started());
}
tick_clock_.SetNowTicks(base::DefaultTickClock::GetInstance()->NowTicks() +
max_queuing_time + base::Seconds(1));
lows_singlehost[0].reset();
base::RunLoop().RunUntilIdle();
for (int i = 1; i < max_low_priority_requests_allowed + 10; ++i) {
EXPECT_EQ(i < max_low_priority_requests_allowed + 1,
lows_singlehost[i]->started());
}
}
TEST_F(ResourceSchedulerTest, MaxQueuingDelayTimerFires) {
base::TimeDelta max_queuing_time = base::Seconds(15);
InitializeMaxQueuingDelayExperiment(max_queuing_time);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int max_low_priority_requests_allowed = 5;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_EQ(i < max_low_priority_requests_allowed,
lows_singlehost[i]->started());
}
tick_clock_.SetNowTicks(base::DefaultTickClock::GetInstance()->NowTicks() +
max_queuing_time + base::Seconds(1));
scheduler()->DispatchLongQueuedRequestsForTesting();
base::RunLoop().RunUntilIdle();
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
EXPECT_TRUE(lows_singlehost[i]->started());
}
}
TEST_F(ResourceSchedulerTest, MaxQueuingDelayTimerNotFired) {
base::TimeDelta max_queuing_time = base::Seconds(15);
InitializeMaxQueuingDelayExperiment(max_queuing_time);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
InitializeScheduler();
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
const int max_low_priority_requests_allowed = 5;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_EQ(i < max_low_priority_requests_allowed,
lows_singlehost[i]->started());
}
tick_clock_.SetNowTicks(base::DefaultTickClock::GetInstance()->NowTicks() +
max_queuing_time + base::Seconds(1));
base::RunLoop().RunUntilIdle();
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
EXPECT_EQ(i < max_low_priority_requests_allowed,
lows_singlehost[i]->started())
<< " i=" << i;
}
}
TEST_F(ResourceSchedulerTest, MaxQueuingDelayTimerRunsOnRequestSchedule) {
base::TimeDelta max_queuing_time = base::Seconds(15);
InitializeMaxQueuingDelayExperiment(max_queuing_time);
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
const int max_low_priority_requests_allowed = 5;
std::vector<std::unique_ptr<TestRequest>> lows_singlehost;
InitializeScheduler();
EXPECT_FALSE(scheduler()->IsLongQueuedRequestsDispatchTimerRunning());
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
EXPECT_EQ(i < max_low_priority_requests_allowed,
lows_singlehost[i]->started());
}
EXPECT_TRUE(scheduler()->IsLongQueuedRequestsDispatchTimerRunning());
scheduler()->DispatchLongQueuedRequestsForTesting();
EXPECT_TRUE(scheduler()->IsLongQueuedRequestsDispatchTimerRunning());
high.reset();
for (auto& request : lows_singlehost) {
request.reset();
}
scheduler()->DispatchLongQueuedRequestsForTesting();
EXPECT_FALSE(scheduler()->IsLongQueuedRequestsDispatchTimerRunning());
std::unique_ptr<TestRequest> high2(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high2->started());
EXPECT_FALSE(scheduler()->IsLongQueuedRequestsDispatchTimerRunning());
for (int i = 0; i < max_low_priority_requests_allowed + 10; ++i) {
std::string url = "http://host" + base::NumberToString(i) + "/low";
lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
}
EXPECT_TRUE(scheduler()->IsLongQueuedRequestsDispatchTimerRunning());
}
TEST_F(ResourceSchedulerTest, NonDelayableRequestArrivesAfterDelayableStarts) {
base::TimeDelta max_queuing_time = base::Seconds(15);
InitializeMaxQueuingDelayExperiment(max_queuing_time);
InitializeScheduler();
std::unique_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
EXPECT_TRUE(low->started());
const base::TimeDelta delay = base::Seconds(5);
tick_clock_.SetNowTicks(base::TimeTicks::Now() + delay);
std::unique_ptr<TestRequest> high(
NewRequest("http://host/high", net::HIGHEST));
EXPECT_TRUE(high->started());
}
TEST_F(ResourceSchedulerTest, ProactiveThrottlingExperiment) {
const struct {
std::string test_case;
bool enable_http_rtt_multiplier_for_proactive_throttling;
} tests[] = {
{
"Enable proactive throttling",
true,
},
{
"Disabled proactive throttling",
false,
},
};
for (const auto& test : tests) {
double http_rtt_multiplier_for_proactive_throttling = 5;
base::TimeDelta http_rtt = base::Seconds(1);
if (test.enable_http_rtt_multiplier_for_proactive_throttling) {
ConfigureProactiveThrottlingExperimentFor2G(
http_rtt_multiplier_for_proactive_throttling);
} else {
ConfigureProactiveThrottlingExperimentFor2G(-1);
}
network_quality_estimator_.SetStartTimeNullHttpRtt(http_rtt);
base::RunLoop().RunUntilIdle();
base::TimeDelta threshold_requests_anticipation =
http_rtt_multiplier_for_proactive_throttling * http_rtt;
std::unique_ptr<TestRequest> high_1(
NewRequest("http://host/high_1", net::HIGHEST));
EXPECT_TRUE(high_1->started());
high_1.reset();
std::unique_ptr<TestRequest> low_1(
NewRequest("http://host/low_1", net::LOWEST));
EXPECT_NE(test.enable_http_rtt_multiplier_for_proactive_throttling,
low_1->started())
<< " test_case=" << test.test_case;
tick_clock_.Advance(threshold_requests_anticipation -
base::Milliseconds(1));
std::unique_ptr<TestRequest> low_2(
NewRequest("http://host/low_2", net::LOWEST));
EXPECT_NE(test.enable_http_rtt_multiplier_for_proactive_throttling,
low_2->started());
tick_clock_.Advance(base::Milliseconds(100));
std::unique_ptr<TestRequest> low_3(
NewRequest("http://host/low_3", net::LOWEST));
EXPECT_TRUE(low_3->started());
std::unique_ptr<TestRequest> high_2(
NewRequest("http://host/high_2", net::HIGHEST));
EXPECT_TRUE(high_2->started());
}
}
TEST_F(ResourceSchedulerTest,
ProactiveThrottlingDoesNotThrottleHighPriorityRequests) {
double http_rtt_multiplier_for_proactive_throttling = 5;
ConfigureProactiveThrottlingExperimentFor2G(
http_rtt_multiplier_for_proactive_throttling);
base::TimeDelta http_rtt = base::Seconds(1);
network_quality_estimator_.SetStartTimeNullHttpRtt(http_rtt);
base::RunLoop().RunUntilIdle();
base::TimeDelta threshold_requests_anticipation =
http_rtt_multiplier_for_proactive_throttling * http_rtt;
std::unique_ptr<TestRequest> high_1(
NewRequest("http://host/high_1", net::HIGHEST));
EXPECT_TRUE(high_1->started());
high_1.reset();
std::unique_ptr<TestRequest> low_1(
NewRequest("http://host/low_1", net::LOWEST));
EXPECT_FALSE(low_1->started());
tick_clock_.Advance(threshold_requests_anticipation - base::Milliseconds(1));
std::unique_ptr<TestRequest> low_2(
NewRequest("http://host/low_2", net::LOWEST));
EXPECT_FALSE(low_2->started());
std::unique_ptr<TestRequest> high_2(
NewRequest("http://host/high_2", net::HIGHEST));
EXPECT_TRUE(high_2->started());
high_2.reset();
low_1.reset();
low_2.reset();
std::unique_ptr<TestRequest> high_3(
NewRequest("http://host/high_3", net::HIGHEST));
EXPECT_TRUE(high_3->started());
high_3.reset();
std::unique_ptr<TestRequest> low_3(
NewRequest("http://host/low_3", net::LOWEST));
EXPECT_FALSE(low_3->started());
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(low_3->started());
network_quality_estimator_.SetAndNotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_4G);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(low_3->started());
}
TEST_F(ResourceSchedulerTest, ProactiveThrottling_UnthrottledOnTimerFired) {
double http_rtt_multiplier_for_proactive_throttling = 5;
ConfigureProactiveThrottlingExperimentFor2G(
http_rtt_multiplier_for_proactive_throttling);
base::TimeDelta http_rtt = base::Seconds(1);
network_quality_estimator_.SetStartTimeNullHttpRtt(http_rtt);
base::RunLoop().RunUntilIdle();
base::TimeDelta threshold_requests_anticipation =
http_rtt_multiplier_for_proactive_throttling * http_rtt;
std::unique_ptr<TestRequest> high_1(
NewRequest("http://host/high_1", net::HIGHEST));
EXPECT_TRUE(high_1->started());
high_1.reset();
std::unique_ptr<TestRequest> low_1(
NewRequest("http://host/low_1", net::LOWEST));
EXPECT_FALSE(low_1->started());
tick_clock_.Advance(threshold_requests_anticipation + base::Milliseconds(1));
scheduler()->DispatchLongQueuedRequestsForTesting();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(low_1->started());
}
}
}