Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <cstring> | 9 #include <cstring> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 22 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 23 #include "base/strings/stringprintf.h" | |
| 23 #include "base/test/histogram_tester.h" | 24 #include "base/test/histogram_tester.h" |
| 24 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
| 25 #include "components/precache/core/precache_switches.h" | 26 #include "components/precache/core/precache_switches.h" |
| 26 #include "components/precache/core/proto/precache.pb.h" | 27 #include "components/precache/core/proto/precache.pb.h" |
| 27 #include "components/precache/core/proto/unfinished_work.pb.h" | 28 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 28 #include "net/base/load_flags.h" | 29 #include "net/base/load_flags.h" |
| 29 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
| 30 #include "net/http/http_status_code.h" | 31 #include "net/http/http_status_code.h" |
| 31 #include "net/url_request/test_url_fetcher_factory.h" | 32 #include "net/url_request/test_url_fetcher_factory.h" |
| 32 #include "net/url_request/url_request_status.h" | 33 #include "net/url_request/url_request_status.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 60 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com"; | 61 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com"; |
| 61 const char kGoodResourceURL[] = "http://good-resource.com"; | 62 const char kGoodResourceURL[] = "http://good-resource.com"; |
| 62 const char kGoodResourceURLA[] = "http://good-resource.com/a"; | 63 const char kGoodResourceURLA[] = "http://good-resource.com/a"; |
| 63 const char kGoodResourceURLB[] = "http://good-resource.com/b"; | 64 const char kGoodResourceURLB[] = "http://good-resource.com/b"; |
| 64 const char kGoodResourceURLC[] = "http://good-resource.com/c"; | 65 const char kGoodResourceURLC[] = "http://good-resource.com/c"; |
| 65 const char kGoodResourceURLD[] = "http://good-resource.com/d"; | 66 const char kGoodResourceURLD[] = "http://good-resource.com/d"; |
| 66 const char kForcedStartingURLManifestURL[] = | 67 const char kForcedStartingURLManifestURL[] = |
| 67 "http://manifest-url-prefix.com/forced-starting-url.com"; | 68 "http://manifest-url-prefix.com/forced-starting-url.com"; |
| 68 const uint32_t kExperimentID = 123; | 69 const uint32_t kExperimentID = 123; |
| 69 | 70 |
| 71 } // namespace | |
| 72 | |
| 70 class TestURLFetcherCallback { | 73 class TestURLFetcherCallback { |
| 71 public: | 74 public: |
| 72 TestURLFetcherCallback() : total_response_bytes_(0) {} | 75 TestURLFetcherCallback() : total_response_bytes_(0) {} |
| 73 | 76 |
| 74 std::unique_ptr<net::FakeURLFetcher> CreateURLFetcher( | 77 std::unique_ptr<net::FakeURLFetcher> CreateURLFetcher( |
| 75 const GURL& url, | 78 const GURL& url, |
| 76 net::URLFetcherDelegate* delegate, | 79 net::URLFetcherDelegate* delegate, |
| 77 const std::string& response_data, | 80 const std::string& response_data, |
| 78 net::HttpStatusCode response_code, | 81 net::HttpStatusCode response_code, |
| 79 net::URLRequestStatus::Status status) { | 82 net::URLRequestStatus::Status status) { |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 } | 330 } |
| 328 | 331 |
| 329 class PrecacheFetcherTest : public testing::Test { | 332 class PrecacheFetcherTest : public testing::Test { |
| 330 public: | 333 public: |
| 331 PrecacheFetcherTest() | 334 PrecacheFetcherTest() |
| 332 : request_context_(new net::TestURLRequestContextGetter( | 335 : request_context_(new net::TestURLRequestContextGetter( |
| 333 base::ThreadTaskRunnerHandle::Get())), | 336 base::ThreadTaskRunnerHandle::Get())), |
| 334 factory_(NULL, | 337 factory_(NULL, |
| 335 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, | 338 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, |
| 336 base::Unretained(&url_callback_))), | 339 base::Unretained(&url_callback_))), |
| 337 expected_total_response_bytes_(0) {} | 340 expected_total_response_bytes_(0), |
| 341 parallel_fetches_beyond_capacity(false) {} | |
| 338 | 342 |
| 339 protected: | 343 protected: |
| 340 void SetDefaultFlags() { | 344 void SetDefaultFlags() { |
| 341 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 345 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 342 switches::kPrecacheConfigSettingsURL, kConfigURL); | 346 switches::kPrecacheConfigSettingsURL, kConfigURL); |
| 343 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 347 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 344 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix); | 348 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix); |
| 345 } | 349 } |
| 346 | 350 |
| 351 // Posts a task to check if more parallel fetches of precache manifest and | |
| 352 // resource URLs were attempted beyond the fetcher pool maximum defined | |
| 353 // capacity. The task will be posted repeatedly until such condition is met. | |
| 354 void CheckUntilParallelFetchesBeyondCapacity( | |
| 355 const PrecacheFetcher* precache_fetcher) { | |
| 356 if (!precache_fetcher->pool_.IsAvailable() && | |
| 357 !precache_fetcher->resource_urls_to_fetch_.empty() && | |
| 358 !precache_fetcher->manifest_urls_to_fetch_.empty()) { | |
| 359 parallel_fetches_beyond_capacity = true; | |
| 360 return; | |
| 361 } | |
| 362 | |
| 363 // Check again after allowing the message loop to process some messages. | |
| 364 loop_.PostTask( | |
| 365 FROM_HERE, | |
| 366 base::Bind( | |
| 367 &PrecacheFetcherTest::CheckUntilParallelFetchesBeyondCapacity, | |
| 368 base::Unretained(this), precache_fetcher)); | |
| 369 } | |
| 370 | |
| 347 base::MessageLoopForUI loop_; | 371 base::MessageLoopForUI loop_; |
| 348 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 372 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 349 TestURLFetcherCallback url_callback_; | 373 TestURLFetcherCallback url_callback_; |
| 350 net::FakeURLFetcherFactory factory_; | 374 net::FakeURLFetcherFactory factory_; |
| 351 TestPrecacheDelegate precache_delegate_; | 375 TestPrecacheDelegate precache_delegate_; |
| 352 int expected_total_response_bytes_; | 376 int expected_total_response_bytes_; |
| 377 | |
| 378 // True if more parallel fetches were attempted beyond the fetcher pool | |
| 379 // maximum capacity. | |
| 380 bool parallel_fetches_beyond_capacity; | |
| 353 }; | 381 }; |
| 354 | 382 |
| 355 TEST_F(PrecacheFetcherTest, FullPrecache) { | 383 TEST_F(PrecacheFetcherTest, FullPrecache) { |
| 356 SetDefaultFlags(); | 384 SetDefaultFlags(); |
| 357 | 385 |
| 358 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | 386 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
| 359 new PrecacheUnfinishedWork()); | 387 new PrecacheUnfinishedWork()); |
| 360 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); | 388 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); |
| 361 unfinished_work->add_top_host()->set_hostname("manifest-fetch-failure.com"); | 389 unfinished_work->add_top_host()->set_hostname("manifest-fetch-failure.com"); |
| 362 unfinished_work->add_top_host()->set_hostname("bad-manifest.com"); | 390 unfinished_work->add_top_host()->set_hostname("bad-manifest.com"); |
| (...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 974 EXPECT_GT(kNumResources, url_callback_.requested_urls().size()); | 1002 EXPECT_GT(kNumResources, url_callback_.requested_urls().size()); |
| 975 | 1003 |
| 976 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 1004 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
| 977 | 1005 |
| 978 // good-manifest.com will not have been completed. | 1006 // good-manifest.com will not have been completed. |
| 979 EXPECT_THAT(histogram.GetAllSamples("Precache.Fetch.PercentCompleted"), | 1007 EXPECT_THAT(histogram.GetAllSamples("Precache.Fetch.PercentCompleted"), |
| 980 ElementsAre(base::Bucket(0, 1))); | 1008 ElementsAre(base::Bucket(0, 1))); |
| 981 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); | 1009 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); |
| 982 } | 1010 } |
| 983 | 1011 |
| 984 } // namespace | 1012 // Tests the parallel fetch behaviour when more precache resource and manifest |
| 1013 // requests are available than the maximum capacity of fetcher pool. | |
| 1014 TEST_F(PrecacheFetcherTest, FetcherPoolMaxLimitReached) { | |
| 1015 SetDefaultFlags(); | |
| 1016 | |
| 1017 const size_t kNumTopHosts = 5; | |
| 1018 const size_t kNumResources = GetDefaultMaxParallelFetches() + 1; | |
| 1019 | |
| 1020 PrecacheConfigurationSettings config; | |
| 1021 PrecacheManifest top_host_manifest[kNumTopHosts]; | |
| 1022 std::multiset<GURL> expected_requested_urls; | |
| 1023 | |
| 1024 config.set_top_sites_count(kNumTopHosts); | |
| 1025 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), | |
| 1026 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | |
| 1027 expected_requested_urls.insert(GURL(kConfigURL)); | |
| 1028 | |
| 1029 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | |
| 1030 new PrecacheUnfinishedWork()); | |
| 1031 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); | |
| 1032 | |
| 1033 for (size_t i = 0; i < kNumTopHosts; ++i) { | |
| 1034 const std::string top_host_url = base::StringPrintf("top-host-%d.com", i); | |
| 1035 unfinished_work->add_top_host()->set_hostname(top_host_url); | |
| 1036 | |
| 1037 for (size_t j = 0; j < kNumResources; ++j) { | |
| 1038 const std::string resource_url = | |
| 1039 base::StringPrintf("http://top-host-%d.com/resource-%d", i, j); | |
| 1040 top_host_manifest[i].add_resource()->set_url(resource_url); | |
| 1041 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK, | |
| 1042 net::URLRequestStatus::SUCCESS); | |
| 1043 expected_requested_urls.insert(GURL(resource_url)); | |
| 1044 } | |
| 1045 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host_url), | |
| 1046 top_host_manifest[i].SerializeAsString(), | |
| 1047 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | |
| 1048 expected_requested_urls.insert(GURL(kManifestURLPrefix + top_host_url)); | |
| 1049 } | |
| 1050 | |
| 1051 base::HistogramTester histogram; | |
| 1052 | |
| 1053 { | |
| 1054 PrecacheFetcher precache_fetcher(request_context_.get(), GURL(), | |
| 1055 std::string(), std::move(unfinished_work), | |
| 1056 kExperimentID, &precache_delegate_); | |
| 1057 precache_fetcher.Start(); | |
|
bengr
2016/06/10 19:49:51
I would change 1018 back to '= 15' and would make
Raj
2016/06/10 21:12:03
Done.
Just added this test as a friend and accessi
| |
| 1058 | |
| 1059 CheckUntilParallelFetchesBeyondCapacity(&precache_fetcher); | |
| 1060 | |
| 1061 loop_.RunUntilIdle(); | |
| 1062 | |
| 1063 // Destroy the PrecacheFetcher after it has finished, to record metrics. | |
| 1064 } | |
| 1065 | |
| 1066 EXPECT_TRUE(parallel_fetches_beyond_capacity); | |
| 1067 | |
| 1068 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | |
| 1069 | |
| 1070 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | |
| 1071 | |
| 1072 histogram.ExpectUniqueSample("Precache.Fetch.PercentCompleted", 100, 1); | |
| 1073 histogram.ExpectUniqueSample("Precache.Fetch.ResponseBytes.Total", | |
| 1074 url_callback_.total_response_bytes(), 1); | |
| 1075 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); | |
| 1076 } | |
| 985 | 1077 |
| 986 } // namespace precache | 1078 } // namespace precache |
| OLD | NEW |