Chromium Code Reviews| 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> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 if (!cur_request_.get()) | 222 if (!cur_request_.get()) |
| 223 return; | 223 return; |
| 224 SpawnerRequestData* data = | 224 SpawnerRequestData* data = |
| 225 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 225 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 226 DCHECK(data); | 226 DCHECK(data); |
| 227 | 227 |
| 228 if (!data->DoesRequestIdMatch(id)) | 228 if (!data->DoesRequestIdMatch(id)) |
| 229 return; | 229 return; |
| 230 // Set the result code and cancel the timed-out task. | 230 // Set the result code and cancel the timed-out task. |
| 231 data->SetResultCode(ERR_TIMED_OUT); | 231 data->SetResultCode(ERR_TIMED_OUT); |
| 232 cur_request_->Cancel(); | 232 int result = cur_request_->Cancel(); |
| 233 OnSpawnerCommandCompleted(cur_request_.get()); | 233 OnSpawnerCommandCompleted(cur_request_.get(), result); |
|
mmenke
2016/08/30 22:13:22
This code is actually buggy - note the ERR_TIMED_O
maksims (do not use this acc)
2016/09/01 12:22:30
Done.
| |
| 234 } | 234 } |
| 235 | 235 |
| 236 void SpawnerCommunicator::OnSpawnerCommandCompleted(URLRequest* request) { | 236 void SpawnerCommunicator::OnSpawnerCommandCompleted(URLRequest* request, |
| 237 int net_error) { | |
| 238 DCHECK_NE(ERR_IO_PENDING, net_error); | |
| 239 | |
| 237 if (!cur_request_.get()) | 240 if (!cur_request_.get()) |
| 238 return; | 241 return; |
| 239 DCHECK_EQ(request, cur_request_.get()); | 242 DCHECK_EQ(request, cur_request_.get()); |
| 240 SpawnerRequestData* data = | 243 SpawnerRequestData* data = |
| 241 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 244 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 242 DCHECK(data); | 245 DCHECK(data); |
| 243 | 246 |
| 244 // If request is faild,return the error code. | 247 // If request is faild,return the error code. |
| 245 if (!cur_request_->status().is_success()) | 248 if (net_error != OK) |
| 246 data->SetResultCode(cur_request_->status().error()); | 249 data->SetResultCode(net_error); |
| 247 | 250 |
| 248 if (!data->IsResultOK()) { | 251 if (!data->IsResultOK()) { |
| 249 LOG(ERROR) << "request failed, status: " | 252 LOG(ERROR) << "request failed, error: " << net_error; |
| 250 << static_cast<int>(request->status().status()) | |
| 251 << ", error: " << request->status().error(); | |
| 252 // Clear the buffer of received data if any net error happened. | 253 // Clear the buffer of received data if any net error happened. |
| 253 data->ClearReceivedData(); | 254 data->ClearReceivedData(); |
| 254 } else { | 255 } else { |
| 255 DCHECK_EQ(1, data->response_started_count()); | 256 DCHECK_EQ(1, data->response_started_count()); |
| 256 } | 257 } |
| 257 | 258 |
| 258 // Clear current request to indicate the completion of sending a command | 259 // Clear current request to indicate the completion of sending a command |
| 259 // to spawner server and getting the result. | 260 // to spawner server and getting the result. |
| 260 cur_request_.reset(); | 261 cur_request_.reset(); |
| 261 context_.reset(); | 262 context_.reset(); |
| 262 // Invalidate the weak pointers on the IO thread. | 263 // Invalidate the weak pointers on the IO thread. |
| 263 weak_factory_.InvalidateWeakPtrs(); | 264 weak_factory_.InvalidateWeakPtrs(); |
| 264 | 265 |
| 265 // Wakeup the caller in user thread. | 266 // Wakeup the caller in user thread. |
| 266 event_.Signal(); | 267 event_.Signal(); |
| 267 } | 268 } |
| 268 | 269 |
| 269 void SpawnerCommunicator::ReadResult(URLRequest* request) { | 270 void SpawnerCommunicator::ReadResult(URLRequest* request) { |
| 270 DCHECK_EQ(request, cur_request_.get()); | 271 DCHECK_EQ(request, cur_request_.get()); |
| 271 SpawnerRequestData* data = | 272 SpawnerRequestData* data = |
| 272 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 273 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 273 DCHECK(data); | 274 DCHECK(data); |
| 274 | 275 |
| 275 IOBuffer* buf = data->buf(); | 276 IOBuffer* buf = data->buf(); |
| 276 // Read as many bytes as are available synchronously. | 277 // Read as many bytes as are available synchronously. |
| 277 while (true) { | 278 while (true) { |
| 278 int num_bytes; | 279 int rv = request->Read(buf, kBufferSize); |
| 279 if (!request->Read(buf, kBufferSize, &num_bytes)) { | 280 if (rv < 0) { |
| 280 // Check whether the read failed synchronously. | 281 // Check whether the read failed synchronously. |
| 281 if (!request->status().is_io_pending()) | 282 if (rv != ERR_IO_PENDING) |
| 282 OnSpawnerCommandCompleted(request); | 283 OnSpawnerCommandCompleted(request, rv); |
| 283 return; | 284 return; |
| 284 } | 285 } |
| 285 if (!data->ConsumeBytesRead(num_bytes)) { | 286 if (!data->ConsumeBytesRead(rv)) { |
| 286 OnSpawnerCommandCompleted(request); | 287 OnSpawnerCommandCompleted(request, rv); |
| 287 return; | 288 return; |
| 288 } | 289 } |
|
mmenke
2016/08/30 22:13:22
As suggested elsewhere, suggest 3 non-nested if st
maksims (do not use this acc)
2016/09/01 12:22:30
Done.
| |
| 289 } | 290 } |
| 290 } | 291 } |
| 291 | 292 |
| 292 void SpawnerCommunicator::OnResponseStarted(URLRequest* request) { | 293 void SpawnerCommunicator::OnResponseStarted(URLRequest* request, |
| 294 int net_error) { | |
| 293 DCHECK_EQ(request, cur_request_.get()); | 295 DCHECK_EQ(request, cur_request_.get()); |
| 296 DCHECK_NE(ERR_IO_PENDING, net_error); | |
| 297 | |
| 294 SpawnerRequestData* data = | 298 SpawnerRequestData* data = |
| 295 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 299 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 296 DCHECK(data); | 300 DCHECK(data); |
| 297 | 301 |
| 298 data->IncreaseResponseStartedCount(); | 302 data->IncreaseResponseStartedCount(); |
| 299 | 303 |
| 300 if (!request->status().is_success()) { | 304 if (net_error != OK) { |
| 301 OnSpawnerCommandCompleted(request); | 305 OnSpawnerCommandCompleted(request, net_error); |
| 302 return; | 306 return; |
| 303 } | 307 } |
| 304 | 308 |
| 305 // Require HTTP responses to have a success status code. | 309 // Require HTTP responses to have a success status code. |
| 306 if (request->GetResponseCode() != 200) { | 310 if (request->GetResponseCode() != 200) { |
| 307 LOG(ERROR) << "Spawner server returned bad status: " | 311 LOG(ERROR) << "Spawner server returned bad status: " |
| 308 << request->response_headers()->GetStatusLine(); | 312 << request->response_headers()->GetStatusLine(); |
| 309 data->SetResultCode(ERR_FAILED); | 313 data->SetResultCode(ERR_FAILED); |
| 310 request->Cancel(); | 314 request->Cancel(); |
| 311 OnSpawnerCommandCompleted(request); | 315 OnSpawnerCommandCompleted(request, ERR_ABORTED); |
| 312 return; | 316 return; |
| 313 } | 317 } |
| 314 | 318 |
| 315 ReadResult(request); | 319 ReadResult(request); |
| 316 } | 320 } |
| 317 | 321 |
| 318 void SpawnerCommunicator::OnReadCompleted(URLRequest* request, int num_bytes) { | 322 void SpawnerCommunicator::OnReadCompleted(URLRequest* request, int num_bytes) { |
| 323 DCHECK_NE(ERR_IO_PENDING, num_bytes); | |
| 324 | |
| 319 if (!cur_request_.get()) | 325 if (!cur_request_.get()) |
| 320 return; | 326 return; |
| 321 DCHECK_EQ(request, cur_request_.get()); | 327 DCHECK_EQ(request, cur_request_.get()); |
| 322 SpawnerRequestData* data = | 328 SpawnerRequestData* data = |
| 323 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); | 329 static_cast<SpawnerRequestData*>(cur_request_->GetUserData(this)); |
| 324 DCHECK(data); | 330 DCHECK(data); |
| 325 | 331 |
| 326 if (data->ConsumeBytesRead(num_bytes)) { | 332 if (data->ConsumeBytesRead(num_bytes)) { |
| 327 // Keep reading. | 333 // Keep reading. |
| 328 ReadResult(request); | 334 ReadResult(request); |
| 329 } else { | 335 } else { |
| 330 OnSpawnerCommandCompleted(request); | 336 // |bytes_read| < 0 |
| 337 int net_error = num_bytes; | |
| 338 OnSpawnerCommandCompleted(request, net_error); | |
| 331 } | 339 } |
| 332 } | 340 } |
| 333 | 341 |
| 334 bool SpawnerCommunicator::StartServer(const std::string& arguments, | 342 bool SpawnerCommunicator::StartServer(const std::string& arguments, |
| 335 uint16_t* port) { | 343 uint16_t* port) { |
| 336 *port = 0; | 344 *port = 0; |
| 337 // Send the start command to spawner server to start the Python test server | 345 // Send the start command to spawner server to start the Python test server |
| 338 // on remote machine. | 346 // on remote machine. |
| 339 std::string server_return_data; | 347 std::string server_return_data; |
| 340 int result_code; | 348 int result_code; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 std::string server_return_data; | 388 std::string server_return_data; |
| 381 int result_code; | 389 int result_code; |
| 382 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); | 390 SendCommandAndWaitForResult("kill", "", &result_code, &server_return_data); |
| 383 Shutdown(); | 391 Shutdown(); |
| 384 if (OK != result_code || server_return_data != "killed") | 392 if (OK != result_code || server_return_data != "killed") |
| 385 return false; | 393 return false; |
| 386 return true; | 394 return true; |
| 387 } | 395 } |
| 388 | 396 |
| 389 } // namespace net | 397 } // namespace net |
| OLD | NEW |