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

Side by Side Diff: components/precache/content/precache_manager_unittest.cc

Issue 2507753003: Do not precache when the cache size is small (Closed)
Patch Set: scoped_refptr Created 4 years, 1 month 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 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/content/precache_manager.h" 5 #include "components/precache/content/precache_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 11 matching lines...) Expand all
22 #include "base/single_thread_task_runner.h" 22 #include "base/single_thread_task_runner.h"
23 #include "base/test/histogram_tester.h" 23 #include "base/test/histogram_tester.h"
24 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
25 #include "components/history/core/browser/history_constants.h" 25 #include "components/history/core/browser/history_constants.h"
26 #include "components/history/core/browser/history_service.h" 26 #include "components/history/core/browser/history_service.h"
27 #include "components/history/core/browser/history_types.h" 27 #include "components/history/core/browser/history_types.h"
28 #include "components/precache/core/precache_database.h" 28 #include "components/precache/core/precache_database.h"
29 #include "components/precache/core/precache_switches.h" 29 #include "components/precache/core/precache_switches.h"
30 #include "components/precache/core/proto/unfinished_work.pb.h" 30 #include "components/precache/core/proto/unfinished_work.pb.h"
31 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/storage_partition.h"
32 #include "content/public/test/test_browser_context.h" 33 #include "content/public/test/test_browser_context.h"
33 #include "content/public/test/test_browser_thread_bundle.h" 34 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "net/base/test_completion_callback.h"
36 #include "net/disk_cache/simple/simple_backend_impl.h"
34 #include "net/http/http_response_headers.h" 37 #include "net/http/http_response_headers.h"
35 #include "net/http/http_response_info.h" 38 #include "net/http/http_response_info.h"
36 #include "net/http/http_status_code.h" 39 #include "net/http/http_status_code.h"
40 #include "net/test/gtest_util.h"
37 #include "net/url_request/test_url_fetcher_factory.h" 41 #include "net/url_request/test_url_fetcher_factory.h"
38 #include "net/url_request/url_request_status.h" 42 #include "net/url_request/url_request_status.h"
39 #include "net/url_request/url_request_test_util.h" 43 #include "net/url_request/url_request_test_util.h"
40 #include "testing/gmock/include/gmock/gmock.h" 44 #include "testing/gmock/include/gmock/gmock.h"
41 #include "testing/gtest/include/gtest/gtest.h" 45 #include "testing/gtest/include/gtest/gtest.h"
42 #include "url/gurl.h" 46 #include "url/gurl.h"
43 47
44 namespace precache { 48 namespace precache {
45 49
46 namespace { 50 namespace {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 // should still complete normally in this case. 189 // should still complete normally in this case.
186 factory_.SetFakeResponse(GURL(kConfigURL), "", 190 factory_.SetFakeResponse(GURL(kConfigURL), "",
187 net::HTTP_INTERNAL_SERVER_ERROR, 191 net::HTTP_INTERNAL_SERVER_ERROR,
188 net::URLRequestStatus::FAILED); 192 net::URLRequestStatus::FAILED);
189 precache_manager_.reset( 193 precache_manager_.reset(
190 new PrecacheManagerUnderTest( 194 new PrecacheManagerUnderTest(
191 &browser_context_, nullptr /* sync_service */, 195 &browser_context_, nullptr /* sync_service */,
192 &history_service_, db_path, std::move(precache_database))); 196 &history_service_, db_path, std::move(precache_database)));
193 base::RunLoop().RunUntilIdle(); 197 base::RunLoop().RunUntilIdle();
194 198
199 // Allow caches of any size to be used.
200 precache_manager_->min_cache_size_bytes_ = 0;
201
195 info_.headers = new net::HttpResponseHeaders(""); 202 info_.headers = new net::HttpResponseHeaders("");
196 } 203 }
197 204
198 void RecordStatsForPrecacheFetch(const GURL& url, 205 void RecordStatsForPrecacheFetch(const GURL& url,
199 const std::string& referrer_host, 206 const std::string& referrer_host,
200 const base::TimeDelta& latency, 207 const base::TimeDelta& latency,
201 const base::Time& fetch_time, 208 const base::Time& fetch_time,
202 const net::HttpResponseInfo& info, 209 const net::HttpResponseInfo& info,
203 int64_t size) { 210 int64_t size) {
204 precache_manager_->RecordStatsForFetch(url, GURL(referrer_host), latency, 211 precache_manager_->RecordStatsForFetch(url, GURL(referrer_host), latency,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. 249 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher.
243 EXPECT_FALSE(precache_manager_->IsPrecaching()); 250 EXPECT_FALSE(precache_manager_->IsPrecaching());
244 EXPECT_TRUE(precache_callback_.was_on_done_called()); 251 EXPECT_TRUE(precache_callback_.was_on_done_called());
245 252
246 std::multiset<GURL> expected_requested_urls; 253 std::multiset<GURL> expected_requested_urls;
247 expected_requested_urls.insert(GURL(kConfigURL)); 254 expected_requested_urls.insert(GURL(kConfigURL));
248 expected_requested_urls.insert(GURL(kGoodManifestURL)); 255 expected_requested_urls.insert(GURL(kGoodManifestURL));
249 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); 256 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
250 } 257 }
251 258
259 TEST_F(PrecacheManagerTest, StartPrecachingWithGoodSizedCache) {
260 precache_manager_->min_cache_size_bytes_ = 1;
261
262 // Let's store something in the cache so we pass the min_cache_size_bytes_
263 // threshold.
264 disk_cache::Backend* cache_backend;
265 {
266 // Get the CacheBackend.
267 net::TestCompletionCallback cb;
268 net::HttpCache* cache =
269 content::BrowserContext::GetDefaultStoragePartition(&browser_context_)
270 ->GetURLRequestContext()
271 ->GetURLRequestContext()
272 ->http_transaction_factory()
273 ->GetCache();
274 CHECK_NE(nullptr, cache);
275 int rv = cache->GetBackend(&cache_backend, cb.callback());
276 CHECK_EQ(net::OK, cb.GetResult(rv));
277 CHECK_NE(nullptr, cache_backend);
278 CHECK_EQ(cache_backend, cache->GetCurrentBackend());
279 }
280 disk_cache::Entry* entry = nullptr;
281 {
282 // Create a cache Entry.
283 net::TestCompletionCallback cb;
284 int rv = cache_backend->CreateEntry("key", &entry, cb.callback());
285 CHECK_EQ(net::OK, cb.GetResult(rv));
286 CHECK_NE(nullptr, entry);
287 }
288 {
289 // Store some data in the cache Entry.
290 const std::string data(precache_manager_->min_cache_size_bytes_, 'a');
291 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data));
292 net::TestCompletionCallback cb;
293 int rv = entry->WriteData(0, 0, buffer.get(), buffer->size(), cb.callback(),
294 false);
295 entry->Close();
296 CHECK_EQ(buffer->size(), cb.GetResult(rv));
297 }
298 {
299 // Make sure everything went according to plan.
300 net::TestCompletionCallback cb;
301 int rv = cache_backend->CalculateSizeOfAllEntries(cb.callback());
302 CHECK_LE(precache_manager_->min_cache_size_bytes_, cb.GetResult(rv));
303 }
304 EXPECT_FALSE(precache_manager_->IsPrecaching());
305
306 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
307 base::RunLoop().RunUntilIdle();
308
309 EXPECT_TRUE(precache_manager_->IsPrecaching());
310 // Now it should be waiting for the top hosts.
311 }
312
313 TEST_F(PrecacheManagerTest, StartPrecachingStopsOnSmallCaches) {
314 // We don't have any entry in the cache, so the reported cache_size = 0 and
315 // thus it will fall below the threshold of 1.
316 precache_manager_->min_cache_size_bytes_ = 1;
317 EXPECT_FALSE(precache_manager_->IsPrecaching());
318
319 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
320 base::RunLoop().RunUntilIdle();
321
322 EXPECT_FALSE(precache_manager_->IsPrecaching());
323 EXPECT_TRUE(precache_callback_.was_on_done_called());
324 EXPECT_TRUE(url_callback_.requested_urls().empty());
325 }
326
252 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) { 327 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) {
253 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( 328 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
254 new PrecacheUnfinishedWork()); 329 new PrecacheUnfinishedWork());
255 unfinished_work->add_top_host()->set_hostname("evil-manifest.com"); 330 unfinished_work->add_top_host()->set_hostname("evil-manifest.com");
256 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); 331 unfinished_work->set_start_time(base::Time::Now().ToInternalValue());
257 precache_database_->SaveUnfinishedWork(std::move(unfinished_work)); 332 precache_database_->SaveUnfinishedWork(std::move(unfinished_work));
258 333
259 EXPECT_FALSE(precache_manager_->IsPrecaching()); 334 EXPECT_FALSE(precache_manager_->IsPrecaching());
260 335
261 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK, 336 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 good_manifest.add_resource()->set_url("http://good-resource.com"); 419 good_manifest.add_resource()->set_url("http://good-resource.com");
345 420
346 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _)) 421 EXPECT_CALL(history_service_, TopHosts(NumTopHosts(), _))
347 .WillOnce(ReturnHosts( 422 .WillOnce(ReturnHosts(
348 history::TopHostsList(1, std::make_pair("starting-url.com", 1)))); 423 history::TopHostsList(1, std::make_pair("starting-url.com", 1))));
349 424
350 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 425 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
351 426
352 EXPECT_TRUE(precache_manager_->IsPrecaching()); 427 EXPECT_TRUE(precache_manager_->IsPrecaching());
353 // Run a task to get unfinished work, and to get hosts. 428 // Run a task to get unfinished work, and to get hosts.
354 for (int i = 0; i < 2; ++i) { 429 // We need to call run_loop.Run as many times as needed to go through the
430 // chain of callbacks :-(.
431 for (int i = 0; i < 4; ++i) {
355 base::RunLoop run_loop; 432 base::RunLoop run_loop;
356 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 433 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
357 run_loop.QuitClosure()); 434 run_loop.QuitClosure());
358 run_loop.Run(); 435 run_loop.Run();
359 } 436 }
360 // base::RunLoop().RunUntilIdle(); 437 // base::RunLoop().RunUntilIdle();
361 precache_manager_->CancelPrecaching(); 438 precache_manager_->CancelPrecaching();
362 EXPECT_FALSE(precache_manager_->IsPrecaching()); 439 EXPECT_FALSE(precache_manager_->IsPrecaching());
363 440
364 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. 441 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 EXPECT_TRUE(precache_manager_->IsPrecaching()); 484 EXPECT_TRUE(precache_manager_->IsPrecaching());
408 RecordStatsForPrecacheFetch(GURL("http://url.com"), std::string(), 485 RecordStatsForPrecacheFetch(GURL("http://url.com"), std::string(),
409 base::TimeDelta(), base::Time(), info_, 1000); 486 base::TimeDelta(), base::Time(), info_, 1000);
410 base::RunLoop().RunUntilIdle(); 487 base::RunLoop().RunUntilIdle();
411 precache_manager_->CancelPrecaching(); 488 precache_manager_->CancelPrecaching();
412 489
413 // For PrecacheFetcher and RecordURLPrecached. 490 // For PrecacheFetcher and RecordURLPrecached.
414 base::RunLoop().RunUntilIdle(); 491 base::RunLoop().RunUntilIdle();
415 EXPECT_THAT( 492 EXPECT_THAT(
416 histograms_.GetTotalCountsForPrefix("Precache."), 493 histograms_.GetTotalCountsForPrefix("Precache."),
417 UnorderedElementsAre(Pair("Precache.DownloadedPrecacheMotivated", 1), 494 UnorderedElementsAre(Pair("Precache.CacheSize.AllEntries", 1),
495 Pair("Precache.DownloadedPrecacheMotivated", 1),
418 Pair("Precache.Fetch.PercentCompleted", 1), 496 Pair("Precache.Fetch.PercentCompleted", 1),
419 Pair("Precache.Fetch.ResponseBytes.Network", 1), 497 Pair("Precache.Fetch.ResponseBytes.Network", 1),
420 Pair("Precache.Fetch.ResponseBytes.Total", 1), 498 Pair("Precache.Fetch.ResponseBytes.Total", 1),
421 Pair("Precache.Fetch.TimeToComplete", 1), 499 Pair("Precache.Fetch.TimeToComplete", 1),
422 Pair("Precache.Latency.Prefetch", 1), 500 Pair("Precache.Latency.Prefetch", 1),
423 Pair("Precache.Freshness.Prefetch", 1))); 501 Pair("Precache.Freshness.Prefetch", 1)));
424 } 502 }
425 503
426 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) { 504 TEST_F(PrecacheManagerTest, RecordStatsForFetchHTTP) {
427 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(), 505 precache_manager_->RecordStatsForFetch(GURL("http://http-url.com"), GURL(),
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 // Precache a bunch of URLs, with different fetch times. 565 // Precache a bunch of URLs, with different fetch times.
488 RecordStatsForPrecacheFetch( 566 RecordStatsForPrecacheFetch(
489 GURL("http://old-fetch.com"), std::string(), base::TimeDelta(), 567 GURL("http://old-fetch.com"), std::string(), base::TimeDelta(),
490 kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000); 568 kCurrentTime - base::TimeDelta::FromDays(61), info_, 1000);
491 RecordStatsForPrecacheFetch( 569 RecordStatsForPrecacheFetch(
492 GURL("http://recent-fetch.com"), std::string(), base::TimeDelta(), 570 GURL("http://recent-fetch.com"), std::string(), base::TimeDelta(),
493 kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000); 571 kCurrentTime - base::TimeDelta::FromDays(59), info_, 1000);
494 RecordStatsForPrecacheFetch( 572 RecordStatsForPrecacheFetch(
495 GURL("http://yesterday-fetch.com"), std::string(), base::TimeDelta(), 573 GURL("http://yesterday-fetch.com"), std::string(), base::TimeDelta(),
496 kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000); 574 kCurrentTime - base::TimeDelta::FromDays(1), info_, 1000);
575 expected_histogram_count_map["Precache.CacheSize.AllEntries"]++;
497 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3; 576 expected_histogram_count_map["Precache.DownloadedPrecacheMotivated"] += 3;
498 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; 577 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++;
499 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; 578 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++;
500 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; 579 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++;
501 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; 580 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++;
502 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3; 581 expected_histogram_count_map["Precache.Latency.Prefetch"] += 3;
503 expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3; 582 expected_histogram_count_map["Precache.Freshness.Prefetch"] += 3;
504 base::RunLoop().RunUntilIdle(); 583 base::RunLoop().RunUntilIdle();
505 584
506 precache_manager_->CancelPrecaching(); 585 precache_manager_->CancelPrecaching();
507 base::RunLoop().RunUntilIdle(); 586 base::RunLoop().RunUntilIdle();
508 587
509 // Disable pause-resume. 588 // Disable pause-resume.
510 precache_database_->DeleteUnfinishedWork(); 589 precache_database_->DeleteUnfinishedWork();
511 base::RunLoop().RunUntilIdle(); 590 base::RunLoop().RunUntilIdle();
512 591
513 // For PrecacheFetcher and RecordURLPrecached. 592 // For PrecacheFetcher and RecordURLPrecached.
514 base::RunLoop().RunUntilIdle(); 593 base::RunLoop().RunUntilIdle();
515 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), 594 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
516 ContainerEq(expected_histogram_count_map)); 595 ContainerEq(expected_histogram_count_map));
517 596
518 // The expired precache will be deleted during precaching this time. 597 // The expired precache will be deleted during precaching this time.
519 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); 598 precache_manager_->StartPrecaching(precache_callback_.GetCallback());
520 EXPECT_TRUE(precache_manager_->IsPrecaching()); 599 EXPECT_TRUE(precache_manager_->IsPrecaching());
521 base::RunLoop().RunUntilIdle(); 600 base::RunLoop().RunUntilIdle();
522 601
523 // The precache fetcher runs until done, which records these histograms, 602 // The precache fetcher runs until done, which records these histograms,
524 // and then cancels precaching, which records these histograms again. 603 // and then cancels precaching, which records these histograms again.
525 // In practice 604 // In practice
605 expected_histogram_count_map["Precache.CacheSize.AllEntries"]++;
526 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++; 606 expected_histogram_count_map["Precache.Fetch.PercentCompleted"]++;
527 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++; 607 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Network"]++;
528 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++; 608 expected_histogram_count_map["Precache.Fetch.ResponseBytes.Total"]++;
529 // For PrecacheFetcher and RecordURLPrecached. 609 // For PrecacheFetcher and RecordURLPrecached.
530 base::RunLoop().RunUntilIdle(); 610 base::RunLoop().RunUntilIdle();
531 EXPECT_FALSE(precache_manager_->IsPrecaching()); 611 EXPECT_FALSE(precache_manager_->IsPrecaching());
532 612
533 // A fetch for the same URL as the expired precache was served from the cache, 613 // A fetch for the same URL as the expired precache was served from the cache,
534 // but it isn't reported as saved bytes because it had expired in the precache 614 // but it isn't reported as saved bytes because it had expired in the precache
535 // history. 615 // history.
(...skipping 27 matching lines...) Expand all
563 expected_histogram_count_map["Precache.Saved"] += 2; 643 expected_histogram_count_map["Precache.Saved"] += 2;
564 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; 644 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2;
565 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; 645 expected_histogram_count_map["Precache.Saved.Freshness"] = 2;
566 646
567 base::RunLoop().RunUntilIdle(); 647 base::RunLoop().RunUntilIdle();
568 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), 648 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."),
569 ContainerEq(expected_histogram_count_map)); 649 ContainerEq(expected_histogram_count_map));
570 } 650 }
571 651
572 } // namespace precache 652 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698