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 <utility> | 13 #include <utility> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
17 #include "base/callback.h" | 17 #include "base/callback.h" |
18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
20 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
21 #include "base/files/scoped_temp_dir.h" | 21 #include "base/files/scoped_temp_dir.h" |
22 #include "base/memory/ptr_util.h" | 22 #include "base/memory/ptr_util.h" |
23 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
24 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
25 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
26 #include "base/single_thread_task_runner.h" | 26 #include "base/single_thread_task_runner.h" |
27 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
28 #include "base/test/histogram_tester.h" | 28 #include "base/test/histogram_tester.h" |
| 29 #include "base/test/scoped_task_environment.h" |
29 #include "base/threading/thread_task_runner_handle.h" | 30 #include "base/threading/thread_task_runner_handle.h" |
30 #include "components/precache/core/precache_database.h" | 31 #include "components/precache/core/precache_database.h" |
31 #include "components/precache/core/precache_switches.h" | 32 #include "components/precache/core/precache_switches.h" |
32 #include "components/precache/core/proto/precache.pb.h" | 33 #include "components/precache/core/proto/precache.pb.h" |
33 #include "components/precache/core/proto/unfinished_work.pb.h" | 34 #include "components/precache/core/proto/unfinished_work.pb.h" |
34 #include "net/base/escape.h" | 35 #include "net/base/escape.h" |
35 #include "net/base/load_flags.h" | 36 #include "net/base/load_flags.h" |
36 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
37 #include "net/http/http_response_info.h" | 38 #include "net/http/http_response_info.h" |
38 #include "net/http/http_status_code.h" | 39 #include "net/http/http_status_code.h" |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 private: | 204 private: |
204 std::string body_; | 205 std::string body_; |
205 F modifier_; | 206 F modifier_; |
206 net::FakeURLFetcher** fetcher_; | 207 net::FakeURLFetcher** fetcher_; |
207 }; | 208 }; |
208 }; | 209 }; |
209 | 210 |
210 class PrecacheFetcherFetcherTest : public testing::Test { | 211 class PrecacheFetcherFetcherTest : public testing::Test { |
211 public: | 212 public: |
212 PrecacheFetcherFetcherTest() | 213 PrecacheFetcherFetcherTest() |
213 : request_context_(new net::TestURLRequestContextGetter( | 214 : scoped_task_environment_( |
| 215 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 216 request_context_(new net::TestURLRequestContextGetter( |
214 base::ThreadTaskRunnerHandle::Get())), | 217 base::ThreadTaskRunnerHandle::Get())), |
215 scoped_url_fetcher_factory_(&factory_), | 218 scoped_url_fetcher_factory_(&factory_), |
216 callback_(base::Bind(&PrecacheFetcherFetcherTest::Callback, | 219 callback_(base::Bind(&PrecacheFetcherFetcherTest::Callback, |
217 base::Unretained(this))) {} | 220 base::Unretained(this))) {} |
218 | 221 |
219 MOCK_METHOD1(Callback, void(const PrecacheFetcher::Fetcher&)); | 222 MOCK_METHOD1(Callback, void(const PrecacheFetcher::Fetcher&)); |
220 | 223 |
221 protected: | 224 protected: |
222 base::MessageLoopForUI loop_; | 225 base::test::ScopedTaskEnvironment scoped_task_environment_; |
223 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 226 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
224 MockURLFetcherFactory factory_; | 227 MockURLFetcherFactory factory_; |
225 net::ScopedURLFetcherFactory scoped_url_fetcher_factory_; | 228 net::ScopedURLFetcherFactory scoped_url_fetcher_factory_; |
226 base::Callback<void(const PrecacheFetcher::Fetcher&)> callback_; | 229 base::Callback<void(const PrecacheFetcher::Fetcher&)> callback_; |
227 }; | 230 }; |
228 | 231 |
229 void CacheMiss(net::FakeURLFetcher* fetcher) { | 232 void CacheMiss(net::FakeURLFetcher* fetcher) { |
230 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 233 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
231 net::ERR_CACHE_MISS)); | 234 net::ERR_CACHE_MISS)); |
232 } | 235 } |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 request_context_.get(), url, url.host(), callback_, | 421 request_context_.get(), url, url.host(), callback_, |
419 true /* is_resource_request */, 99 /* max_bytes */, | 422 true /* is_resource_request */, 99 /* max_bytes */, |
420 false /* revalidation_only */); | 423 false /* revalidation_only */); |
421 | 424 |
422 base::RunLoop().RunUntilIdle(); | 425 base::RunLoop().RunUntilIdle(); |
423 } | 426 } |
424 | 427 |
425 class PrecacheFetcherTest : public testing::Test { | 428 class PrecacheFetcherTest : public testing::Test { |
426 public: | 429 public: |
427 PrecacheFetcherTest() | 430 PrecacheFetcherTest() |
428 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 431 : scoped_task_environment_( |
| 432 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 433 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
429 request_context_(new net::TestURLRequestContextGetter( | 434 request_context_(new net::TestURLRequestContextGetter( |
430 base::ThreadTaskRunnerHandle::Get())), | 435 base::ThreadTaskRunnerHandle::Get())), |
431 factory_(NULL, | 436 factory_(NULL, |
432 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, | 437 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, |
433 base::Unretained(&url_callback_))), | 438 base::Unretained(&url_callback_))), |
434 expected_total_response_bytes_(0), | 439 expected_total_response_bytes_(0), |
435 parallel_fetches_beyond_capacity_(false) {} | 440 parallel_fetches_beyond_capacity_(false) {} |
436 | 441 |
437 void UpdatePrecacheReferrerHost(const std::string& hostname, | 442 void UpdatePrecacheReferrerHost(const std::string& hostname, |
438 int64_t manifest_id) { | 443 int64_t manifest_id) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 void CheckUntilParallelFetchesBeyondCapacity( | 480 void CheckUntilParallelFetchesBeyondCapacity( |
476 const PrecacheFetcher* precache_fetcher) { | 481 const PrecacheFetcher* precache_fetcher) { |
477 if (!precache_fetcher->pool_.IsAvailable() && | 482 if (!precache_fetcher->pool_.IsAvailable() && |
478 (!precache_fetcher->top_hosts_to_fetch_.empty() || | 483 (!precache_fetcher->top_hosts_to_fetch_.empty() || |
479 !precache_fetcher->resources_to_fetch_.empty())) { | 484 !precache_fetcher->resources_to_fetch_.empty())) { |
480 parallel_fetches_beyond_capacity_ = true; | 485 parallel_fetches_beyond_capacity_ = true; |
481 return; | 486 return; |
482 } | 487 } |
483 | 488 |
484 // Check again after allowing the message loop to process some messages. | 489 // Check again after allowing the message loop to process some messages. |
485 loop_.task_runner()->PostTask( | 490 base::ThreadTaskRunnerHandle::Get()->PostTask( |
486 FROM_HERE, | 491 FROM_HERE, |
487 base::Bind( | 492 base::Bind( |
488 &PrecacheFetcherTest::CheckUntilParallelFetchesBeyondCapacity, | 493 &PrecacheFetcherTest::CheckUntilParallelFetchesBeyondCapacity, |
489 base::Unretained(this), precache_fetcher)); | 494 base::Unretained(this), precache_fetcher)); |
490 } | 495 } |
491 | 496 |
492 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const { | 497 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const { |
493 return task_runner_; | 498 return task_runner_; |
494 } | 499 } |
495 | 500 |
496 // To allow friend access. | 501 // To allow friend access. |
497 void Flush() { precache_database_.Flush(); } | 502 void Flush() { precache_database_.Flush(); } |
498 | 503 |
499 // Must be declared first so that it is destroyed last. | 504 // Must be declared first so that it is destroyed last. |
500 base::MessageLoopForUI loop_; | 505 base::test::ScopedTaskEnvironment scoped_task_environment_; |
501 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 506 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
502 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 507 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
503 TestURLFetcherCallback url_callback_; | 508 TestURLFetcherCallback url_callback_; |
504 net::FakeURLFetcherFactory factory_; | 509 net::FakeURLFetcherFactory factory_; |
505 TestPrecacheDelegate precache_delegate_; | 510 TestPrecacheDelegate precache_delegate_; |
506 base::ScopedTempDir scoped_temp_dir_; | 511 base::ScopedTempDir scoped_temp_dir_; |
507 PrecacheDatabase precache_database_; | 512 PrecacheDatabase precache_database_; |
508 int expected_total_response_bytes_; | 513 int expected_total_response_bytes_; |
509 | 514 |
510 // True if more parallel fetches were attempted beyond the fetcher pool | 515 // True if more parallel fetches were attempted beyond the fetcher pool |
(...skipping 1222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1733 | 1738 |
1734 // Run the loop until all tophost manifest fetches are complete, but some | 1739 // Run the loop until all tophost manifest fetches are complete, but some |
1735 // resource fetches are pending. | 1740 // resource fetches are pending. |
1736 while (--remaining_tries != 0 && | 1741 while (--remaining_tries != 0 && |
1737 (!precache_fetcher.top_hosts_to_fetch_.empty() || | 1742 (!precache_fetcher.top_hosts_to_fetch_.empty() || |
1738 !precache_fetcher.top_hosts_fetching_.empty() || | 1743 !precache_fetcher.top_hosts_fetching_.empty() || |
1739 !precache_fetcher.unfinished_work_->has_config_settings() || | 1744 !precache_fetcher.unfinished_work_->has_config_settings() || |
1740 precache_fetcher.resources_to_fetch_.empty())) { | 1745 precache_fetcher.resources_to_fetch_.empty())) { |
1741 LOG(INFO) << "remaining_tries: " << remaining_tries; | 1746 LOG(INFO) << "remaining_tries: " << remaining_tries; |
1742 base::RunLoop run_loop; | 1747 base::RunLoop run_loop; |
1743 loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure()); | 1748 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1749 run_loop.QuitClosure()); |
1744 run_loop.Run(); | 1750 run_loop.Run(); |
1745 } | 1751 } |
1746 | 1752 |
1747 // Cancel precaching. | 1753 // Cancel precaching. |
1748 cancelled_work = precache_fetcher.CancelPrecaching(); | 1754 cancelled_work = precache_fetcher.CancelPrecaching(); |
1749 EXPECT_TRUE(precache_fetcher.top_hosts_to_fetch_.empty()); | 1755 EXPECT_TRUE(precache_fetcher.top_hosts_to_fetch_.empty()); |
1750 EXPECT_TRUE(precache_fetcher.resources_to_fetch_.empty()); | 1756 EXPECT_TRUE(precache_fetcher.resources_to_fetch_.empty()); |
1751 } | 1757 } |
1752 EXPECT_NE(cancelled_work, nullptr); | 1758 EXPECT_NE(cancelled_work, nullptr); |
1753 EXPECT_TRUE(cancelled_work->top_host().empty()); | 1759 EXPECT_TRUE(cancelled_work->top_host().empty()); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1970 | 1976 |
1971 // Run the loop until all tophost manifest fetches are complete, but some | 1977 // Run the loop until all tophost manifest fetches are complete, but some |
1972 // resource fetches are pending. | 1978 // resource fetches are pending. |
1973 while (--remaining_tries != 0 && | 1979 while (--remaining_tries != 0 && |
1974 (!precache_fetcher.top_hosts_to_fetch_.empty() || | 1980 (!precache_fetcher.top_hosts_to_fetch_.empty() || |
1975 !precache_fetcher.top_hosts_fetching_.empty() || | 1981 !precache_fetcher.top_hosts_fetching_.empty() || |
1976 !precache_fetcher.unfinished_work_->has_config_settings() || | 1982 !precache_fetcher.unfinished_work_->has_config_settings() || |
1977 precache_fetcher.resources_to_fetch_.empty())) { | 1983 precache_fetcher.resources_to_fetch_.empty())) { |
1978 LOG(INFO) << "remaining_tries: " << remaining_tries; | 1984 LOG(INFO) << "remaining_tries: " << remaining_tries; |
1979 base::RunLoop run_loop; | 1985 base::RunLoop run_loop; |
1980 loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure()); | 1986 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1987 run_loop.QuitClosure()); |
1981 run_loop.Run(); | 1988 run_loop.Run(); |
1982 } | 1989 } |
1983 | 1990 |
1984 // Cancel precaching. | 1991 // Cancel precaching. |
1985 cancelled_work = precache_fetcher.CancelPrecaching(); | 1992 cancelled_work = precache_fetcher.CancelPrecaching(); |
1986 EXPECT_TRUE(precache_fetcher.top_hosts_to_fetch_.empty()); | 1993 EXPECT_TRUE(precache_fetcher.top_hosts_to_fetch_.empty()); |
1987 EXPECT_TRUE(precache_fetcher.resources_to_fetch_.empty()); | 1994 EXPECT_TRUE(precache_fetcher.resources_to_fetch_.empty()); |
1988 } | 1995 } |
1989 ASSERT_NE(nullptr, cancelled_work); | 1996 ASSERT_NE(nullptr, cancelled_work); |
1990 EXPECT_TRUE(cancelled_work->top_host().empty()); | 1997 EXPECT_TRUE(cancelled_work->top_host().empty()); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2108 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 2115 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
2109 EXPECT_TRUE(precache_delegate_.get_manifest_hosts().empty()); | 2116 EXPECT_TRUE(precache_delegate_.get_manifest_hosts().empty()); |
2110 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 2117 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
2111 | 2118 |
2112 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); | 2119 histogram.ExpectTotalCount("Precache.Fetch.PercentCompleted", 2); |
2113 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); | 2120 histogram.ExpectTotalCount("Precache.Fetch.ResponseBytes.Total", 2); |
2114 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); | 2121 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 2); |
2115 } | 2122 } |
2116 | 2123 |
2117 } // namespace precache | 2124 } // namespace precache |
OLD | NEW |