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

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

Issue 2300703004: Fix precache issue with cancel and resume (Closed)
Patch Set: Addressed sclittle comments Created 4 years, 3 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 // Tests cancel precaching when all tophost manifests are fetched, but some
1293 // resource fetches are pending.
1294 TEST_F(PrecacheFetcherTest, CancelPrecachingAfterAllManifestFetch) {
1295 SetDefaultFlags();
1296
1297 const size_t kNumTopHosts = 5;
1298 const size_t kNumResources = 5;
1299 const size_t kMaxParallelFetches = 10;
1300
1301 PrecacheConfigurationSettings config;
1302 PrecacheManifest top_host_manifest[kNumTopHosts];
1303 std::multiset<GURL> expected_requested_urls;
1304 std::unique_ptr<PrecacheUnfinishedWork> cancelled_work;
1305
1306 config.set_top_sites_count(kNumTopHosts);
1307 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(),
1308 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1309 expected_requested_urls.insert(GURL(kConfigURL));
1310
1311 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
1312 new PrecacheUnfinishedWork());
1313 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue());
1314
1315 for (size_t i = 0; i < kNumTopHosts; ++i) {
1316 const std::string top_host_url = base::StringPrintf("top-host-%zu.com", i);
1317 unfinished_work->add_top_host()->set_hostname(top_host_url);
1318
1319 for (size_t j = 0; j < kNumResources; ++j) {
1320 const std::string resource_url =
1321 base::StringPrintf("http://top-host-%zu.com/resource-%zu", i, j);
1322 top_host_manifest[i].add_resource()->set_url(resource_url);
1323 factory_.SetFakeResponse(GURL(resource_url), "good", net::HTTP_OK,
1324 net::URLRequestStatus::SUCCESS);
1325 if (i < kNumTopHosts - 1)
1326 expected_requested_urls.insert(GURL(resource_url));
1327 }
1328 factory_.SetFakeResponse(GURL(kManifestURLPrefix + top_host_url),
1329 top_host_manifest[i].SerializeAsString(),
1330 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
1331 expected_requested_urls.insert(GURL(kManifestURLPrefix + top_host_url));
1332 }
1333
1334 {
1335 uint32_t remaining_tries = 100;
1336 PrecacheFetcher precache_fetcher(
1337 request_context_.get(), GURL(), std::string(),
1338 std::move(unfinished_work), kExperimentID,
1339 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1340 precache_fetcher.Start();
1341
1342 // Run the loop until all tophost manifest fetches are complete, but some
1343 // resource fetches are pending.
1344 while (--remaining_tries != 0 &&
1345 (!precache_fetcher.top_hosts_to_fetch_.empty() ||
1346 !precache_fetcher.unfinished_work_->has_config_settings() ||
1347 precache_fetcher.resources_to_fetch_.empty())) {
1348 base::RunLoop run_loop;
1349 loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
1350 run_loop.Run();
1351 }
1352
1353 // Cancel precaching.
1354 cancelled_work = precache_fetcher.CancelPrecaching();
1355 EXPECT_TRUE(precache_fetcher.top_hosts_to_fetch_.empty());
1356 EXPECT_TRUE(precache_fetcher.resources_to_fetch_.empty());
1357 }
1358 EXPECT_NE(cancelled_work, nullptr);
1359 EXPECT_TRUE(cancelled_work->top_host().empty());
1360 EXPECT_EQ(static_cast<size_t>(cancelled_work->resource().size()),
1361 kMaxParallelFetches + kNumResources);
1362
1363 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1364
1365 EXPECT_FALSE(precache_delegate_.was_on_done_called());
1366
1367 // Continuing with the precache should only fetch the pending resources in the
1368 // previous run.
1369 expected_requested_urls.clear();
1370 url_callback_.clear_requested_urls();
1371 for (size_t i = 2; i < kNumTopHosts; ++i) {
1372 for (size_t j = 0; j < kNumResources; ++j) {
1373 expected_requested_urls.insert(GURL(
1374 base::StringPrintf("http://top-host-%zu.com/resource-%zu", i, j)));
1375 }
1376 }
1377 {
1378 PrecacheFetcher precache_fetcher(
1379 request_context_.get(), GURL(), std::string(),
1380 std::move(cancelled_work), kExperimentID,
1381 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_);
1382 precache_fetcher.Start();
1383 base::RunLoop().RunUntilIdle();
1384 }
1385 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());
1386 EXPECT_TRUE(precache_delegate_.was_on_done_called());
1387 }
1388
1292 } // namespace precache 1389 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/core/precache_fetcher.cc ('k') | components/precache/core/precache_url_table.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698