diff options
author | jkarlin@chromium.org <jkarlin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-09 22:16:15 +0000 |
---|---|---|
committer | jkarlin@chromium.org <jkarlin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-09 22:16:15 +0000 |
commit | 5584eab1352afd0d2db7fa856fbbc886522e1732 (patch) | |
tree | 61df6d86bbd692659b4a7910c5954a5fd5251eed /content/browser/loader/resource_dispatcher_host_unittest.cc | |
parent | aa52489e2c6c7637c1d8c34424b5e1576dae7705 (diff) | |
download | chromium_src-5584eab1352afd0d2db7fa856fbbc886522e1732.zip chromium_src-5584eab1352afd0d2db7fa856fbbc886522e1732.tar.gz chromium_src-5584eab1352afd0d2db7fa856fbbc886522e1732.tar.bz2 |
Adds the new URLRequest::OnBeforeNetworkStart hook to the
ResourceThrottle so that the ResourceScheduler can eventually throttle
closer to the start of network usage.
This is the second step in the design doc:
https://docs.google.com/document/d/1TSI3jwozVB_nueWJxzi8uKbgDfsGZRZqw8tvtD-P1Iw/edit?usp=sharing
The third step is to create the new ResourceScheduler.
BUG=328741
Review URL: https://codereview.chromium.org/129173002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243994 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/loader/resource_dispatcher_host_unittest.cc')
-rw-r--r-- | content/browser/loader/resource_dispatcher_host_unittest.cc | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/content/browser/loader/resource_dispatcher_host_unittest.cc b/content/browser/loader/resource_dispatcher_host_unittest.cc index 9d58b8f..009b364 100644 --- a/content/browser/loader/resource_dispatcher_host_unittest.cc +++ b/content/browser/loader/resource_dispatcher_host_unittest.cc @@ -210,6 +210,33 @@ class ForwardingFilter : public ResourceMessageFilter { DISALLOW_COPY_AND_ASSIGN(ForwardingFilter); }; +// This class is a variation on URLRequestTestJob that will call +// URLRequest::OnBeforeNetworkStart before starting. +class URLRequestTestDelayedNetworkJob : public net::URLRequestTestJob { + public: + URLRequestTestDelayedNetworkJob(net::URLRequest* request, + net::NetworkDelegate* network_delegate) + : net::URLRequestTestJob(request, network_delegate) {} + + // Only start if not deferred for network start. + virtual void Start() OVERRIDE { + bool defer = false; + NotifyBeforeNetworkStart(&defer); + if (defer) + return; + net::URLRequestTestJob::Start(); + } + + virtual void ResumeNetworkStart() OVERRIDE { + net::URLRequestTestJob::StartAsync(); + } + + private: + virtual ~URLRequestTestDelayedNetworkJob() {} + + DISALLOW_COPY_AND_ASSIGN(URLRequestTestDelayedNetworkJob); +}; + // This class is a variation on URLRequestTestJob in that it does // not complete start upon entry, only when specifically told to. class URLRequestTestDelayedStartJob : public net::URLRequestTestJob { @@ -394,7 +421,8 @@ enum GenericResourceThrottleFlags { NONE = 0, DEFER_STARTING_REQUEST = 1 << 0, DEFER_PROCESSING_RESPONSE = 1 << 1, - CANCEL_BEFORE_START = 1 << 2 + CANCEL_BEFORE_START = 1 << 2, + DEFER_NETWORK_START = 1 << 3 }; // Throttle that tracks the current throttle blocking a request. Only one @@ -441,6 +469,15 @@ class GenericResourceThrottle : public ResourceThrottle { } } + virtual void OnBeforeNetworkStart(bool* defer) OVERRIDE { + ASSERT_EQ(NULL, active_throttle_); + + if (flags_ & DEFER_NETWORK_START) { + active_throttle_ = this; + *defer = true; + } + } + virtual const char* GetNameForLogging() const OVERRIDE { return "GenericResourceThrottle"; } @@ -573,6 +610,7 @@ class ResourceDispatcherHostTest : public testing::Test, EnsureTestSchemeIsAllowed(); delay_start_ = false; delay_complete_ = false; + network_start_notification_ = false; url_request_jobs_created_count_ = 0; } @@ -670,6 +708,8 @@ class ResourceDispatcherHostTest : public testing::Test, } else if (delay_complete_) { return new URLRequestTestDelayedCompletionJob(request, network_delegate); + } else if (network_start_notification_) { + return new URLRequestTestDelayedNetworkJob(request, network_delegate); } else if (scheme == "big-job") { return new URLRequestBigJob(request, network_delegate); } else { @@ -703,6 +743,10 @@ class ResourceDispatcherHostTest : public testing::Test, delay_complete_ = delay_job_complete; } + void SetNetworkStartNotificationJobGeneration(bool notification) { + network_start_notification_ = notification; + } + void GenerateDataReceivedACK(const IPC::Message& msg) { EXPECT_EQ(ResourceMsg_DataReceived::ID, msg.type()); @@ -736,12 +780,14 @@ class ResourceDispatcherHostTest : public testing::Test, static ResourceDispatcherHostTest* test_fixture_; static bool delay_start_; static bool delay_complete_; + static bool network_start_notification_; static int url_request_jobs_created_count_; }; // Static. ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; bool ResourceDispatcherHostTest::delay_start_ = false; bool ResourceDispatcherHostTest::delay_complete_ = false; +bool ResourceDispatcherHostTest::network_start_notification_ = false; int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; void ResourceDispatcherHostTest::MakeTestRequest(int render_view_id, @@ -1228,6 +1274,33 @@ TEST_F(ResourceDispatcherHostTest, PausedStartError) { EXPECT_EQ(0, host_.pending_requests()); } +// Test the OnBeforeNetworkStart throttle. +TEST_F(ResourceDispatcherHostTest, ThrottleNetworkStart) { + // Arrange to have requests deferred before processing response headers. + TestResourceDispatcherHostDelegate delegate; + delegate.set_flags(DEFER_NETWORK_START); + host_.SetDelegate(&delegate); + + SetNetworkStartNotificationJobGeneration(true); + MakeTestRequest(0, 1, net::URLRequestTestJob::test_url_2()); + + // Should have deferred for network start. + GenericResourceThrottle* first_throttle = + GenericResourceThrottle::active_throttle(); + ASSERT_TRUE(first_throttle); + EXPECT_EQ(0, network_delegate()->completed_requests()); + EXPECT_EQ(1, host_.pending_requests()); + + first_throttle->Resume(); + + // Flush all the pending requests. + while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} + base::MessageLoop::current()->RunUntilIdle(); + + EXPECT_EQ(1, network_delegate()->completed_requests()); + EXPECT_EQ(0, host_.pending_requests()); +} + TEST_F(ResourceDispatcherHostTest, ThrottleAndResumeTwice) { // Arrange to have requests deferred before starting. TestResourceDispatcherHostDelegate delegate; |