| 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 "ipc/ipc_sync_channel.h" | 5 #include "ipc/ipc_sync_channel.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 using base::WaitableEvent; | 33 using base::WaitableEvent; |
| 34 | 34 |
| 35 namespace IPC { | 35 namespace IPC { |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // Base class for a "process" with listener and IPC threads. | 38 // Base class for a "process" with listener and IPC threads. |
| 39 class Worker : public Listener, public Sender { | 39 class Worker : public Listener, public Sender { |
| 40 public: | 40 public: |
| 41 // Will create a channel without a name. | 41 // Will create a channel without a name. |
| 42 Worker(Channel::Mode mode, const std::string& thread_name) | 42 Worker(Channel::Mode mode, |
| 43 const std::string& thread_name, |
| 44 const std::string& channel_name) |
| 43 : done_(new WaitableEvent(false, false)), | 45 : done_(new WaitableEvent(false, false)), |
| 44 channel_created_(new WaitableEvent(false, false)), | 46 channel_created_(new WaitableEvent(false, false)), |
| 47 channel_name_(channel_name), |
| 45 mode_(mode), | 48 mode_(mode), |
| 46 ipc_thread_((thread_name + "_ipc").c_str()), | 49 ipc_thread_((thread_name + "_ipc").c_str()), |
| 47 listener_thread_((thread_name + "_listener").c_str()), | 50 listener_thread_((thread_name + "_listener").c_str()), |
| 48 overrided_thread_(NULL), | 51 overrided_thread_(NULL), |
| 49 shutdown_event_(true, false), | 52 shutdown_event_(true, false), |
| 50 is_shutdown_(false) { | 53 is_shutdown_(false) {} |
| 51 } | |
| 52 | 54 |
| 53 // Will create a named channel and use this name for the threads' name. | 55 // Will create a named channel and use this name for the threads' name. |
| 54 Worker(const std::string& channel_name, Channel::Mode mode) | 56 Worker(const std::string& channel_name, Channel::Mode mode) |
| 55 : done_(new WaitableEvent(false, false)), | 57 : done_(new WaitableEvent(false, false)), |
| 56 channel_created_(new WaitableEvent(false, false)), | 58 channel_created_(new WaitableEvent(false, false)), |
| 57 channel_name_(channel_name), | 59 channel_name_(channel_name), |
| 58 mode_(mode), | 60 mode_(mode), |
| 59 ipc_thread_((channel_name + "_ipc").c_str()), | 61 ipc_thread_((channel_name + "_ipc").c_str()), |
| 60 listener_thread_((channel_name + "_listener").c_str()), | 62 listener_thread_((channel_name + "_listener").c_str()), |
| 61 overrided_thread_(NULL), | 63 overrided_thread_(NULL), |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 271 |
| 270 class IPCSyncChannelTest : public testing::Test { | 272 class IPCSyncChannelTest : public testing::Test { |
| 271 private: | 273 private: |
| 272 base::MessageLoop message_loop_; | 274 base::MessageLoop message_loop_; |
| 273 }; | 275 }; |
| 274 | 276 |
| 275 //------------------------------------------------------------------------------ | 277 //------------------------------------------------------------------------------ |
| 276 | 278 |
| 277 class SimpleServer : public Worker { | 279 class SimpleServer : public Worker { |
| 278 public: | 280 public: |
| 279 explicit SimpleServer(bool pump_during_send) | 281 SimpleServer(bool pump_during_send, const std::string& channel_name) |
| 280 : Worker(Channel::MODE_SERVER, "simpler_server"), | 282 : Worker(Channel::MODE_SERVER, "simpler_server", channel_name), |
| 281 pump_during_send_(pump_during_send) { } | 283 pump_during_send_(pump_during_send) {} |
| 282 void Run() override { | 284 void Run() override { |
| 283 SendAnswerToLife(pump_during_send_, true); | 285 SendAnswerToLife(pump_during_send_, true); |
| 284 Done(); | 286 Done(); |
| 285 } | 287 } |
| 286 | 288 |
| 287 bool pump_during_send_; | 289 bool pump_during_send_; |
| 288 }; | 290 }; |
| 289 | 291 |
| 290 class SimpleClient : public Worker { | 292 class SimpleClient : public Worker { |
| 291 public: | 293 public: |
| 292 SimpleClient() : Worker(Channel::MODE_CLIENT, "simple_client") { } | 294 explicit SimpleClient(const std::string& channel_name) |
| 295 : Worker(Channel::MODE_CLIENT, "simple_client", channel_name) {} |
| 293 | 296 |
| 294 void OnAnswer(int* answer) override { | 297 void OnAnswer(int* answer) override { |
| 295 *answer = 42; | 298 *answer = 42; |
| 296 Done(); | 299 Done(); |
| 297 } | 300 } |
| 298 }; | 301 }; |
| 299 | 302 |
| 300 void Simple(bool pump_during_send) { | 303 void Simple(bool pump_during_send) { |
| 301 std::vector<Worker*> workers; | 304 std::vector<Worker*> workers; |
| 302 workers.push_back(new SimpleServer(pump_during_send)); | 305 workers.push_back(new SimpleServer(pump_during_send, "Simple")); |
| 303 workers.push_back(new SimpleClient()); | 306 workers.push_back(new SimpleClient("Simple")); |
| 304 RunTest(workers); | 307 RunTest(workers); |
| 305 } | 308 } |
| 306 | 309 |
| 307 #if defined(OS_ANDROID) | 310 #if defined(OS_ANDROID) |
| 308 #define MAYBE_Simple DISABLED_Simple | 311 #define MAYBE_Simple DISABLED_Simple |
| 309 #else | 312 #else |
| 310 #define MAYBE_Simple Simple | 313 #define MAYBE_Simple Simple |
| 311 #endif | 314 #endif |
| 312 // Tests basic synchronous call | 315 // Tests basic synchronous call |
| 313 TEST_F(IPCSyncChannelTest, MAYBE_Simple) { | 316 TEST_F(IPCSyncChannelTest, MAYBE_Simple) { |
| 314 Simple(false); | 317 Simple(false); |
| 315 Simple(true); | 318 Simple(true); |
| 316 } | 319 } |
| 317 | 320 |
| 318 //------------------------------------------------------------------------------ | 321 //------------------------------------------------------------------------------ |
| 319 | 322 |
| 320 // Worker classes which override how the sync channel is created to use the | 323 // Worker classes which override how the sync channel is created to use the |
| 321 // two-step initialization (calling the lightweight constructor and then | 324 // two-step initialization (calling the lightweight constructor and then |
| 322 // ChannelProxy::Init separately) process. | 325 // ChannelProxy::Init separately) process. |
| 323 class TwoStepServer : public Worker { | 326 class TwoStepServer : public Worker { |
| 324 public: | 327 public: |
| 325 explicit TwoStepServer(bool create_pipe_now) | 328 TwoStepServer(bool create_pipe_now, const std::string& channel_name) |
| 326 : Worker(Channel::MODE_SERVER, "simpler_server"), | 329 : Worker(Channel::MODE_SERVER, "simpler_server", channel_name), |
| 327 create_pipe_now_(create_pipe_now) { } | 330 create_pipe_now_(create_pipe_now) {} |
| 328 | 331 |
| 329 void Run() override { | 332 void Run() override { |
| 330 SendAnswerToLife(false, true); | 333 SendAnswerToLife(false, true); |
| 331 Done(); | 334 Done(); |
| 332 } | 335 } |
| 333 | 336 |
| 334 SyncChannel* CreateChannel() override { | 337 SyncChannel* CreateChannel() override { |
| 335 SyncChannel* channel = | 338 SyncChannel* channel = |
| 336 SyncChannel::Create(channel_name(), mode(), this, | 339 SyncChannel::Create(channel_name(), mode(), this, |
| 337 ipc_thread().task_runner().get(), create_pipe_now_, | 340 ipc_thread().task_runner().get(), create_pipe_now_, |
| 338 shutdown_event()) | 341 shutdown_event()) |
| 339 .release(); | 342 .release(); |
| 340 return channel; | 343 return channel; |
| 341 } | 344 } |
| 342 | 345 |
| 343 bool create_pipe_now_; | 346 bool create_pipe_now_; |
| 344 }; | 347 }; |
| 345 | 348 |
| 346 class TwoStepClient : public Worker { | 349 class TwoStepClient : public Worker { |
| 347 public: | 350 public: |
| 348 TwoStepClient(bool create_pipe_now) | 351 TwoStepClient(bool create_pipe_now, const std::string& channel_name) |
| 349 : Worker(Channel::MODE_CLIENT, "simple_client"), | 352 : Worker(Channel::MODE_CLIENT, "simple_client", channel_name), |
| 350 create_pipe_now_(create_pipe_now) { } | 353 create_pipe_now_(create_pipe_now) {} |
| 351 | 354 |
| 352 void OnAnswer(int* answer) override { | 355 void OnAnswer(int* answer) override { |
| 353 *answer = 42; | 356 *answer = 42; |
| 354 Done(); | 357 Done(); |
| 355 } | 358 } |
| 356 | 359 |
| 357 SyncChannel* CreateChannel() override { | 360 SyncChannel* CreateChannel() override { |
| 358 SyncChannel* channel = | 361 SyncChannel* channel = |
| 359 SyncChannel::Create(channel_name(), mode(), this, | 362 SyncChannel::Create(channel_name(), mode(), this, |
| 360 ipc_thread().task_runner().get(), create_pipe_now_, | 363 ipc_thread().task_runner().get(), create_pipe_now_, |
| 361 shutdown_event()) | 364 shutdown_event()) |
| 362 .release(); | 365 .release(); |
| 363 return channel; | 366 return channel; |
| 364 } | 367 } |
| 365 | 368 |
| 366 bool create_pipe_now_; | 369 bool create_pipe_now_; |
| 367 }; | 370 }; |
| 368 | 371 |
| 369 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { | 372 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { |
| 370 std::vector<Worker*> workers; | 373 std::vector<Worker*> workers; |
| 371 workers.push_back(new TwoStepServer(create_server_pipe_now)); | 374 workers.push_back(new TwoStepServer(create_server_pipe_now, "TwoStep")); |
| 372 workers.push_back(new TwoStepClient(create_client_pipe_now)); | 375 workers.push_back(new TwoStepClient(create_client_pipe_now, "TwoStep")); |
| 373 RunTest(workers); | 376 RunTest(workers); |
| 374 } | 377 } |
| 375 | 378 |
| 376 // Tests basic two-step initialization, where you call the lightweight | 379 // Tests basic two-step initialization, where you call the lightweight |
| 377 // constructor then Init. | 380 // constructor then Init. |
| 378 TEST_F(IPCSyncChannelTest, TwoStepInitialization) { | 381 TEST_F(IPCSyncChannelTest, TwoStepInitialization) { |
| 379 TwoStep(false, false); | 382 TwoStep(false, false); |
| 380 TwoStep(false, true); | 383 TwoStep(false, true); |
| 381 TwoStep(true, false); | 384 TwoStep(true, false); |
| 382 TwoStep(true, true); | 385 TwoStep(true, true); |
| 383 } | 386 } |
| 384 | 387 |
| 385 //------------------------------------------------------------------------------ | 388 //------------------------------------------------------------------------------ |
| 386 | 389 |
| 387 class DelayClient : public Worker { | 390 class DelayClient : public Worker { |
| 388 public: | 391 public: |
| 389 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } | 392 explicit DelayClient(const std::string& channel_name) |
| 393 : Worker(Channel::MODE_CLIENT, "delay_client", channel_name) {} |
| 390 | 394 |
| 391 void OnAnswerDelay(Message* reply_msg) override { | 395 void OnAnswerDelay(Message* reply_msg) override { |
| 392 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); | 396 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 393 Send(reply_msg); | 397 Send(reply_msg); |
| 394 Done(); | 398 Done(); |
| 395 } | 399 } |
| 396 }; | 400 }; |
| 397 | 401 |
| 398 void DelayReply(bool pump_during_send) { | 402 void DelayReply(bool pump_during_send) { |
| 399 std::vector<Worker*> workers; | 403 std::vector<Worker*> workers; |
| 400 workers.push_back(new SimpleServer(pump_during_send)); | 404 workers.push_back(new SimpleServer(pump_during_send, "DelayReply")); |
| 401 workers.push_back(new DelayClient()); | 405 workers.push_back(new DelayClient("DelayReply")); |
| 402 RunTest(workers); | 406 RunTest(workers); |
| 403 } | 407 } |
| 404 | 408 |
| 405 // Tests that asynchronous replies work | 409 // Tests that asynchronous replies work |
| 406 TEST_F(IPCSyncChannelTest, DelayReply) { | 410 TEST_F(IPCSyncChannelTest, DelayReply) { |
| 407 DelayReply(false); | 411 DelayReply(false); |
| 408 DelayReply(true); | 412 DelayReply(true); |
| 409 } | 413 } |
| 410 | 414 |
| 411 //------------------------------------------------------------------------------ | 415 //------------------------------------------------------------------------------ |
| 412 | 416 |
| 413 class NoHangServer : public Worker { | 417 class NoHangServer : public Worker { |
| 414 public: | 418 public: |
| 415 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) | 419 NoHangServer(WaitableEvent* got_first_reply, |
| 416 : Worker(Channel::MODE_SERVER, "no_hang_server"), | 420 bool pump_during_send, |
| 421 const std::string& channel_name) |
| 422 : Worker(Channel::MODE_SERVER, "no_hang_server", channel_name), |
| 417 got_first_reply_(got_first_reply), | 423 got_first_reply_(got_first_reply), |
| 418 pump_during_send_(pump_during_send) { } | 424 pump_during_send_(pump_during_send) {} |
| 419 void Run() override { | 425 void Run() override { |
| 420 SendAnswerToLife(pump_during_send_, true); | 426 SendAnswerToLife(pump_during_send_, true); |
| 421 got_first_reply_->Signal(); | 427 got_first_reply_->Signal(); |
| 422 | 428 |
| 423 SendAnswerToLife(pump_during_send_, false); | 429 SendAnswerToLife(pump_during_send_, false); |
| 424 Done(); | 430 Done(); |
| 425 } | 431 } |
| 426 | 432 |
| 427 WaitableEvent* got_first_reply_; | 433 WaitableEvent* got_first_reply_; |
| 428 bool pump_during_send_; | 434 bool pump_during_send_; |
| 429 }; | 435 }; |
| 430 | 436 |
| 431 class NoHangClient : public Worker { | 437 class NoHangClient : public Worker { |
| 432 public: | 438 public: |
| 433 explicit NoHangClient(WaitableEvent* got_first_reply) | 439 NoHangClient(WaitableEvent* got_first_reply, const std::string& channel_name) |
| 434 : Worker(Channel::MODE_CLIENT, "no_hang_client"), | 440 : Worker(Channel::MODE_CLIENT, "no_hang_client", channel_name), |
| 435 got_first_reply_(got_first_reply) { } | 441 got_first_reply_(got_first_reply) {} |
| 436 | 442 |
| 437 void OnAnswerDelay(Message* reply_msg) override { | 443 void OnAnswerDelay(Message* reply_msg) override { |
| 438 // Use the DELAY_REPLY macro so that we can force the reply to be sent | 444 // Use the DELAY_REPLY macro so that we can force the reply to be sent |
| 439 // before this function returns (when the channel will be reset). | 445 // before this function returns (when the channel will be reset). |
| 440 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); | 446 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 441 Send(reply_msg); | 447 Send(reply_msg); |
| 442 got_first_reply_->Wait(); | 448 got_first_reply_->Wait(); |
| 443 CloseChannel(); | 449 CloseChannel(); |
| 444 Done(); | 450 Done(); |
| 445 } | 451 } |
| 446 | 452 |
| 447 WaitableEvent* got_first_reply_; | 453 WaitableEvent* got_first_reply_; |
| 448 }; | 454 }; |
| 449 | 455 |
| 450 void NoHang(bool pump_during_send) { | 456 void NoHang(bool pump_during_send) { |
| 451 WaitableEvent got_first_reply(false, false); | 457 WaitableEvent got_first_reply(false, false); |
| 452 std::vector<Worker*> workers; | 458 std::vector<Worker*> workers; |
| 453 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); | 459 workers.push_back( |
| 454 workers.push_back(new NoHangClient(&got_first_reply)); | 460 new NoHangServer(&got_first_reply, pump_during_send, "NoHang")); |
| 461 workers.push_back(new NoHangClient(&got_first_reply, "NoHang")); |
| 455 RunTest(workers); | 462 RunTest(workers); |
| 456 } | 463 } |
| 457 | 464 |
| 458 // Tests that caller doesn't hang if receiver dies | 465 // Tests that caller doesn't hang if receiver dies |
| 459 TEST_F(IPCSyncChannelTest, NoHang) { | 466 TEST_F(IPCSyncChannelTest, NoHang) { |
| 460 NoHang(false); | 467 NoHang(false); |
| 461 NoHang(true); | 468 NoHang(true); |
| 462 } | 469 } |
| 463 | 470 |
| 464 //------------------------------------------------------------------------------ | 471 //------------------------------------------------------------------------------ |
| 465 | 472 |
| 466 class UnblockServer : public Worker { | 473 class UnblockServer : public Worker { |
| 467 public: | 474 public: |
| 468 UnblockServer(bool pump_during_send, bool delete_during_send) | 475 UnblockServer(bool pump_during_send, |
| 469 : Worker(Channel::MODE_SERVER, "unblock_server"), | 476 bool delete_during_send, |
| 470 pump_during_send_(pump_during_send), | 477 const std::string& channel_name) |
| 471 delete_during_send_(delete_during_send) { } | 478 : Worker(Channel::MODE_SERVER, "unblock_server", channel_name), |
| 479 pump_during_send_(pump_during_send), |
| 480 delete_during_send_(delete_during_send) {} |
| 472 void Run() override { | 481 void Run() override { |
| 473 if (delete_during_send_) { | 482 if (delete_during_send_) { |
| 474 // Use custom code since race conditions mean the answer may or may not be | 483 // Use custom code since race conditions mean the answer may or may not be |
| 475 // available. | 484 // available. |
| 476 int answer = 0; | 485 int answer = 0; |
| 477 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer); | 486 SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer); |
| 478 if (pump_during_send_) | 487 if (pump_during_send_) |
| 479 msg->EnableMessagePumping(); | 488 msg->EnableMessagePumping(); |
| 480 Send(msg); | 489 Send(msg); |
| 481 } else { | 490 } else { |
| 482 SendAnswerToLife(pump_during_send_, true); | 491 SendAnswerToLife(pump_during_send_, true); |
| 483 } | 492 } |
| 484 Done(); | 493 Done(); |
| 485 } | 494 } |
| 486 | 495 |
| 487 void OnDoubleDelay(int in, Message* reply_msg) override { | 496 void OnDoubleDelay(int in, Message* reply_msg) override { |
| 488 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); | 497 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 489 Send(reply_msg); | 498 Send(reply_msg); |
| 490 if (delete_during_send_) | 499 if (delete_during_send_) |
| 491 ResetChannel(); | 500 ResetChannel(); |
| 492 } | 501 } |
| 493 | 502 |
| 494 bool pump_during_send_; | 503 bool pump_during_send_; |
| 495 bool delete_during_send_; | 504 bool delete_during_send_; |
| 496 }; | 505 }; |
| 497 | 506 |
| 498 class UnblockClient : public Worker { | 507 class UnblockClient : public Worker { |
| 499 public: | 508 public: |
| 500 explicit UnblockClient(bool pump_during_send) | 509 UnblockClient(bool pump_during_send, const std::string& channel_name) |
| 501 : Worker(Channel::MODE_CLIENT, "unblock_client"), | 510 : Worker(Channel::MODE_CLIENT, "unblock_client", channel_name), |
| 502 pump_during_send_(pump_during_send) { } | 511 pump_during_send_(pump_during_send) {} |
| 503 | 512 |
| 504 void OnAnswer(int* answer) override { | 513 void OnAnswer(int* answer) override { |
| 505 SendDouble(pump_during_send_, true); | 514 SendDouble(pump_during_send_, true); |
| 506 *answer = 42; | 515 *answer = 42; |
| 507 Done(); | 516 Done(); |
| 508 } | 517 } |
| 509 | 518 |
| 510 bool pump_during_send_; | 519 bool pump_during_send_; |
| 511 }; | 520 }; |
| 512 | 521 |
| 513 void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { | 522 void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { |
| 514 std::vector<Worker*> workers; | 523 std::vector<Worker*> workers; |
| 515 workers.push_back(new UnblockServer(server_pump, delete_during_send)); | 524 workers.push_back( |
| 516 workers.push_back(new UnblockClient(client_pump)); | 525 new UnblockServer(server_pump, delete_during_send, "Unblock")); |
| 526 workers.push_back(new UnblockClient(client_pump, "Unblock")); |
| 517 RunTest(workers); | 527 RunTest(workers); |
| 518 } | 528 } |
| 519 | 529 |
| 520 // Tests that the caller unblocks to answer a sync message from the receiver. | 530 // Tests that the caller unblocks to answer a sync message from the receiver. |
| 521 TEST_F(IPCSyncChannelTest, Unblock) { | 531 TEST_F(IPCSyncChannelTest, Unblock) { |
| 522 Unblock(false, false, false); | 532 Unblock(false, false, false); |
| 523 Unblock(false, true, false); | 533 Unblock(false, true, false); |
| 524 Unblock(true, false, false); | 534 Unblock(true, false, false); |
| 525 Unblock(true, true, false); | 535 Unblock(true, true, false); |
| 526 } | 536 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 537 Unblock(false, false, true); | 547 Unblock(false, false, true); |
| 538 Unblock(false, true, true); | 548 Unblock(false, true, true); |
| 539 Unblock(true, false, true); | 549 Unblock(true, false, true); |
| 540 Unblock(true, true, true); | 550 Unblock(true, true, true); |
| 541 } | 551 } |
| 542 | 552 |
| 543 //------------------------------------------------------------------------------ | 553 //------------------------------------------------------------------------------ |
| 544 | 554 |
| 545 class RecursiveServer : public Worker { | 555 class RecursiveServer : public Worker { |
| 546 public: | 556 public: |
| 547 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second) | 557 RecursiveServer(bool expected_send_result, |
| 548 : Worker(Channel::MODE_SERVER, "recursive_server"), | 558 bool pump_first, |
| 559 bool pump_second, |
| 560 const std::string& channel_name) |
| 561 : Worker(Channel::MODE_SERVER, "recursive_server", channel_name), |
| 549 expected_send_result_(expected_send_result), | 562 expected_send_result_(expected_send_result), |
| 550 pump_first_(pump_first), pump_second_(pump_second) {} | 563 pump_first_(pump_first), |
| 564 pump_second_(pump_second) {} |
| 551 void Run() override { | 565 void Run() override { |
| 552 SendDouble(pump_first_, expected_send_result_); | 566 SendDouble(pump_first_, expected_send_result_); |
| 553 Done(); | 567 Done(); |
| 554 } | 568 } |
| 555 | 569 |
| 556 void OnDouble(int in, int* out) override { | 570 void OnDouble(int in, int* out) override { |
| 557 *out = in * 2; | 571 *out = in * 2; |
| 558 SendAnswerToLife(pump_second_, expected_send_result_); | 572 SendAnswerToLife(pump_second_, expected_send_result_); |
| 559 } | 573 } |
| 560 | 574 |
| 561 bool expected_send_result_, pump_first_, pump_second_; | 575 bool expected_send_result_, pump_first_, pump_second_; |
| 562 }; | 576 }; |
| 563 | 577 |
| 564 class RecursiveClient : public Worker { | 578 class RecursiveClient : public Worker { |
| 565 public: | 579 public: |
| 566 RecursiveClient(bool pump_during_send, bool close_channel) | 580 RecursiveClient(bool pump_during_send, |
| 567 : Worker(Channel::MODE_CLIENT, "recursive_client"), | 581 bool close_channel, |
| 568 pump_during_send_(pump_during_send), close_channel_(close_channel) {} | 582 const std::string& channel_name) |
| 583 : Worker(Channel::MODE_CLIENT, "recursive_client", channel_name), |
| 584 pump_during_send_(pump_during_send), |
| 585 close_channel_(close_channel) {} |
| 569 | 586 |
| 570 void OnDoubleDelay(int in, Message* reply_msg) override { | 587 void OnDoubleDelay(int in, Message* reply_msg) override { |
| 571 SendDouble(pump_during_send_, !close_channel_); | 588 SendDouble(pump_during_send_, !close_channel_); |
| 572 if (close_channel_) { | 589 if (close_channel_) { |
| 573 delete reply_msg; | 590 delete reply_msg; |
| 574 } else { | 591 } else { |
| 575 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); | 592 SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2); |
| 576 Send(reply_msg); | 593 Send(reply_msg); |
| 577 } | 594 } |
| 578 Done(); | 595 Done(); |
| 579 } | 596 } |
| 580 | 597 |
| 581 void OnAnswerDelay(Message* reply_msg) override { | 598 void OnAnswerDelay(Message* reply_msg) override { |
| 582 if (close_channel_) { | 599 if (close_channel_) { |
| 583 delete reply_msg; | 600 delete reply_msg; |
| 584 CloseChannel(); | 601 CloseChannel(); |
| 585 } else { | 602 } else { |
| 586 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); | 603 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 587 Send(reply_msg); | 604 Send(reply_msg); |
| 588 } | 605 } |
| 589 } | 606 } |
| 590 | 607 |
| 591 bool pump_during_send_, close_channel_; | 608 bool pump_during_send_, close_channel_; |
| 592 }; | 609 }; |
| 593 | 610 |
| 594 void Recursive( | 611 void Recursive( |
| 595 bool server_pump_first, bool server_pump_second, bool client_pump) { | 612 bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 596 std::vector<Worker*> workers; | 613 std::vector<Worker*> workers; |
| 597 workers.push_back( | 614 workers.push_back(new RecursiveServer(true, server_pump_first, |
| 598 new RecursiveServer(true, server_pump_first, server_pump_second)); | 615 server_pump_second, "Recursive")); |
| 599 workers.push_back(new RecursiveClient(client_pump, false)); | 616 workers.push_back(new RecursiveClient(client_pump, false, "Recursive")); |
| 600 RunTest(workers); | 617 RunTest(workers); |
| 601 } | 618 } |
| 602 | 619 |
| 603 // Tests a server calling Send while another Send is pending. | 620 // Tests a server calling Send while another Send is pending. |
| 604 TEST_F(IPCSyncChannelTest, Recursive) { | 621 TEST_F(IPCSyncChannelTest, Recursive) { |
| 605 Recursive(false, false, false); | 622 Recursive(false, false, false); |
| 606 Recursive(false, false, true); | 623 Recursive(false, false, true); |
| 607 Recursive(false, true, false); | 624 Recursive(false, true, false); |
| 608 Recursive(false, true, true); | 625 Recursive(false, true, true); |
| 609 Recursive(true, false, false); | 626 Recursive(true, false, false); |
| 610 Recursive(true, false, true); | 627 Recursive(true, false, true); |
| 611 Recursive(true, true, false); | 628 Recursive(true, true, false); |
| 612 Recursive(true, true, true); | 629 Recursive(true, true, true); |
| 613 } | 630 } |
| 614 | 631 |
| 615 //------------------------------------------------------------------------------ | 632 //------------------------------------------------------------------------------ |
| 616 | 633 |
| 617 void RecursiveNoHang( | 634 void RecursiveNoHang( |
| 618 bool server_pump_first, bool server_pump_second, bool client_pump) { | 635 bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 619 std::vector<Worker*> workers; | 636 std::vector<Worker*> workers; |
| 620 workers.push_back( | 637 workers.push_back(new RecursiveServer(false, server_pump_first, |
| 621 new RecursiveServer(false, server_pump_first, server_pump_second)); | 638 server_pump_second, "RecursiveNoHang")); |
| 622 workers.push_back(new RecursiveClient(client_pump, true)); | 639 workers.push_back(new RecursiveClient(client_pump, true, "RecursiveNoHang")); |
| 623 RunTest(workers); | 640 RunTest(workers); |
| 624 } | 641 } |
| 625 | 642 |
| 626 // Tests that if a caller makes a sync call during an existing sync call and | 643 // Tests that if a caller makes a sync call during an existing sync call and |
| 627 // the receiver dies, neither of the Send() calls hang. | 644 // the receiver dies, neither of the Send() calls hang. |
| 628 TEST_F(IPCSyncChannelTest, RecursiveNoHang) { | 645 TEST_F(IPCSyncChannelTest, RecursiveNoHang) { |
| 629 RecursiveNoHang(false, false, false); | 646 RecursiveNoHang(false, false, false); |
| 630 RecursiveNoHang(false, false, true); | 647 RecursiveNoHang(false, false, true); |
| 631 RecursiveNoHang(false, true, false); | 648 RecursiveNoHang(false, true, false); |
| 632 RecursiveNoHang(false, true, true); | 649 RecursiveNoHang(false, true, true); |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 // pumps messages while waiting for a response. | 870 // pumps messages while waiting for a response. |
| 854 TEST_F(IPCSyncChannelTest, QueuedReply) { | 871 TEST_F(IPCSyncChannelTest, QueuedReply) { |
| 855 QueuedReply(false); | 872 QueuedReply(false); |
| 856 QueuedReply(true); | 873 QueuedReply(true); |
| 857 } | 874 } |
| 858 | 875 |
| 859 //------------------------------------------------------------------------------ | 876 //------------------------------------------------------------------------------ |
| 860 | 877 |
| 861 class ChattyClient : public Worker { | 878 class ChattyClient : public Worker { |
| 862 public: | 879 public: |
| 863 ChattyClient() : | 880 explicit ChattyClient(const std::string& channel_name) |
| 864 Worker(Channel::MODE_CLIENT, "chatty_client") { } | 881 : Worker(Channel::MODE_CLIENT, "chatty_client", channel_name) {} |
| 865 | 882 |
| 866 void OnAnswer(int* answer) override { | 883 void OnAnswer(int* answer) override { |
| 867 // The PostMessage limit is 10k. Send 20% more than that. | 884 // The PostMessage limit is 10k. Send 20% more than that. |
| 868 const int kMessageLimit = 10000; | 885 const int kMessageLimit = 10000; |
| 869 const int kMessagesToSend = kMessageLimit * 120 / 100; | 886 const int kMessagesToSend = kMessageLimit * 120 / 100; |
| 870 for (int i = 0; i < kMessagesToSend; ++i) { | 887 for (int i = 0; i < kMessagesToSend; ++i) { |
| 871 if (!SendDouble(false, true)) | 888 if (!SendDouble(false, true)) |
| 872 break; | 889 break; |
| 873 } | 890 } |
| 874 *answer = 42; | 891 *answer = 42; |
| 875 Done(); | 892 Done(); |
| 876 } | 893 } |
| 877 }; | 894 }; |
| 878 | 895 |
| 879 void ChattyServer(bool pump_during_send) { | 896 void ChattyServer(bool pump_during_send) { |
| 880 std::vector<Worker*> workers; | 897 std::vector<Worker*> workers; |
| 881 workers.push_back(new UnblockServer(pump_during_send, false)); | 898 workers.push_back(new UnblockServer(pump_during_send, false, "ChattyServer")); |
| 882 workers.push_back(new ChattyClient()); | 899 workers.push_back(new ChattyClient("ChattyServer")); |
| 883 RunTest(workers); | 900 RunTest(workers); |
| 884 } | 901 } |
| 885 | 902 |
| 886 #if defined(OS_ANDROID) | 903 #if defined(OS_ANDROID) |
| 887 // Times out. | 904 // Times out. |
| 888 #define MAYBE_ChattyServer DISABLED_ChattyServer | 905 #define MAYBE_ChattyServer DISABLED_ChattyServer |
| 889 #else | 906 #else |
| 890 #define MAYBE_ChattyServer ChattyServer | 907 #define MAYBE_ChattyServer ChattyServer |
| 891 #endif | 908 #endif |
| 892 // Tests http://b/1093251 - that sending lots of sync messages while | 909 // Tests http://b/1093251 - that sending lots of sync messages while |
| (...skipping 13 matching lines...) Expand all Loading... |
| 906 } | 923 } |
| 907 | 924 |
| 908 bool timeout_occurred = false; | 925 bool timeout_occurred = false; |
| 909 | 926 |
| 910 void TimeoutCallback() { | 927 void TimeoutCallback() { |
| 911 timeout_occurred = true; | 928 timeout_occurred = true; |
| 912 } | 929 } |
| 913 | 930 |
| 914 class DoneEventRaceServer : public Worker { | 931 class DoneEventRaceServer : public Worker { |
| 915 public: | 932 public: |
| 916 DoneEventRaceServer() | 933 explicit DoneEventRaceServer(const std::string& channel_name) |
| 917 : Worker(Channel::MODE_SERVER, "done_event_race_server") { } | 934 : Worker(Channel::MODE_SERVER, "done_event_race_server", channel_name) {} |
| 918 | 935 |
| 919 void Run() override { | 936 void Run() override { |
| 920 base::ThreadTaskRunnerHandle::Get()->PostTask( | 937 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 921 FROM_HERE, base::Bind(&NestedCallback, this)); | 938 FROM_HERE, base::Bind(&NestedCallback, this)); |
| 922 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 939 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 923 FROM_HERE, base::Bind(&TimeoutCallback), | 940 FROM_HERE, base::Bind(&TimeoutCallback), |
| 924 base::TimeDelta::FromSeconds(9)); | 941 base::TimeDelta::FromSeconds(9)); |
| 925 // Even though we have a timeout on the Send, it will succeed since for this | 942 // Even though we have a timeout on the Send, it will succeed since for this |
| 926 // bug, the reply message comes back and is deserialized, however the done | 943 // bug, the reply message comes back and is deserialized, however the done |
| 927 // event wasn't set. So we indirectly use the timeout task to notice if a | 944 // event wasn't set. So we indirectly use the timeout task to notice if a |
| 928 // timeout occurred. | 945 // timeout occurred. |
| 929 SendAnswerToLife(true, true); | 946 SendAnswerToLife(true, true); |
| 930 DCHECK(!timeout_occurred); | 947 DCHECK(!timeout_occurred); |
| 931 Done(); | 948 Done(); |
| 932 } | 949 } |
| 933 }; | 950 }; |
| 934 | 951 |
| 935 #if defined(OS_ANDROID) | 952 #if defined(OS_ANDROID) |
| 936 #define MAYBE_DoneEventRace DISABLED_DoneEventRace | 953 #define MAYBE_DoneEventRace DISABLED_DoneEventRace |
| 937 #else | 954 #else |
| 938 #define MAYBE_DoneEventRace DoneEventRace | 955 #define MAYBE_DoneEventRace DoneEventRace |
| 939 #endif | 956 #endif |
| 940 // Tests http://b/1474092 - that if after the done_event is set but before | 957 // Tests http://b/1474092 - that if after the done_event is set but before |
| 941 // OnObjectSignaled is called another message is sent out, then after its | 958 // OnObjectSignaled is called another message is sent out, then after its |
| 942 // reply comes back OnObjectSignaled will be called for the first message. | 959 // reply comes back OnObjectSignaled will be called for the first message. |
| 943 TEST_F(IPCSyncChannelTest, MAYBE_DoneEventRace) { | 960 TEST_F(IPCSyncChannelTest, MAYBE_DoneEventRace) { |
| 944 std::vector<Worker*> workers; | 961 std::vector<Worker*> workers; |
| 945 workers.push_back(new DoneEventRaceServer()); | 962 workers.push_back(new DoneEventRaceServer("DoneEventRace")); |
| 946 workers.push_back(new SimpleClient()); | 963 workers.push_back(new SimpleClient("DoneEventRace")); |
| 947 RunTest(workers); | 964 RunTest(workers); |
| 948 } | 965 } |
| 949 | 966 |
| 950 //------------------------------------------------------------------------------ | 967 //------------------------------------------------------------------------------ |
| 951 | 968 |
| 952 class TestSyncMessageFilter : public SyncMessageFilter { | 969 class TestSyncMessageFilter : public SyncMessageFilter { |
| 953 public: | 970 public: |
| 954 TestSyncMessageFilter( | 971 TestSyncMessageFilter( |
| 955 base::WaitableEvent* shutdown_event, | 972 base::WaitableEvent* shutdown_event, |
| 956 Worker* worker, | 973 Worker* worker, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 977 | 994 |
| 978 private: | 995 private: |
| 979 ~TestSyncMessageFilter() override {} | 996 ~TestSyncMessageFilter() override {} |
| 980 | 997 |
| 981 Worker* worker_; | 998 Worker* worker_; |
| 982 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 999 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 983 }; | 1000 }; |
| 984 | 1001 |
| 985 class SyncMessageFilterServer : public Worker { | 1002 class SyncMessageFilterServer : public Worker { |
| 986 public: | 1003 public: |
| 987 SyncMessageFilterServer() | 1004 explicit SyncMessageFilterServer(const std::string& channel_name) |
| 988 : Worker(Channel::MODE_SERVER, "sync_message_filter_server"), | 1005 : Worker(Channel::MODE_SERVER, |
| 1006 "sync_message_filter_server", |
| 1007 channel_name), |
| 989 thread_("helper_thread") { | 1008 thread_("helper_thread") { |
| 990 base::Thread::Options options; | 1009 base::Thread::Options options; |
| 991 options.message_loop_type = base::MessageLoop::TYPE_DEFAULT; | 1010 options.message_loop_type = base::MessageLoop::TYPE_DEFAULT; |
| 992 thread_.StartWithOptions(options); | 1011 thread_.StartWithOptions(options); |
| 993 filter_ = new TestSyncMessageFilter(shutdown_event(), this, | 1012 filter_ = new TestSyncMessageFilter(shutdown_event(), this, |
| 994 thread_.task_runner()); | 1013 thread_.task_runner()); |
| 995 } | 1014 } |
| 996 | 1015 |
| 997 void Run() override { | 1016 void Run() override { |
| 998 channel()->AddFilter(filter_.get()); | 1017 channel()->AddFilter(filter_.get()); |
| 999 } | 1018 } |
| 1000 | 1019 |
| 1001 base::Thread thread_; | 1020 base::Thread thread_; |
| 1002 scoped_refptr<TestSyncMessageFilter> filter_; | 1021 scoped_refptr<TestSyncMessageFilter> filter_; |
| 1003 }; | 1022 }; |
| 1004 | 1023 |
| 1005 // This class provides functionality to test the case that a Send on the sync | 1024 // This class provides functionality to test the case that a Send on the sync |
| 1006 // channel does not crash after the channel has been closed. | 1025 // channel does not crash after the channel has been closed. |
| 1007 class ServerSendAfterClose : public Worker { | 1026 class ServerSendAfterClose : public Worker { |
| 1008 public: | 1027 public: |
| 1009 ServerSendAfterClose() | 1028 explicit ServerSendAfterClose(const std::string& channel_name) |
| 1010 : Worker(Channel::MODE_SERVER, "simpler_server"), | 1029 : Worker(Channel::MODE_SERVER, "simpler_server", channel_name), |
| 1011 send_result_(true) { | 1030 send_result_(true) {} |
| 1012 } | |
| 1013 | 1031 |
| 1014 bool SendDummy() { | 1032 bool SendDummy() { |
| 1015 ListenerThread()->task_runner()->PostTask( | 1033 ListenerThread()->task_runner()->PostTask( |
| 1016 FROM_HERE, base::Bind(base::IgnoreResult(&ServerSendAfterClose::Send), | 1034 FROM_HERE, base::Bind(base::IgnoreResult(&ServerSendAfterClose::Send), |
| 1017 this, new SyncChannelTestMsg_NoArgs)); | 1035 this, new SyncChannelTestMsg_NoArgs)); |
| 1018 return true; | 1036 return true; |
| 1019 } | 1037 } |
| 1020 | 1038 |
| 1021 bool send_result() const { | 1039 bool send_result() const { |
| 1022 return send_result_; | 1040 return send_result_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1033 Done(); | 1051 Done(); |
| 1034 return send_result_; | 1052 return send_result_; |
| 1035 } | 1053 } |
| 1036 | 1054 |
| 1037 bool send_result_; | 1055 bool send_result_; |
| 1038 }; | 1056 }; |
| 1039 | 1057 |
| 1040 // Tests basic synchronous call | 1058 // Tests basic synchronous call |
| 1041 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { | 1059 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1042 std::vector<Worker*> workers; | 1060 std::vector<Worker*> workers; |
| 1043 workers.push_back(new SyncMessageFilterServer()); | 1061 workers.push_back(new SyncMessageFilterServer("SyncMessageFilter")); |
| 1044 workers.push_back(new SimpleClient()); | 1062 workers.push_back(new SimpleClient("SyncMessageFilter")); |
| 1045 RunTest(workers); | 1063 RunTest(workers); |
| 1046 } | 1064 } |
| 1047 | 1065 |
| 1048 // Test the case when the channel is closed and a Send is attempted after that. | 1066 // Test the case when the channel is closed and a Send is attempted after that. |
| 1049 TEST_F(IPCSyncChannelTest, SendAfterClose) { | 1067 TEST_F(IPCSyncChannelTest, SendAfterClose) { |
| 1050 ServerSendAfterClose server; | 1068 ServerSendAfterClose server("SendAfterClose"); |
| 1051 server.Start(); | 1069 server.Start(); |
| 1052 | 1070 |
| 1053 server.done_event()->Wait(); | 1071 server.done_event()->Wait(); |
| 1054 server.done_event()->Reset(); | 1072 server.done_event()->Reset(); |
| 1055 | 1073 |
| 1056 server.SendDummy(); | 1074 server.SendDummy(); |
| 1057 server.done_event()->Wait(); | 1075 server.done_event()->Wait(); |
| 1058 | 1076 |
| 1059 EXPECT_FALSE(server.send_result()); | 1077 EXPECT_FALSE(server.send_result()); |
| 1060 | 1078 |
| (...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1805 } | 1823 } |
| 1806 | 1824 |
| 1807 // Windows needs to send an out-of-band secret to verify the client end of the | 1825 // Windows needs to send an out-of-band secret to verify the client end of the |
| 1808 // channel. Test that we still connect correctly in that case. | 1826 // channel. Test that we still connect correctly in that case. |
| 1809 TEST_F(IPCSyncChannelTest, Verified) { | 1827 TEST_F(IPCSyncChannelTest, Verified) { |
| 1810 Verified(); | 1828 Verified(); |
| 1811 } | 1829 } |
| 1812 | 1830 |
| 1813 } // namespace | 1831 } // namespace |
| 1814 } // namespace IPC | 1832 } // namespace IPC |
| OLD | NEW |