Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(569)

Side by Side Diff: net/test/embedded_test_server/embedded_test_server.cc

Issue 83633004: Do not spawn a thread in browser/interactive ui tests before spawning sandbox host process (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: style fix Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/test/embedded_test_server/embedded_test_server.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 CHECK(PostTaskToIOThreadAndWait(base::Bind(
140 &EmbeddedTestServer::ListenOnIOThread, base::Unretained(this))));
130 } 141 }
131 142
132 bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() { 143 bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() {
133 DCHECK(thread_checker_.CalledOnValidThread()); 144 DCHECK(thread_checker_.CalledOnValidThread());
134 145
135 return PostTaskToIOThreadAndWait(base::Bind( 146 return PostTaskToIOThreadAndWait(base::Bind(
136 &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this))); 147 &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this)));
137 } 148 }
138 149
150 void EmbeddedTestServer::StartThread() {
151 DCHECK(!io_thread_.get());
152 base::Thread::Options thread_options;
153 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
154 io_thread_.reset(new base::Thread("EmbeddedTestServer io thread"));
155 CHECK(io_thread_->StartWithOptions(thread_options));
156 }
157
139 void EmbeddedTestServer::InitializeOnIOThread() { 158 void EmbeddedTestServer::InitializeOnIOThread() {
140 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); 159 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread());
141 DCHECK(!Started()); 160 DCHECK(!Started());
142 161
143 SocketDescriptor socket_descriptor = 162 SocketDescriptor socket_descriptor =
144 TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port_); 163 TCPListenSocket::CreateAndBindAnyPort("127.0.0.1", &port_);
145 if (socket_descriptor == kInvalidSocket) 164 if (socket_descriptor == kInvalidSocket)
146 return; 165 return;
147 166
148 listen_socket_.reset(new HttpListenSocket(socket_descriptor, this)); 167 listen_socket_.reset(new HttpListenSocket(socket_descriptor, this));
149 listen_socket_->Listen(); 168 listen_socket_->Listen();
150 169
151 IPEndPoint address; 170 IPEndPoint address;
152 int result = listen_socket_->GetLocalAddress(&address); 171 int result = listen_socket_->GetLocalAddress(&address);
153 if (result == OK) { 172 if (result == OK) {
154 base_url_ = GURL(std::string("http://") + address.ToString()); 173 base_url_ = GURL(std::string("http://") + address.ToString());
155 } else { 174 } else {
156 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result); 175 LOG(ERROR) << "GetLocalAddress failed: " << ErrorToString(result);
157 } 176 }
158 } 177 }
159 178
179 void EmbeddedTestServer::ListenOnIOThread() {
180 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread());
181 DCHECK(Started());
182 listen_socket_->Listen();
183 }
184
160 void EmbeddedTestServer::ShutdownOnIOThread() { 185 void EmbeddedTestServer::ShutdownOnIOThread() {
161 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); 186 DCHECK(io_thread_->message_loop_proxy()->BelongsToCurrentThread());
162 187
163 listen_socket_.reset(); 188 listen_socket_.reset();
164 STLDeleteContainerPairSecondPointers(connections_.begin(), 189 STLDeleteContainerPairSecondPointers(connections_.begin(),
165 connections_.end()); 190 connections_.end());
166 connections_.clear(); 191 connections_.clear();
167 } 192 }
168 193
169 void EmbeddedTestServer::HandleRequest(HttpConnection* connection, 194 void EmbeddedTestServer::HandleRequest(HttpConnection* connection,
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 FROM_HERE, closure, run_loop.QuitClosure())) { 308 FROM_HERE, closure, run_loop.QuitClosure())) {
284 return false; 309 return false;
285 } 310 }
286 run_loop.Run(); 311 run_loop.Run();
287 312
288 return true; 313 return true;
289 } 314 }
290 315
291 } // namespace test_server 316 } // namespace test_server
292 } // namespace net 317 } // namespace net
OLDNEW
« no previous file with comments | « net/test/embedded_test_server/embedded_test_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698