Chromium Code Reviews| 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. | |
|
Charlie Harrison
2017/02/09 19:08:04
Won't this be wrong if the variation param changes
jkarlin
2017/02/09 19:48:13
The variation param isn't part of any of the tests
| |
| 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 std::vector<std::unique_ptr<TestRequest>> requests; | |
| 336 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i) | |
| 337 requests.push_back(NewRequest("http://host/higher", net::HIGHEST)); | |
| 338 | |
| 339 for (int i = 0; i < kMaxRequestsBeforeYielding; ++i) | |
| 340 EXPECT_TRUE(requests[i]->started()); | |
| 341 | |
| 342 EXPECT_FALSE(requests[kMaxRequestsBeforeYielding]->started()); | |
| 343 base::RunLoop().RunUntilIdle(); | |
| 344 EXPECT_TRUE(requests[kMaxRequestsBeforeYielding]->started()); | |
| 345 } | |
| 346 | |
| 347 TEST_F(ResourceSchedulerTest, SchedulerDoesNotYieldWithFeatureDisabled) { | |
| 348 base::test::ScopedFeatureList scoped_feature_list; | |
| 349 scoped_feature_list.InitFromCommandLine("", kNetworkSchedulerYielding); | |
| 350 InitializeScheduler(); | |
| 351 | |
| 352 // Use spdy so that we don't throttle. | |
| 353 http_server_properties_.SetSupportsSpdy( | |
| 354 url::SchemeHostPort("https", "spdyhost", 443), true); | |
| 355 | |
| 356 std::vector<std::unique_ptr<TestRequest>> requests; | |
| 357 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i) | |
| 358 requests.push_back(NewRequest("http://host/higher", net::HIGHEST)); | |
| 359 | |
| 360 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i) | |
| 361 EXPECT_TRUE(requests[i]->started()); | |
| 362 } | |
| 363 | |
| 320 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) { | 364 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) { |
| 321 base::test::ScopedFeatureList scoped_feature_list; | 365 base::test::ScopedFeatureList scoped_feature_list; |
| 322 scoped_feature_list.InitFromCommandLine("", | 366 scoped_feature_list.InitFromCommandLine("", |
| 323 kPrioritySupportedRequestsDelayable); | 367 kPrioritySupportedRequestsDelayable); |
| 324 InitializeScheduler(); | 368 InitializeScheduler(); |
| 325 | 369 |
| 326 http_server_properties_.SetSupportsSpdy( | 370 http_server_properties_.SetSupportsSpdy( |
| 327 url::SchemeHostPort("https", "spdyhost", 443), true); | 371 url::SchemeHostPort("https", "spdyhost", 443), true); |
| 328 std::unique_ptr<TestRequest> high( | 372 std::unique_ptr<TestRequest> high( |
| 329 NewRequest("http://host/high", net::HIGHEST)); | 373 NewRequest("http://host/high", net::HIGHEST)); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 | 496 |
| 453 scheduler()->OnWillInsertBody(kChildId, kRouteId); | 497 scheduler()->OnWillInsertBody(kChildId, kRouteId); |
| 454 high.reset(); | 498 high.reset(); |
| 455 base::RunLoop().RunUntilIdle(); | 499 base::RunLoop().RunUntilIdle(); |
| 456 EXPECT_TRUE(low1->started()); | 500 EXPECT_TRUE(low1->started()); |
| 457 EXPECT_TRUE(low2->started()); | 501 EXPECT_TRUE(low2->started()); |
| 458 EXPECT_TRUE(low4->started()); | 502 EXPECT_TRUE(low4->started()); |
| 459 } | 503 } |
| 460 | 504 |
| 461 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) { | 505 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) { |
| 506 // The yielding feature will sometimes yield requests before they get a | |
| 507 // chance to start, which conflicts this test. So disable the feature. | |
| 508 base::test::ScopedFeatureList scoped_feature_list; | |
| 509 scoped_feature_list.InitFromCommandLine("", kNetworkSchedulerYielding); | |
| 510 | |
| 462 // We only load low priority resources if there's a body. | 511 // We only load low priority resources if there's a body. |
| 463 scheduler()->OnWillInsertBody(kChildId, kRouteId); | 512 scheduler()->OnWillInsertBody(kChildId, kRouteId); |
| 464 | 513 |
| 465 // Throw in one high priority request to make sure that's not a factor. | 514 // Throw in one high priority request to make sure that's not a factor. |
| 466 std::unique_ptr<TestRequest> high( | 515 std::unique_ptr<TestRequest> high( |
| 467 NewRequest("http://host/high", net::HIGHEST)); | 516 NewRequest("http://host/high", net::HIGHEST)); |
| 468 EXPECT_TRUE(high->started()); | 517 EXPECT_TRUE(high->started()); |
| 469 | 518 |
| 470 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc. | 519 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc. |
| 471 const int kMaxNumDelayableRequestsPerHost = 6; | 520 const int kMaxNumDelayableRequestsPerHost = 6; |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 830 scheduler_->OnClientDeleted(kChildId2, kRouteId2); | 879 scheduler_->OnClientDeleted(kChildId2, kRouteId2); |
| 831 high.reset(); | 880 high.reset(); |
| 832 delayable_requests.clear(); | 881 delayable_requests.clear(); |
| 833 base::RunLoop().RunUntilIdle(); | 882 base::RunLoop().RunUntilIdle(); |
| 834 EXPECT_TRUE(lowest->started()); | 883 EXPECT_TRUE(lowest->started()); |
| 835 } | 884 } |
| 836 | 885 |
| 837 } // unnamed namespace | 886 } // unnamed namespace |
| 838 | 887 |
| 839 } // namespace content | 888 } // namespace content |
| OLD | NEW |