| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/field_trial.h" | |
| 13 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/test/mock_entropy_provider.h" | |
| 16 #include "base/timer/mock_timer.h" | 14 #include "base/timer/mock_timer.h" |
| 17 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 18 #include "content/browser/browser_thread_impl.h" | 16 #include "content/browser/browser_thread_impl.h" |
| 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 17 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 20 #include "content/public/browser/resource_context.h" | 18 #include "content/public/browser/resource_context.h" |
| 21 #include "content/public/browser/resource_controller.h" | 19 #include "content/public/browser/resource_controller.h" |
| 22 #include "content/public/browser/resource_throttle.h" | 20 #include "content/public/browser/resource_throttle.h" |
| 23 #include "content/public/common/content_switches.h" | |
| 24 #include "content/public/test/mock_render_process_host.h" | 21 #include "content/public/test/mock_render_process_host.h" |
| 25 #include "content/public/test/test_browser_context.h" | 22 #include "content/public/test/test_browser_context.h" |
| 26 #include "content/test/test_render_view_host_factory.h" | 23 #include "content/test/test_render_view_host_factory.h" |
| 27 #include "content/test/test_web_contents.h" | 24 #include "content/test/test_web_contents.h" |
| 28 #include "net/base/host_port_pair.h" | 25 #include "net/base/host_port_pair.h" |
| 29 #include "net/base/request_priority.h" | 26 #include "net/base/request_priority.h" |
| 30 #include "net/http/http_server_properties_impl.h" | 27 #include "net/http/http_server_properties_impl.h" |
| 31 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 class FakeResourceContext : public ResourceContext { | 119 class FakeResourceContext : public ResourceContext { |
| 123 private: | 120 private: |
| 124 net::HostResolver* GetHostResolver() override { return NULL; } | 121 net::HostResolver* GetHostResolver() override { return NULL; } |
| 125 net::URLRequestContext* GetRequestContext() override { return NULL; } | 122 net::URLRequestContext* GetRequestContext() override { return NULL; } |
| 126 }; | 123 }; |
| 127 | 124 |
| 128 class ResourceSchedulerTest : public testing::Test { | 125 class ResourceSchedulerTest : public testing::Test { |
| 129 protected: | 126 protected: |
| 130 ResourceSchedulerTest() | 127 ResourceSchedulerTest() |
| 131 : ui_thread_(BrowserThread::UI, &message_loop_), | 128 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 132 io_thread_(BrowserThread::IO, &message_loop_), | 129 io_thread_(BrowserThread::IO, &message_loop_) { |
| 133 field_trial_list_(base::MakeUnique<base::MockEntropyProvider>()) { | |
| 134 InitializeScheduler(); | 130 InitializeScheduler(); |
| 135 context_.set_http_server_properties(&http_server_properties_); | 131 context_.set_http_server_properties(&http_server_properties_); |
| 136 } | 132 } |
| 137 | 133 |
| 138 ~ResourceSchedulerTest() override { | 134 ~ResourceSchedulerTest() override { |
| 139 CleanupScheduler(); | 135 CleanupScheduler(); |
| 140 } | 136 } |
| 141 | 137 |
| 142 // Done separately from construction to allow for modification of command | 138 // Done separately from construction to allow for modification of command |
| 143 // line flags in tests. | 139 // line flags in tests. |
| 144 void InitializeScheduler() { | 140 void InitializeScheduler() { |
| 145 CleanupScheduler(); | 141 CleanupScheduler(); |
| 146 | 142 |
| 147 // Destroys previous scheduler, also destroys any previously created | 143 // Destroys previous scheduler, also destroys any previously created |
| 148 // mock_timer_. | 144 // mock_timer_. |
| 149 scheduler_.reset(new ResourceScheduler()); | 145 scheduler_.reset(new ResourceScheduler()); |
| 150 | 146 |
| 151 scheduler_->OnClientCreated(kChildId, kRouteId); | 147 scheduler_->OnClientCreated(kChildId, kRouteId); |
| 152 scheduler_->OnClientCreated( | 148 scheduler_->OnClientCreated( |
| 153 kBackgroundChildId, kBackgroundRouteId); | 149 kBackgroundChildId, kBackgroundRouteId); |
| 154 } | 150 } |
| 155 | 151 |
| 156 void CleanupScheduler() { | 152 void CleanupScheduler() { |
| 157 if (scheduler_) { | 153 if (scheduler_) { |
| 158 scheduler_->OnClientDeleted(kChildId, kRouteId); | 154 scheduler_->OnClientDeleted(kChildId, kRouteId); |
| 159 scheduler_->OnClientDeleted(kBackgroundChildId, kBackgroundRouteId); | 155 scheduler_->OnClientDeleted(kBackgroundChildId, kBackgroundRouteId); |
| 160 } | 156 } |
| 161 } | 157 } |
| 162 | 158 |
| 163 // Create field trials based on the argument, which has the same format | |
| 164 // as the argument to kForceFieldTrials. | |
| 165 bool InitializeFieldTrials(const std::string& force_field_trial_argument) { | |
| 166 return base::FieldTrialList::CreateTrialsFromString( | |
| 167 force_field_trial_argument, std::set<std::string>()); | |
| 168 } | |
| 169 | |
| 170 std::unique_ptr<net::URLRequest> NewURLRequestWithChildAndRoute( | 159 std::unique_ptr<net::URLRequest> NewURLRequestWithChildAndRoute( |
| 171 const char* url, | 160 const char* url, |
| 172 net::RequestPriority priority, | 161 net::RequestPriority priority, |
| 173 int child_id, | 162 int child_id, |
| 174 int route_id) { | 163 int route_id) { |
| 175 std::unique_ptr<net::URLRequest> url_request( | 164 std::unique_ptr<net::URLRequest> url_request( |
| 176 context_.CreateRequest(GURL(url), priority, NULL)); | 165 context_.CreateRequest(GURL(url), priority, NULL)); |
| 177 return url_request; | 166 return url_request; |
| 178 } | 167 } |
| 179 | 168 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 240 |
| 252 ResourceScheduler* scheduler() { | 241 ResourceScheduler* scheduler() { |
| 253 return scheduler_.get(); | 242 return scheduler_.get(); |
| 254 } | 243 } |
| 255 | 244 |
| 256 base::MessageLoopForIO message_loop_; | 245 base::MessageLoopForIO message_loop_; |
| 257 BrowserThreadImpl ui_thread_; | 246 BrowserThreadImpl ui_thread_; |
| 258 BrowserThreadImpl io_thread_; | 247 BrowserThreadImpl io_thread_; |
| 259 ResourceDispatcherHostImpl rdh_; | 248 ResourceDispatcherHostImpl rdh_; |
| 260 std::unique_ptr<ResourceScheduler> scheduler_; | 249 std::unique_ptr<ResourceScheduler> scheduler_; |
| 261 base::FieldTrialList field_trial_list_; | |
| 262 base::MockTimer* mock_timer_; | 250 base::MockTimer* mock_timer_; |
| 263 net::HttpServerPropertiesImpl http_server_properties_; | 251 net::HttpServerPropertiesImpl http_server_properties_; |
| 264 net::TestURLRequestContext context_; | 252 net::TestURLRequestContext context_; |
| 265 }; | 253 }; |
| 266 | 254 |
| 267 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) { | 255 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) { |
| 268 std::unique_ptr<TestRequest> request( | 256 std::unique_ptr<TestRequest> request( |
| 269 NewRequest("http://host/1", net::LOWEST)); | 257 NewRequest("http://host/1", net::LOWEST)); |
| 270 EXPECT_TRUE(request->started()); | 258 EXPECT_TRUE(request->started()); |
| 271 } | 259 } |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 // Reprioritize a request after we learn the server supports SPDY. | 675 // Reprioritize a request after we learn the server supports SPDY. |
| 688 EXPECT_TRUE(low2_spdy->started()); | 676 EXPECT_TRUE(low2_spdy->started()); |
| 689 http_server_properties_.SetSupportsSpdy( | 677 http_server_properties_.SetSupportsSpdy( |
| 690 url::SchemeHostPort("http", "spdyhost2", 8080), true); | 678 url::SchemeHostPort("http", "spdyhost2", 8080), true); |
| 691 ChangeRequestPriority(low2_spdy.get(), net::LOWEST); | 679 ChangeRequestPriority(low2_spdy.get(), net::LOWEST); |
| 692 base::RunLoop().RunUntilIdle(); | 680 base::RunLoop().RunUntilIdle(); |
| 693 std::unique_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST)); | 681 std::unique_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST)); |
| 694 EXPECT_TRUE(low2->started()); | 682 EXPECT_TRUE(low2->started()); |
| 695 } | 683 } |
| 696 | 684 |
| 697 TEST_F(ResourceSchedulerTest, OustandingRequestLimitEnforced) { | 685 // Async revalidations which are not started when the tab is closed must be |
| 698 const int kRequestLimit = 3; | |
| 699 ASSERT_TRUE(InitializeFieldTrials( | |
| 700 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/", | |
| 701 kRequestLimit))); | |
| 702 InitializeScheduler(); | |
| 703 | |
| 704 // Throw in requests up to the above limit; make sure they are started. | |
| 705 ScopedVector<TestRequest> requests; | |
| 706 for (int i = 0; i < kRequestLimit; ++i) { | |
| 707 string url = "http://host/medium"; | |
| 708 requests.push_back(NewRequest(url.c_str(), net::MEDIUM)); | |
| 709 EXPECT_TRUE(requests[i]->started()); | |
| 710 } | |
| 711 | |
| 712 // Confirm that another request will indeed fail. | |
| 713 string url = "http://host/medium"; | |
| 714 requests.push_back(NewRequest(url.c_str(), net::MEDIUM)); | |
| 715 EXPECT_FALSE(requests[kRequestLimit]->started()); | |
| 716 } | |
| 717 | |
| 718 // Confirm that outstanding requests limits apply to requests to hosts | |
| 719 // that support request priority. | |
| 720 TEST_F(ResourceSchedulerTest, | |
| 721 OutstandingRequestsLimitsEnforcedForRequestPriority) { | |
| 722 const int kRequestLimit = 3; | |
| 723 ASSERT_TRUE(InitializeFieldTrials( | |
| 724 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/", | |
| 725 kRequestLimit))); | |
| 726 InitializeScheduler(); | |
| 727 | |
| 728 http_server_properties_.SetSupportsSpdy( | |
| 729 url::SchemeHostPort("https", "spdyhost", 443), true); | |
| 730 | |
| 731 // Throw in requests up to the above limit; make sure they are started. | |
| 732 ScopedVector<TestRequest> requests; | |
| 733 for (int i = 0; i < kRequestLimit; ++i) { | |
| 734 string url = "http://spdyhost/medium"; | |
| 735 requests.push_back(NewRequest(url.c_str(), net::MEDIUM)); | |
| 736 EXPECT_TRUE(requests[i]->started()); | |
| 737 } | |
| 738 | |
| 739 // Confirm that another request will indeed fail. | |
| 740 string url = "http://spdyhost/medium"; | |
| 741 requests.push_back(NewRequest(url.c_str(), net::MEDIUM)); | |
| 742 EXPECT_FALSE(requests[kRequestLimit]->started()); | |
| 743 } | |
| 744 | |
| 745 TEST_F(ResourceSchedulerTest, OutstandingRequestLimitDelays) { | |
| 746 const int kRequestLimit = 3; | |
| 747 ASSERT_TRUE(InitializeFieldTrials( | |
| 748 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/", | |
| 749 kRequestLimit))); | |
| 750 | |
| 751 InitializeScheduler(); | |
| 752 std::unique_ptr<TestRequest> high( | |
| 753 NewRequest("http://host/high", net::HIGHEST)); | |
| 754 std::unique_ptr<TestRequest> low(NewRequest("http://host/low", | |
| 755 net::LOWEST)); | |
| 756 std::unique_ptr<TestRequest> low2(NewRequest("http://host/low", | |
| 757 net::LOWEST)); | |
| 758 EXPECT_TRUE(high->started()); | |
| 759 EXPECT_FALSE(low->started()); | |
| 760 EXPECT_FALSE(low2->started()); | |
| 761 | |
| 762 high.reset(); | |
| 763 base::RunLoop().RunUntilIdle(); | |
| 764 EXPECT_TRUE(low->started()); | |
| 765 EXPECT_FALSE(low2->started()); | |
| 766 } | |
| 767 | |
| 768 // Async revalidations which are not started when the tab is closed must be | |
| 769 // started at some point, or they will hang around forever and prevent other | 686 // started at some point, or they will hang around forever and prevent other |
| 770 // async revalidations to the same URL from being issued. | 687 // async revalidations to the same URL from being issued. |
| 771 TEST_F(ResourceSchedulerTest, RequestStartedAfterClientDeleted) { | 688 TEST_F(ResourceSchedulerTest, RequestStartedAfterClientDeleted) { |
| 772 scheduler_->OnClientCreated(kChildId2, kRouteId2); | 689 scheduler_->OnClientCreated(kChildId2, kRouteId2); |
| 773 std::unique_ptr<TestRequest> high(NewRequestWithChildAndRoute( | 690 std::unique_ptr<TestRequest> high(NewRequestWithChildAndRoute( |
| 774 "http://host/high", net::HIGHEST, kChildId2, kRouteId2)); | 691 "http://host/high", net::HIGHEST, kChildId2, kRouteId2)); |
| 775 std::unique_ptr<TestRequest> lowest1(NewRequestWithChildAndRoute( | 692 std::unique_ptr<TestRequest> lowest1(NewRequestWithChildAndRoute( |
| 776 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2)); | 693 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2)); |
| 777 std::unique_ptr<TestRequest> lowest2(NewRequestWithChildAndRoute( | 694 std::unique_ptr<TestRequest> lowest2(NewRequestWithChildAndRoute( |
| 778 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2)); | 695 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2)); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 806 scheduler_->OnClientDeleted(kChildId2, kRouteId2); | 723 scheduler_->OnClientDeleted(kChildId2, kRouteId2); |
| 807 high.reset(); | 724 high.reset(); |
| 808 delayable_requests.clear(); | 725 delayable_requests.clear(); |
| 809 base::RunLoop().RunUntilIdle(); | 726 base::RunLoop().RunUntilIdle(); |
| 810 EXPECT_TRUE(lowest->started()); | 727 EXPECT_TRUE(lowest->started()); |
| 811 } | 728 } |
| 812 | 729 |
| 813 } // unnamed namespace | 730 } // unnamed namespace |
| 814 | 731 |
| 815 } // namespace content | 732 } // namespace content |
| OLD | NEW |