OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/test/chromedriver/net/test_http_server.h" | 5 #include "chrome/test/chromedriver/net/test_http_server.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/server/http_server_request_info.h" | 15 #include "net/server/http_server_request_info.h" |
16 #include "net/socket/tcp_server_socket.h" | 16 #include "net/socket/tcp_server_socket.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 const int kBufferSize = 100 * 1024 * 1024; // 100 MB | 19 const int kBufferSize = 100 * 1024 * 1024; // 100 MB |
20 | 20 |
21 TestHttpServer::TestHttpServer() | 21 TestHttpServer::TestHttpServer() |
22 : thread_("ServerThread"), | 22 : thread_("ServerThread"), |
23 all_closed_event_(false, true), | 23 all_closed_event_(false, true), |
24 request_action_(kAccept), | 24 request_action_(kAccept), |
25 message_action_(kEchoMessage) { | 25 message_action_(kEchoMessage) { |
26 } | 26 } |
27 | 27 |
28 TestHttpServer::~TestHttpServer() { | 28 TestHttpServer::~TestHttpServer() { |
29 } | 29 } |
30 | 30 |
31 bool TestHttpServer::Start() { | 31 bool TestHttpServer::Start() { |
32 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | 32 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
33 bool thread_started = thread_.StartWithOptions(options); | 33 bool thread_started = thread_.StartWithOptions(options); |
34 EXPECT_TRUE(thread_started); | 34 EXPECT_TRUE(thread_started); |
35 if (!thread_started) | 35 if (!thread_started) |
36 return false; | 36 return false; |
37 bool success; | 37 bool success; |
38 base::WaitableEvent event(false, false); | 38 base::WaitableEvent event(false, false); |
39 thread_.message_loop_proxy()->PostTask( | 39 thread_.task_runner()->PostTask( |
40 FROM_HERE, | 40 FROM_HERE, base::Bind(&TestHttpServer::StartOnServerThread, |
41 base::Bind(&TestHttpServer::StartOnServerThread, | 41 base::Unretained(this), &success, &event)); |
42 base::Unretained(this), &success, &event)); | |
43 event.Wait(); | 42 event.Wait(); |
44 return success; | 43 return success; |
45 } | 44 } |
46 | 45 |
47 void TestHttpServer::Stop() { | 46 void TestHttpServer::Stop() { |
48 if (!thread_.IsRunning()) | 47 if (!thread_.IsRunning()) |
49 return; | 48 return; |
50 base::WaitableEvent event(false, false); | 49 base::WaitableEvent event(false, false); |
51 thread_.message_loop_proxy()->PostTask( | 50 thread_.task_runner()->PostTask( |
52 FROM_HERE, | 51 FROM_HERE, base::Bind(&TestHttpServer::StopOnServerThread, |
53 base::Bind(&TestHttpServer::StopOnServerThread, | 52 base::Unretained(this), &event)); |
54 base::Unretained(this), &event)); | |
55 event.Wait(); | 53 event.Wait(); |
56 thread_.Stop(); | 54 thread_.Stop(); |
57 } | 55 } |
58 | 56 |
59 bool TestHttpServer::WaitForConnectionsToClose() { | 57 bool TestHttpServer::WaitForConnectionsToClose() { |
60 return all_closed_event_.TimedWait(base::TimeDelta::FromSeconds(10)); | 58 return all_closed_event_.TimedWait(base::TimeDelta::FromSeconds(10)); |
61 } | 59 } |
62 | 60 |
63 void TestHttpServer::SetRequestAction(WebSocketRequestAction action) { | 61 void TestHttpServer::SetRequestAction(WebSocketRequestAction action) { |
64 base::AutoLock lock(action_lock_); | 62 base::AutoLock lock(action_lock_); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 server_.reset(NULL); | 143 server_.reset(NULL); |
146 } | 144 } |
147 *success = server_.get(); | 145 *success = server_.get(); |
148 event->Signal(); | 146 event->Signal(); |
149 } | 147 } |
150 | 148 |
151 void TestHttpServer::StopOnServerThread(base::WaitableEvent* event) { | 149 void TestHttpServer::StopOnServerThread(base::WaitableEvent* event) { |
152 server_.reset(NULL); | 150 server_.reset(NULL); |
153 event->Signal(); | 151 event->Signal(); |
154 } | 152 } |
OLD | NEW |