OLD | NEW |
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> |
11 #include <set> | 11 #include <set> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/callback.h" | 16 #include "base/callback.h" |
17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
19 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
20 #include "base/files/scoped_temp_dir.h" | 20 #include "base/files/scoped_temp_dir.h" |
21 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
22 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
23 #include "base/memory/weak_ptr.h" | 23 #include "base/memory/weak_ptr.h" |
| 24 #include "base/rand_util.h" |
24 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
25 #include "base/single_thread_task_runner.h" | 26 #include "base/single_thread_task_runner.h" |
26 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
27 #include "base/test/histogram_tester.h" | 28 #include "base/test/histogram_tester.h" |
28 #include "base/threading/thread_task_runner_handle.h" | 29 #include "base/threading/thread_task_runner_handle.h" |
29 #include "components/precache/core/precache_database.h" | 30 #include "components/precache/core/precache_database.h" |
30 #include "components/precache/core/precache_switches.h" | 31 #include "components/precache/core/precache_switches.h" |
31 #include "components/precache/core/proto/precache.pb.h" | 32 #include "components/precache/core/proto/precache.pb.h" |
32 #include "components/precache/core/proto/unfinished_work.pb.h" | 33 #include "components/precache/core/proto/unfinished_work.pb.h" |
33 #include "net/base/escape.h" | 34 #include "net/base/escape.h" |
(...skipping 1248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1282 PrecacheFetcher precache_fetcher( | 1283 PrecacheFetcher precache_fetcher( |
1283 request_context_.get(), GURL(), std::string(), | 1284 request_context_.get(), GURL(), std::string(), |
1284 std::move(unfinished_work), kExperimentID, | 1285 std::move(unfinished_work), kExperimentID, |
1285 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); | 1286 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
1286 precache_fetcher.Start(); | 1287 precache_fetcher.Start(); |
1287 | 1288 |
1288 base::RunLoop().RunUntilIdle(); | 1289 base::RunLoop().RunUntilIdle(); |
1289 } | 1290 } |
1290 } | 1291 } |
1291 | 1292 |
| 1293 TEST_F(PrecacheFetcherTest, DailyQuota) { |
| 1294 SetDefaultFlags(); |
| 1295 |
| 1296 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
| 1297 new PrecacheUnfinishedWork()); |
| 1298 unfinished_work->set_start_time(base::Time::UnixEpoch().ToInternalValue()); |
| 1299 unfinished_work->add_top_host()->set_hostname("top-host-1.com"); |
| 1300 unfinished_work->add_top_host()->set_hostname("top-host-2.com"); |
| 1301 unfinished_work->add_top_host()->set_hostname("top-host-3.com"); |
| 1302 |
| 1303 PrecacheConfigurationSettings config; |
| 1304 config.set_top_sites_count(3); |
| 1305 config.set_daily_quota_total(10000000); |
| 1306 |
| 1307 PrecacheManifest good_manifest; |
| 1308 good_manifest.add_resource()->set_url(kGoodResourceURL); |
| 1309 std::string resource_data = base::RandBytesAsString(5000000); |
| 1310 |
| 1311 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), |
| 1312 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 1313 factory_.SetFakeResponse( |
| 1314 GURL(std::string(kManifestURLPrefix) + "top-host-1.com"), |
| 1315 good_manifest.SerializeAsString(), net::HTTP_OK, |
| 1316 net::URLRequestStatus::SUCCESS); |
| 1317 factory_.SetFakeResponse(GURL(kGoodResourceURL), resource_data, net::HTTP_OK, |
| 1318 net::URLRequestStatus::SUCCESS); |
| 1319 factory_.SetFakeResponse( |
| 1320 GURL(std::string(kManifestURLPrefix) + "top-host-2.com"), |
| 1321 good_manifest.SerializeAsString(), net::HTTP_OK, |
| 1322 net::URLRequestStatus::SUCCESS); |
| 1323 factory_.SetFakeResponse(GURL(kGoodResourceURL), resource_data, net::HTTP_OK, |
| 1324 net::URLRequestStatus::SUCCESS); |
| 1325 factory_.SetFakeResponse( |
| 1326 GURL(std::string(kManifestURLPrefix) + "top-host-3.com"), |
| 1327 good_manifest.SerializeAsString(), net::HTTP_OK, |
| 1328 net::URLRequestStatus::SUCCESS); |
| 1329 factory_.SetFakeResponse(GURL(kGoodResourceURL), resource_data, net::HTTP_OK, |
| 1330 net::URLRequestStatus::SUCCESS); |
| 1331 |
| 1332 base::HistogramTester histogram; |
| 1333 |
| 1334 { |
| 1335 PrecacheFetcher precache_fetcher( |
| 1336 request_context_.get(), GURL(), std::string(), |
| 1337 std::move(unfinished_work), kExperimentID, |
| 1338 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
| 1339 precache_fetcher.Start(); |
| 1340 |
| 1341 base::RunLoop().RunUntilIdle(); |
| 1342 |
| 1343 EXPECT_EQ(0U, precache_fetcher.quota_.remaining()); |
| 1344 unfinished_work = precache_fetcher.CancelPrecaching(); |
| 1345 } |
| 1346 |
| 1347 std::multiset<GURL> expected_requested_urls; |
| 1348 expected_requested_urls.insert(GURL(kConfigURL)); |
| 1349 expected_requested_urls.insert( |
| 1350 GURL((std::string(kManifestURLPrefix) + "top-host-1.com"))); |
| 1351 expected_requested_urls.insert( |
| 1352 GURL((std::string(kManifestURLPrefix) + "top-host-2.com"))); |
| 1353 expected_requested_urls.insert( |
| 1354 GURL((std::string(kManifestURLPrefix) + "top-host-3.com"))); |
| 1355 expected_requested_urls.insert(GURL(kGoodResourceURL)); |
| 1356 expected_requested_urls.insert(GURL(kGoodResourceURL)); |
| 1357 expected_requested_urls.insert(GURL(kGoodResourceURL)); |
| 1358 |
| 1359 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 1360 |
| 1361 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
| 1362 |
| 1363 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 1); |
| 1364 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 1); |
| 1365 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); |
| 1366 |
| 1367 // Continuing with the precache when quota limit is reached ,will not fetch |
| 1368 // any resources. |
| 1369 expected_requested_urls.clear(); |
| 1370 url_callback_.clear_requested_urls(); |
| 1371 { |
| 1372 PrecacheFetcher precache_fetcher( |
| 1373 request_context_.get(), GURL(), std::string(), |
| 1374 std::move(unfinished_work), kExperimentID, |
| 1375 precache_database_.GetWeakPtr(), task_runner(), &precache_delegate_); |
| 1376 precache_fetcher.Start(); |
| 1377 base::RunLoop().RunUntilIdle(); |
| 1378 |
| 1379 EXPECT_EQ(0U, precache_fetcher.quota_.remaining()); |
| 1380 } |
| 1381 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 1382 |
| 1383 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
| 1384 |
| 1385 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); |
| 1386 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); |
| 1387 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); |
| 1388 } |
| 1389 |
1292 } // namespace precache | 1390 } // namespace precache |
OLD | NEW |