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

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

Issue 2335913002: Add daily quota for precache (Closed)
Patch Set: Addressed sclittle@ comments 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 1271 matching lines...) Expand 10 before | Expand all | Expand 10 after
1282 PrecacheFetcher precache_fetcher( 1282 PrecacheFetcher precache_fetcher(
1283 request_context_.get(), GURL(), std::string(), 1283 request_context_.get(), GURL(), std::string(),
1284 std::move(unfinished_work), kExperimentID, 1284 std::move(unfinished_work), kExperimentID,
1285 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); 1285 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1286 precache_fetcher.Start(); 1286 precache_fetcher.Start();
1287 1287
1288 base::RunLoop().RunUntilIdle(); 1288 base::RunLoop().RunUntilIdle();
1289 } 1289 }
1290 } 1290 }
1291 1291
1292 TEST_F(PrecacheFetcherTest, DailyQuota) {
1293 SetDefaultFlags();
1294
1295 const size_t kNumTopHosts = 3;
1296
1297 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
1298 new PrecacheUnfinishedWork());
1299 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue());
1300
1301 PrecacheConfigurationSettings config;
1302 config.set_top_sites_count(kNumTopHosts);
1303 config.set_daily_quota_total(10000);
1304 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(),
1305 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1306 std::multiset<GURL> expected_requested_urls;
1307 expected_requested_urls.insert(GURL(kConfigURL));
1308
1309 for (size_t i = 0; i < kNumTopHosts; ++i) {
1310 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i);
1311 const std::string resource_url =
1312 base::StringPrintf("http://top-host-%zu.com/resource.html", i);
1313 PrecacheManifest manifest;
1314 manifest.add_resource()->set_url(resource_url);
1315
1316 unfinished_work->add_top_host()->set_hostname(top_host_url);
1317 factory_.SetFakeResponse(
1318 GURL(std::string(kManifestURLPrefix) + top_host_url),
1319 manifest.SerializeAsString(), net::HTTP_OK,
1320 net::URLRequestStatus::SUCCESS);
1321 // Set a 5000 byte resource.
1322 factory_.SetFakeResponse(GURL(resource_url), std::string(5000, 'a'),
1323 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1324
1325 expected_requested_urls.insert(
1326 GURL((std::string(kManifestURLPrefix) + top_host_url)));
1327 expected_requested_urls.insert(GURL(resource_url));
1328 }
1329
1330 base::HistogramTester histogram;
1331
1332 {
1333 PrecacheFetcher precache_fetcher(
1334 request_context_.get(), GURL(), std::string(),
1335 std::move(unfinished_work), kExperimentID,
1336 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1337 precache_fetcher.Start();
1338
1339 base::RunLoop().RunUntilIdle();
1340
1341 EXPECT_EQ(0U, precache_fetcher.quota_.remaining());
1342 unfinished_work = precache_fetcher.CancelPrecaching();
1343 }
1344
1345 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1346
1347 EXPECT_TRUE(precache_delegate_.was_on_done_called());
1348
1349 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 1);
1350 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 1);
1351 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1);
1352
1353 // Continuing with the precache when quota limit is reached, will not fetch
1354 // any resources.
1355 expected_requested_urls.clear();
1356 url_callback_.clear_requested_urls();
1357 {
1358 PrecacheFetcher precache_fetcher(
1359 request_context_.get(), GURL(), std::string(),
1360 std::move(unfinished_work), kExperimentID,
1361 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1362 precache_fetcher.Start();
1363 base::RunLoop().RunUntilIdle();
1364
1365 EXPECT_EQ(0U, precache_fetcher.quota_.remaining());
1366 }
1367 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1368
1369 EXPECT_TRUE(precache_delegate_.was_on_done_called());
1370
1371 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2);
1372 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2);
1373 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2);
1374 }
1375
1292 } // namespace precache 1376 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698