| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/loader/resource_scheduler.h" | 5 #include "content/browser/loader/resource_scheduler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/metrics/field_trial_param_associator.h" |
| 12 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/test/mock_entropy_provider.h" |
| 14 #include "base/test/scoped_feature_list.h" | 17 #include "base/test/scoped_feature_list.h" |
| 15 #include "base/timer/mock_timer.h" | 18 #include "base/timer/mock_timer.h" |
| 16 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 17 #include "content/public/browser/resource_context.h" | 20 #include "content/public/browser/resource_context.h" |
| 18 #include "content/public/browser/resource_throttle.h" | 21 #include "content/public/browser/resource_throttle.h" |
| 19 #include "content/public/test/mock_render_process_host.h" | 22 #include "content/public/test/mock_render_process_host.h" |
| 20 #include "content/public/test/test_browser_context.h" | 23 #include "content/public/test/test_browser_context.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "content/test/test_render_view_host_factory.h" | 25 #include "content/test/test_render_view_host_factory.h" |
| 23 #include "content/test/test_web_contents.h" | 26 #include "content/test/test_web_contents.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 41 const int kChildId = 30; | 44 const int kChildId = 30; |
| 42 const int kRouteId = 75; | 45 const int kRouteId = 75; |
| 43 const int kChildId2 = 43; | 46 const int kChildId2 = 43; |
| 44 const int kRouteId2 = 67; | 47 const int kRouteId2 = 67; |
| 45 const int kBackgroundChildId = 35; | 48 const int kBackgroundChildId = 35; |
| 46 const int kBackgroundRouteId = 43; | 49 const int kBackgroundRouteId = 43; |
| 47 | 50 |
| 48 const char kPrioritySupportedRequestsDelayable[] = | 51 const char kPrioritySupportedRequestsDelayable[] = |
| 49 "PrioritySupportedRequestsDelayable"; | 52 "PrioritySupportedRequestsDelayable"; |
| 50 | 53 |
| 54 const char kNetworkSchedulerYielding[] = "NetworkSchedulerYielding"; |
| 55 const int kMaxRequestsBeforeYielding = 5; // sync with .cc. |
| 56 |
| 51 class TestRequest : public ResourceThrottle::Delegate { | 57 class TestRequest : public ResourceThrottle::Delegate { |
| 52 public: | 58 public: |
| 53 TestRequest(std::unique_ptr<net::URLRequest> url_request, | 59 TestRequest(std::unique_ptr<net::URLRequest> url_request, |
| 54 std::unique_ptr<ResourceThrottle> throttle, | 60 std::unique_ptr<ResourceThrottle> throttle, |
| 55 ResourceScheduler* scheduler) | 61 ResourceScheduler* scheduler) |
| 56 : started_(false), | 62 : started_(false), |
| 57 url_request_(std::move(url_request)), | 63 url_request_(std::move(url_request)), |
| 58 throttle_(std::move(throttle)), | 64 throttle_(std::move(throttle)), |
| 59 scheduler_(scheduler) { | 65 scheduler_(scheduler) { |
| 60 throttle_->set_delegate_for_testing(this); | 66 throttle_->set_delegate_for_testing(this); |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 NewRequest("http://host/lowest", net::LOWEST)); | 316 NewRequest("http://host/lowest", net::LOWEST)); |
| 311 EXPECT_TRUE(medium->started()); | 317 EXPECT_TRUE(medium->started()); |
| 312 EXPECT_TRUE(lowest->started()); | 318 EXPECT_TRUE(lowest->started()); |
| 313 EXPECT_FALSE(lowest2->started()); | 319 EXPECT_FALSE(lowest2->started()); |
| 314 | 320 |
| 315 scheduler()->OnWillInsertBody(kChildId, kRouteId); | 321 scheduler()->OnWillInsertBody(kChildId, kRouteId); |
| 316 base::RunLoop().RunUntilIdle(); | 322 base::RunLoop().RunUntilIdle(); |
| 317 EXPECT_TRUE(lowest2->started()); | 323 EXPECT_TRUE(lowest2->started()); |
| 318 } | 324 } |
| 319 | 325 |
| 326 TEST_F(ResourceSchedulerTest, SchedulerYieldsWithFeatureEnabled) { |
| 327 base::test::ScopedFeatureList scoped_feature_list; |
| 328 scoped_feature_list.InitFromCommandLine(kNetworkSchedulerYielding, ""); |
| 329 InitializeScheduler(); |
| 330 |
| 331 // Use spdy so that we don't throttle. |
| 332 http_server_properties_.SetSupportsSpdy( |
| 333 url::SchemeHostPort("https", "spdyhost", 443), true); |
| 334 |
| 335 // Add enough async requests that the last one should yield. |
| 336 std::vector<std::unique_ptr<TestRequest>> requests; |
| 337 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i) |
| 338 requests.push_back(NewRequest("http://host/higher", net::HIGHEST)); |
| 339 |
| 340 // Verify that the number of requests before yielding started. |
| 341 for (int i = 0; i < kMaxRequestsBeforeYielding; ++i) |
| 342 EXPECT_TRUE(requests[i]->started()); |
| 343 |
| 344 // The next async request should have yielded. |
| 345 EXPECT_FALSE(requests[kMaxRequestsBeforeYielding]->started()); |
| 346 |
| 347 // Verify that with time the yielded request eventually runs. |
| 348 base::RunLoop().RunUntilIdle(); |
| 349 EXPECT_TRUE(requests[kMaxRequestsBeforeYielding]->started()); |
| 350 } |
| 351 |
| 352 TEST_F(ResourceSchedulerTest, SchedulerDoesNotYieldForSyncRequests) { |
| 353 base::test::ScopedFeatureList scoped_feature_list; |
| 354 scoped_feature_list.InitFromCommandLine(kNetworkSchedulerYielding, ""); |
| 355 InitializeScheduler(); |
| 356 |
| 357 // Use spdy so that we don't throttle. |
| 358 http_server_properties_.SetSupportsSpdy( |
| 359 url::SchemeHostPort("https", "spdyhost", 443), true); |
| 360 |
| 361 // Add enough async requests that the last one should yield. |
| 362 std::vector<std::unique_ptr<TestRequest>> requests; |
| 363 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i) |
| 364 requests.push_back(NewRequest("http://host/higher", net::HIGHEST)); |
| 365 |
| 366 // Add a sync requests. |
| 367 requests.push_back(NewSyncRequest("http://host/higher", net::HIGHEST)); |
| 368 |
| 369 // Verify that the number of requests before yielding started. |
| 370 for (int i = 0; i < kMaxRequestsBeforeYielding; ++i) |
| 371 EXPECT_TRUE(requests[i]->started()); |
| 372 |
| 373 // The next async request should have yielded. |
| 374 EXPECT_FALSE(requests[kMaxRequestsBeforeYielding]->started()); |
| 375 |
| 376 // The next sync request should have started even though async is yielding. |
| 377 EXPECT_TRUE(requests[kMaxRequestsBeforeYielding + 1]->started()); |
| 378 |
| 379 // Verify that with time the yielded request eventually runs. |
| 380 base::RunLoop().RunUntilIdle(); |
| 381 EXPECT_TRUE(requests[kMaxRequestsBeforeYielding]->started()); |
| 382 } |
| 383 |
| 384 TEST_F(ResourceSchedulerTest, SchedulerDoesNotYieldWithFeatureDisabled) { |
| 385 base::test::ScopedFeatureList scoped_feature_list; |
| 386 scoped_feature_list.InitFromCommandLine("", kNetworkSchedulerYielding); |
| 387 InitializeScheduler(); |
| 388 |
| 389 // Use spdy so that we don't throttle. |
| 390 http_server_properties_.SetSupportsSpdy( |
| 391 url::SchemeHostPort("https", "spdyhost", 443), true); |
| 392 |
| 393 // Add enough async requests that the last one would yield if yielding were |
| 394 // enabled. |
| 395 std::vector<std::unique_ptr<TestRequest>> requests; |
| 396 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i) |
| 397 requests.push_back(NewRequest("http://host/higher", net::HIGHEST)); |
| 398 |
| 399 // Verify that none of the requests yield. |
| 400 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i) |
| 401 EXPECT_TRUE(requests[i]->started()); |
| 402 } |
| 403 |
| 320 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) { | 404 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) { |
| 321 base::test::ScopedFeatureList scoped_feature_list; | 405 base::test::ScopedFeatureList scoped_feature_list; |
| 322 scoped_feature_list.InitFromCommandLine("", | 406 scoped_feature_list.InitFromCommandLine("", |
| 323 kPrioritySupportedRequestsDelayable); | 407 kPrioritySupportedRequestsDelayable); |
| 324 InitializeScheduler(); | 408 InitializeScheduler(); |
| 325 | 409 |
| 326 http_server_properties_.SetSupportsSpdy( | 410 http_server_properties_.SetSupportsSpdy( |
| 327 url::SchemeHostPort("https", "spdyhost", 443), true); | 411 url::SchemeHostPort("https", "spdyhost", 443), true); |
| 328 std::unique_ptr<TestRequest> high( | 412 std::unique_ptr<TestRequest> high( |
| 329 NewRequest("http://host/high", net::HIGHEST)); | 413 NewRequest("http://host/high", net::HIGHEST)); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 | 536 |
| 453 scheduler()->OnWillInsertBody(kChildId, kRouteId); | 537 scheduler()->OnWillInsertBody(kChildId, kRouteId); |
| 454 high.reset(); | 538 high.reset(); |
| 455 base::RunLoop().RunUntilIdle(); | 539 base::RunLoop().RunUntilIdle(); |
| 456 EXPECT_TRUE(low1->started()); | 540 EXPECT_TRUE(low1->started()); |
| 457 EXPECT_TRUE(low2->started()); | 541 EXPECT_TRUE(low2->started()); |
| 458 EXPECT_TRUE(low4->started()); | 542 EXPECT_TRUE(low4->started()); |
| 459 } | 543 } |
| 460 | 544 |
| 461 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) { | 545 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) { |
| 546 // The yielding feature will sometimes yield requests before they get a |
| 547 // chance to start, which conflicts this test. So disable the feature. |
| 548 base::test::ScopedFeatureList scoped_feature_list; |
| 549 scoped_feature_list.InitFromCommandLine("", kNetworkSchedulerYielding); |
| 550 |
| 462 // We only load low priority resources if there's a body. | 551 // We only load low priority resources if there's a body. |
| 463 scheduler()->OnWillInsertBody(kChildId, kRouteId); | 552 scheduler()->OnWillInsertBody(kChildId, kRouteId); |
| 464 | 553 |
| 465 // Throw in one high priority request to make sure that's not a factor. | 554 // Throw in one high priority request to make sure that's not a factor. |
| 466 std::unique_ptr<TestRequest> high( | 555 std::unique_ptr<TestRequest> high( |
| 467 NewRequest("http://host/high", net::HIGHEST)); | 556 NewRequest("http://host/high", net::HIGHEST)); |
| 468 EXPECT_TRUE(high->started()); | 557 EXPECT_TRUE(high->started()); |
| 469 | 558 |
| 470 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc. | 559 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc. |
| 471 const int kMaxNumDelayableRequestsPerHost = 6; | 560 const int kMaxNumDelayableRequestsPerHost = 6; |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 scheduler_->OnClientDeleted(kChildId2, kRouteId2); | 919 scheduler_->OnClientDeleted(kChildId2, kRouteId2); |
| 831 high.reset(); | 920 high.reset(); |
| 832 delayable_requests.clear(); | 921 delayable_requests.clear(); |
| 833 base::RunLoop().RunUntilIdle(); | 922 base::RunLoop().RunUntilIdle(); |
| 834 EXPECT_TRUE(lowest->started()); | 923 EXPECT_TRUE(lowest->started()); |
| 835 } | 924 } |
| 836 | 925 |
| 837 } // unnamed namespace | 926 } // unnamed namespace |
| 838 | 927 |
| 839 } // namespace content | 928 } // namespace content |
| OLD | NEW |