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/test/embedded_test_server/embedded_test_server.h" | 5 #include "net/test/embedded_test_server/embedded_test_server.h" |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
9 #include "net/http/http_response_headers.h" | 9 #include "net/http/http_response_headers.h" |
10 #include "net/test/embedded_test_server/http_request.h" | 10 #include "net/test/embedded_test_server/http_request.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 class EmbeddedTestServerTest: public testing::Test, | 43 class EmbeddedTestServerTest: public testing::Test, |
44 public URLFetcherDelegate { | 44 public URLFetcherDelegate { |
45 public: | 45 public: |
46 EmbeddedTestServerTest() | 46 EmbeddedTestServerTest() |
47 : num_responses_received_(0), | 47 : num_responses_received_(0), |
48 num_responses_expected_(0), | 48 num_responses_expected_(0), |
49 io_thread_("io_thread") { | 49 io_thread_("io_thread") { |
50 } | 50 } |
51 | 51 |
52 virtual void SetUp() OVERRIDE { | 52 virtual void SetUp() override { |
53 base::Thread::Options thread_options; | 53 base::Thread::Options thread_options; |
54 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 54 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
55 ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); | 55 ASSERT_TRUE(io_thread_.StartWithOptions(thread_options)); |
56 | 56 |
57 request_context_getter_ = new TestURLRequestContextGetter( | 57 request_context_getter_ = new TestURLRequestContextGetter( |
58 io_thread_.message_loop_proxy()); | 58 io_thread_.message_loop_proxy()); |
59 | 59 |
60 server_.reset(new EmbeddedTestServer); | 60 server_.reset(new EmbeddedTestServer); |
61 ASSERT_TRUE(server_->InitializeAndWaitUntilReady()); | 61 ASSERT_TRUE(server_->InitializeAndWaitUntilReady()); |
62 } | 62 } |
63 | 63 |
64 virtual void TearDown() OVERRIDE { | 64 virtual void TearDown() override { |
65 ASSERT_TRUE(server_->ShutdownAndWaitUntilComplete()); | 65 ASSERT_TRUE(server_->ShutdownAndWaitUntilComplete()); |
66 } | 66 } |
67 | 67 |
68 // URLFetcherDelegate override. | 68 // URLFetcherDelegate override. |
69 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { | 69 virtual void OnURLFetchComplete(const URLFetcher* source) override { |
70 ++num_responses_received_; | 70 ++num_responses_received_; |
71 if (num_responses_received_ == num_responses_expected_) | 71 if (num_responses_received_ == num_responses_expected_) |
72 base::MessageLoop::current()->Quit(); | 72 base::MessageLoop::current()->Quit(); |
73 } | 73 } |
74 | 74 |
75 // Waits until the specified number of responses are received. | 75 // Waits until the specified number of responses are received. |
76 void WaitForResponses(int num_responses) { | 76 void WaitForResponses(int num_responses) { |
77 num_responses_received_ = 0; | 77 num_responses_received_ = 0; |
78 num_responses_expected_ = num_responses; | 78 num_responses_expected_ = num_responses; |
79 // Will be terminated in OnURLFetchComplete(). | 79 // Will be terminated in OnURLFetchComplete(). |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 : public base::PlatformThread::Delegate, | 253 : public base::PlatformThread::Delegate, |
254 public URLFetcherDelegate { | 254 public URLFetcherDelegate { |
255 public: | 255 public: |
256 EmbeddedTestServerThreadingTestDelegate( | 256 EmbeddedTestServerThreadingTestDelegate( |
257 bool message_loop_present_on_initialize, | 257 bool message_loop_present_on_initialize, |
258 bool message_loop_present_on_shutdown) | 258 bool message_loop_present_on_shutdown) |
259 : message_loop_present_on_initialize_(message_loop_present_on_initialize), | 259 : message_loop_present_on_initialize_(message_loop_present_on_initialize), |
260 message_loop_present_on_shutdown_(message_loop_present_on_shutdown) {} | 260 message_loop_present_on_shutdown_(message_loop_present_on_shutdown) {} |
261 | 261 |
262 // base::PlatformThread::Delegate: | 262 // base::PlatformThread::Delegate: |
263 virtual void ThreadMain() OVERRIDE { | 263 virtual void ThreadMain() override { |
264 scoped_refptr<base::SingleThreadTaskRunner> io_thread_runner; | 264 scoped_refptr<base::SingleThreadTaskRunner> io_thread_runner; |
265 base::Thread io_thread("io_thread"); | 265 base::Thread io_thread("io_thread"); |
266 base::Thread::Options thread_options; | 266 base::Thread::Options thread_options; |
267 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 267 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
268 ASSERT_TRUE(io_thread.StartWithOptions(thread_options)); | 268 ASSERT_TRUE(io_thread.StartWithOptions(thread_options)); |
269 io_thread_runner = io_thread.message_loop_proxy(); | 269 io_thread_runner = io_thread.message_loop_proxy(); |
270 | 270 |
271 scoped_ptr<base::MessageLoop> loop; | 271 scoped_ptr<base::MessageLoop> loop; |
272 if (message_loop_present_on_initialize_) | 272 if (message_loop_present_on_initialize_) |
273 loop.reset(new base::MessageLoopForIO); | 273 loop.reset(new base::MessageLoopForIO); |
(...skipping 17 matching lines...) Expand all Loading... |
291 fetcher.reset(); | 291 fetcher.reset(); |
292 | 292 |
293 // Shut down. | 293 // Shut down. |
294 if (message_loop_present_on_shutdown_) | 294 if (message_loop_present_on_shutdown_) |
295 loop.reset(); | 295 loop.reset(); |
296 | 296 |
297 ASSERT_TRUE(server.ShutdownAndWaitUntilComplete()); | 297 ASSERT_TRUE(server.ShutdownAndWaitUntilComplete()); |
298 } | 298 } |
299 | 299 |
300 // URLFetcherDelegate override. | 300 // URLFetcherDelegate override. |
301 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { | 301 virtual void OnURLFetchComplete(const URLFetcher* source) override { |
302 base::MessageLoop::current()->Quit(); | 302 base::MessageLoop::current()->Quit(); |
303 } | 303 } |
304 | 304 |
305 private: | 305 private: |
306 bool message_loop_present_on_initialize_; | 306 bool message_loop_present_on_initialize_; |
307 bool message_loop_present_on_shutdown_; | 307 bool message_loop_present_on_shutdown_; |
308 | 308 |
309 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServerThreadingTestDelegate); | 309 DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServerThreadingTestDelegate); |
310 }; | 310 }; |
311 | 311 |
312 TEST_P(EmbeddedTestServerThreadingTest, RunTest) { | 312 TEST_P(EmbeddedTestServerThreadingTest, RunTest) { |
313 // The actual test runs on a separate thread so it can screw with the presence | 313 // The actual test runs on a separate thread so it can screw with the presence |
314 // of a MessageLoop - the test suite already sets up a MessageLoop for the | 314 // of a MessageLoop - the test suite already sets up a MessageLoop for the |
315 // main test thread. | 315 // main test thread. |
316 base::PlatformThreadHandle thread_handle; | 316 base::PlatformThreadHandle thread_handle; |
317 EmbeddedTestServerThreadingTestDelegate delegate( | 317 EmbeddedTestServerThreadingTestDelegate delegate( |
318 std::tr1::get<0>(GetParam()), | 318 std::tr1::get<0>(GetParam()), |
319 std::tr1::get<1>(GetParam())); | 319 std::tr1::get<1>(GetParam())); |
320 ASSERT_TRUE(base::PlatformThread::Create(0, &delegate, &thread_handle)); | 320 ASSERT_TRUE(base::PlatformThread::Create(0, &delegate, &thread_handle)); |
321 base::PlatformThread::Join(thread_handle); | 321 base::PlatformThread::Join(thread_handle); |
322 } | 322 } |
323 | 323 |
324 INSTANTIATE_TEST_CASE_P(EmbeddedTestServerThreadingTestInstantiation, | 324 INSTANTIATE_TEST_CASE_P(EmbeddedTestServerThreadingTestInstantiation, |
325 EmbeddedTestServerThreadingTest, | 325 EmbeddedTestServerThreadingTest, |
326 testing::Combine(testing::Bool(), testing::Bool())); | 326 testing::Combine(testing::Bool(), testing::Bool())); |
327 | 327 |
328 } // namespace test_server | 328 } // namespace test_server |
329 } // namespace net | 329 } // namespace net |
OLD | NEW |