| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind_helpers.h" |
| 5 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 8 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
| 9 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| 10 #include "net/base/request_priority.h" | 11 #include "net/base/request_priority.h" |
| 11 #include "net/url_request/url_request_job.h" | 12 #include "net/url_request/url_request_job.h" |
| 12 #include "net/url_request/url_request_job_factory.h" | 13 #include "net/url_request/url_request_job_factory.h" |
| 13 #include "net/url_request/url_request_job_factory_impl.h" | 14 #include "net/url_request/url_request_job_factory_impl.h" |
| 14 #include "net/url_request/url_request_simple_job.h" | 15 #include "net/url_request/url_request_simple_job.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 const char kTestData[] = "Huge data array"; | 23 const char kTestData[] = "Huge data array"; |
| 23 const int kRangeFirstPosition = 5; | 24 const int kRangeFirstPosition = 5; |
| 24 const int kRangeLastPosition = 8; | 25 const int kRangeLastPosition = 8; |
| 25 static_assert(kRangeFirstPosition > 0 && | 26 static_assert(kRangeFirstPosition > 0 && |
| 26 kRangeFirstPosition < kRangeLastPosition && | 27 kRangeFirstPosition < kRangeLastPosition && |
| 27 kRangeLastPosition < | 28 kRangeLastPosition < |
| 28 static_cast<int>(arraysize(kTestData) - 1), | 29 static_cast<int>(arraysize(kTestData) - 1), |
| 29 "invalid range"); | 30 "invalid range"); |
| 30 | 31 |
| 31 // This function does nothing. | |
| 32 void DoNothing() { | |
| 33 } | |
| 34 | |
| 35 class MockSimpleJob : public URLRequestSimpleJob { | 32 class MockSimpleJob : public URLRequestSimpleJob { |
| 36 public: | 33 public: |
| 37 MockSimpleJob(URLRequest* request, | 34 MockSimpleJob(URLRequest* request, |
| 38 NetworkDelegate* network_delegate, | 35 NetworkDelegate* network_delegate, |
| 39 scoped_refptr<base::TaskRunner> task_runner, | 36 scoped_refptr<base::TaskRunner> task_runner, |
| 40 std::string data) | 37 std::string data) |
| 41 : URLRequestSimpleJob(request, network_delegate), | 38 : URLRequestSimpleJob(request, network_delegate), |
| 42 data_(data), | 39 data_(data), |
| 43 task_runner_(task_runner) {} | 40 task_runner_(task_runner) {} |
| 44 | 41 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 63 | 60 |
| 64 const std::string data_; | 61 const std::string data_; |
| 65 | 62 |
| 66 scoped_refptr<base::TaskRunner> task_runner_; | 63 scoped_refptr<base::TaskRunner> task_runner_; |
| 67 | 64 |
| 68 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); | 65 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); |
| 69 }; | 66 }; |
| 70 | 67 |
| 71 class CancelURLRequestDelegate : public URLRequest::Delegate { | 68 class CancelURLRequestDelegate : public URLRequest::Delegate { |
| 72 public: | 69 public: |
| 73 explicit CancelURLRequestDelegate() | 70 CancelURLRequestDelegate() |
| 74 : buf_(new IOBuffer(kBufferSize)), run_loop_(new base::RunLoop) {} | 71 : buf_(new IOBuffer(kBufferSize)), run_loop_(new base::RunLoop) {} |
| 75 | 72 |
| 76 void OnResponseStarted(URLRequest* request) override { | 73 void OnResponseStarted(URLRequest* request) override { |
| 77 int bytes_read = 0; | 74 int bytes_read = 0; |
| 78 EXPECT_FALSE(request->Read(buf_.get(), kBufferSize, &bytes_read)); | 75 EXPECT_FALSE(request->Read(buf_.get(), kBufferSize, &bytes_read)); |
| 79 EXPECT_TRUE(request->status().is_io_pending()); | 76 EXPECT_TRUE(request->status().is_io_pending()); |
| 80 request->Cancel(); | 77 request->Cancel(); |
| 81 run_loop_->Quit(); | 78 run_loop_->Quit(); |
| 82 } | 79 } |
| 83 | 80 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( | 212 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( |
| 216 new CancelURLRequestDelegate()); | 213 new CancelURLRequestDelegate()); |
| 217 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 214 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
| 218 cancel_delegate.get()); | 215 cancel_delegate.get()); |
| 219 request_->Start(); | 216 request_->Start(); |
| 220 cancel_delegate->WaitUntilHeadersReceived(); | 217 cancel_delegate->WaitUntilHeadersReceived(); |
| 221 | 218 |
| 222 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 219 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
| 223 // callbacks which are invoked in ReadRawData have completed safely. | 220 // callbacks which are invoked in ReadRawData have completed safely. |
| 224 base::RunLoop run_loop; | 221 base::RunLoop run_loop; |
| 225 EXPECT_TRUE(task_runner_->PostTaskAndReply(FROM_HERE, base::Bind(&DoNothing), | 222 EXPECT_TRUE(task_runner_->PostTaskAndReply( |
| 226 run_loop.QuitClosure())); | 223 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); |
| 227 run_loop.Run(); | 224 run_loop.Run(); |
| 228 } | 225 } |
| 229 | 226 |
| 230 } // namespace net | 227 } // namespace net |
| OLD | NEW |