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

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

Issue 2586813004: Report downloaded resources at most once (Closed)
Patch Set: Improved tests Created 4 years 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 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 FROM_HERE, 408 FROM_HERE,
409 base::Bind( 409 base::Bind(
410 &PrecacheFetcherTest::CheckUntilParallelFetchesBeyondCapacity, 410 &PrecacheFetcherTest::CheckUntilParallelFetchesBeyondCapacity,
411 base::Unretained(this), precache_fetcher)); 411 base::Unretained(this), precache_fetcher));
412 } 412 }
413 413
414 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const { 414 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const {
415 return task_runner_; 415 return task_runner_;
416 } 416 }
417 417
418 // To allow friend access.
419 void Flush() { precache_database_.Flush(); }
420
418 // Must be declared first so that it is destroyed last. 421 // Must be declared first so that it is destroyed last.
419 base::MessageLoopForUI loop_; 422 base::MessageLoopForUI loop_;
420 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 423 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
421 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 424 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
422 TestURLFetcherCallback url_callback_; 425 TestURLFetcherCallback url_callback_;
423 net::FakeURLFetcherFactory factory_; 426 net::FakeURLFetcherFactory factory_;
424 TestPrecacheDelegate precache_delegate_; 427 TestPrecacheDelegate precache_delegate_;
425 base::ScopedTempDir scoped_temp_dir_; 428 base::ScopedTempDir scoped_temp_dir_;
426 PrecacheDatabase precache_database_; 429 PrecacheDatabase precache_database_;
427 int expected_total_response_bytes_; 430 int expected_total_response_bytes_;
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 {GURL("http://used-resource-1/b.js")})); 1237 {GURL("http://used-resource-1/b.js")}));
1235 EXPECT_EQ("CmvACGJ4k08=", PrecacheFetcher::GetResourceURLBase64HashForTesting( 1238 EXPECT_EQ("CmvACGJ4k08=", PrecacheFetcher::GetResourceURLBase64HashForTesting(
1236 {GURL("http://used-resource-1/c.js")})); 1239 {GURL("http://used-resource-1/c.js")}));
1237 1240
1238 EXPECT_EQ("dVSI/sC1cGkH8lzom+6xlA==", 1241 EXPECT_EQ("dVSI/sC1cGkH8lzom+6xlA==",
1239 PrecacheFetcher::GetResourceURLBase64HashForTesting( 1242 PrecacheFetcher::GetResourceURLBase64HashForTesting(
1240 {GURL("http://used-resource-1/a.js"), 1243 {GURL("http://used-resource-1/a.js"),
1241 GURL("http://used-resource-1/b.js")})); 1244 GURL("http://used-resource-1/b.js")}));
1242 } 1245 }
1243 1246
1244 TEST_F(PrecacheFetcherTest, SendUsedUnusedResourceHash) { 1247 TEST_F(PrecacheFetcherTest, SendUsedDownloadedResourceHash) {
1245 SetDefaultFlags(); 1248 SetDefaultFlags();
1246 1249
1247 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( 1250 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
1248 new PrecacheUnfinishedWork()); 1251 new PrecacheUnfinishedWork());
1249 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); 1252 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue());
1250 unfinished_work->add_top_host()->set_hostname("top-host-1.com"); 1253 unfinished_work->add_top_host()->set_hostname("top-host-1.com");
1251 unfinished_work->add_top_host()->set_hostname("top-host-2.com"); 1254 unfinished_work->add_top_host()->set_hostname("top-host-2.com");
1252 unfinished_work->add_top_host()->set_hostname("top-host-3.com"); 1255 unfinished_work->add_top_host()->set_hostname("top-host-3.com");
1253 1256
1254 UpdatePrecacheReferrerHost("top-host-1.com", 1001); 1257 UpdatePrecacheReferrerHost("top-host-1.com", 1001);
(...skipping 16 matching lines...) Expand all
1271 factory_.SetFakeResponse(GURL(kConfigURL), std::string(), net::HTTP_OK, 1274 factory_.SetFakeResponse(GURL(kConfigURL), std::string(), net::HTTP_OK,
1272 net::URLRequestStatus::SUCCESS); 1275 net::URLRequestStatus::SUCCESS);
1273 factory_.SetFakeResponse( 1276 factory_.SetFakeResponse(
1274 GURL(std::string(kManifestURLPrefix) + 1277 GURL(std::string(kManifestURLPrefix) +
1275 "top-host-1.com?manifest=1001&used_resources=" + 1278 "top-host-1.com?manifest=1001&used_resources=" +
1276 net::EscapeQueryParamValue( 1279 net::EscapeQueryParamValue(
1277 PrecacheFetcher::GetResourceURLBase64HashForTesting( 1280 PrecacheFetcher::GetResourceURLBase64HashForTesting(
1278 {GURL("http://used-resource-1/a.js"), 1281 {GURL("http://used-resource-1/a.js"),
1279 GURL("http://used-resource-1/b.js")}), 1282 GURL("http://used-resource-1/b.js")}),
1280 true) + 1283 true) +
1281 "&unused_resources=" + 1284 "&d=" + net::EscapeQueryParamValue(
1282 net::EscapeQueryParamValue( 1285 PrecacheFetcher::GetResourceURLBase64HashForTesting(
1283 PrecacheFetcher::GetResourceURLBase64HashForTesting( 1286 {GURL("http://used-resource-1/a.js"),
1284 {GURL("http://unused-resource-1/c.js")}), 1287 GURL("http://used-resource-1/b.js"),
1285 true)), 1288 GURL("http://unused-resource-1/c.js")}),
1289 true)),
1286 std::string(), net::HTTP_OK, net::URLRequestStatus::SUCCESS); 1290 std::string(), net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1287 factory_.SetFakeResponse( 1291 factory_.SetFakeResponse(
1288 GURL(std::string(kManifestURLPrefix) + 1292 GURL(std::string(kManifestURLPrefix) +
1289 "top-host-2.com?manifest=1002&used_resources=&unused_resources=" + 1293 "top-host-2.com?manifest=1002&used_resources=&d=" +
1290 net::EscapeQueryParamValue( 1294 net::EscapeQueryParamValue(
1291 PrecacheFetcher::GetResourceURLBase64HashForTesting( 1295 PrecacheFetcher::GetResourceURLBase64HashForTesting(
1292 {GURL("http://unused-resource-2/a.js"), 1296 {GURL("http://unused-resource-2/a.js"),
1293 GURL("http://unused-resource-2/b.js")}), 1297 GURL("http://unused-resource-2/b.js")}),
1294 true)), 1298 true)),
1295 std::string(), net::HTTP_OK, net::URLRequestStatus::SUCCESS); 1299 std::string(), net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1296 factory_.SetFakeResponse( 1300 factory_.SetFakeResponse(
1297 GURL(std::string(kManifestURLPrefix) + 1301 GURL(std::string(kManifestURLPrefix) +
1298 "top-host-3.com?manifest=1003&used_resources=&unused_resources="), 1302 "top-host-3.com?manifest=1003&used_resources=&d="),
1299 std::string(), net::HTTP_OK, net::URLRequestStatus::SUCCESS); 1303 std::string(), net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1300 1304
1301 { 1305 {
1302 PrecacheFetcher precache_fetcher( 1306 PrecacheFetcher precache_fetcher(
1303 request_context_.get(), GURL(), std::string(), 1307 request_context_.get(), GURL(), std::string(),
1304 std::move(unfinished_work), kExperimentID, 1308 std::move(unfinished_work), kExperimentID,
1305 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); 1309 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1306 precache_fetcher.Start(); 1310 precache_fetcher.Start();
1307 1311
1308 base::RunLoop().RunUntilIdle(); 1312 base::RunLoop().RunUntilIdle();
1309 } 1313 }
1314
1315 // If we run the precache again, no download should be reported.
1316 factory_.ClearFakeResponses();
1317 factory_.SetFakeResponse(GURL(kConfigURL), std::string(), net::HTTP_OK,
1318 net::URLRequestStatus::SUCCESS);
1319 // Since we returned an empty proto, the manifest id was set to 0.
1320 // The d='s are empty because precache fetches are tried first solely from the
1321 // cache and, since any matching request to the fake factory succeeds, it is
1322 // hardcoded to be cached even though we didn't specify it as such in the fake
1323 // response.
1324 factory_.SetFakeResponse(GURL(std::string(kManifestURLPrefix) +
1325 "top-host-1.com?manifest=0&used_resources=&d="),
1326 std::string(), net::HTTP_OK,
1327 net::URLRequestStatus::SUCCESS);
1328 factory_.SetFakeResponse(GURL(std::string(kManifestURLPrefix) +
1329 "top-host-2.com?manifest=0&used_resources=&d="),
1330 std::string(), net::HTTP_OK,
1331 net::URLRequestStatus::SUCCESS);
1332 factory_.SetFakeResponse(GURL(std::string(kManifestURLPrefix) +
1333 "top-host-3.com?manifest=0&used_resources=&d="),
1334 std::string(), net::HTTP_OK,
1335 net::URLRequestStatus::SUCCESS);
1336 // Flush so that previous UpdatePrecacheReferrerHost calls make it through.
1337 // Otherwise, manifest_id may be non 0 for some of the hosts.
1338 Flush();
1339 {
1340 std::unique_ptr<PrecacheUnfinishedWork> more_work(
1341 new PrecacheUnfinishedWork());
1342 more_work->set_start_time(base::Time::UnixEpoch().ToInternalValue());
1343 more_work->add_top_host()->set_hostname("top-host-1.com");
1344 more_work->add_top_host()->set_hostname("top-host-2.com");
1345 more_work->add_top_host()->set_hostname("top-host-3.com");
1346 PrecacheFetcher precache_fetcher(
1347 request_context_.get(), GURL(), std::string(), std::move(more_work),
1348 kExperimentID, precache_database_.GetWeakPtr(), task_runner(),
1349 &precache_delegate_);
1350 precache_fetcher.Start();
1351
1352 base::RunLoop().RunUntilIdle();
1353 }
1310 } 1354 }
1311 1355
1312 TEST_F(PrecacheFetcherTest, GloballyRankResources) { 1356 TEST_F(PrecacheFetcherTest, GloballyRankResources) {
1313 SetDefaultFlags(); 1357 SetDefaultFlags();
1314 1358
1315 const size_t kNumTopHosts = 5; 1359 const size_t kNumTopHosts = 5;
1316 const size_t kNumResources = 5; 1360 const size_t kNumResources = 5;
1317 1361
1318 std::vector<GURL> expected_requested_urls; 1362 std::vector<GURL> expected_requested_urls;
1319 1363
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
1793 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); 1837 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1794 1838
1795 EXPECT_TRUE(precache_delegate_.was_on_done_called()); 1839 EXPECT_TRUE(precache_delegate_.was_on_done_called());
1796 1840
1797 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); 1841 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2);
1798 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); 1842 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2);
1799 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); 1843 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2);
1800 } 1844 }
1801 1845
1802 } // namespace precache 1846 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/core/precache_fetcher.cc ('k') | components/precache/core/precache_url_table.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698