This source file includes following definitions.
- GenerateSpawnerCommandURL
- response_started_count_
- DoesRequestIdMatch
- buf
- IsResultOK
- ClearReceivedData
- SetResultCode
- IncreaseResponseStartedCount
- response_started_count
- ConsumeBytesRead
- is_running_
- WaitForResponse
- StartIOThread
- Shutdown
- SendCommandAndWaitForResult
- SendCommandAndWaitForResultOnIOThread
- OnTimeout
- OnSpawnerCommandCompleted
- ReadResult
- OnResponseStarted
- OnReadCompleted
- StartServer
- StopServer
#include "net/test/spawned_test_server/spawner_communicator.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/supports_user_data.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/net_util.h"
#include "net/base/request_priority.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request_test_util.h"
#include "url/gurl.h"
namespace net {
namespace {
GURL GenerateSpawnerCommandURL(const std::string& command, uint16 port) {
return GURL(base::StringPrintf("%s:%u/%s", "http://127.0.0.1", port,
command.c_str()));
}
int kBufferSize = 2048;
class SpawnerRequestData : public base::SupportsUserData::Data {
public:
SpawnerRequestData(int id, int* result_code, std::string* data_received)
: request_id_(id),
buf_(new IOBuffer(kBufferSize)),
result_code_(result_code),
data_received_(data_received),
response_started_count_(0) {
DCHECK(result_code);
*result_code_ = OK;
DCHECK(data_received);
data_received_->clear();
}
virtual ~SpawnerRequestData() {}
bool DoesRequestIdMatch(int request_id) const {
return request_id_ == request_id;
}
IOBuffer* buf() const { return buf_.get(); }
bool IsResultOK() const { return *result_code_ == OK; }
void ClearReceivedData() { data_received_->clear(); }
void SetResultCode(int result_code) { *result_code_ = result_code; }
void IncreaseResponseStartedCount() { response_started_count_++; }
int response_started_count() const { return response_started_count_; }
bool ConsumeBytesRead(int num_bytes) {
if (num_bytes <= 0)
return false;
data_received_->append(buf_->data(), num_bytes);
return true;
}
private:
int request_id_;
scoped_refptr<IOBuffer> buf_;
int* result_code_;
std::string* data_received_;
int response_started_count_;
DISALLOW_COPY_AND_ASSIGN(SpawnerRequestData);
};
}
SpawnerCommunicator::SpawnerCommunicator(uint16 port)
: io_thread_("spawner_communicator"),
event_(false, false),
port_(port),
next_id_(0),
weak_factory_(this),
is_running_(false) {}
SpawnerCommunicator::~SpawnerCommunicator() {
DCHECK(!is_running_);
}
void SpawnerCommunicator::WaitForResponse() {
DCHECK_NE(base::MessageLoop::current(), io_thread_.message_loop());
event_.Wait();
event_.Reset();
}
void SpawnerCommunicator::StartIOThread() {
DCHECK_NE(base::MessageLoop::current(), io_thread_.message_loop());
if (is_running_)
return;
allowed_port_.reset(new ScopedPortException(port_));
base::Thread::Options options;
options.message_loop_type = base::MessageLoop::TYPE_IO;
is_running_ = io_thread_.StartWithOptions(options);
DCHECK(is_running_);
}
void SpawnerCommunicator::Shutdown() {
DCHECK_NE(base::MessageLoop::current(), io_thread_.message_loop());
DCHECK(is_running_);
DCHECK(!cur_request_.get());
DCHECK(!context_.get());
is_running_ = false;
io_thread_.Stop();
allowed_port_.reset();
}
void SpawnerCommunicator::SendCommandAndWaitForResult(
const std::string& command,
const std::string& post_data,
int* result_code,
std::string* data_received) {
if (!result_code || !data_received)
return;
StartIOThread();
DCHECK(io_thread_.message_loop());
io_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
&SpawnerCommunicator::SendCommandAndWaitForResultOnIOThread,
base::Unretained(this), command, post_data, result_code, data_received));
WaitForResponse();
}
void SpawnerCommunicator::SendCommandAndWaitForResultOnIOThread(
const std::string& command,
const std::string& post_data,
int* result_code,
std::string* data_received) {
base::MessageLoop* loop = io_thread_.message_loop();
DCHECK(loop);
DCHECK_EQ(base::MessageLoop::current(), loop);
DCHECK(!cur_request_.get());
context_.reset(new TestURLRequestContext);
cur_request_ = context_->CreateRequest(
GenerateSpawnerCommandURL(command, port_), DEFAULT_PRIORITY, this, NULL);
DCHECK(cur_request_);
int current_request_id = ++next_id_;
SpawnerRequestData* data = new SpawnerRequestData(current_request_id,
result_code,
data_received);
DCHECK(data);
cur_request_->SetUserData(this, data);
if (post_data.empty()) {
cur_request_->set_method("GET");
} else {
cur_request_->set_method("POST");
scoped_ptr<UploadElementReader> reader(
UploadOwnedBytesElementReader::CreateWithString(post_data));
cur_request_->set_upload(make_scoped_ptr(
UploadDataStream::CreateWithReader(reader.Pass(), 0)));
net::HttpRequestHeaders headers;
headers.SetHeader(net::HttpRequestHeaders::kContentType,
"application/json");
cur_request_->SetExtraRequestHeaders(headers);
}
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&SpawnerCommunicator::OnTimeout,
weak_factory_.GetWeakPtr(),
current_request_id),
TestTimeouts::action_max_timeout());
cur_request_->Start();
}
void SpawnerCommunicator::OnTimeout(int id) {
if (!cur_request_.get())
return;
SpawnerRequestData* data =
static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this));
DCHECK(data);
if (!data->DoesRequestIdMatch(id))
return;
data->SetResultCode(ERR_TIMED_OUT);
cur_request_->Cancel();
OnSpawnerCommandCompleted(cur_request_.get());
}
void SpawnerCommunicator::OnSpawnerCommandCompleted(URLRequest* request) {
if (!cur_request_.get())
return;
DCHECK_EQ(request, cur_request_.get());
SpawnerRequestData* data =
static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this));
DCHECK(data);
if (!cur_request_->status().is_success())
data->SetResultCode(cur_request_->status().error());
if (!data->IsResultOK()) {
LOG(ERROR) << "request failed, status: "
<< static_cast<int>(request->status().status())
<< ", error: " << request->status().error();
data->ClearReceivedData();
} else {
DCHECK_EQ(1, data->response_started_count());
}
cur_request_.reset();
context_.reset();
weak_factory_.InvalidateWeakPtrs();
event_.Signal();
}
void SpawnerCommunicator::ReadResult(URLRequest* request) {
DCHECK_EQ(request, cur_request_.get());
SpawnerRequestData* data =
static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this));
DCHECK(data);
IOBuffer* buf = data->buf();
while (true) {
int num_bytes;
if (!request->Read(buf, kBufferSize, &num_bytes)) {
if (!request->status().is_io_pending())
OnSpawnerCommandCompleted(request);
return;
}
if (!data->ConsumeBytesRead(num_bytes)) {
OnSpawnerCommandCompleted(request);
return;
}
}
}
void SpawnerCommunicator::OnResponseStarted(URLRequest* request) {
DCHECK_EQ(request, cur_request_.get());
SpawnerRequestData* data =
static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this));
DCHECK(data);
data->IncreaseResponseStartedCount();
if (!request->status().is_success()) {
OnSpawnerCommandCompleted(request);
return;
}
if (request->GetResponseCode() != 200) {
LOG(ERROR) << "Spawner server returned bad status: "
<< request->response_headers()->GetStatusLine();
data->SetResultCode(ERR_FAILED);
request->Cancel();
OnSpawnerCommandCompleted(request);
return;
}
ReadResult(request);
}
void SpawnerCommunicator::OnReadCompleted(URLRequest* request, int num_bytes) {
if (!cur_request_.get())
return;
DCHECK_EQ(request, cur_request_.get());
SpawnerRequestData* data =
static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this));
DCHECK(data);
if (data->ConsumeBytesRead(num_bytes)) {
ReadResult(request);
} else {
OnSpawnerCommandCompleted(request);
}
}
bool SpawnerCommunicator::StartServer(const std::string& arguments,
uint16* port) {
*port = 0;
std::string server_return_data;
int result_code;
SendCommandAndWaitForResult("start", arguments, &result_code,
&server_return_data);
if (OK != result_code || server_return_data.empty())
return false;
scoped_ptr<base::Value> value(base::JSONReader::Read(server_return_data));
if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) {
LOG(ERROR) << "Invalid server data: " << server_return_data.c_str();
return false;
}
base::DictionaryValue* server_data =
static_cast<base::DictionaryValue*>(value.get());
std::string message;
if (!server_data->GetString("message", &message) || message != "started") {
LOG(ERROR) << "Invalid message in server data: ";
return false;
}
int int_port;
if (!server_data->GetInteger("port", &int_port) || int_port <= 0 ||
int_port > kuint16max) {
LOG(ERROR) << "Invalid port value: " << int_port;
return false;
}
*port = static_cast<uint16>(int_port);
return true;
}
bool SpawnerCommunicator::StopServer() {
if (!is_running_)
return true;
std::string server_return_data;
int result_code;
SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data);
Shutdown();
if (OK != result_code || server_return_data != "killed")
return false;
return true;
}
}