1 // Copyright (c) 2012 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.
5 #include "content/browser/loader/resource_scheduler.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/test/mock_entropy_provider.h"
13 #include "base/timer/mock_timer.h"
14 #include "base/timer/timer.h"
15 #include "content/browser/browser_thread_impl.h"
16 #include "content/browser/loader/resource_dispatcher_host_impl.h"
17 #include "content/browser/loader/resource_message_filter.h"
18 #include "content/browser/loader/resource_request_info_impl.h"
19 #include "content/common/resource_messages.h"
20 #include "content/public/browser/resource_context.h"
21 #include "content/public/browser/resource_controller.h"
22 #include "content/public/browser/resource_throttle.h"
23 #include "content/public/common/content_switches.h"
24 #include "content/public/common/process_type.h"
25 #include "content/public/common/resource_type.h"
26 #include "content/public/test/mock_render_process_host.h"
27 #include "content/public/test/test_browser_context.h"
28 #include "content/test/test_render_view_host_factory.h"
29 #include "content/test/test_web_contents.h"
30 #include "net/base/host_port_pair.h"
31 #include "net/base/request_priority.h"
32 #include "net/http/http_server_properties_impl.h"
33 #include "net/url_request/url_request.h"
34 #include "net/url_request/url_request_test_util.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36 #include "ui/events/latency_info.h"
44 class TestRequestFactory
;
46 const int kChildId
= 30;
47 const int kRouteId
= 75;
48 const int kChildId2
= 43;
49 const int kRouteId2
= 67;
50 const int kBackgroundChildId
= 35;
51 const int kBackgroundRouteId
= 43;
52 const int kBackgroundChildId2
= 54;
53 const int kBackgroundRouteId2
= 82;
55 class TestRequest
: public ResourceController
{
57 TestRequest(scoped_ptr
<ResourceThrottle
> throttle
,
58 scoped_ptr
<net::URLRequest
> url_request
)
60 throttle_(throttle
.Pass()),
61 url_request_(url_request
.Pass()) {
62 throttle_
->set_controller_for_testing(this);
64 ~TestRequest() override
{}
66 bool started() const { return started_
; }
69 bool deferred
= false;
70 throttle_
->WillStartRequest(&deferred
);
74 void Cancel() override
{
75 // Alert the scheduler that the request can be deleted.
79 const net::URLRequest
* url_request() const { return url_request_
.get(); }
82 // ResourceController interface:
83 void CancelAndIgnore() override
{}
84 void CancelWithError(int error_code
) override
{}
85 void Resume() override
{ started_
= true; }
89 scoped_ptr
<ResourceThrottle
> throttle_
;
90 scoped_ptr
<net::URLRequest
> url_request_
;
93 class CancelingTestRequest
: public TestRequest
{
95 CancelingTestRequest(scoped_ptr
<ResourceThrottle
> throttle
,
96 scoped_ptr
<net::URLRequest
> url_request
)
97 : TestRequest(throttle
.Pass(), url_request
.Pass()) {}
99 void set_request_to_cancel(scoped_ptr
<TestRequest
> request_to_cancel
) {
100 request_to_cancel_
= request_to_cancel
.Pass();
104 void Resume() override
{
105 TestRequest::Resume();
106 request_to_cancel_
.reset();
109 scoped_ptr
<TestRequest
> request_to_cancel_
;
112 class FakeResourceContext
: public ResourceContext
{
114 net::HostResolver
* GetHostResolver() override
{ return NULL
; }
115 net::URLRequestContext
* GetRequestContext() override
{ return NULL
; }
118 class FakeResourceMessageFilter
: public ResourceMessageFilter
{
120 FakeResourceMessageFilter(int child_id
)
121 : ResourceMessageFilter(
123 PROCESS_TYPE_RENDERER
,
124 NULL
/* appcache_service */,
125 NULL
/* blob_storage_context */,
126 NULL
/* file_system_context */,
127 NULL
/* service_worker_context */,
128 NULL
/* host_zoom_level_context */,
129 base::Bind(&FakeResourceMessageFilter::GetContexts
,
130 base::Unretained(this))) {
134 ~FakeResourceMessageFilter() override
{}
136 void GetContexts(const ResourceHostMsg_Request
& request
,
137 ResourceContext
** resource_context
,
138 net::URLRequestContext
** request_context
) {
139 *resource_context
= &context_
;
140 *request_context
= NULL
;
143 FakeResourceContext context_
;
146 class ResourceSchedulerTest
: public testing::Test
{
148 ResourceSchedulerTest()
149 : next_request_id_(0),
150 ui_thread_(BrowserThread::UI
, &message_loop_
),
151 io_thread_(BrowserThread::IO
, &message_loop_
),
152 field_trial_list_(new base::MockEntropyProvider()) {
153 InitializeScheduler();
154 context_
.set_http_server_properties(http_server_properties_
.GetWeakPtr());
157 ~ResourceSchedulerTest() override
{
161 // Done separately from construction to allow for modification of command
162 // line flags in tests.
163 void InitializeScheduler() {
166 // Destroys previous scheduler, also destroys any previously created
168 scheduler_
.reset(new ResourceScheduler());
170 mock_timer_
= new base::MockTimer(true, true);
171 scheduler_
->set_timer_for_testing(scoped_ptr
<base::Timer
>(mock_timer_
));
173 // TODO(aiolos): Remove when throttling and coalescing have both landed.
174 scheduler_
->SetThrottleOptionsForTesting(true /* should_throttle */,
175 false /* should_coalesce */);
177 scheduler_
->OnClientCreated(kChildId
, kRouteId
, true, false);
178 scheduler_
->OnClientCreated(
179 kBackgroundChildId
, kBackgroundRouteId
, false, false);
182 void CleanupScheduler() {
184 scheduler_
->OnClientDeleted(kChildId
, kRouteId
);
185 scheduler_
->OnClientDeleted(kBackgroundChildId
, kBackgroundRouteId
);
189 // Create field trials based on the argument, which has the same format
190 // as the argument to kForceFieldTrials.
191 bool InitializeFieldTrials(const std::string
& force_field_trial_argument
) {
192 return base::FieldTrialList::CreateTrialsFromString(
193 force_field_trial_argument
,
194 base::FieldTrialList::DONT_ACTIVATE_TRIALS
,
195 std::set
<std::string
>());
198 scoped_ptr
<net::URLRequest
> NewURLRequestWithChildAndRoute(
200 net::RequestPriority priority
,
204 scoped_ptr
<net::URLRequest
> url_request(
205 context_
.CreateRequest(GURL(url
), priority
, NULL
));
206 ResourceRequestInfoImpl
* info
= new ResourceRequestInfoImpl(
207 PROCESS_TYPE_RENDERER
, // process_type
208 child_id
, // child_id
209 route_id
, // route_id
210 -1, // frame_tree_node_id
212 ++next_request_id_
, // request_id
213 MSG_ROUTING_NONE
, // render_frame_id
214 false, // is_main_frame
215 false, // parent_is_main_frame
216 0, // parent_render_frame_id
217 RESOURCE_TYPE_SUB_RESOURCE
, // resource_type
218 ui::PAGE_TRANSITION_LINK
, // transition_type
219 false, // should_replace_current_entry
220 false, // is_download
222 true, // allow_download
223 false, // has_user_gesture
224 false, // enable_load_timing
225 false, // enable_upload_progress
226 false, // do_not_prompt_for_login
227 blink::WebReferrerPolicyDefault
, // referrer_policy
228 blink::WebPageVisibilityStateVisible
, // visibility_state
230 base::WeakPtr
<ResourceMessageFilter
>(), // filter
231 is_async
); // is_async
232 info
->AssociateWithRequest(url_request
.get());
233 return url_request
.Pass();
236 scoped_ptr
<net::URLRequest
> NewURLRequest(const char* url
,
237 net::RequestPriority priority
) {
238 return NewURLRequestWithChildAndRoute(
239 url
, priority
, kChildId
, kRouteId
, true);
242 TestRequest
* NewRequestWithRoute(const char* url
,
243 net::RequestPriority priority
,
245 return NewRequestWithChildAndRoute(url
, priority
, route_id
, kChildId
);
248 TestRequest
* NewRequestWithChildAndRoute(const char* url
,
249 net::RequestPriority priority
,
252 return GetNewTestRequest(url
, priority
, child_id
, route_id
, true);
255 TestRequest
* NewRequest(const char* url
, net::RequestPriority priority
) {
256 return NewRequestWithChildAndRoute(url
, priority
, kChildId
, kRouteId
);
259 TestRequest
* NewBackgroundRequest(const char* url
,
260 net::RequestPriority priority
) {
261 return NewRequestWithChildAndRoute(
262 url
, priority
, kBackgroundChildId
, kBackgroundRouteId
);
265 TestRequest
* NewSyncRequest(const char* url
, net::RequestPriority priority
) {
266 return NewSyncRequestWithChildAndRoute(url
, priority
, kChildId
, kRouteId
);
269 TestRequest
* NewBackgroundSyncRequest(const char* url
,
270 net::RequestPriority priority
) {
271 return NewSyncRequestWithChildAndRoute(
272 url
, priority
, kBackgroundChildId
, kBackgroundRouteId
);
275 TestRequest
* NewSyncRequestWithChildAndRoute(const char* url
,
276 net::RequestPriority priority
,
279 return GetNewTestRequest(url
, priority
, child_id
, route_id
, false);
282 TestRequest
* GetNewTestRequest(const char* url
,
283 net::RequestPriority priority
,
287 scoped_ptr
<net::URLRequest
> url_request(NewURLRequestWithChildAndRoute(
288 url
, priority
, child_id
, route_id
, is_async
));
289 scoped_ptr
<ResourceThrottle
> throttle(
290 scheduler_
->ScheduleRequest(child_id
, route_id
, url_request
.get()));
291 TestRequest
* request
= new TestRequest(throttle
.Pass(), url_request
.Pass());
296 void ChangeRequestPriority(TestRequest
* request
,
297 net::RequestPriority new_priority
,
298 int intra_priority
= 0) {
299 scoped_refptr
<FakeResourceMessageFilter
> filter(
300 new FakeResourceMessageFilter(kChildId
));
301 const ResourceRequestInfoImpl
* info
= ResourceRequestInfoImpl::ForRequest(
302 request
->url_request());
303 const GlobalRequestID
& id
= info
->GetGlobalRequestID();
304 ResourceHostMsg_DidChangePriority
msg(id
.request_id
, new_priority
,
306 rdh_
.OnMessageReceived(msg
, filter
.get());
309 void FireCoalescingTimer() {
310 EXPECT_TRUE(mock_timer_
->IsRunning());
314 ResourceScheduler
* scheduler() {
315 return scheduler_
.get();
318 int next_request_id_
;
319 base::MessageLoopForIO message_loop_
;
320 BrowserThreadImpl ui_thread_
;
321 BrowserThreadImpl io_thread_
;
322 ResourceDispatcherHostImpl rdh_
;
323 scoped_ptr
<ResourceScheduler
> scheduler_
;
324 base::FieldTrialList field_trial_list_
;
325 base::MockTimer
* mock_timer_
;
326 net::HttpServerPropertiesImpl http_server_properties_
;
327 net::TestURLRequestContext context_
;
330 TEST_F(ResourceSchedulerTest
, OneIsolatedLowRequest
) {
331 scoped_ptr
<TestRequest
> request(NewRequest("http://host/1", net::LOWEST
));
332 EXPECT_TRUE(request
->started());
335 TEST_F(ResourceSchedulerTest
, OneLowLoadsUntilIdle
) {
336 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
337 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
338 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
339 EXPECT_TRUE(high
->started());
340 EXPECT_TRUE(low
->started());
341 EXPECT_FALSE(low2
->started());
343 EXPECT_TRUE(low2
->started());
346 TEST_F(ResourceSchedulerTest
, OneLowLoadsUntilBodyInserted
) {
347 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
348 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
349 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
350 EXPECT_TRUE(high
->started());
351 EXPECT_TRUE(low
->started());
352 EXPECT_FALSE(low2
->started());
354 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
355 EXPECT_TRUE(low2
->started());
358 TEST_F(ResourceSchedulerTest
, OneLowLoadsUntilCriticalComplete
) {
359 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
360 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
361 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
362 EXPECT_TRUE(high
->started());
363 EXPECT_TRUE(low
->started());
364 EXPECT_FALSE(low2
->started());
365 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
366 EXPECT_FALSE(low2
->started());
368 EXPECT_TRUE(low2
->started());
371 TEST_F(ResourceSchedulerTest
, LowDoesNotBlockCriticalComplete
) {
372 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOW
));
373 scoped_ptr
<TestRequest
> lowest(NewRequest("http://host/lowest", net::LOWEST
));
374 scoped_ptr
<TestRequest
> lowest2(
375 NewRequest("http://host/lowest", net::LOWEST
));
376 EXPECT_TRUE(low
->started());
377 EXPECT_TRUE(lowest
->started());
378 EXPECT_FALSE(lowest2
->started());
379 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
380 EXPECT_TRUE(lowest2
->started());
383 TEST_F(ResourceSchedulerTest
, OneLowLoadsUntilBodyInsertedExceptSpdy
) {
384 http_server_properties_
.SetSupportsSpdy(
385 net::HostPortPair("spdyhost", 443), true);
386 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
387 scoped_ptr
<TestRequest
> low_spdy(
388 NewRequest("https://spdyhost/low", net::LOWEST
));
389 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
390 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
391 EXPECT_TRUE(high
->started());
392 EXPECT_TRUE(low_spdy
->started());
393 EXPECT_TRUE(low
->started());
394 EXPECT_FALSE(low2
->started());
395 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
397 EXPECT_TRUE(low2
->started());
400 TEST_F(ResourceSchedulerTest
, NavigationResetsState
) {
401 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
402 scheduler()->OnNavigate(kChildId
, kRouteId
);
403 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
404 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
405 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
406 EXPECT_TRUE(high
->started());
407 EXPECT_TRUE(low
->started());
408 EXPECT_FALSE(low2
->started());
411 TEST_F(ResourceSchedulerTest
, BackgroundRequestStartsImmediately
) {
412 const int route_id
= 0; // Indicates a background request.
413 scoped_ptr
<TestRequest
> request(NewRequestWithRoute("http://host/1",
414 net::LOWEST
, route_id
));
415 EXPECT_TRUE(request
->started());
418 TEST_F(ResourceSchedulerTest
, StartMultipleLowRequestsWhenIdle
) {
419 scoped_ptr
<TestRequest
> high1(NewRequest("http://host/high1", net::HIGHEST
));
420 scoped_ptr
<TestRequest
> high2(NewRequest("http://host/high2", net::HIGHEST
));
421 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
422 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
423 EXPECT_TRUE(high1
->started());
424 EXPECT_TRUE(high2
->started());
425 EXPECT_TRUE(low
->started());
426 EXPECT_FALSE(low2
->started());
428 EXPECT_FALSE(low2
->started());
430 EXPECT_TRUE(low2
->started());
433 TEST_F(ResourceSchedulerTest
, CancelOtherRequestsWhileResuming
) {
434 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
435 scoped_ptr
<TestRequest
> low1(NewRequest("http://host/low1", net::LOWEST
));
437 scoped_ptr
<net::URLRequest
> url_request(
438 NewURLRequest("http://host/low2", net::LOWEST
));
439 scoped_ptr
<ResourceThrottle
> throttle(
440 scheduler()->ScheduleRequest(kChildId
, kRouteId
, url_request
.get()));
441 scoped_ptr
<CancelingTestRequest
> low2(new CancelingTestRequest(
442 throttle
.Pass(), url_request
.Pass()));
445 scoped_ptr
<TestRequest
> low3(NewRequest("http://host/low3", net::LOWEST
));
446 low2
->set_request_to_cancel(low3
.Pass());
447 scoped_ptr
<TestRequest
> low4(NewRequest("http://host/low4", net::LOWEST
));
449 EXPECT_TRUE(high
->started());
450 EXPECT_FALSE(low2
->started());
452 EXPECT_TRUE(low1
->started());
453 EXPECT_TRUE(low2
->started());
454 EXPECT_TRUE(low4
->started());
457 TEST_F(ResourceSchedulerTest
, LimitedNumberOfDelayableRequestsInFlight
) {
458 // We only load low priority resources if there's a body.
459 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
461 // Throw in one high priority request to make sure that's not a factor.
462 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
463 EXPECT_TRUE(high
->started());
465 const int kMaxNumDelayableRequestsPerClient
= 10; // Should match the .cc.
466 const int kMaxNumDelayableRequestsPerHost
= 6;
467 ScopedVector
<TestRequest
> lows_singlehost
;
468 // Queue up to the per-host limit (we subtract the current high-pri request).
469 for (int i
= 0; i
< kMaxNumDelayableRequestsPerHost
- 1; ++i
) {
470 string url
= "http://host/low" + base::IntToString(i
);
471 lows_singlehost
.push_back(NewRequest(url
.c_str(), net::LOWEST
));
472 EXPECT_TRUE(lows_singlehost
[i
]->started());
475 scoped_ptr
<TestRequest
> second_last_singlehost(NewRequest("http://host/last",
477 scoped_ptr
<TestRequest
> last_singlehost(NewRequest("http://host/s_last",
480 EXPECT_FALSE(second_last_singlehost
->started());
482 EXPECT_TRUE(second_last_singlehost
->started());
483 EXPECT_FALSE(last_singlehost
->started());
484 lows_singlehost
.erase(lows_singlehost
.begin());
485 EXPECT_TRUE(last_singlehost
->started());
487 // Queue more requests from different hosts until we reach the total limit.
488 int expected_slots_left
=
489 kMaxNumDelayableRequestsPerClient
- kMaxNumDelayableRequestsPerHost
;
490 EXPECT_GT(expected_slots_left
, 0);
491 ScopedVector
<TestRequest
> lows_differenthosts
;
492 for (int i
= 0; i
< expected_slots_left
; ++i
) {
493 string url
= "http://host" + base::IntToString(i
) + "/low";
494 lows_differenthosts
.push_back(NewRequest(url
.c_str(), net::LOWEST
));
495 EXPECT_TRUE(lows_differenthosts
[i
]->started());
498 scoped_ptr
<TestRequest
> last_differenthost(NewRequest("http://host_new/last",
500 EXPECT_FALSE(last_differenthost
->started());
503 TEST_F(ResourceSchedulerTest
, RaisePriorityAndStart
) {
504 // Dummies to enforce scheduling.
505 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
506 scoped_ptr
<TestRequest
> low(NewRequest("http://host/req", net::LOWEST
));
508 scoped_ptr
<TestRequest
> request(NewRequest("http://host/req", net::LOWEST
));
509 EXPECT_FALSE(request
->started());
511 ChangeRequestPriority(request
.get(), net::HIGHEST
);
512 EXPECT_TRUE(request
->started());
515 TEST_F(ResourceSchedulerTest
, RaisePriorityInQueue
) {
516 // Dummies to enforce scheduling.
517 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
518 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
520 scoped_ptr
<TestRequest
> request(NewRequest("http://host/req", net::IDLE
));
521 scoped_ptr
<TestRequest
> idle(NewRequest("http://host/idle", net::IDLE
));
522 EXPECT_FALSE(request
->started());
523 EXPECT_FALSE(idle
->started());
525 ChangeRequestPriority(request
.get(), net::LOWEST
);
526 EXPECT_FALSE(request
->started());
527 EXPECT_FALSE(idle
->started());
529 const int kMaxNumDelayableRequestsPerClient
= 10; // Should match the .cc.
530 ScopedVector
<TestRequest
> lows
;
531 for (int i
= 0; i
< kMaxNumDelayableRequestsPerClient
- 1; ++i
) {
532 string url
= "http://host/low" + base::IntToString(i
);
533 lows
.push_back(NewRequest(url
.c_str(), net::LOWEST
));
536 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
539 EXPECT_TRUE(request
->started());
540 EXPECT_FALSE(idle
->started());
543 TEST_F(ResourceSchedulerTest
, LowerPriority
) {
544 // Dummies to enforce scheduling.
545 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
546 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
548 scoped_ptr
<TestRequest
> request(NewRequest("http://host/req", net::LOWEST
));
549 scoped_ptr
<TestRequest
> idle(NewRequest("http://host/idle", net::IDLE
));
550 EXPECT_FALSE(request
->started());
551 EXPECT_FALSE(idle
->started());
553 ChangeRequestPriority(request
.get(), net::IDLE
);
554 EXPECT_FALSE(request
->started());
555 EXPECT_FALSE(idle
->started());
557 const int kMaxNumDelayableRequestsPerClient
= 10; // Should match the .cc.
558 // 2 fewer filler requests: 1 for the "low" dummy at the start, and 1 for the
559 // one at the end, which will be tested.
560 const int kNumFillerRequests
= kMaxNumDelayableRequestsPerClient
- 2;
561 ScopedVector
<TestRequest
> lows
;
562 for (int i
= 0; i
< kNumFillerRequests
; ++i
) {
563 string url
= "http://host" + base::IntToString(i
) + "/low";
564 lows
.push_back(NewRequest(url
.c_str(), net::LOWEST
));
567 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
570 EXPECT_FALSE(request
->started());
571 EXPECT_TRUE(idle
->started());
574 TEST_F(ResourceSchedulerTest
, ReprioritizedRequestGoesToBackOfQueue
) {
575 // Dummies to enforce scheduling.
576 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
577 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
579 scoped_ptr
<TestRequest
> request(NewRequest("http://host/req", net::LOWEST
));
580 scoped_ptr
<TestRequest
> idle(NewRequest("http://host/idle", net::IDLE
));
581 EXPECT_FALSE(request
->started());
582 EXPECT_FALSE(idle
->started());
584 const int kMaxNumDelayableRequestsPerClient
= 10; // Should match the .cc.
585 ScopedVector
<TestRequest
> lows
;
586 for (int i
= 0; i
< kMaxNumDelayableRequestsPerClient
; ++i
) {
587 string url
= "http://host/low" + base::IntToString(i
);
588 lows
.push_back(NewRequest(url
.c_str(), net::LOWEST
));
591 ChangeRequestPriority(request
.get(), net::IDLE
);
592 EXPECT_FALSE(request
->started());
593 EXPECT_FALSE(idle
->started());
595 ChangeRequestPriority(request
.get(), net::LOWEST
);
596 EXPECT_FALSE(request
->started());
597 EXPECT_FALSE(idle
->started());
599 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
600 EXPECT_FALSE(request
->started());
601 EXPECT_FALSE(idle
->started());
604 TEST_F(ResourceSchedulerTest
, HigherIntraPriorityGoesToFrontOfQueue
) {
605 // Dummies to enforce scheduling.
606 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
607 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
609 const int kMaxNumDelayableRequestsPerClient
= 10; // Should match the .cc.
610 ScopedVector
<TestRequest
> lows
;
611 for (int i
= 0; i
< kMaxNumDelayableRequestsPerClient
; ++i
) {
612 string url
= "http://host/low" + base::IntToString(i
);
613 lows
.push_back(NewRequest(url
.c_str(), net::IDLE
));
616 scoped_ptr
<TestRequest
> request(NewRequest("http://host/req", net::IDLE
));
617 EXPECT_FALSE(request
->started());
619 ChangeRequestPriority(request
.get(), net::IDLE
, 1);
620 EXPECT_FALSE(request
->started());
622 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
624 EXPECT_TRUE(request
->started());
627 TEST_F(ResourceSchedulerTest
, NonHTTPSchedulesImmediately
) {
628 // Dummies to enforce scheduling.
629 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
630 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
632 scoped_ptr
<TestRequest
> request(
633 NewRequest("chrome-extension://req", net::LOWEST
));
634 EXPECT_TRUE(request
->started());
637 TEST_F(ResourceSchedulerTest
, ActiveLoadingSyncSchedulesImmediately
) {
638 // TODO(aiolos): remove when throttling and coalescing have both landed
639 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
640 false /* should_coalesce */);
641 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
642 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
643 // Dummies to enforce scheduling.
644 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
645 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
647 scoped_ptr
<TestRequest
> request(
648 NewSyncRequest("http://host/req", net::LOWEST
));
649 EXPECT_TRUE(request
->started());
652 TEST_F(ResourceSchedulerTest
, UnthrottledSyncSchedulesImmediately
) {
653 // TODO(aiolos): remove when throttling and coalescing have both landed
654 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
655 false /* should_coalesce */);
656 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
657 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
658 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
659 kBackgroundRouteId
));
660 // Dummies to enforce scheduling.
661 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
662 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
664 scoped_ptr
<TestRequest
> request(
665 NewBackgroundSyncRequest("http://host/req", net::LOWEST
));
666 EXPECT_TRUE(request
->started());
669 TEST_F(ResourceSchedulerTest
, SpdyProxySchedulesImmediately
) {
670 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
671 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
673 scoped_ptr
<TestRequest
> request(NewRequest("http://host/req", net::IDLE
));
674 EXPECT_FALSE(request
->started());
676 scheduler()->OnReceivedSpdyProxiedHttpResponse(kChildId
, kRouteId
);
677 EXPECT_TRUE(request
->started());
679 scoped_ptr
<TestRequest
> after(NewRequest("http://host/after", net::IDLE
));
680 EXPECT_TRUE(after
->started());
683 TEST_F(ResourceSchedulerTest
, NewSpdyHostInDelayableRequests
) {
684 scheduler()->OnWillInsertBody(kChildId
, kRouteId
);
685 const int kMaxNumDelayableRequestsPerClient
= 10; // Should match the .cc.
687 scoped_ptr
<TestRequest
> low1_spdy(
688 NewRequest("http://spdyhost1:8080/low", net::LOWEST
));
689 // Cancel a request after we learn the server supports SPDY.
690 ScopedVector
<TestRequest
> lows
;
691 for (int i
= 0; i
< kMaxNumDelayableRequestsPerClient
- 1; ++i
) {
692 string url
= "http://host" + base::IntToString(i
) + "/low";
693 lows
.push_back(NewRequest(url
.c_str(), net::LOWEST
));
695 scoped_ptr
<TestRequest
> low1(NewRequest("http://host/low", net::LOWEST
));
696 EXPECT_FALSE(low1
->started());
697 http_server_properties_
.SetSupportsSpdy(
698 net::HostPortPair("spdyhost1", 8080), true);
700 EXPECT_TRUE(low1
->started());
703 scoped_ptr
<TestRequest
> low2_spdy(
704 NewRequest("http://spdyhost2:8080/low", net::IDLE
));
705 // Reprioritize a request after we learn the server supports SPDY.
706 EXPECT_TRUE(low2_spdy
->started());
707 http_server_properties_
.SetSupportsSpdy(
708 net::HostPortPair("spdyhost2", 8080), true);
709 ChangeRequestPriority(low2_spdy
.get(), net::LOWEST
);
710 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
711 EXPECT_TRUE(low2
->started());
714 TEST_F(ResourceSchedulerTest
, ThrottledClientCreation
) {
715 // TODO(aiolos): remove when throttling and coalescing have both landed
716 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
717 false /* should_coalesce */);
718 EXPECT_TRUE(scheduler()->should_throttle());
719 scheduler()->OnClientCreated(
720 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
722 EXPECT_EQ(ResourceScheduler::THROTTLED
,
723 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
724 kBackgroundRouteId2
));
725 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
728 TEST_F(ResourceSchedulerTest
, ActiveClientThrottleUpdateOnLoadingChange
) {
729 // TODO(aiolos): remove when throttling and coalescing have both landed
730 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
731 false /* should_coalesce */);
732 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
733 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
734 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
735 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
736 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
737 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, false);
738 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
739 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
742 TEST_F(ResourceSchedulerTest
, CoalesceBackgroundClientOnLoadCompletion
) {
743 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
744 true /* should_coalesce */);
745 EXPECT_EQ(ResourceScheduler::THROTTLED
,
746 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
747 kBackgroundRouteId
));
748 scheduler()->OnLoadingStateChanged(
749 kBackgroundChildId
, kBackgroundRouteId
, true);
750 EXPECT_EQ(ResourceScheduler::THROTTLED
,
751 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
752 kBackgroundRouteId
));
753 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
754 EXPECT_EQ(ResourceScheduler::COALESCED
,
755 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
756 kBackgroundRouteId
));
759 TEST_F(ResourceSchedulerTest
, UnthrottleBackgroundClientOnLoadingStarted
) {
760 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
761 true /* should_coalesce */);
762 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
763 scheduler()->OnLoadingStateChanged(
764 kBackgroundChildId
, kBackgroundRouteId
, true);
765 EXPECT_EQ(ResourceScheduler::COALESCED
,
766 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
767 kBackgroundRouteId
));
769 scheduler()->OnLoadingStateChanged(
770 kBackgroundChildId
, kBackgroundRouteId
, false);
771 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
772 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
773 kBackgroundRouteId
));
776 TEST_F(ResourceSchedulerTest
, OneRequestPerThrottledClient
) {
777 // TODO(aiolos): remove when throttling and coalescing have both landed
778 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
779 false /* should_coalesce */);
780 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
781 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
782 EXPECT_EQ(ResourceScheduler::THROTTLED
,
783 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
784 kBackgroundRouteId
));
785 scoped_ptr
<TestRequest
> high(
786 NewBackgroundRequest("http://host/high", net::HIGHEST
));
787 scoped_ptr
<TestRequest
> request(
788 NewBackgroundRequest("http://host/req", net::IDLE
));
790 EXPECT_TRUE(high
->started());
791 EXPECT_FALSE(request
->started());
794 TEST_F(ResourceSchedulerTest
, UnthrottleNewlyVisibleClient
) {
795 // TODO(aiolos): remove when throttling and coalescing have both landed
796 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
797 false /* should_coalesce */);
798 EXPECT_EQ(ResourceScheduler::THROTTLED
,
799 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
800 kBackgroundRouteId
));
801 scoped_ptr
<TestRequest
> high(
802 NewBackgroundRequest("http://host/high", net::HIGHEST
));
803 scoped_ptr
<TestRequest
> request(
804 NewBackgroundRequest("http://host/req", net::IDLE
));
805 EXPECT_FALSE(request
->started());
807 scheduler()->OnVisibilityChanged(
808 kBackgroundChildId
, kBackgroundRouteId
, true);
809 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
810 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
811 kBackgroundRouteId
));
812 EXPECT_TRUE(request
->started());
815 TEST_F(ResourceSchedulerTest
, UnthrottleNewlyAudibleClient
) {
816 // TODO(aiolos): remove when throttling and coalescing have both landed
817 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
818 false /* should_coalesce */);
819 EXPECT_EQ(ResourceScheduler::THROTTLED
,
820 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
821 kBackgroundRouteId
));
822 scoped_ptr
<TestRequest
> high(
823 NewBackgroundRequest("http://host/high", net::HIGHEST
));
824 scoped_ptr
<TestRequest
> request(
825 NewBackgroundRequest("http://host/req", net::IDLE
));
826 EXPECT_FALSE(request
->started());
828 scheduler()->OnAudibilityChanged(
829 kBackgroundChildId
, kBackgroundRouteId
, true);
830 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
831 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
832 kBackgroundRouteId
));
833 EXPECT_TRUE(request
->started());
836 TEST_F(ResourceSchedulerTest
, VisibleClientStillUnthrottledOnAudabilityChange
) {
837 // TODO(aiolos): remove when throttling and coalescing have both landed
838 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
839 false /* should_coalesce */);
840 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
841 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
842 EXPECT_EQ(ResourceScheduler::THROTTLED
,
843 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
844 kBackgroundRouteId
));
846 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, true);
847 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
848 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
849 EXPECT_EQ(ResourceScheduler::THROTTLED
,
850 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
851 kBackgroundRouteId
));
853 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, false);
854 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
855 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
856 EXPECT_EQ(ResourceScheduler::THROTTLED
,
857 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
858 kBackgroundRouteId
));
861 TEST_F(ResourceSchedulerTest
, AudibleClientStillUnthrottledOnVisabilityChange
) {
862 // TODO(aiolos): remove when throttling and coalescing have both landed
863 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
864 false /* should_coalesce */);
865 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
866 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, true);
867 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
868 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
869 EXPECT_EQ(ResourceScheduler::THROTTLED
,
870 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
871 kBackgroundRouteId
));
873 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, true);
874 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
875 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
876 EXPECT_EQ(ResourceScheduler::THROTTLED
,
877 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
878 kBackgroundRouteId
));
880 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
881 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
882 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
883 EXPECT_EQ(ResourceScheduler::THROTTLED
,
884 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
885 kBackgroundRouteId
));
888 TEST_F(ResourceSchedulerTest
, ThrottledClientStartsNextHighestPriorityRequest
) {
889 // TODO(aiolos): remove when throttling and coalescing have both landed
890 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
891 false /* should_coalesce */);
892 scoped_ptr
<TestRequest
> request(
893 NewBackgroundRequest("http://host/req", net::IDLE
));
894 // Lower priority request started first to test request prioritizaton.
895 scoped_ptr
<TestRequest
> low(
896 NewBackgroundRequest("http://host/high", net::IDLE
));
897 scoped_ptr
<TestRequest
> high(
898 NewBackgroundRequest("http://host/high", net::HIGHEST
));
900 EXPECT_FALSE(low
->started());
901 EXPECT_FALSE(high
->started());
903 // request->CancelRequest();
905 EXPECT_TRUE(high
->started());
906 EXPECT_FALSE(low
->started());
909 TEST_F(ResourceSchedulerTest
, ThrottledSpdyProxySchedulesImmediately
) {
910 // TODO(aiolos): remove when throttling and coalescing have both landed
911 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
912 false /* should_coalesce */);
913 EXPECT_EQ(ResourceScheduler::THROTTLED
,
914 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
915 kBackgroundRouteId
));
916 scoped_ptr
<TestRequest
> high(
917 NewBackgroundRequest("http://host/high", net::HIGHEST
));
918 scoped_ptr
<TestRequest
> request(
919 NewBackgroundRequest("http://host/req", net::IDLE
));
921 EXPECT_FALSE(request
->started());
923 scheduler()->OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId
,
925 EXPECT_TRUE(request
->started());
927 scoped_ptr
<TestRequest
> after(
928 NewBackgroundRequest("http://host/after", net::IDLE
));
929 EXPECT_TRUE(after
->started());
932 TEST_F(ResourceSchedulerTest
, CoalescedClientIssuesNoRequests
) {
933 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
934 true /* should_coalesce */);
935 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
936 scheduler()->OnLoadingStateChanged(
937 kBackgroundChildId
, kBackgroundRouteId
, true);
938 EXPECT_EQ(ResourceScheduler::COALESCED
,
939 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
940 kBackgroundRouteId
));
941 scoped_ptr
<TestRequest
> high(
942 NewBackgroundRequest("http://host/high", net::HIGHEST
));
943 scoped_ptr
<TestRequest
> request(
944 NewBackgroundRequest("http://host/req", net::IDLE
));
946 EXPECT_FALSE(high
->started());
947 EXPECT_FALSE(request
->started());
949 scheduler()->OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId
,
951 EXPECT_FALSE(high
->started());
953 scoped_ptr
<TestRequest
> after(
954 NewBackgroundRequest("http://host/after", net::HIGHEST
));
955 EXPECT_FALSE(after
->started());
958 TEST_F(ResourceSchedulerTest
, CoalescedSpdyProxyWaits
) {
959 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
960 true /* should_coalesce */);
961 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
962 scheduler()->OnLoadingStateChanged(
963 kBackgroundChildId
, kBackgroundRouteId
, true);
964 EXPECT_EQ(ResourceScheduler::COALESCED
,
965 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
966 kBackgroundRouteId
));
967 scoped_ptr
<TestRequest
> high(
968 NewBackgroundRequest("http://host/high", net::HIGHEST
));
969 scoped_ptr
<TestRequest
> request(
970 NewBackgroundRequest("http://host/req", net::IDLE
));
972 EXPECT_FALSE(request
->started());
974 scheduler()->OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId
,
976 EXPECT_FALSE(request
->started());
978 scoped_ptr
<TestRequest
> after(
979 NewBackgroundRequest("http://host/after", net::IDLE
));
980 EXPECT_FALSE(after
->started());
983 TEST_F(ResourceSchedulerTest
, ThrottledNonHTTPSchedulesImmediately
) {
984 // TODO(aiolos): remove when throttling and coalescing have both landed
985 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
986 false /* should_coalesce */);
987 // Dummies to enforce scheduling.
988 scoped_ptr
<TestRequest
> high(
989 NewBackgroundRequest("http://host/high", net::HIGHEST
));
990 scoped_ptr
<TestRequest
> low(
991 NewBackgroundRequest("http://host/low", net::LOWEST
));
993 scoped_ptr
<TestRequest
> request(
994 NewBackgroundRequest("chrome-extension://req", net::LOWEST
));
995 EXPECT_TRUE(request
->started());
996 EXPECT_FALSE(low
->started());
999 TEST_F(ResourceSchedulerTest
, CoalescedNonHTTPSchedulesImmediately
) {
1000 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1001 true /* should_coalesce */);
1002 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1003 scheduler()->OnLoadingStateChanged(
1004 kBackgroundChildId
, kBackgroundRouteId
, true);
1005 EXPECT_EQ(ResourceScheduler::COALESCED
,
1006 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1007 kBackgroundRouteId
));
1008 // Dummies to enforce scheduling.
1009 scoped_ptr
<TestRequest
> high(
1010 NewBackgroundRequest("http://host/high", net::HIGHEST
));
1011 scoped_ptr
<TestRequest
> low(
1012 NewBackgroundRequest("http://host/low", net::LOWEST
));
1014 scoped_ptr
<TestRequest
> request(
1015 NewBackgroundRequest("chrome-extension://req", net::LOWEST
));
1016 EXPECT_TRUE(request
->started());
1017 EXPECT_FALSE(low
->started());
1020 TEST_F(ResourceSchedulerTest
, ThrottledSyncSchedulesImmediately
) {
1021 // TODO(aiolos): remove when throttling and coalescing have both landed
1022 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1023 false /* should_coalesce */);
1024 // Dummies to enforce scheduling.
1025 scoped_ptr
<TestRequest
> high(
1026 NewBackgroundRequest("http://host/high", net::HIGHEST
));
1027 scoped_ptr
<TestRequest
> low(
1028 NewBackgroundRequest("http://host/low", net::LOWEST
));
1030 scoped_ptr
<TestRequest
> request(
1031 NewBackgroundSyncRequest("http://host/req", net::LOWEST
));
1032 EXPECT_TRUE(request
->started());
1033 EXPECT_FALSE(low
->started());
1036 TEST_F(ResourceSchedulerTest
, CoalescedSyncSchedulesImmediately
) {
1037 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1038 true /* should_coalesce */);
1039 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1040 scheduler()->OnLoadingStateChanged(
1041 kBackgroundChildId
, kBackgroundRouteId
, true);
1042 EXPECT_EQ(ResourceScheduler::COALESCED
,
1043 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1044 kBackgroundRouteId
));
1045 // Dummies to enforce scheduling.
1046 scoped_ptr
<TestRequest
> high(
1047 NewBackgroundRequest("http://host/high", net::HIGHEST
));
1048 scoped_ptr
<TestRequest
> low(
1049 NewBackgroundRequest("http://host/low", net::LOWEST
));
1051 scoped_ptr
<TestRequest
> request(
1052 NewBackgroundSyncRequest("http://host/req", net::LOWEST
));
1053 EXPECT_TRUE(request
->started());
1054 EXPECT_FALSE(low
->started());
1055 EXPECT_FALSE(high
->started());
1058 TEST_F(ResourceSchedulerTest
, AllBackgroundClientsUnthrottle
) {
1059 // TODO(aiolos): remove when throttling and coalescing have both landed
1060 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1061 false /* should_coalesce */);
1062 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1063 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1064 kBackgroundRouteId
));
1065 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1066 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1067 EXPECT_FALSE(scheduler()->active_clients_loaded());
1069 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1070 EXPECT_TRUE(scheduler()->active_clients_loaded());
1071 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1072 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1073 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1074 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1075 kBackgroundRouteId
));
1077 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1078 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1079 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1080 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1081 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1082 kBackgroundRouteId
));
1084 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, false);
1085 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1086 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1087 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1088 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1089 kBackgroundRouteId
));
1091 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1092 scheduler()->OnLoadingStateChanged(
1093 kBackgroundChildId
, kBackgroundRouteId
, true);
1094 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1095 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1096 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1097 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1098 kBackgroundRouteId
));
1101 TEST_F(ResourceSchedulerTest
,
1102 UnloadedClientVisibilityChangedCorrectlyUnthrottles
) {
1103 // TODO(aiolos): remove when throttling and coalescing have both landed
1104 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1105 false /* should_coalesce */);
1106 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, false, false);
1107 scheduler()->OnClientCreated(
1108 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1109 scheduler()->OnLoadingStateChanged(kChildId2
, kRouteId2
, true);
1110 scheduler()->OnLoadingStateChanged(
1111 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1113 // 1 visible, 3 hidden
1114 EXPECT_FALSE(scheduler()->active_clients_loaded());
1115 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1116 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1117 kBackgroundRouteId
));
1118 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1119 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1120 kBackgroundRouteId2
));
1121 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1122 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1123 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1124 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1126 // 2 visible, 2 hidden
1127 scheduler()->OnVisibilityChanged(kChildId2
, kRouteId2
, true);
1128 EXPECT_FALSE(scheduler()->active_clients_loaded());
1129 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1130 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1131 kBackgroundRouteId
));
1132 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1133 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1134 kBackgroundRouteId2
));
1135 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1136 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1137 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1138 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1140 // 1 visible, 3 hidden
1141 scheduler()->OnVisibilityChanged(kChildId2
, kRouteId2
, false);
1142 EXPECT_FALSE(scheduler()->active_clients_loaded());
1143 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1144 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1145 kBackgroundRouteId
));
1146 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1147 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1148 kBackgroundRouteId2
));
1149 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1150 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1151 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1152 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1154 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1155 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1158 TEST_F(ResourceSchedulerTest
,
1159 UnloadedClientAudibilityChangedCorrectlyUnthrottles
) {
1160 // TODO(aiolos): remove when throttling and coalescing have both landed
1161 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1162 false /* should_coalesce */);
1163 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, false, false);
1164 scheduler()->OnClientCreated(
1165 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1166 scheduler()->OnLoadingStateChanged(
1167 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1168 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1169 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, true);
1171 // 1 audible, 3 hidden
1172 EXPECT_FALSE(scheduler()->active_clients_loaded());
1173 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1174 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1175 kBackgroundRouteId
));
1176 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1177 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1178 kBackgroundRouteId2
));
1179 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1180 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1181 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1182 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1184 // 2 audible, 2 hidden
1185 scheduler()->OnAudibilityChanged(kChildId2
, kRouteId2
, true);
1186 EXPECT_FALSE(scheduler()->active_clients_loaded());
1187 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1188 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1189 kBackgroundRouteId
));
1190 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1191 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1192 kBackgroundRouteId2
));
1193 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1194 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1195 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1196 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1198 // 1 audible, 3 hidden
1199 scheduler()->OnAudibilityChanged(kChildId2
, kRouteId2
, false);
1200 EXPECT_FALSE(scheduler()->active_clients_loaded());
1201 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1202 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1203 kBackgroundRouteId
));
1204 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1205 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1206 kBackgroundRouteId2
));
1207 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1208 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1209 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1210 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1212 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1213 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1216 TEST_F(ResourceSchedulerTest
,
1217 LoadedClientVisibilityChangedCorrectlyUnthrottles
) {
1218 // TODO(aiolos): remove when throttling and coalescing have both landed
1219 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1220 false /* should_coalesce */);
1221 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, false, false);
1222 scheduler()->OnClientCreated(
1223 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1224 scheduler()->OnLoadingStateChanged(kChildId2
, kRouteId2
, true);
1225 scheduler()->OnLoadingStateChanged(
1226 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1227 // 1 visible, 3 hidden
1228 EXPECT_FALSE(scheduler()->active_clients_loaded());
1229 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1230 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1231 kBackgroundRouteId
));
1232 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1233 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1234 kBackgroundRouteId2
));
1235 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1236 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1237 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1238 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1240 // 2 visible, 2 hidden
1241 scheduler()->OnVisibilityChanged(kChildId2
, kRouteId2
, true);
1242 EXPECT_FALSE(scheduler()->active_clients_loaded());
1243 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1244 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1245 kBackgroundRouteId
));
1246 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1247 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1248 kBackgroundRouteId2
));
1249 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1250 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1251 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1252 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1254 // 1 visible, 3 hidden
1255 scheduler()->OnVisibilityChanged(kChildId2
, kRouteId2
, false);
1256 EXPECT_FALSE(scheduler()->active_clients_loaded());
1257 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1258 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1259 kBackgroundRouteId
));
1260 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1261 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1262 kBackgroundRouteId2
));
1263 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1264 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1265 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1266 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1268 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1269 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1272 TEST_F(ResourceSchedulerTest
,
1273 LoadedClientAudibilityChangedCorrectlyUnthrottles
) {
1274 // TODO(aiolos): remove when throttling and coalescing have both landed
1275 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1276 false /* should_coalesce */);
1277 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, false, false);
1278 scheduler()->OnClientCreated(
1279 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1280 scheduler()->OnLoadingStateChanged(kChildId2
, kRouteId2
, true);
1281 scheduler()->OnLoadingStateChanged(
1282 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1283 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1284 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, true);
1285 // 1 audible, 3 hidden
1286 EXPECT_FALSE(scheduler()->active_clients_loaded());
1287 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1288 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1289 kBackgroundRouteId
));
1290 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1291 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1292 kBackgroundRouteId2
));
1293 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1294 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1295 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1296 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1298 // 2 audible, 2 hidden
1299 scheduler()->OnAudibilityChanged(kChildId2
, kRouteId2
, true);
1300 EXPECT_FALSE(scheduler()->active_clients_loaded());
1301 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1302 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1303 kBackgroundRouteId
));
1304 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1305 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1306 kBackgroundRouteId2
));
1307 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1308 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1309 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1310 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1312 // 1 audible, 3 hidden
1313 scheduler()->OnAudibilityChanged(kChildId2
, kRouteId2
, false);
1314 EXPECT_FALSE(scheduler()->active_clients_loaded());
1315 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1316 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1317 kBackgroundRouteId
));
1318 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1319 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1320 kBackgroundRouteId2
));
1321 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1322 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1323 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1324 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1326 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1327 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1330 TEST_F(ResourceSchedulerTest
, UnloadedClientBecomesHiddenCorrectlyUnthrottles
) {
1331 // TODO(aiolos): remove when throttling and coalescing have both landed
1332 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1333 false /* should_coalesce */);
1334 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, true, false);
1335 scheduler()->OnClientCreated(
1336 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1337 scheduler()->OnLoadingStateChanged(
1338 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1340 // 2 visible, 2 hidden
1341 EXPECT_FALSE(scheduler()->active_clients_loaded());
1342 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1343 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1344 kBackgroundRouteId
));
1345 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1346 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1347 kBackgroundRouteId2
));
1348 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1349 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1350 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1351 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1353 // 1 visible, 3 hidden
1354 scheduler()->OnVisibilityChanged(kChildId2
, kRouteId2
, false);
1355 EXPECT_FALSE(scheduler()->active_clients_loaded());
1356 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1357 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1358 kBackgroundRouteId
));
1359 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1360 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1361 kBackgroundRouteId2
));
1362 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1363 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1364 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1365 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1367 // 0 visible, 4 hidden
1368 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1369 EXPECT_TRUE(scheduler()->active_clients_loaded());
1370 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1371 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1372 kBackgroundRouteId
));
1373 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1374 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1375 kBackgroundRouteId2
));
1376 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1377 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1378 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1379 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1381 // 1 visible, 3 hidden
1382 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, true);
1383 EXPECT_FALSE(scheduler()->active_clients_loaded());
1384 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1385 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1386 kBackgroundRouteId
));
1387 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1388 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1389 kBackgroundRouteId2
));
1390 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1391 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1392 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1393 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1395 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1396 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1399 TEST_F(ResourceSchedulerTest
, UnloadedClientBecomesSilentCorrectlyUnthrottles
) {
1400 // TODO(aiolos): remove when throttling and coalescing have both landed
1401 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1402 false /* should_coalesce */);
1403 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, false, true);
1404 scheduler()->OnClientCreated(
1405 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1406 scheduler()->OnLoadingStateChanged(
1407 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1408 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, true);
1409 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1410 // 2 audible, 2 hidden
1411 EXPECT_FALSE(scheduler()->active_clients_loaded());
1412 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1413 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1414 kBackgroundRouteId
));
1415 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1416 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1417 kBackgroundRouteId2
));
1418 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1419 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1420 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1421 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1423 // 1 audible, 3 hidden
1424 scheduler()->OnAudibilityChanged(kChildId2
, kRouteId2
, false);
1425 EXPECT_FALSE(scheduler()->active_clients_loaded());
1426 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1427 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1428 kBackgroundRouteId
));
1429 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1430 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1431 kBackgroundRouteId2
));
1432 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1433 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1434 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1435 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1437 // 0 audible, 4 hidden
1438 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, false);
1439 EXPECT_TRUE(scheduler()->active_clients_loaded());
1440 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1441 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1442 kBackgroundRouteId
));
1443 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1444 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1445 kBackgroundRouteId2
));
1446 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1447 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1448 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1449 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1451 // 1 audible, 3 hidden
1452 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, true);
1453 EXPECT_FALSE(scheduler()->active_clients_loaded());
1454 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1455 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1456 kBackgroundRouteId
));
1457 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1458 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1459 kBackgroundRouteId2
));
1460 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1461 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1462 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1463 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1465 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1466 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1469 TEST_F(ResourceSchedulerTest
, LoadedClientBecomesHiddenCorrectlyThrottles
) {
1470 // TODO(aiolos): remove when throttling and coalescing have both landed
1471 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1472 false /* should_coalesce */);
1473 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, true, false);
1474 scheduler()->OnClientCreated(
1475 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1476 scheduler()->OnLoadingStateChanged(
1477 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1478 scheduler()->OnLoadingStateChanged(kChildId2
, kRouteId2
, true);
1479 // 2 visible, 2 hidden
1480 EXPECT_FALSE(scheduler()->active_clients_loaded());
1481 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1482 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1483 kBackgroundRouteId
));
1484 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1485 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1486 kBackgroundRouteId2
));
1487 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1488 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1489 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1490 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1492 // 1 visible, 3 hidden
1493 scheduler()->OnVisibilityChanged(kChildId2
, kRouteId2
, false);
1494 EXPECT_FALSE(scheduler()->active_clients_loaded());
1495 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1496 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1497 kBackgroundRouteId
));
1498 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1499 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1500 kBackgroundRouteId2
));
1501 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1502 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1503 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1504 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1506 // 0 visible, 4 hidden
1507 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1508 EXPECT_TRUE(scheduler()->active_clients_loaded());
1509 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1510 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1511 kBackgroundRouteId
));
1512 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1513 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1514 kBackgroundRouteId2
));
1515 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1516 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1517 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1518 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1520 // 1 visible, 3 hidden
1521 scheduler()->OnVisibilityChanged(kChildId2
, kRouteId2
, true);
1522 EXPECT_TRUE(scheduler()->active_clients_loaded());
1523 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1524 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1525 kBackgroundRouteId
));
1526 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1527 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1528 kBackgroundRouteId2
));
1529 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1530 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1531 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1532 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1534 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1535 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1538 TEST_F(ResourceSchedulerTest
, LoadedClientBecomesSilentCorrectlyThrottles
) {
1539 // TODO(aiolos): remove when throttling and coalescing have both landed
1540 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1541 false /* should_coalesce */);
1542 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, false, true);
1543 scheduler()->OnClientCreated(
1544 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1545 scheduler()->OnLoadingStateChanged(
1546 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1547 scheduler()->OnLoadingStateChanged(kChildId2
, kRouteId2
, true);
1548 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1549 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, true);
1550 // 2 audible, 2 hidden
1551 EXPECT_FALSE(scheduler()->active_clients_loaded());
1552 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1553 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1554 kBackgroundRouteId
));
1555 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1556 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1557 kBackgroundRouteId2
));
1558 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1559 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1560 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1561 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1563 // 1 audible, 3 hidden
1564 scheduler()->OnAudibilityChanged(kChildId2
, kRouteId2
, false);
1565 EXPECT_FALSE(scheduler()->active_clients_loaded());
1566 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1567 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1568 kBackgroundRouteId
));
1569 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1570 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1571 kBackgroundRouteId2
));
1572 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1573 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1574 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1575 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1577 // 0 audible, 4 hidden
1578 scheduler()->OnAudibilityChanged(kChildId
, kRouteId
, false);
1579 EXPECT_TRUE(scheduler()->active_clients_loaded());
1580 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1581 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1582 kBackgroundRouteId
));
1583 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1584 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1585 kBackgroundRouteId2
));
1586 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1587 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1588 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1589 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1591 // 1 audible, 3 hidden
1592 scheduler()->OnAudibilityChanged(kChildId2
, kRouteId2
, true);
1593 EXPECT_TRUE(scheduler()->active_clients_loaded());
1594 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1595 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1596 kBackgroundRouteId
));
1597 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1598 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1599 kBackgroundRouteId2
));
1600 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1601 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1602 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1603 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1605 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1606 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1609 TEST_F(ResourceSchedulerTest
, HiddenLoadedChangesCorrectlyStayThrottled
) {
1610 // TODO(aiolos): remove when throttling and coalescing have both landed
1611 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1612 false /* should_coalesce */);
1613 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, true, false);
1614 scheduler()->OnClientCreated(
1615 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1617 // 1 visible and 2 hidden loading, 1 visible loaded
1618 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1619 EXPECT_FALSE(scheduler()->active_clients_loaded());
1620 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1621 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1622 kBackgroundRouteId
));
1623 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1624 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1625 kBackgroundRouteId2
));
1626 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1627 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1628 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1629 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1631 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1632 scheduler()->OnLoadingStateChanged(
1633 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1634 EXPECT_FALSE(scheduler()->active_clients_loaded());
1635 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1636 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1637 kBackgroundRouteId
));
1638 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1639 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1640 kBackgroundRouteId2
));
1641 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1642 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1643 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1644 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1646 // 1 visible loading, 1 visible and 2 hidden loaded
1647 scheduler()->OnLoadingStateChanged(
1648 kBackgroundChildId
, kBackgroundRouteId
, true);
1649 EXPECT_FALSE(scheduler()->active_clients_loaded());
1650 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1651 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1652 kBackgroundRouteId
));
1653 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1654 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1655 kBackgroundRouteId2
));
1656 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1657 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1658 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1659 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1661 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1662 scheduler()->OnLoadingStateChanged(
1663 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1664 EXPECT_FALSE(scheduler()->active_clients_loaded());
1665 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1666 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1667 kBackgroundRouteId
));
1668 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1669 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1670 kBackgroundRouteId2
));
1671 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1672 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1673 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1674 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1676 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1677 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1680 TEST_F(ResourceSchedulerTest
, PartialVisibleClientLoadedDoesNotUnthrottle
) {
1681 // TODO(aiolos): remove when throttling and coalescing have both landed
1682 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1683 false /* should_coalesce */);
1684 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, true, false);
1685 scheduler()->OnClientCreated(
1686 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1688 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1689 scheduler()->OnLoadingStateChanged(
1690 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1691 EXPECT_FALSE(scheduler()->active_clients_loaded());
1692 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1693 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1694 kBackgroundRouteId
));
1695 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1696 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1697 kBackgroundRouteId2
));
1698 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1699 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1700 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1701 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1703 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1704 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1705 EXPECT_FALSE(scheduler()->active_clients_loaded());
1706 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1707 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1708 kBackgroundRouteId
));
1709 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1710 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1711 kBackgroundRouteId2
));
1712 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1713 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1714 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1715 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1717 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1718 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, false);
1719 EXPECT_FALSE(scheduler()->active_clients_loaded());
1720 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1721 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1722 kBackgroundRouteId
));
1723 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1724 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1725 kBackgroundRouteId2
));
1726 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1727 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1728 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1729 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1731 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1732 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1735 TEST_F(ResourceSchedulerTest
, FullVisibleLoadedCorrectlyUnthrottle
) {
1736 // TODO(aiolos): remove when throttling and coalescing have both landed
1737 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1738 false /* should_coalesce */);
1739 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, true, false);
1740 scheduler()->OnClientCreated(
1741 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1743 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1744 scheduler()->OnLoadingStateChanged(
1745 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1746 scheduler()->OnLoadingStateChanged(kChildId2
, kRouteId2
, true);
1747 EXPECT_FALSE(scheduler()->active_clients_loaded());
1748 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1749 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1750 kBackgroundRouteId
));
1751 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1752 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1753 kBackgroundRouteId2
));
1754 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1755 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1756 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1757 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1759 scoped_ptr
<TestRequest
> high(
1760 NewBackgroundRequest("http://host/high", net::HIGHEST
));
1761 scoped_ptr
<TestRequest
> low(
1762 NewBackgroundRequest("http://host/low", net::LOWEST
));
1764 EXPECT_TRUE(high
->started());
1765 EXPECT_FALSE(low
->started());
1767 // 2 visible loaded, 1 hidden loading, 1 hidden loaded
1768 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1769 EXPECT_TRUE(scheduler()->active_clients_loaded());
1770 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1771 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1772 kBackgroundRouteId
));
1773 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1774 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1775 kBackgroundRouteId2
));
1776 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1777 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1778 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1779 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1780 // kBackgroundClientId unthrottling should unthrottle it's request.
1781 EXPECT_TRUE(low
->started());
1783 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1784 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, false);
1785 EXPECT_FALSE(scheduler()->active_clients_loaded());
1786 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1787 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1788 kBackgroundRouteId
));
1789 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1790 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1791 kBackgroundRouteId2
));
1792 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1793 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1794 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1795 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1797 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1798 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1801 TEST_F(ResourceSchedulerTest
,
1802 ActiveAndLoadingClientDeletedCorrectlyUnthrottle
) {
1803 // TODO(aiolos): remove when throttling and coalescing have both landed
1804 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1805 false /* should_coalesce */);
1806 scheduler()->OnClientCreated(kChildId2
, kRouteId2
, true, false);
1807 scheduler()->OnClientCreated(
1808 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1810 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1811 scheduler()->OnLoadingStateChanged(
1812 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1813 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1814 EXPECT_FALSE(scheduler()->active_clients_loaded());
1815 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1816 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1817 kBackgroundRouteId
));
1818 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1819 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1820 kBackgroundRouteId2
));
1821 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1822 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1823 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1824 scheduler()->GetClientStateForTesting(kChildId2
, kRouteId2
));
1826 // 1 visible loaded, 1 hidden loading, 1 hidden loaded
1827 scheduler()->OnClientDeleted(kChildId2
, kRouteId2
);
1828 EXPECT_TRUE(scheduler()->active_clients_loaded());
1829 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1830 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1831 kBackgroundRouteId
));
1832 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1833 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1834 kBackgroundRouteId2
));
1835 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1836 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1838 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1839 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, false);
1840 EXPECT_FALSE(scheduler()->active_clients_loaded());
1841 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1842 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1843 kBackgroundRouteId
));
1844 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1845 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1846 kBackgroundRouteId2
));
1847 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1848 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1850 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1853 TEST_F(ResourceSchedulerTest
, CoalescedClientCreationStartsTimer
) {
1854 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1855 true /* should_coalesce */);
1856 EXPECT_FALSE(mock_timer_
->IsRunning());
1857 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1858 EXPECT_FALSE(mock_timer_
->IsRunning());
1859 scheduler()->OnLoadingStateChanged(
1860 kBackgroundChildId
, kBackgroundRouteId
, true);
1861 EXPECT_EQ(ResourceScheduler::COALESCED
,
1862 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1863 kBackgroundRouteId
));
1864 EXPECT_TRUE(mock_timer_
->IsRunning());
1867 TEST_F(ResourceSchedulerTest
, ActiveLoadingClientLoadedAndHiddenStartsTimer
) {
1868 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1869 true /* should_coalesce */);
1870 EXPECT_FALSE(mock_timer_
->IsRunning());
1871 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1872 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1873 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1874 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1875 kBackgroundRouteId
));
1876 EXPECT_FALSE(mock_timer_
->IsRunning());
1878 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1879 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1880 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1881 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1882 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1883 kBackgroundRouteId
));
1884 EXPECT_FALSE(mock_timer_
->IsRunning());
1886 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1887 EXPECT_EQ(ResourceScheduler::COALESCED
,
1888 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1889 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1890 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1891 kBackgroundRouteId
));
1892 EXPECT_TRUE(mock_timer_
->IsRunning());
1895 TEST_F(ResourceSchedulerTest
, ActiveLoadingClientHiddenAndLoadedStartsTimer
) {
1896 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1897 true /* should_coalesce */);
1898 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
1899 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1900 EXPECT_EQ(ResourceScheduler::THROTTLED
,
1901 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1902 kBackgroundRouteId
));
1903 EXPECT_FALSE(mock_timer_
->IsRunning());
1905 scheduler()->OnVisibilityChanged(kChildId
, kRouteId
, false);
1906 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1907 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1908 EXPECT_FALSE(mock_timer_
->IsRunning());
1910 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1911 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1912 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1913 kBackgroundRouteId
));
1914 EXPECT_EQ(ResourceScheduler::COALESCED
,
1915 scheduler()->GetClientStateForTesting(kChildId
, kRouteId
));
1916 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1917 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1918 kBackgroundRouteId
));
1919 EXPECT_TRUE(mock_timer_
->IsRunning());
1922 TEST_F(ResourceSchedulerTest
, CoalescedClientBecomesAudibleStopsTimer
) {
1923 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1924 true /* should_coalesce */);
1925 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1926 EXPECT_FALSE(mock_timer_
->IsRunning());
1927 scheduler()->OnLoadingStateChanged(
1928 kBackgroundChildId
, kBackgroundRouteId
, true);
1929 EXPECT_EQ(ResourceScheduler::COALESCED
,
1930 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1931 kBackgroundRouteId
));
1932 EXPECT_TRUE(mock_timer_
->IsRunning());
1934 scheduler()->OnAudibilityChanged(
1935 kBackgroundChildId
, kBackgroundRouteId
, true);
1936 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
1937 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1938 kBackgroundRouteId
));
1939 EXPECT_FALSE(mock_timer_
->IsRunning());
1942 TEST_F(ResourceSchedulerTest
, LastCoalescedClientDeletionStopsTimer
) {
1943 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1944 true /* should_coalesce */);
1945 scheduler()->OnClientCreated(
1946 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1947 EXPECT_FALSE(mock_timer_
->IsRunning());
1948 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1949 EXPECT_FALSE(mock_timer_
->IsRunning());
1950 scheduler()->OnLoadingStateChanged(
1951 kBackgroundChildId
, kBackgroundRouteId
, true);
1952 EXPECT_EQ(ResourceScheduler::COALESCED
,
1953 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1954 kBackgroundRouteId
));
1955 scheduler()->OnLoadingStateChanged(
1956 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1957 EXPECT_EQ(ResourceScheduler::COALESCED
,
1958 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1959 kBackgroundRouteId2
));
1960 EXPECT_TRUE(mock_timer_
->IsRunning());
1962 scheduler()->OnClientDeleted(kBackgroundChildId
, kBackgroundRouteId
);
1963 EXPECT_TRUE(mock_timer_
->IsRunning());
1965 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
1966 EXPECT_FALSE(mock_timer_
->IsRunning());
1968 // To avoid errors on test tear down.
1969 scheduler()->OnClientCreated(
1970 kBackgroundChildId
, kBackgroundRouteId
, false, false);
1973 TEST_F(ResourceSchedulerTest
, LastCoalescedClientStartsLoadingStopsTimer
) {
1974 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1975 true /* should_coalesce */);
1976 scheduler()->OnClientCreated(
1977 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
1978 EXPECT_FALSE(mock_timer_
->IsRunning());
1979 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
1980 EXPECT_FALSE(mock_timer_
->IsRunning());
1981 scheduler()->OnLoadingStateChanged(
1982 kBackgroundChildId
, kBackgroundRouteId
, true);
1983 EXPECT_EQ(ResourceScheduler::COALESCED
,
1984 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
1985 kBackgroundRouteId
));
1986 scheduler()->OnLoadingStateChanged(
1987 kBackgroundChildId2
, kBackgroundRouteId2
, true);
1988 EXPECT_EQ(ResourceScheduler::COALESCED
,
1989 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
1990 kBackgroundRouteId2
));
1991 EXPECT_TRUE(mock_timer_
->IsRunning());
1993 scheduler()->OnLoadingStateChanged(
1994 kBackgroundChildId
, kBackgroundRouteId
, false);
1995 EXPECT_TRUE(mock_timer_
->IsRunning());
1997 scheduler()->OnLoadingStateChanged(
1998 kBackgroundChildId2
, kBackgroundRouteId2
, false);
1999 EXPECT_FALSE(mock_timer_
->IsRunning());
2001 // This is needed to avoid errors on test tear down.
2002 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
2005 TEST_F(ResourceSchedulerTest
, LastCoalescedClientBecomesVisibleStopsTimer
) {
2006 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2007 true /* should_coalesce */);
2008 scheduler()->OnClientCreated(
2009 kBackgroundChildId2
, kBackgroundRouteId2
, false, false);
2010 EXPECT_FALSE(mock_timer_
->IsRunning());
2011 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
2012 EXPECT_FALSE(mock_timer_
->IsRunning());
2013 scheduler()->OnLoadingStateChanged(
2014 kBackgroundChildId
, kBackgroundRouteId
, true);
2015 EXPECT_EQ(ResourceScheduler::COALESCED
,
2016 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
2017 kBackgroundRouteId
));
2018 scheduler()->OnLoadingStateChanged(
2019 kBackgroundChildId2
, kBackgroundRouteId2
, true);
2020 EXPECT_EQ(ResourceScheduler::COALESCED
,
2021 scheduler()->GetClientStateForTesting(kBackgroundChildId2
,
2022 kBackgroundRouteId2
));
2023 EXPECT_TRUE(mock_timer_
->IsRunning());
2025 scheduler()->OnVisibilityChanged(
2026 kBackgroundChildId
, kBackgroundRouteId
, true);
2027 EXPECT_TRUE(mock_timer_
->IsRunning());
2029 scheduler()->OnVisibilityChanged(
2030 kBackgroundChildId2
, kBackgroundRouteId2
, true);
2031 EXPECT_FALSE(mock_timer_
->IsRunning());
2033 // To avoid errors on test tear down.
2034 scheduler()->OnClientDeleted(kBackgroundChildId2
, kBackgroundRouteId2
);
2037 TEST_F(ResourceSchedulerTest
,
2038 CoalescedClientBecomesLoadingAndVisibleStopsTimer
) {
2039 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2040 true /* should_coalesce */);
2041 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
2042 EXPECT_FALSE(mock_timer_
->IsRunning());
2043 scheduler()->OnLoadingStateChanged(
2044 kBackgroundChildId
, kBackgroundRouteId
, true);
2045 EXPECT_EQ(ResourceScheduler::COALESCED
,
2046 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
2047 kBackgroundRouteId
));
2048 EXPECT_TRUE(mock_timer_
->IsRunning());
2050 scheduler()->OnLoadingStateChanged(
2051 kBackgroundChildId
, kBackgroundRouteId
, false);
2052 EXPECT_EQ(ResourceScheduler::UNTHROTTLED
,
2053 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
2054 kBackgroundRouteId
));
2055 EXPECT_FALSE(mock_timer_
->IsRunning());
2057 scheduler()->OnVisibilityChanged(
2058 kBackgroundChildId
, kBackgroundRouteId
, true);
2059 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING
,
2060 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
2061 kBackgroundRouteId
));
2062 EXPECT_FALSE(mock_timer_
->IsRunning());
2065 TEST_F(ResourceSchedulerTest
, CoalescedRequestsIssueOnTimer
) {
2066 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2067 true /* should_coalesce */);
2068 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
2069 scheduler()->OnLoadingStateChanged(
2070 kBackgroundChildId
, kBackgroundRouteId
, true);
2071 EXPECT_EQ(ResourceScheduler::COALESCED
,
2072 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
2073 kBackgroundRouteId
));
2074 EXPECT_TRUE(scheduler()->active_clients_loaded());
2076 scoped_ptr
<TestRequest
> high(
2077 NewBackgroundRequest("http://host/high", net::HIGHEST
));
2078 scoped_ptr
<TestRequest
> low(
2079 NewBackgroundRequest("http://host/low", net::LOWEST
));
2080 EXPECT_FALSE(high
->started());
2081 EXPECT_FALSE(low
->started());
2083 FireCoalescingTimer();
2085 EXPECT_TRUE(high
->started());
2086 EXPECT_TRUE(low
->started());
2089 TEST_F(ResourceSchedulerTest
, CoalescedRequestsUnthrottleCorrectlyOnTimer
) {
2090 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2091 true /* should_coalesce */);
2092 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
2093 scheduler()->OnLoadingStateChanged(
2094 kBackgroundChildId
, kBackgroundRouteId
, true);
2095 EXPECT_EQ(ResourceScheduler::COALESCED
,
2096 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
2097 kBackgroundRouteId
));
2098 EXPECT_TRUE(scheduler()->active_clients_loaded());
2100 scoped_ptr
<TestRequest
> high(
2101 NewBackgroundRequest("http://host/high", net::HIGHEST
));
2102 scoped_ptr
<TestRequest
> high2(
2103 NewBackgroundRequest("http://host/high", net::HIGHEST
));
2104 scoped_ptr
<TestRequest
> high3(
2105 NewBackgroundRequest("http://host/high", net::HIGHEST
));
2106 scoped_ptr
<TestRequest
> high4(
2107 NewBackgroundRequest("http://host/high", net::HIGHEST
));
2108 scoped_ptr
<TestRequest
> low(
2109 NewBackgroundRequest("http://host/low", net::LOWEST
));
2110 scoped_ptr
<TestRequest
> low2(
2111 NewBackgroundRequest("http://host/low", net::LOWEST
));
2112 scoped_ptr
<TestRequest
> low3(
2113 NewBackgroundRequest("http://host/low", net::LOWEST
));
2114 scoped_ptr
<TestRequest
> low4(
2115 NewBackgroundRequest("http://host/low", net::LOWEST
));
2117 http_server_properties_
.SetSupportsSpdy(net::HostPortPair("spdyhost", 443),
2119 scoped_ptr
<TestRequest
> low_spdy(
2120 NewBackgroundRequest("https://spdyhost/low", net::LOW
));
2121 scoped_ptr
<TestRequest
> sync_request(
2122 NewBackgroundSyncRequest("http://host/req", net::LOW
));
2123 scoped_ptr
<TestRequest
> non_http_request(
2124 NewBackgroundRequest("chrome-extension://req", net::LOW
));
2126 // Sync requests should issue immediately.
2127 EXPECT_TRUE(sync_request
->started());
2128 // Non-http(s) requests should issue immediately.
2129 EXPECT_TRUE(non_http_request
->started());
2130 // Nothing else should issue without a timer fire.
2131 EXPECT_FALSE(high
->started());
2132 EXPECT_FALSE(high2
->started());
2133 EXPECT_FALSE(high3
->started());
2134 EXPECT_FALSE(high4
->started());
2135 EXPECT_FALSE(low
->started());
2136 EXPECT_FALSE(low2
->started());
2137 EXPECT_FALSE(low3
->started());
2138 EXPECT_FALSE(low4
->started());
2139 EXPECT_FALSE(low_spdy
->started());
2141 FireCoalescingTimer();
2143 // All high priority requests should issue.
2144 EXPECT_TRUE(high
->started());
2145 EXPECT_TRUE(high2
->started());
2146 EXPECT_TRUE(high3
->started());
2147 EXPECT_TRUE(high4
->started());
2148 // There should only be one net::LOWEST priority request issued with
2149 // non-delayable requests in flight.
2150 EXPECT_TRUE(low
->started());
2151 EXPECT_FALSE(low2
->started());
2152 EXPECT_FALSE(low3
->started());
2153 EXPECT_FALSE(low4
->started());
2154 // Spdy-Enable requests should issue regardless of priority.
2155 EXPECT_TRUE(low_spdy
->started());
2158 TEST_F(ResourceSchedulerTest
, CoalescedRequestsWaitForNextTimer
) {
2159 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2160 true /* should_coalesce */);
2161 scheduler()->OnLoadingStateChanged(kChildId
, kRouteId
, true);
2162 scheduler()->OnLoadingStateChanged(
2163 kBackgroundChildId
, kBackgroundRouteId
, true);
2165 EXPECT_EQ(ResourceScheduler::COALESCED
,
2166 scheduler()->GetClientStateForTesting(kBackgroundChildId
,
2167 kBackgroundRouteId
));
2168 EXPECT_TRUE(scheduler()->active_clients_loaded());
2170 scoped_ptr
<TestRequest
> high(
2171 NewBackgroundRequest("http://host/high", net::HIGHEST
));
2172 EXPECT_FALSE(high
->started());
2174 FireCoalescingTimer();
2176 scoped_ptr
<TestRequest
> high2(
2177 NewBackgroundRequest("http://host/high2", net::HIGHEST
));
2178 scoped_ptr
<TestRequest
> low(
2179 NewBackgroundRequest("http://host/low", net::LOWEST
));
2181 EXPECT_TRUE(high
->started());
2182 EXPECT_FALSE(high2
->started());
2183 EXPECT_FALSE(low
->started());
2185 FireCoalescingTimer();
2187 EXPECT_TRUE(high
->started());
2188 EXPECT_TRUE(high2
->started());
2189 EXPECT_TRUE(low
->started());
2192 TEST_F(ResourceSchedulerTest
, GetVisualSignalFromRenderViewHost
) {
2193 scoped_ptr
<MockRenderProcessHostFactory
> render_process_host_factory
;
2194 scoped_ptr
<TestRenderViewHostFactory
> render_view_host_factory
;
2195 scoped_ptr
<TestBrowserContext
> browser_context
;
2196 scoped_ptr
<TestWebContents
> web_contents_1
;
2197 scoped_ptr
<TestWebContents
> web_contents_2
;
2198 render_process_host_factory
.reset(new MockRenderProcessHostFactory());
2199 render_view_host_factory
.reset(
2200 new TestRenderViewHostFactory(render_process_host_factory
.get()));
2202 browser_context
.reset(new TestBrowserContext());
2203 scoped_refptr
<SiteInstance
> site_instance_1
=
2204 SiteInstance::Create(browser_context
.get());
2205 scoped_refptr
<SiteInstance
> site_instance_2
=
2206 SiteInstance::Create(browser_context
.get());
2207 SiteInstanceImpl::set_render_process_host_factory(
2208 render_process_host_factory
.get());
2210 web_contents_1
.reset(
2211 TestWebContents::Create(browser_context
.get(), site_instance_1
.get()));
2212 web_contents_2
.reset(
2213 TestWebContents::Create(browser_context
.get(), site_instance_2
.get()));
2214 base::RunLoop().RunUntilIdle();
2216 RenderViewHostImpl
* rvh1
= web_contents_1
->GetRenderViewHost();
2217 RenderViewHostImpl
* rvh2
= web_contents_2
->GetRenderViewHost();
2218 ResourceScheduler
* scheduler
= ResourceDispatcherHostImpl::Get()->scheduler();
2220 // Check initial visibility is set correctly.
2221 EXPECT_EQ(scheduler
->IsClientVisibleForTesting(rvh1
->GetProcess()->GetID(),
2222 rvh1
->GetRoutingID()),
2223 !rvh1
->is_hidden());
2224 EXPECT_EQ(scheduler
->IsClientVisibleForTesting(rvh2
->GetProcess()->GetID(),
2225 rvh1
->GetRoutingID()),
2226 !rvh2
->is_hidden());
2228 // 1 visible, 1 hidden
2229 rvh1
->WasShown(ui::LatencyInfo());
2231 base::RunLoop().RunUntilIdle();
2233 EXPECT_TRUE(scheduler
->IsClientVisibleForTesting(rvh1
->GetProcess()->GetID(),
2234 rvh1
->GetRoutingID()));
2235 EXPECT_FALSE(scheduler
->IsClientVisibleForTesting(rvh2
->GetProcess()->GetID(),
2236 rvh2
->GetRoutingID()));
2238 // Flip the visibility and check again.
2240 rvh2
->WasShown(ui::LatencyInfo());
2241 base::RunLoop().RunUntilIdle();
2243 EXPECT_FALSE(scheduler
->IsClientVisibleForTesting(rvh1
->GetProcess()->GetID(),
2244 rvh1
->GetRoutingID()));
2245 EXPECT_TRUE(scheduler
->IsClientVisibleForTesting(rvh2
->GetProcess()->GetID(),
2246 rvh2
->GetRoutingID()));
2248 web_contents_1
.reset();
2249 web_contents_2
.reset();
2250 base::RunLoop().RunUntilIdle();
2252 browser_context
.reset();
2253 render_process_host_factory
.reset();
2256 TEST_F(ResourceSchedulerTest
, OustandingRequestLimitEnforced
) {
2257 const int kRequestLimit
= 3;
2258 ASSERT_TRUE(InitializeFieldTrials(
2259 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/",
2261 InitializeScheduler();
2263 // Throw in requests up to the above limit; make sure they are started.
2264 ScopedVector
<TestRequest
> requests
;
2265 for (int i
= 0; i
< kRequestLimit
; ++i
) {
2266 string url
= "http://host/medium";
2267 requests
.push_back(NewRequest(url
.c_str(), net::MEDIUM
));
2268 EXPECT_TRUE(requests
[i
]->started());
2271 // Confirm that another request will indeed fail.
2272 string url
= "http://host/medium";
2273 requests
.push_back(NewRequest(url
.c_str(), net::MEDIUM
));
2274 EXPECT_FALSE(requests
[kRequestLimit
]->started());
2277 // Confirm that outstanding requests limits apply to requests to hosts
2278 // that support request priority.
2279 TEST_F(ResourceSchedulerTest
,
2280 OutstandingRequestsLimitsEnforcedForRequestPriority
) {
2281 const int kRequestLimit
= 3;
2282 ASSERT_TRUE(InitializeFieldTrials(
2283 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/",
2285 InitializeScheduler();
2287 http_server_properties_
.SetSupportsSpdy(
2288 net::HostPortPair("spdyhost", 443), true);
2290 // Throw in requests up to the above limit; make sure they are started.
2291 ScopedVector
<TestRequest
> requests
;
2292 for (int i
= 0; i
< kRequestLimit
; ++i
) {
2293 string url
= "http://spdyhost/medium";
2294 requests
.push_back(NewRequest(url
.c_str(), net::MEDIUM
));
2295 EXPECT_TRUE(requests
[i
]->started());
2298 // Confirm that another request will indeed fail.
2299 string url
= "http://spdyhost/medium";
2300 requests
.push_back(NewRequest(url
.c_str(), net::MEDIUM
));
2301 EXPECT_FALSE(requests
[kRequestLimit
]->started());
2304 TEST_F(ResourceSchedulerTest
, OutstandingRequestLimitDelays
) {
2305 const int kRequestLimit
= 3;
2306 ASSERT_TRUE(InitializeFieldTrials(
2307 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/",
2310 InitializeScheduler();
2311 scoped_ptr
<TestRequest
> high(NewRequest("http://host/high", net::HIGHEST
));
2312 scoped_ptr
<TestRequest
> low(NewRequest("http://host/low", net::LOWEST
));
2313 scoped_ptr
<TestRequest
> low2(NewRequest("http://host/low", net::LOWEST
));
2314 EXPECT_TRUE(high
->started());
2315 EXPECT_FALSE(low
->started());
2316 EXPECT_FALSE(low2
->started());
2318 EXPECT_TRUE(low
->started());
2319 EXPECT_TRUE(low2
->started());
2322 // Async revalidations which are not started when the tab is closed must be
2323 // started at some point, or they will hang around forever and prevent other
2324 // async revalidations to the same URL from being issued.
2325 TEST_F(ResourceSchedulerTest
, RequestStartedAfterClientDeleted
) {
2326 scheduler_
->OnClientCreated(kChildId2
, kRouteId2
, false, false);
2327 scoped_ptr
<TestRequest
> high(NewRequestWithChildAndRoute(
2328 "http://host/high", net::HIGHEST
, kChildId2
, kRouteId2
));
2329 scoped_ptr
<TestRequest
> lowest1(NewRequestWithChildAndRoute(
2330 "http://host/lowest", net::LOWEST
, kChildId2
, kRouteId2
));
2331 scoped_ptr
<TestRequest
> lowest2(NewRequestWithChildAndRoute(
2332 "http://host/lowest", net::LOWEST
, kChildId2
, kRouteId2
));
2333 EXPECT_FALSE(lowest2
->started());
2334 scheduler_
->OnClientDeleted(kChildId2
, kRouteId2
);
2337 EXPECT_TRUE(lowest2
->started());
2340 // The ResourceScheduler::Client destructor calls
2341 // LoadAnyStartablePendingRequests(), which may start some pending requests.
2342 // This test is to verify that requests will be started at some point
2343 // even if they were not started by the destructor.
2344 TEST_F(ResourceSchedulerTest
, RequestStartedAfterClientDeletedManyDelayable
) {
2345 scheduler_
->OnClientCreated(kChildId2
, kRouteId2
, false, false);
2346 scoped_ptr
<TestRequest
> high(NewRequestWithChildAndRoute(
2347 "http://host/high", net::HIGHEST
, kChildId2
, kRouteId2
));
2348 const int kMaxNumDelayableRequestsPerClient
= 10;
2349 ScopedVector
<TestRequest
> delayable_requests
;
2350 for (int i
= 0; i
< kMaxNumDelayableRequestsPerClient
+ 1; ++i
) {
2351 delayable_requests
.push_back(NewRequestWithChildAndRoute(
2352 "http://host/lowest", net::LOWEST
, kChildId2
, kRouteId2
));
2354 scoped_ptr
<TestRequest
> lowest(NewRequestWithChildAndRoute(
2355 "http://host/lowest", net::LOWEST
, kChildId2
, kRouteId2
));
2356 EXPECT_FALSE(lowest
->started());
2357 scheduler_
->OnClientDeleted(kChildId2
, kRouteId2
);
2359 delayable_requests
.clear();
2360 EXPECT_TRUE(lowest
->started());
2363 } // unnamed namespace
2365 } // namespace content