| 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 <limits> | |
| 8 | |
| 9 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 10 #include "base/logging.h" | 8 #include "base/logging.h" |
| 11 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 12 #include "base/supports_user_data.h" | 10 #include "base/supports_user_data.h" |
| 13 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 14 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 15 #include "base/values.h" | 13 #include "base/values.h" |
| 16 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 17 #include "net/base/elements_upload_data_stream.h" | 15 #include "net/base/elements_upload_data_stream.h" |
| 18 #include "net/base/port_util.h" | 16 #include "net/base/port_util.h" |
| 19 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
| 20 #include "net/base/upload_bytes_element_reader.h" | 18 #include "net/base/upload_bytes_element_reader.h" |
| 21 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
| 22 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 23 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 24 | 22 |
| 25 namespace net { | 23 namespace net { |
| 26 | 24 |
| 27 namespace { | 25 namespace { |
| 28 | 26 |
| 29 GURL GenerateSpawnerCommandURL(const std::string& command, uint16_t port) { | 27 GURL GenerateSpawnerCommandURL(const std::string& command, uint16 port) { |
| 30 // Always performs HTTP request for sending command to the spawner server. | 28 // Always performs HTTP request for sending command to the spawner server. |
| 31 return GURL(base::StringPrintf("%s:%u/%s", "http://127.0.0.1", port, | 29 return GURL(base::StringPrintf("%s:%u/%s", "http://127.0.0.1", port, |
| 32 command.c_str())); | 30 command.c_str())); |
| 33 } | 31 } |
| 34 | 32 |
| 35 int kBufferSize = 2048; | 33 int kBufferSize = 2048; |
| 36 | 34 |
| 37 // 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. |
| 38 class SpawnerRequestData : public base::SupportsUserData::Data { | 36 class SpawnerRequestData : public base::SupportsUserData::Data { |
| 39 public: | 37 public: |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 91 |
| 94 // Used to track how many times the OnResponseStarted get called after | 92 // Used to track how many times the OnResponseStarted get called after |
| 95 // sending a command to spawner server. | 93 // sending a command to spawner server. |
| 96 int response_started_count_; | 94 int response_started_count_; |
| 97 | 95 |
| 98 DISALLOW_COPY_AND_ASSIGN(SpawnerRequestData); | 96 DISALLOW_COPY_AND_ASSIGN(SpawnerRequestData); |
| 99 }; | 97 }; |
| 100 | 98 |
| 101 } // namespace | 99 } // namespace |
| 102 | 100 |
| 103 SpawnerCommunicator::SpawnerCommunicator(uint16_t port) | 101 SpawnerCommunicator::SpawnerCommunicator(uint16 port) |
| 104 : io_thread_("spawner_communicator"), | 102 : io_thread_("spawner_communicator"), |
| 105 event_(false, false), | 103 event_(false, false), |
| 106 port_(port), | 104 port_(port), |
| 107 next_id_(0), | 105 next_id_(0), |
| 108 is_running_(false), | 106 is_running_(false), |
| 109 weak_factory_(this) {} | 107 weak_factory_(this) {} |
| 110 | 108 |
| 111 SpawnerCommunicator::~SpawnerCommunicator() { | 109 SpawnerCommunicator::~SpawnerCommunicator() { |
| 112 DCHECK(!is_running_); | 110 DCHECK(!is_running_); |
| 113 } | 111 } |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 317 |
| 320 if (data->ConsumeBytesRead(num_bytes)) { | 318 if (data->ConsumeBytesRead(num_bytes)) { |
| 321 // Keep reading. | 319 // Keep reading. |
| 322 ReadResult(request); | 320 ReadResult(request); |
| 323 } else { | 321 } else { |
| 324 OnSpawnerCommandCompleted(request); | 322 OnSpawnerCommandCompleted(request); |
| 325 } | 323 } |
| 326 } | 324 } |
| 327 | 325 |
| 328 bool SpawnerCommunicator::StartServer(const std::string& arguments, | 326 bool SpawnerCommunicator::StartServer(const std::string& arguments, |
| 329 uint16_t* port) { | 327 uint16* port) { |
| 330 *port = 0; | 328 *port = 0; |
| 331 // Send the start command to spawner server to start the Python test server | 329 // Send the start command to spawner server to start the Python test server |
| 332 // on remote machine. | 330 // on remote machine. |
| 333 std::string server_return_data; | 331 std::string server_return_data; |
| 334 int result_code; | 332 int result_code; |
| 335 SendCommandAndWaitForResult("start", arguments, &result_code, | 333 SendCommandAndWaitForResult("start", arguments, &result_code, |
| 336 &server_return_data); | 334 &server_return_data); |
| 337 if (OK != result_code || server_return_data.empty()) | 335 if (OK != result_code || server_return_data.empty()) |
| 338 return false; | 336 return false; |
| 339 | 337 |
| 340 // Check whether the data returned from spawner server is JSON-formatted. | 338 // Check whether the data returned from spawner server is JSON-formatted. |
| 341 scoped_ptr<base::Value> value = base::JSONReader::Read(server_return_data); | 339 scoped_ptr<base::Value> value = base::JSONReader::Read(server_return_data); |
| 342 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 340 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 343 LOG(ERROR) << "Invalid server data: " << server_return_data.c_str(); | 341 LOG(ERROR) << "Invalid server data: " << server_return_data.c_str(); |
| 344 return false; | 342 return false; |
| 345 } | 343 } |
| 346 | 344 |
| 347 // Check whether spawner server returns valid data. | 345 // Check whether spawner server returns valid data. |
| 348 base::DictionaryValue* server_data = | 346 base::DictionaryValue* server_data = |
| 349 static_cast<base::DictionaryValue*>(value.get()); | 347 static_cast<base::DictionaryValue*>(value.get()); |
| 350 std::string message; | 348 std::string message; |
| 351 if (!server_data->GetString("message", &message) || message != "started") { | 349 if (!server_data->GetString("message", &message) || message != "started") { |
| 352 LOG(ERROR) << "Invalid message in server data: "; | 350 LOG(ERROR) << "Invalid message in server data: "; |
| 353 return false; | 351 return false; |
| 354 } | 352 } |
| 355 int int_port; | 353 int int_port; |
| 356 if (!server_data->GetInteger("port", &int_port) || int_port <= 0 || | 354 if (!server_data->GetInteger("port", &int_port) || int_port <= 0 || |
| 357 int_port > std::numeric_limits<uint16_t>::max()) { | 355 int_port > kuint16max) { |
| 358 LOG(ERROR) << "Invalid port value: " << int_port; | 356 LOG(ERROR) << "Invalid port value: " << int_port; |
| 359 return false; | 357 return false; |
| 360 } | 358 } |
| 361 *port = static_cast<uint16_t>(int_port); | 359 *port = static_cast<uint16>(int_port); |
| 362 return true; | 360 return true; |
| 363 } | 361 } |
| 364 | 362 |
| 365 bool SpawnerCommunicator::StopServer() { | 363 bool SpawnerCommunicator::StopServer() { |
| 366 // It's OK to stop the SpawnerCommunicator without starting it. Some tests | 364 // It's OK to stop the SpawnerCommunicator without starting it. Some tests |
| 367 // have test server on their test fixture but do not actually use it. | 365 // have test server on their test fixture but do not actually use it. |
| 368 if (!is_running_) | 366 if (!is_running_) |
| 369 return true; | 367 return true; |
| 370 | 368 |
| 371 // When the test is done, ask the test server spawner to kill the test server | 369 // When the test is done, ask the test server spawner to kill the test server |
| 372 // on the remote machine. | 370 // on the remote machine. |
| 373 std::string server_return_data; | 371 std::string server_return_data; |
| 374 int result_code; | 372 int result_code; |
| 375 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); | 373 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); |
| 376 Shutdown(); | 374 Shutdown(); |
| 377 if (OK != result_code || server_return_data != "killed") | 375 if (OK != result_code || server_return_data != "killed") |
| 378 return false; | 376 return false; |
| 379 return true; | 377 return true; |
| 380 } | 378 } |
| 381 | 379 |
| 382 } // namespace net | 380 } // namespace net |
| OLD | NEW |