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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 | 92 |
93 void HttpListenSocket::Listen() { | 93 void HttpListenSocket::Listen() { |
94 DCHECK(thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
95 TCPListenSocket::Listen(); | 95 TCPListenSocket::Listen(); |
96 } | 96 } |
97 | 97 |
98 HttpListenSocket::~HttpListenSocket() { | 98 HttpListenSocket::~HttpListenSocket() { |
99 DCHECK(thread_checker_.CalledOnValidThread()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
100 } | 100 } |
101 | 101 |
102 void HttpListenSocket::DetachFromThread() { | |
103 thread_checker_.DetachFromThread(); | |
104 } | |
105 | |
102 EmbeddedTestServer::EmbeddedTestServer() | 106 EmbeddedTestServer::EmbeddedTestServer() |
103 : port_(-1), | 107 : port_(-1), |
104 weak_factory_(this) { | 108 weak_factory_(this) { |
105 DCHECK(thread_checker_.CalledOnValidThread()); | 109 DCHECK(thread_checker_.CalledOnValidThread()); |
106 } | 110 } |
107 | 111 |
108 EmbeddedTestServer::~EmbeddedTestServer() { | 112 EmbeddedTestServer::~EmbeddedTestServer() { |
109 DCHECK(thread_checker_.CalledOnValidThread()); | 113 DCHECK(thread_checker_.CalledOnValidThread()); |
110 | 114 |
111 if (Started() && !ShutdownAndWaitUntilComplete()) { | 115 if (Started() && !ShutdownAndWaitUntilComplete()) { |
112 LOG(ERROR) << "EmbeddedTestServer failed to shut down."; | 116 LOG(ERROR) << "EmbeddedTestServer failed to shut down."; |
113 } | 117 } |
114 } | 118 } |
115 | 119 |
116 bool EmbeddedTestServer::InitializeAndWaitUntilReady() { | 120 bool EmbeddedTestServer::InitializeAndWaitUntilReady() { |
117 base::Thread::Options thread_options; | 121 StartThread(); |
118 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | |
119 io_thread_.reset(new base::Thread("EmbeddedTestServer io thread")); | |
120 CHECK(io_thread_->StartWithOptions(thread_options)); | |
121 | |
122 DCHECK(thread_checker_.CalledOnValidThread()); | 122 DCHECK(thread_checker_.CalledOnValidThread()); |
123 | |
124 if (!PostTaskToIOThreadAndWait(base::Bind( | 123 if (!PostTaskToIOThreadAndWait(base::Bind( |
125 &EmbeddedTestServer::InitializeOnIOThread, base::Unretained(this)))) { | 124 &EmbeddedTestServer::InitializeOnIOThread, base::Unretained(this)))) { |
126 return false; | 125 return false; |
127 } | 126 } |
127 return Started() && base_url_.is_valid(); | |
128 } | |
128 | 129 |
129 return Started() && base_url_.is_valid(); | 130 void EmbeddedTestServer::StopThread() { |
131 io_thread_->Stop(); | |
132 io_thread_.reset(); | |
133 thread_checker_.DetachFromThread(); | |
134 listen_socket_->DetachFromThread(); | |
135 } | |
136 | |
137 void EmbeddedTestServer::RestartThreadAndListen() { | |
138 StartThread(); | |
139 if (!PostTaskToIOThreadAndWait(base::Bind( | |
140 &EmbeddedTestServer::ListenOnIOThread, base::Unretained(this)))) { | |
satorux1
2013/11/27 03:27:17
did you meant to add LOG(ERROR) here? Otherwise th
oshima
2013/11/27 07:41:54
changed to CHECK. This should never fail in tests
| |
141 return; | |
142 } | |
130 } | 143 } |
131 | 144 |
132 bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() { | 145 bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() { |
133 DCHECK(thread_checker_.CalledOnValidThread()); | 146 DCHECK(thread_checker_.CalledOnValidThread()); |
134 | 147 |
135 return PostTaskToIOThreadAndWait(base::Bind( | 148 return PostTaskToIOThreadAndWait(base::Bind( |
136 &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this))); | 149 &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this))); |
137 } | 150 } |
138 | 151 |
152 void EmbeddedTestServer::StartThread() { | |
153 DCHECK(!io_thread_.get()); | |
154 base::Thread::Options thread_options; | |
155 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | |
156 io_thread_.reset(new base::Thread("EmbeddedTestServer io thread")); | |
157 CHECK(io_thread_->StartWithOptions(thread_options)); | |
158 } | |
159 | |
139 void EmbeddedTestServer::InitializeOnIOThread() { | 160 void EmbeddedTestServer::InitializeOnIOThread() { |
140 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); | 161 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); |
141 DCHECK(!Started()); | 162 DCHECK(!Started()); |
142 | 163 |
143 SocketDescriptor socket_descriptor = | 164 SocketDescriptor socket_descriptor = |
144 TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port_); | 165 TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port_); |
145 if (socket_descriptor == kInvalidSocket) | 166 if (socket_descriptor == kInvalidSocket) |
146 return; | 167 return; |
147 | 168 |
148 listen_socket_.reset(new HttpListenSocket(socket_descriptor, this)); | 169 listen_socket_.reset(new HttpListenSocket(socket_descriptor, this)); |
149 listen_socket_->Listen(); | 170 listen_socket_->Listen(); |
150 | 171 |
151 IPEndPoint address; | 172 IPEndPoint address; |
152 int result = listen_socket_->GetLocalAddress(&address); | 173 int result = listen_socket_->GetLocalAddress(&address); |
153 if (result == OK) { | 174 if (result == OK) { |
154 base_url_ = GURL(std::string("http://") + address.ToString()); | 175 base_url_ = GURL(std::string("http://") + address.ToString()); |
155 } else { | 176 } else { |
156 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result); | 177 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result); |
157 } | 178 } |
158 } | 179 } |
159 | 180 |
181 void EmbeddedTestServer::ListenOnIOThread() { | |
182 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); | |
183 DCHECK(Started()); | |
184 listen_socket_->Listen(); | |
185 } | |
186 | |
160 void EmbeddedTestServer::ShutdownOnIOThread() { | 187 void EmbeddedTestServer::ShutdownOnIOThread() { |
161 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); | 188 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); |
162 | 189 |
163 listen_socket_.reset(); | 190 listen_socket_.reset(); |
164 STLDeleteContainerPairSecondPointers(connections_.begin(), | 191 STLDeleteContainerPairSecondPointers(connections_.begin(), |
165 connections_.end()); | 192 connections_.end()); |
166 connections_.clear(); | 193 connections_.clear(); |
167 } | 194 } |
168 | 195 |
169 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, | 196 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 FROM_HERE, closure, run_loop.QuitClosure())) { | 310 FROM_HERE, closure, run_loop.QuitClosure())) { |
284 return false; | 311 return false; |
285 } | 312 } |
286 run_loop.Run(); | 313 run_loop.Run(); |
287 | 314 |
288 return true; | 315 return true; |
289 } | 316 } |
290 | 317 |
291 } // namespace test_server | 318 } // namespace test_server |
292 } // namespace net | 319 } // namespace net |
OLD | NEW |