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

Side by Side Diff: components/precache/core/precache_fetcher_unittest.cc

Issue 2119673002: Precache manifest should not be added to fetcher pool which is full (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2743
Patch Set: Created 4 years, 5 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
« no previous file with comments | « components/precache/core/precache_fetcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "net/base/load_flags.h" 28 #include "net/base/load_flags.h"
28 #include "net/http/http_response_headers.h" 29 #include "net/http/http_response_headers.h"
29 #include "net/http/http_status_code.h" 30 #include "net/http/http_status_code.h"
30 #include "net/url_request/test_url_fetcher_factory.h" 31 #include "net/url_request/test_url_fetcher_factory.h"
31 #include "net/url_request/url_request_status.h" 32 #include "net/url_request/url_request_status.h"
32 #include "net/url_request/url_request_test_util.h" 33 #include "net/url_request/url_request_test_util.h"
(...skipping 21 matching lines...) Expand all
54 "http://manifest-url-prefix.com/bad-manifest.com"; 55 "http://manifest-url-prefix.com/bad-manifest.com";
55 const char kGoodManifestURL[] = 56 const char kGoodManifestURL[] =
56 "http://manifest-url-prefix.com/good-manifest.com"; 57 "http://manifest-url-prefix.com/good-manifest.com";
57 const char kCustomGoodManifestURL[] = 58 const char kCustomGoodManifestURL[] =
58 "http://custom-manifest-url-prefix.com/good-manifest.com"; 59 "http://custom-manifest-url-prefix.com/good-manifest.com";
59 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com"; 60 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com";
60 const char kGoodResourceURL[] = "http://good-resource.com"; 61 const char kGoodResourceURL[] = "http://good-resource.com";
61 const char kForcedStartingURLManifestURL[] = 62 const char kForcedStartingURLManifestURL[] =
62 "http://manifest-url-prefix.com/forced-starting-url.com"; 63 "http://manifest-url-prefix.com/forced-starting-url.com";
63 64
65 } // namespace
66
64 class TestURLFetcherCallback { 67 class TestURLFetcherCallback {
65 public: 68 public:
66 TestURLFetcherCallback() : total_response_bytes_(0) {} 69 TestURLFetcherCallback() : total_response_bytes_(0) {}
67 70
68 std::unique_ptr<net::FakeURLFetcher> CreateURLFetcher( 71 std::unique_ptr<net::FakeURLFetcher> CreateURLFetcher(
69 const GURL& url, 72 const GURL& url,
70 net::URLFetcherDelegate* delegate, 73 net::URLFetcherDelegate* delegate,
71 const std::string& response_data, 74 const std::string& response_data,
72 net::HttpStatusCode response_code, 75 net::HttpStatusCode response_code,
73 net::URLRequestStatus::Status status) { 76 net::URLRequestStatus::Status status) {
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 } 323 }
321 324
322 class PrecacheFetcherTest : public testing::Test { 325 class PrecacheFetcherTest : public testing::Test {
323 public: 326 public:
324 PrecacheFetcherTest() 327 PrecacheFetcherTest()
325 : request_context_(new net::TestURLRequestContextGetter( 328 : request_context_(new net::TestURLRequestContextGetter(
326 base::ThreadTaskRunnerHandle::Get())), 329 base::ThreadTaskRunnerHandle::Get())),
327 factory_(NULL, 330 factory_(NULL,
328 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, 331 base::Bind(&TestURLFetcherCallback::CreateURLFetcher,
329 base::Unretained(&url_callback_))), 332 base::Unretained(&url_callback_))),
330 expected_total_response_bytes_(0) {} 333 expected_total_response_bytes_(0),
334 parallel_fetches_beyond_capacity_(false) {}
331 335
332 protected: 336 protected:
333 void SetDefaultFlags() { 337 void SetDefaultFlags() {
334 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 338 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
335 switches::kPrecacheConfigSettingsURL, kConfigURL); 339 switches::kPrecacheConfigSettingsURL, kConfigURL);
336 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 340 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
337 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix); 341 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix);
338 } 342 }
339 343
344 // Posts a task to check if more parallel fetches of precache manifest and
345 // resource URLs were attempted beyond the fetcher pool maximum defined
346 // capacity. The task will be posted repeatedly until such condition is met.
347 void CheckUntilParallelFetchesBeyondCapacity(
348 const PrecacheFetcher* precache_fetcher) {
349 if (!precache_fetcher->pool_.IsAvailable() &&
350 !precache_fetcher->resource_urls_to_fetch_.empty() &&
351 !precache_fetcher->manifest_urls_to_fetch_.empty()) {
352 parallel_fetches_beyond_capacity_ = true;
353 return;
354 }
355
356 // Check again after allowing the message loop to process some messages.
357 loop_.PostTask(
358 FROM_HERE,
359 base::Bind(
360 &PrecacheFetcherTest::CheckUntilParallelFetchesBeyondCapacity,
361 base::Unretained(this), precache_fetcher));
362 }
363
340 base::MessageLoopForUI loop_; 364 base::MessageLoopForUI loop_;
341 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 365 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
342 TestURLFetcherCallback url_callback_; 366 TestURLFetcherCallback url_callback_;
343 net::FakeURLFetcherFactory factory_; 367 net::FakeURLFetcherFactory factory_;
344 TestPrecacheDelegate precache_delegate_; 368 TestPrecacheDelegate precache_delegate_;
345 int expected_total_response_bytes_; 369 int expected_total_response_bytes_;
370
371 // True if more parallel fetches were attempted beyond the fetcher pool
372 // maximum capacity.
373 bool parallel_fetches_beyond_capacity_;
346 }; 374 };
347 375
348 TEST_F(PrecacheFetcherTest, FullPrecache) { 376 TEST_F(PrecacheFetcherTest, FullPrecache) {
349 SetDefaultFlags(); 377 SetDefaultFlags();
350 378
351 std::vector<std::string> starting_hosts; 379 std::vector<std::string> starting_hosts;
352 starting_hosts.push_back("manifest-fetch-failure.com"); 380 starting_hosts.push_back("manifest-fetch-failure.com");
353 starting_hosts.push_back("bad-manifest.com"); 381 starting_hosts.push_back("bad-manifest.com");
354 starting_hosts.push_back("good-manifest.com"); 382 starting_hosts.push_back("good-manifest.com");
355 starting_hosts.push_back("not-in-top-3.com"); 383 starting_hosts.push_back("not-in-top-3.com");
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 EXPECT_GT(kNumResources, url_callback_.requested_urls().size()); 744 EXPECT_GT(kNumResources, url_callback_.requested_urls().size());
717 745
718 EXPECT_TRUE(precache_delegate_.was_on_done_called()); 746 EXPECT_TRUE(precache_delegate_.was_on_done_called());
719 747
720 // good-manifest.com will not have been completed. 748 // good-manifest.com will not have been completed.
721 EXPECT_THAT(histogram.GetAllSamples("Precache.Fetch.PercentCompleted"), 749 EXPECT_THAT(histogram.GetAllSamples("Precache.Fetch.PercentCompleted"),
722 ElementsAre(base::Bucket(0, 1))); 750 ElementsAre(base::Bucket(0, 1)));
723 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); 751 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1);
724 } 752 }
725 753
726 } // namespace 754 // Tests the parallel fetch behaviour when more precache resource and manifest
755 // requests are available than the maximum capacity of fetcher pool.
756 TEST_F(PrecacheFetcherTest, FetcherPoolMaxLimitReached) {
757 SetDefaultFlags();
758
759 const size_t kNumTopHosts = 5;
760 const size_t kNumResources = 15;
761
762 PrecacheConfigurationSettings config;
763 PrecacheManifest top_host_manifest[kNumTopHosts];
764 std::multiset<GURL> expected_requested_urls;
765
766 config.set_top_sites_count(kNumTopHosts);
767 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(),
768 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
769 expected_requested_urls.insert(GURL(kConfigURL));
770
771 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
772 new PrecacheUnfinishedWork());
773 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue());
774
775 for (size_t i = 0; i < kNumTopHosts; ++i) {
776 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i);
777 unfinished_work->add_top_host()->set_hostname(top_host_url);
778
779 for (size_t j = 0; j < kNumResources; ++j) {
780 const std::string resource_url =
781 base::StringPrintf("http://top-host-%zu.com/resource-%zu", i, j);
782 top_host_manifest[i].add_resource()->set_url(resource_url);
783 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK,
784 net::URLRequestStatus::SUCCESS);
785 expected_requested_urls.insert(GURL(resource_url));
786 }
787 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host_url),
788 top_host_manifest[i].SerializeAsString(),
789 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
790 expected_requested_urls.insert(GURL(kManifestURLPrefix + top_host_url));
791 }
792
793 base::HistogramTester histogram;
794
795 {
796 PrecacheFetcher precache_fetcher(request_context_.get(), GURL(),
797 std::string(), std::move(unfinished_work),
798 kExperimentID, &precache_delegate_);
799 precache_fetcher.Start();
800
801 EXPECT_GT(kNumResources, precache_fetcher.pool_.max_size());
802 CheckUntilParallelFetchesBeyondCapacity(&precache_fetcher);
803
804 loop_.RunUntilIdle();
805
806 // Destroy the PrecacheFetcher after it has finished, to record metrics.
807 }
808
809 EXPECT_TRUE(parallel_fetches_beyond_capacity_);
810
811 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
812
813 EXPECT_TRUE(precache_delegate_.was_on_done_called());
814
815 histogram.ExpectUniqueSample("Precache.Fetch.PercentCompleted", 100, 1);
816 histogram.ExpectUniqueSample("Precache.Fetch.ResponseBytes.Total",
817 url_callback_.total_response_bytes(), 1);
818 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1);
819 }
727 820
728 } // namespace precache 821 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/core/precache_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698