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

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

Issue 2335913002: Add daily quota for precache (Closed)
Patch Set: rebased Created 4 years, 2 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
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>
(...skipping 1368 matching lines...) Expand 10 before | Expand all | Expand 10 after
1379 request_context_.get(), GURL(), std::string(), 1379 request_context_.get(), GURL(), std::string(),
1380 std::move(cancelled_work), kExperimentID, 1380 std::move(cancelled_work), kExperimentID,
1381 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); 1381 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1382 precache_fetcher.Start(); 1382 precache_fetcher.Start();
1383 base::RunLoop().RunUntilIdle(); 1383 base::RunLoop().RunUntilIdle();
1384 } 1384 }
1385 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); 1385 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1386 EXPECT_TRUE(precache_delegate_.was_on_done_called()); 1386 EXPECT_TRUE(precache_delegate_.was_on_done_called());
1387 } 1387 }
1388 1388
1389 TEST_F(PrecacheFetcherTest, DailyQuota) {
1390 SetDefaultFlags();
1391
1392 const size_t kNumTopHosts = 3;
1393
1394 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
1395 new PrecacheUnfinishedWork());
1396 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue());
1397
1398 PrecacheConfigurationSettings config;
1399 config.set_top_sites_count(kNumTopHosts);
1400 config.set_daily_quota_total(10000);
1401 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(),
1402 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1403 std::multiset<GURL> expected_requested_urls;
1404 expected_requested_urls.insert(GURL(kConfigURL));
1405
1406 for (size_t i = 0; i < kNumTopHosts; ++i) {
1407 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i);
1408 const std::string resource_url =
1409 base::StringPrintf("http://top-host-%zu.com/resource.html", i);
1410 PrecacheManifest manifest;
1411 manifest.add_resource()->set_url(resource_url);
1412
1413 unfinished_work->add_top_host()->set_hostname(top_host_url);
1414 factory_.SetFakeResponse(
1415 GURL(std::string(kManifestURLPrefix) + top_host_url),
1416 manifest.SerializeAsString(), net::HTTP_OK,
1417 net::URLRequestStatus::SUCCESS);
1418 // Set a 5000 byte resource.
1419 factory_.SetFakeResponse(GURL(resource_url), std::string(5000, 'a'),
1420 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1421
1422 expected_requested_urls.insert(
1423 GURL((std::string(kManifestURLPrefix) + top_host_url)));
1424 expected_requested_urls.insert(GURL(resource_url));
1425 }
1426
1427 base::HistogramTester histogram;
1428
1429 {
1430 PrecacheFetcher precache_fetcher(
1431 request_context_.get(), GURL(), std::string(),
1432 std::move(unfinished_work), kExperimentID,
1433 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1434 precache_fetcher.Start();
1435
1436 base::RunLoop().RunUntilIdle();
1437
1438 EXPECT_EQ(0U, precache_fetcher.quota_.remaining());
1439 unfinished_work = precache_fetcher.CancelPrecaching();
1440 }
1441
1442 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1443
1444 EXPECT_TRUE(precache_delegate_.was_on_done_called());
1445
1446 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 1);
1447 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 1);
1448 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1);
1449
1450 // Continuing with the precache when quota limit is reached, will not fetch
1451 // any resources.
1452 expected_requested_urls.clear();
1453 url_callback_.clear_requested_urls();
1454 {
1455 PrecacheFetcher precache_fetcher(
1456 request_context_.get(), GURL(), std::string(),
1457 std::move(unfinished_work), kExperimentID,
1458 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1459 precache_fetcher.Start();
1460 base::RunLoop().RunUntilIdle();
1461
1462 EXPECT_EQ(0U, precache_fetcher.quota_.remaining());
1463 }
1464 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1465
1466 EXPECT_TRUE(precache_delegate_.was_on_done_called());
1467
1468 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2);
1469 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2);
1470 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2);
1471 }
1472
1389 } // namespace precache 1473 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/core/precache_fetcher.cc ('k') | components/precache/core/precache_session_table.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698