Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 18 #include "base/files/scoped_temp_dir.h" | 18 #include "base/files/scoped_temp_dir.h" |
| 19 #include "base/location.h" | 19 #include "base/location.h" |
| 20 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 22 #include "base/path_service.h" | 22 #include "base/path_service.h" |
| 23 #include "base/run_loop.h" | 23 #include "base/run_loop.h" |
| 24 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
| 25 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
| 26 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
| 27 #include "base/synchronization/waitable_event.h" | 27 #include "base/synchronization/waitable_event.h" |
| 28 #include "base/task_scheduler/post_task.h" | 28 #include "base/task_scheduler/post_task.h" |
| 29 #include "base/test/scoped_task_scheduler.h" | 29 #include "base/test/scoped_task_environment.h" |
| 30 #include "base/test/test_timeouts.h" | 30 #include "base/test/test_timeouts.h" |
| 31 #include "base/threading/platform_thread.h" | 31 #include "base/threading/platform_thread.h" |
| 32 #include "base/threading/thread.h" | 32 #include "base/threading/thread.h" |
| 33 #include "base/threading/thread_task_runner_handle.h" | 33 #include "base/threading/thread_task_runner_handle.h" |
| 34 #include "build/build_config.h" | 34 #include "build/build_config.h" |
| 35 #include "crypto/nss_util.h" | 35 #include "crypto/nss_util.h" |
| 36 #include "net/base/elements_upload_data_stream.h" | 36 #include "net/base/elements_upload_data_stream.h" |
| 37 #include "net/base/network_change_notifier.h" | 37 #include "net/base/network_change_notifier.h" |
| 38 #include "net/base/upload_bytes_element_reader.h" | 38 #include "net/base/upload_bytes_element_reader.h" |
| 39 #include "net/base/upload_element_reader.h" | 39 #include "net/base/upload_element_reader.h" |
| 40 #include "net/base/upload_file_element_reader.h" | 40 #include "net/base/upload_file_element_reader.h" |
| 41 #include "net/dns/mock_host_resolver.h" | 41 #include "net/dns/mock_host_resolver.h" |
| 42 #include "net/http/http_response_headers.h" | 42 #include "net/http/http_response_headers.h" |
| 43 #include "net/test/embedded_test_server/embedded_test_server.h" | 43 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 44 #include "net/test/gtest_util.h" | 44 #include "net/test/gtest_util.h" |
| 45 #include "net/test/net_test_suite.h" | |
| 45 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 46 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 46 #include "net/url_request/url_fetcher_delegate.h" | 47 #include "net/url_request/url_fetcher_delegate.h" |
| 47 #include "net/url_request/url_request_context_getter.h" | 48 #include "net/url_request/url_request_context_getter.h" |
| 48 #include "net/url_request/url_request_test_util.h" | 49 #include "net/url_request/url_request_test_util.h" |
| 49 #include "net/url_request/url_request_throttler_manager.h" | 50 #include "net/url_request/url_request_throttler_manager.h" |
| 50 #include "testing/gmock/include/gmock/gmock.h" | 51 #include "testing/gmock/include/gmock/gmock.h" |
| 51 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
| 52 | 53 |
| 53 #if defined(USE_NSS_CERTS) | 54 #if defined(USE_NSS_CERTS) |
| 54 #include "net/cert_net/nss_ocsp.h" | 55 #include "net/cert_net/nss_ocsp.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 URLFetcher* fetcher() const { return fetcher_.get(); } | 105 URLFetcher* fetcher() const { return fetcher_.get(); } |
| 105 | 106 |
| 106 // Wait until the request has completed or been canceled. | 107 // Wait until the request has completed or been canceled. |
| 107 void StartFetcherAndWait() { | 108 void StartFetcherAndWait() { |
| 108 fetcher_->Start(); | 109 fetcher_->Start(); |
| 109 WaitForComplete(); | 110 WaitForComplete(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 // Wait until the request has completed or been canceled. Does not start the | 113 // Wait until the request has completed or been canceled. Does not start the |
| 113 // request. | 114 // request. |
| 114 void WaitForComplete() { run_loop_.Run(); } | 115 void WaitForComplete() { |
| 116 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 117 run_loop_.Run(); | |
| 118 } | |
| 115 | 119 |
| 116 // Cancels the fetch by deleting the fetcher. | 120 // Cancels the fetch by deleting the fetcher. |
| 117 void CancelFetch() { | 121 void CancelFetch() { |
| 118 EXPECT_TRUE(fetcher_); | 122 EXPECT_TRUE(fetcher_); |
| 119 fetcher_.reset(); | 123 fetcher_.reset(); |
| 120 run_loop_.Quit(); | 124 task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
| 121 } | 125 } |
| 122 | 126 |
| 123 // URLFetcherDelegate: | 127 // URLFetcherDelegate: |
| 124 void OnURLFetchComplete(const URLFetcher* source) override { | 128 void OnURLFetchComplete(const URLFetcher* source) override { |
| 125 EXPECT_FALSE(did_complete_); | 129 EXPECT_FALSE(did_complete_); |
| 126 EXPECT_TRUE(fetcher_); | 130 EXPECT_TRUE(fetcher_); |
| 127 EXPECT_EQ(fetcher_.get(), source); | 131 EXPECT_EQ(fetcher_.get(), source); |
| 128 did_complete_ = true; | 132 did_complete_ = true; |
| 129 run_loop_.Quit(); | 133 task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
|
fdoray
2017/05/09 21:43:47
TaskScheduler tasks run on dedicated thread when t
mmenke
2017/05/10 15:43:44
I'm not really following, nor do I understand why
fdoray
2017/05/10 16:16:12
- run_loop_.Quit() and run_loop_.Run() must be cal
mmenke
2017/05/11 15:30:39
URLFetchers are cross-thread objects. You start t
fdoray
2017/05/11 16:15:08
URLFetcherTest.SequencedTaskTest creates a URLFetc
| |
| 130 } | 134 } |
| 131 | 135 |
| 132 void OnURLFetchDownloadProgress(const URLFetcher* source, | 136 void OnURLFetchDownloadProgress(const URLFetcher* source, |
| 133 int64_t current, | 137 int64_t current, |
| 134 int64_t total, | 138 int64_t total, |
| 135 int64_t current_network_bytes) override { | 139 int64_t current_network_bytes) override { |
| 136 // Note that the current progress may be greater than the previous progress, | 140 // Note that the current progress may be greater than the previous progress, |
| 137 // in the case of retrying the request. | 141 // in the case of retrying the request. |
| 138 EXPECT_FALSE(did_complete_); | 142 EXPECT_FALSE(did_complete_); |
| 139 EXPECT_TRUE(fetcher_); | 143 EXPECT_TRUE(fetcher_); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 159 if (total >= 0) | 163 if (total >= 0) |
| 160 EXPECT_LE(current, total); | 164 EXPECT_LE(current, total); |
| 161 } | 165 } |
| 162 | 166 |
| 163 bool did_complete() const { return did_complete_; } | 167 bool did_complete() const { return did_complete_; } |
| 164 | 168 |
| 165 private: | 169 private: |
| 166 bool did_complete_; | 170 bool did_complete_; |
| 167 | 171 |
| 168 std::unique_ptr<URLFetcherImpl> fetcher_; | 172 std::unique_ptr<URLFetcherImpl> fetcher_; |
| 173 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_ = | |
| 174 base::ThreadTaskRunnerHandle::Get(); | |
| 169 base::RunLoop run_loop_; | 175 base::RunLoop run_loop_; |
| 170 | 176 |
| 171 DISALLOW_COPY_AND_ASSIGN(WaitingURLFetcherDelegate); | 177 DISALLOW_COPY_AND_ASSIGN(WaitingURLFetcherDelegate); |
| 172 }; | 178 }; |
| 173 | 179 |
| 174 // A TestURLRequestContext with a ThrottleManager and a MockHostResolver. | 180 // A TestURLRequestContext with a ThrottleManager and a MockHostResolver. |
| 175 class FetcherTestURLRequestContext : public TestURLRequestContext { | 181 class FetcherTestURLRequestContext : public TestURLRequestContext { |
| 176 public: | 182 public: |
| 177 // All requests for |hanging_domain| will hang on host resolution until the | 183 // All requests for |hanging_domain| will hang on host resolution until the |
| 178 // mock_resolver()->ResolveAllPending() is called. | 184 // mock_resolver()->ResolveAllPending() is called. |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 521 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 516 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 522 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 517 std::string data; | 523 std::string data; |
| 518 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 524 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 519 EXPECT_EQ(kDefaultResponseBody, data); | 525 EXPECT_EQ(kDefaultResponseBody, data); |
| 520 } | 526 } |
| 521 | 527 |
| 522 // Create the fetcher from a sequenced (not single-threaded) task. Verify that | 528 // Create the fetcher from a sequenced (not single-threaded) task. Verify that |
| 523 // the expected response is received. | 529 // the expected response is received. |
| 524 TEST_F(URLFetcherTest, SequencedTaskTest) { | 530 TEST_F(URLFetcherTest, SequencedTaskTest) { |
| 525 base::test::ScopedTaskScheduler scoped_task_scheduler( | |
| 526 base::MessageLoop::current()); | |
| 527 auto sequenced_task_runner = base::CreateSequencedTaskRunnerWithTraits({}); | 531 auto sequenced_task_runner = base::CreateSequencedTaskRunnerWithTraits({}); |
| 528 | |
| 529 auto delegate = base::MakeUnique<WaitingURLFetcherDelegate>(); | 532 auto delegate = base::MakeUnique<WaitingURLFetcherDelegate>(); |
| 530 sequenced_task_runner->PostTask( | 533 sequenced_task_runner->PostTask( |
| 531 FROM_HERE, base::Bind(&WaitingURLFetcherDelegate::CreateFetcher, | 534 FROM_HERE, base::Bind(&WaitingURLFetcherDelegate::CreateFetcher, |
| 532 base::Unretained(delegate.get()), | 535 base::Unretained(delegate.get()), |
| 533 test_server_->GetURL(kDefaultResponsePath), | 536 test_server_->GetURL(kDefaultResponsePath), |
| 534 URLFetcher::GET, CreateCrossThreadContextGetter())); | 537 URLFetcher::GET, CreateCrossThreadContextGetter())); |
| 535 base::RunLoop().RunUntilIdle(); | 538 NetTestSuite::GetScopedTaskEnvironment()->RunUntilIdle(); |
| 536 delegate->StartFetcherAndWait(); | 539 delegate->StartFetcherAndWait(); |
| 537 | 540 |
| 538 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); | 541 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); |
| 539 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); | 542 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); |
| 540 std::string data; | 543 std::string data; |
| 541 ASSERT_TRUE(delegate->fetcher()->GetResponseAsString(&data)); | 544 ASSERT_TRUE(delegate->fetcher()->GetResponseAsString(&data)); |
| 542 EXPECT_EQ(kDefaultResponseBody, data); | 545 EXPECT_EQ(kDefaultResponseBody, data); |
| 543 | 546 |
| 544 sequenced_task_runner->DeleteSoon(FROM_HERE, delegate.release()); | 547 sequenced_task_runner->DeleteSoon(FROM_HERE, delegate.release()); |
| 545 base::RunLoop().RunUntilIdle(); | 548 NetTestSuite::GetScopedTaskEnvironment()->RunUntilIdle(); |
| 546 } | 549 } |
| 547 | 550 |
| 548 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | 551 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. |
| 549 TEST_F(URLFetcherTest, CancelAll) { | 552 TEST_F(URLFetcherTest, CancelAll) { |
| 550 EXPECT_EQ(0, GetNumFetcherCores()); | 553 EXPECT_EQ(0, GetNumFetcherCores()); |
| 551 | 554 |
| 552 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( | 555 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( |
| 553 CreateSameThreadContextGetter()); | 556 CreateSameThreadContextGetter()); |
| 554 // Force context creation. | 557 // Force context creation. |
| 555 context_getter->GetURLRequestContext(); | 558 context_getter->GetURLRequestContext(); |
| (...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1569 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); | 1572 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); |
| 1570 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); | 1573 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); |
| 1571 std::string data; | 1574 std::string data; |
| 1572 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1575 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 1573 EXPECT_TRUE(data.empty()); | 1576 EXPECT_TRUE(data.empty()); |
| 1574 } | 1577 } |
| 1575 | 1578 |
| 1576 } // namespace | 1579 } // namespace |
| 1577 | 1580 |
| 1578 } // namespace net | 1581 } // namespace net |
| OLD | NEW |