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/spawned_test_server/spawner_communicator.h" | 5 #include "net/test/spawned_test_server/spawner_communicator.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/supports_user_data.h" | 10 #include "base/supports_user_data.h" |
11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
16 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
17 #include "net/base/upload_bytes_element_reader.h" | 17 #include "net/base/upload_bytes_element_reader.h" |
18 #include "net/base/upload_data_stream.h" | 18 #include "net/base/upload_data_stream.h" |
19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
20 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
23 namespace net { | 23 namespace net { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 GURL GenerateSpawnerCommandURL(const std::string& command, uint16 port) { | 27 GURL GenerateSpawnerCommandURL(const std::string& command, uint16 port) { |
28 // Always performs HTTP request for sending command to the spawner server. | 28 // Always performs HTTP request for sending command to the spawner server. |
29 return GURL(base::StringPrintf("%s:%u/%s", "http://127.0.0.1", port, | 29 return GURL(base::StringPrintf( |
30 command.c_str())); | 30 "%s:%u/%s", "http://127.0.0.1", port, command.c_str())); |
31 } | 31 } |
32 | 32 |
33 int kBufferSize = 2048; | 33 int kBufferSize = 2048; |
34 | 34 |
35 // A class to hold all data needed to send a command to spawner server. | 35 // A class to hold all data needed to send a command to spawner server. |
36 class SpawnerRequestData : public base::SupportsUserData::Data { | 36 class SpawnerRequestData : public base::SupportsUserData::Data { |
37 public: | 37 public: |
38 SpawnerRequestData(int id, int* result_code, std::string* data_received) | 38 SpawnerRequestData(int id, int* result_code, std::string* data_received) |
39 : request_id_(id), | 39 : request_id_(id), |
40 buf_(new IOBuffer(kBufferSize)), | 40 buf_(new IOBuffer(kBufferSize)), |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 }; | 97 }; |
98 | 98 |
99 } // namespace | 99 } // namespace |
100 | 100 |
101 SpawnerCommunicator::SpawnerCommunicator(uint16 port) | 101 SpawnerCommunicator::SpawnerCommunicator(uint16 port) |
102 : io_thread_("spawner_communicator"), | 102 : io_thread_("spawner_communicator"), |
103 event_(false, false), | 103 event_(false, false), |
104 port_(port), | 104 port_(port), |
105 next_id_(0), | 105 next_id_(0), |
106 weak_factory_(this), | 106 weak_factory_(this), |
107 is_running_(false) {} | 107 is_running_(false) { |
| 108 } |
108 | 109 |
109 SpawnerCommunicator::~SpawnerCommunicator() { | 110 SpawnerCommunicator::~SpawnerCommunicator() { |
110 DCHECK(!is_running_); | 111 DCHECK(!is_running_); |
111 } | 112 } |
112 | 113 |
113 void SpawnerCommunicator::WaitForResponse() { | 114 void SpawnerCommunicator::WaitForResponse() { |
114 DCHECK_NE(base::MessageLoop::current(), io_thread_.message_loop()); | 115 DCHECK_NE(base::MessageLoop::current(), io_thread_.message_loop()); |
115 event_.Wait(); | 116 event_.Wait(); |
116 event_.Reset(); | 117 event_.Reset(); |
117 } | 118 } |
(...skipping 29 matching lines...) Expand all Loading... |
147 std::string* data_received) { | 148 std::string* data_received) { |
148 if (!result_code || !data_received) | 149 if (!result_code || !data_received) |
149 return; | 150 return; |
150 // Start the communicator thread to talk to test server spawner. | 151 // Start the communicator thread to talk to test server spawner. |
151 StartIOThread(); | 152 StartIOThread(); |
152 DCHECK(io_thread_.message_loop()); | 153 DCHECK(io_thread_.message_loop()); |
153 | 154 |
154 // Since the method will be blocked until SpawnerCommunicator gets result | 155 // Since the method will be blocked until SpawnerCommunicator gets result |
155 // from the spawner server or timed-out. It's safe to use base::Unretained | 156 // from the spawner server or timed-out. It's safe to use base::Unretained |
156 // when using base::Bind. | 157 // when using base::Bind. |
157 io_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | 158 io_thread_.message_loop()->PostTask( |
158 &SpawnerCommunicator::SendCommandAndWaitForResultOnIOThread, | 159 FROM_HERE, |
159 base::Unretained(this), command, post_data, result_code, data_received)); | 160 base::Bind(&SpawnerCommunicator::SendCommandAndWaitForResultOnIOThread, |
| 161 base::Unretained(this), |
| 162 command, |
| 163 post_data, |
| 164 result_code, |
| 165 data_received)); |
160 WaitForResponse(); | 166 WaitForResponse(); |
161 } | 167 } |
162 | 168 |
163 void SpawnerCommunicator::SendCommandAndWaitForResultOnIOThread( | 169 void SpawnerCommunicator::SendCommandAndWaitForResultOnIOThread( |
164 const std::string& command, | 170 const std::string& command, |
165 const std::string& post_data, | 171 const std::string& post_data, |
166 int* result_code, | 172 int* result_code, |
167 std::string* data_received) { | 173 std::string* data_received) { |
168 base::MessageLoop* loop = io_thread_.message_loop(); | 174 base::MessageLoop* loop = io_thread_.message_loop(); |
169 DCHECK(loop); | 175 DCHECK(loop); |
170 DCHECK_EQ(base::MessageLoop::current(), loop); | 176 DCHECK_EQ(base::MessageLoop::current(), loop); |
171 | 177 |
172 // Prepare the URLRequest for sending the command. | 178 // Prepare the URLRequest for sending the command. |
173 DCHECK(!cur_request_.get()); | 179 DCHECK(!cur_request_.get()); |
174 context_.reset(new TestURLRequestContext); | 180 context_.reset(new TestURLRequestContext); |
175 cur_request_ = context_->CreateRequest( | 181 cur_request_ = context_->CreateRequest( |
176 GenerateSpawnerCommandURL(command, port_), DEFAULT_PRIORITY, this, NULL); | 182 GenerateSpawnerCommandURL(command, port_), DEFAULT_PRIORITY, this, NULL); |
177 DCHECK(cur_request_); | 183 DCHECK(cur_request_); |
178 int current_request_id = ++next_id_; | 184 int current_request_id = ++next_id_; |
179 SpawnerRequestData* data = new SpawnerRequestData(current_request_id, | 185 SpawnerRequestData* data = |
180 result_code, | 186 new SpawnerRequestData(current_request_id, result_code, data_received); |
181 data_received); | |
182 DCHECK(data); | 187 DCHECK(data); |
183 cur_request_->SetUserData(this, data); | 188 cur_request_->SetUserData(this, data); |
184 | 189 |
185 if (post_data.empty()) { | 190 if (post_data.empty()) { |
186 cur_request_->set_method("GET"); | 191 cur_request_->set_method("GET"); |
187 } else { | 192 } else { |
188 cur_request_->set_method("POST"); | 193 cur_request_->set_method("POST"); |
189 scoped_ptr<UploadElementReader> reader( | 194 scoped_ptr<UploadElementReader> reader( |
190 UploadOwnedBytesElementReader::CreateWithString(post_data)); | 195 UploadOwnedBytesElementReader::CreateWithString(post_data)); |
191 cur_request_->set_upload(make_scoped_ptr( | 196 cur_request_->set_upload( |
192 UploadDataStream::CreateWithReader(reader.Pass(), 0))); | 197 make_scoped_ptr(UploadDataStream::CreateWithReader(reader.Pass(), 0))); |
193 net::HttpRequestHeaders headers; | 198 net::HttpRequestHeaders headers; |
194 headers.SetHeader(net::HttpRequestHeaders::kContentType, | 199 headers.SetHeader(net::HttpRequestHeaders::kContentType, |
195 "application/json"); | 200 "application/json"); |
196 cur_request_->SetExtraRequestHeaders(headers); | 201 cur_request_->SetExtraRequestHeaders(headers); |
197 } | 202 } |
198 | 203 |
199 // Post a task to timeout this request if it takes too long. | 204 // Post a task to timeout this request if it takes too long. |
200 base::MessageLoop::current()->PostDelayedTask( | 205 base::MessageLoop::current()->PostDelayedTask( |
201 FROM_HERE, | 206 FROM_HERE, |
202 base::Bind(&SpawnerCommunicator::OnTimeout, | 207 base::Bind(&SpawnerCommunicator::OnTimeout, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 } | 328 } |
324 } | 329 } |
325 | 330 |
326 bool SpawnerCommunicator::StartServer(const std::string& arguments, | 331 bool SpawnerCommunicator::StartServer(const std::string& arguments, |
327 uint16* port) { | 332 uint16* port) { |
328 *port = 0; | 333 *port = 0; |
329 // Send the start command to spawner server to start the Python test server | 334 // Send the start command to spawner server to start the Python test server |
330 // on remote machine. | 335 // on remote machine. |
331 std::string server_return_data; | 336 std::string server_return_data; |
332 int result_code; | 337 int result_code; |
333 SendCommandAndWaitForResult("start", arguments, &result_code, | 338 SendCommandAndWaitForResult( |
334 &server_return_data); | 339 "start", arguments, &result_code, &server_return_data); |
335 if (OK != result_code || server_return_data.empty()) | 340 if (OK != result_code || server_return_data.empty()) |
336 return false; | 341 return false; |
337 | 342 |
338 // Check whether the data returned from spawner server is JSON-formatted. | 343 // Check whether the data returned from spawner server is JSON-formatted. |
339 scoped_ptr<base::Value> value(base::JSONReader::Read(server_return_data)); | 344 scoped_ptr<base::Value> value(base::JSONReader::Read(server_return_data)); |
340 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 345 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
341 LOG(ERROR) << "Invalid server data: " << server_return_data.c_str(); | 346 LOG(ERROR) << "Invalid server data: " << server_return_data.c_str(); |
342 return false; | 347 return false; |
343 } | 348 } |
344 | 349 |
(...skipping 26 matching lines...) Expand all Loading... |
371 std::string server_return_data; | 376 std::string server_return_data; |
372 int result_code; | 377 int result_code; |
373 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); | 378 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); |
374 Shutdown(); | 379 Shutdown(); |
375 if (OK != result_code || server_return_data != "killed") | 380 if (OK != result_code || server_return_data != "killed") |
376 return false; | 381 return false; |
377 return true; | 382 return true; |
378 } | 383 } |
379 | 384 |
380 } // namespace net | 385 } // namespace net |
OLD | NEW |