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/task_scheduler/task_scheduler.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" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 URLFetcher* fetcher() const { return fetcher_.get(); } | 104 URLFetcher* fetcher() const { return fetcher_.get(); } |
105 | 105 |
106 // Wait until the request has completed or been canceled. | 106 // Wait until the request has completed or been canceled. |
107 void StartFetcherAndWait() { | 107 void StartFetcherAndWait() { |
108 fetcher_->Start(); | 108 fetcher_->Start(); |
109 WaitForComplete(); | 109 WaitForComplete(); |
110 } | 110 } |
111 | 111 |
112 // Wait until the request has completed or been canceled. Does not start the | 112 // Wait until the request has completed or been canceled. Does not start the |
113 // request. | 113 // request. |
114 void WaitForComplete() { run_loop_.Run(); } | 114 void WaitForComplete() { |
115 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
116 run_loop_.Run(); | |
117 } | |
115 | 118 |
116 // Cancels the fetch by deleting the fetcher. | 119 // Cancels the fetch by deleting the fetcher. |
117 void CancelFetch() { | 120 void CancelFetch() { |
118 EXPECT_TRUE(fetcher_); | 121 EXPECT_TRUE(fetcher_); |
119 fetcher_.reset(); | 122 fetcher_.reset(); |
120 run_loop_.Quit(); | 123 task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
121 } | 124 } |
122 | 125 |
123 // URLFetcherDelegate: | 126 // URLFetcherDelegate: |
124 void OnURLFetchComplete(const URLFetcher* source) override { | 127 void OnURLFetchComplete(const URLFetcher* source) override { |
125 EXPECT_FALSE(did_complete_); | 128 EXPECT_FALSE(did_complete_); |
126 EXPECT_TRUE(fetcher_); | 129 EXPECT_TRUE(fetcher_); |
127 EXPECT_EQ(fetcher_.get(), source); | 130 EXPECT_EQ(fetcher_.get(), source); |
128 did_complete_ = true; | 131 did_complete_ = true; |
129 run_loop_.Quit(); | 132 task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
130 } | 133 } |
131 | 134 |
132 void OnURLFetchDownloadProgress(const URLFetcher* source, | 135 void OnURLFetchDownloadProgress(const URLFetcher* source, |
133 int64_t current, | 136 int64_t current, |
134 int64_t total, | 137 int64_t total, |
135 int64_t current_network_bytes) override { | 138 int64_t current_network_bytes) override { |
136 // Note that the current progress may be greater than the previous progress, | 139 // Note that the current progress may be greater than the previous progress, |
137 // in the case of retrying the request. | 140 // in the case of retrying the request. |
138 EXPECT_FALSE(did_complete_); | 141 EXPECT_FALSE(did_complete_); |
139 EXPECT_TRUE(fetcher_); | 142 EXPECT_TRUE(fetcher_); |
(...skipping 19 matching lines...) Expand all Loading... | |
159 if (total >= 0) | 162 if (total >= 0) |
160 EXPECT_LE(current, total); | 163 EXPECT_LE(current, total); |
161 } | 164 } |
162 | 165 |
163 bool did_complete() const { return did_complete_; } | 166 bool did_complete() const { return did_complete_; } |
164 | 167 |
165 private: | 168 private: |
166 bool did_complete_; | 169 bool did_complete_; |
167 | 170 |
168 std::unique_ptr<URLFetcherImpl> fetcher_; | 171 std::unique_ptr<URLFetcherImpl> fetcher_; |
172 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_ = | |
173 base::ThreadTaskRunnerHandle::Get(); | |
169 base::RunLoop run_loop_; | 174 base::RunLoop run_loop_; |
170 | 175 |
171 DISALLOW_COPY_AND_ASSIGN(WaitingURLFetcherDelegate); | 176 DISALLOW_COPY_AND_ASSIGN(WaitingURLFetcherDelegate); |
172 }; | 177 }; |
173 | 178 |
174 // A TestURLRequestContext with a ThrottleManager and a MockHostResolver. | 179 // A TestURLRequestContext with a ThrottleManager and a MockHostResolver. |
175 class FetcherTestURLRequestContext : public TestURLRequestContext { | 180 class FetcherTestURLRequestContext : public TestURLRequestContext { |
176 public: | 181 public: |
177 // All requests for |hanging_domain| will hang on host resolution until the | 182 // All requests for |hanging_domain| will hang on host resolution until the |
178 // mock_resolver()->ResolveAllPending() is called. | 183 // 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()); | 520 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
516 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 521 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
517 std::string data; | 522 std::string data; |
518 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 523 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
519 EXPECT_EQ(kDefaultResponseBody, data); | 524 EXPECT_EQ(kDefaultResponseBody, data); |
520 } | 525 } |
521 | 526 |
522 // Create the fetcher from a sequenced (not single-threaded) task. Verify that | 527 // Create the fetcher from a sequenced (not single-threaded) task. Verify that |
523 // the expected response is received. | 528 // the expected response is received. |
524 TEST_F(URLFetcherTest, SequencedTaskTest) { | 529 TEST_F(URLFetcherTest, SequencedTaskTest) { |
525 base::test::ScopedTaskScheduler scoped_task_scheduler( | |
526 base::MessageLoop::current()); | |
527 auto sequenced_task_runner = base::CreateSequencedTaskRunnerWithTraits({}); | 530 auto sequenced_task_runner = base::CreateSequencedTaskRunnerWithTraits({}); |
528 | |
529 auto delegate = base::MakeUnique<WaitingURLFetcherDelegate>(); | 531 auto delegate = base::MakeUnique<WaitingURLFetcherDelegate>(); |
530 sequenced_task_runner->PostTask( | 532 sequenced_task_runner->PostTask( |
531 FROM_HERE, base::Bind(&WaitingURLFetcherDelegate::CreateFetcher, | 533 FROM_HERE, base::Bind(&WaitingURLFetcherDelegate::CreateFetcher, |
532 base::Unretained(delegate.get()), | 534 base::Unretained(delegate.get()), |
533 test_server_->GetURL(kDefaultResponsePath), | 535 test_server_->GetURL(kDefaultResponsePath), |
534 URLFetcher::GET, CreateCrossThreadContextGetter())); | 536 URLFetcher::GET, CreateCrossThreadContextGetter())); |
535 base::RunLoop().RunUntilIdle(); | 537 base::TaskScheduler::GetInstance()->FlushForTesting(); |
mmenke
2017/05/09 15:40:44
In most of the other tests you do a flush + RunUnt
fdoray
2017/05/09 21:43:47
Because the RunLoop::RunUntilIdle() was not requir
| |
536 delegate->StartFetcherAndWait(); | 538 delegate->StartFetcherAndWait(); |
537 | 539 |
538 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); | 540 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); |
539 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); | 541 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); |
540 std::string data; | 542 std::string data; |
541 ASSERT_TRUE(delegate->fetcher()->GetResponseAsString(&data)); | 543 ASSERT_TRUE(delegate->fetcher()->GetResponseAsString(&data)); |
542 EXPECT_EQ(kDefaultResponseBody, data); | 544 EXPECT_EQ(kDefaultResponseBody, data); |
543 | 545 |
544 sequenced_task_runner->DeleteSoon(FROM_HERE, delegate.release()); | 546 sequenced_task_runner->DeleteSoon(FROM_HERE, delegate.release()); |
545 base::RunLoop().RunUntilIdle(); | 547 base::TaskScheduler::GetInstance()->FlushForTesting(); |
546 } | 548 } |
547 | 549 |
548 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | 550 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. |
549 TEST_F(URLFetcherTest, CancelAll) { | 551 TEST_F(URLFetcherTest, CancelAll) { |
550 EXPECT_EQ(0, GetNumFetcherCores()); | 552 EXPECT_EQ(0, GetNumFetcherCores()); |
551 | 553 |
552 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( | 554 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( |
553 CreateSameThreadContextGetter()); | 555 CreateSameThreadContextGetter()); |
554 // Force context creation. | 556 // Force context creation. |
555 context_getter->GetURLRequestContext(); | 557 context_getter->GetURLRequestContext(); |
(...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1569 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); | 1571 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); |
1570 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); | 1572 EXPECT_FALSE(delegate.fetcher()->GetResponseHeaders()); |
1571 std::string data; | 1573 std::string data; |
1572 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1574 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
1573 EXPECT_TRUE(data.empty()); | 1575 EXPECT_TRUE(data.empty()); |
1574 } | 1576 } |
1575 | 1577 |
1576 } // namespace | 1578 } // namespace |
1577 | 1579 |
1578 } // namespace net | 1580 } // namespace net |
OLD | NEW |