Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: content/browser/loader/resource_scheduler_unittest.cc

Issue 2711633003: Reland of [ResourceScheduler] Yield after starting several requests (Closed)
Patch Set: Address comments from PS3 Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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, SchedulerDoesNotYieldForAlternativeSchemes) {
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 should yield.
394 std::vector<std::unique_ptr<TestRequest>> requests;
395 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i)
396 requests.push_back(NewRequest("http://host/higher", net::HIGHEST));
397
398 // Add a non-http request.
399 requests.push_back(NewRequest("zzz://host/higher", net::HIGHEST));
400
401 // Verify that the number of requests before yielding started.
402 for (int i = 0; i < kMaxRequestsBeforeYielding; ++i)
403 EXPECT_TRUE(requests[i]->started());
404
405 // The next async request should have yielded.
406 EXPECT_FALSE(requests[kMaxRequestsBeforeYielding]->started());
407
408 // The non-http(s) request should have started even though async is
409 // yielding.
410 EXPECT_TRUE(requests[kMaxRequestsBeforeYielding + 1]->started());
411
412 // Verify that with time the yielded request eventually runs.
413 base::RunLoop().RunUntilIdle();
414 EXPECT_TRUE(requests[kMaxRequestsBeforeYielding]->started());
415 }
416
417 TEST_F(ResourceSchedulerTest, SchedulerDoesNotYieldWithFeatureDisabled) {
418 base::test::ScopedFeatureList scoped_feature_list;
419 scoped_feature_list.InitFromCommandLine("", kNetworkSchedulerYielding);
420 InitializeScheduler();
421
422 // Use spdy so that we don't throttle.
423 http_server_properties_.SetSupportsSpdy(
424 url::SchemeHostPort("https", "spdyhost", 443), true);
425
426 // Add enough async requests that the last one would yield if yielding were
427 // enabled.
428 std::vector<std::unique_ptr<TestRequest>> requests;
429 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i)
430 requests.push_back(NewRequest("http://host/higher", net::HIGHEST));
431
432 // Verify that none of the requests yield.
433 for (int i = 0; i < kMaxRequestsBeforeYielding + 1; ++i)
434 EXPECT_TRUE(requests[i]->started());
435 }
436
320 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) { 437 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) {
321 base::test::ScopedFeatureList scoped_feature_list; 438 base::test::ScopedFeatureList scoped_feature_list;
322 scoped_feature_list.InitFromCommandLine("", 439 scoped_feature_list.InitFromCommandLine("",
323 kPrioritySupportedRequestsDelayable); 440 kPrioritySupportedRequestsDelayable);
324 InitializeScheduler(); 441 InitializeScheduler();
325 442
326 http_server_properties_.SetSupportsSpdy( 443 http_server_properties_.SetSupportsSpdy(
327 url::SchemeHostPort("https", "spdyhost", 443), true); 444 url::SchemeHostPort("https", "spdyhost", 443), true);
328 std::unique_ptr<TestRequest> high( 445 std::unique_ptr<TestRequest> high(
329 NewRequest("http://host/high", net::HIGHEST)); 446 NewRequest("http://host/high", net::HIGHEST));
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 569
453 scheduler()->OnWillInsertBody(kChildId, kRouteId); 570 scheduler()->OnWillInsertBody(kChildId, kRouteId);
454 high.reset(); 571 high.reset();
455 base::RunLoop().RunUntilIdle(); 572 base::RunLoop().RunUntilIdle();
456 EXPECT_TRUE(low1->started()); 573 EXPECT_TRUE(low1->started());
457 EXPECT_TRUE(low2->started()); 574 EXPECT_TRUE(low2->started());
458 EXPECT_TRUE(low4->started()); 575 EXPECT_TRUE(low4->started());
459 } 576 }
460 577
461 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) { 578 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) {
579 // The yielding feature will sometimes yield requests before they get a
580 // chance to start, which conflicts this test. So disable the feature.
581 base::test::ScopedFeatureList scoped_feature_list;
582 scoped_feature_list.InitFromCommandLine("", kNetworkSchedulerYielding);
583
462 // We only load low priority resources if there's a body. 584 // We only load low priority resources if there's a body.
463 scheduler()->OnWillInsertBody(kChildId, kRouteId); 585 scheduler()->OnWillInsertBody(kChildId, kRouteId);
464 586
465 // Throw in one high priority request to make sure that's not a factor. 587 // Throw in one high priority request to make sure that's not a factor.
466 std::unique_ptr<TestRequest> high( 588 std::unique_ptr<TestRequest> high(
467 NewRequest("http://host/high", net::HIGHEST)); 589 NewRequest("http://host/high", net::HIGHEST));
468 EXPECT_TRUE(high->started()); 590 EXPECT_TRUE(high->started());
469 591
470 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc. 592 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
471 const int kMaxNumDelayableRequestsPerHost = 6; 593 const int kMaxNumDelayableRequestsPerHost = 6;
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 scheduler_->OnClientDeleted(kChildId2, kRouteId2); 952 scheduler_->OnClientDeleted(kChildId2, kRouteId2);
831 high.reset(); 953 high.reset();
832 delayable_requests.clear(); 954 delayable_requests.clear();
833 base::RunLoop().RunUntilIdle(); 955 base::RunLoop().RunUntilIdle();
834 EXPECT_TRUE(lowest->started()); 956 EXPECT_TRUE(lowest->started());
835 } 957 }
836 958
837 } // unnamed namespace 959 } // unnamed namespace
838 960
839 } // namespace content 961 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/resource_scheduler.cc ('k') | testing/variations/fieldtrial_testing_config.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698