| 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 // Unit test for SyncChannel. | 5 // Unit test for SyncChannel. |
| 6 | 6 |
| 7 #include "ipc/ipc_sync_channel.h" | 7 #include "ipc/ipc_sync_channel.h" |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "ipc/ipc_listener.h" | 22 #include "ipc/ipc_listener.h" |
| 23 #include "ipc/ipc_message.h" | 23 #include "ipc/ipc_message.h" |
| 24 #include "ipc/ipc_sender.h" | 24 #include "ipc/ipc_sender.h" |
| 25 #include "ipc/ipc_sync_message_filter.h" | 25 #include "ipc/ipc_sync_message_filter.h" |
| 26 #include "ipc/ipc_sync_message_unittest.h" | 26 #include "ipc/ipc_sync_message_unittest.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 using base::WaitableEvent; | 29 using base::WaitableEvent; |
| 30 | 30 |
| 31 namespace IPC { | 31 namespace IPC { |
| 32 | |
| 33 namespace { | 32 namespace { |
| 34 | 33 |
| 35 // Base class for a "process" with listener and IPC threads. | 34 // Base class for a "process" with listener and IPC threads. |
| 36 class Worker : public Listener, public Sender { | 35 class Worker : public Listener, public Sender { |
| 37 public: | 36 public: |
| 38 // Will create a channel without a name. | 37 // Will create a channel without a name. |
| 39 Worker(Channel::Mode mode, const std::string& thread_name) | 38 Worker(Channel::Mode mode, const std::string& thread_name) |
| 40 : done_(new WaitableEvent(false, false)), | 39 : done_(new WaitableEvent(false, false)), |
| 41 channel_created_(new WaitableEvent(false, false)), | 40 channel_created_(new WaitableEvent(false, false)), |
| 42 mode_(mode), | 41 mode_(mode), |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 // wait for all the workers to finish | 258 // wait for all the workers to finish |
| 260 for (size_t i = 0; i < workers.size(); ++i) | 259 for (size_t i = 0; i < workers.size(); ++i) |
| 261 workers[i]->done_event()->Wait(); | 260 workers[i]->done_event()->Wait(); |
| 262 | 261 |
| 263 for (size_t i = 0; i < workers.size(); ++i) { | 262 for (size_t i = 0; i < workers.size(); ++i) { |
| 264 workers[i]->Shutdown(); | 263 workers[i]->Shutdown(); |
| 265 delete workers[i]; | 264 delete workers[i]; |
| 266 } | 265 } |
| 267 } | 266 } |
| 268 | 267 |
| 269 } // namespace | |
| 270 | |
| 271 class IPCSyncChannelTest : public testing::Test { | 268 class IPCSyncChannelTest : public testing::Test { |
| 272 private: | 269 private: |
| 273 MessageLoop message_loop_; | 270 MessageLoop message_loop_; |
| 274 }; | 271 }; |
| 275 | 272 |
| 276 //----------------------------------------------------------------------------- | 273 //------------------------------------------------------------------------------ |
| 277 | |
| 278 namespace { | |
| 279 | 274 |
| 280 class SimpleServer : public Worker { | 275 class SimpleServer : public Worker { |
| 281 public: | 276 public: |
| 282 explicit SimpleServer(bool pump_during_send) | 277 explicit SimpleServer(bool pump_during_send) |
| 283 : Worker(Channel::MODE_SERVER, "simpler_server"), | 278 : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 284 pump_during_send_(pump_during_send) { } | 279 pump_during_send_(pump_during_send) { } |
| 285 void Run() { | 280 void Run() { |
| 286 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); | 281 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 287 Done(); | 282 Done(); |
| 288 } | 283 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 300 } | 295 } |
| 301 }; | 296 }; |
| 302 | 297 |
| 303 void Simple(bool pump_during_send) { | 298 void Simple(bool pump_during_send) { |
| 304 std::vector<Worker*> workers; | 299 std::vector<Worker*> workers; |
| 305 workers.push_back(new SimpleServer(pump_during_send)); | 300 workers.push_back(new SimpleServer(pump_during_send)); |
| 306 workers.push_back(new SimpleClient()); | 301 workers.push_back(new SimpleClient()); |
| 307 RunTest(workers); | 302 RunTest(workers); |
| 308 } | 303 } |
| 309 | 304 |
| 310 } // namespace | |
| 311 | |
| 312 // Tests basic synchronous call | 305 // Tests basic synchronous call |
| 313 TEST_F(IPCSyncChannelTest, Simple) { | 306 TEST_F(IPCSyncChannelTest, Simple) { |
| 314 Simple(false); | 307 Simple(false); |
| 315 Simple(true); | 308 Simple(true); |
| 316 } | 309 } |
| 317 | 310 |
| 318 //----------------------------------------------------------------------------- | 311 //------------------------------------------------------------------------------ |
| 319 | |
| 320 namespace { | |
| 321 | 312 |
| 322 // Worker classes which override how the sync channel is created to use the | 313 // Worker classes which override how the sync channel is created to use the |
| 323 // two-step initialization (calling the lightweight constructor and then | 314 // two-step initialization (calling the lightweight constructor and then |
| 324 // ChannelProxy::Init separately) process. | 315 // ChannelProxy::Init separately) process. |
| 325 class TwoStepServer : public Worker { | 316 class TwoStepServer : public Worker { |
| 326 public: | 317 public: |
| 327 explicit TwoStepServer(bool create_pipe_now) | 318 explicit TwoStepServer(bool create_pipe_now) |
| 328 : Worker(Channel::MODE_SERVER, "simpler_server"), | 319 : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 329 create_pipe_now_(create_pipe_now) { } | 320 create_pipe_now_(create_pipe_now) { } |
| 330 | 321 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 bool create_pipe_now_; | 355 bool create_pipe_now_; |
| 365 }; | 356 }; |
| 366 | 357 |
| 367 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { | 358 void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { |
| 368 std::vector<Worker*> workers; | 359 std::vector<Worker*> workers; |
| 369 workers.push_back(new TwoStepServer(create_server_pipe_now)); | 360 workers.push_back(new TwoStepServer(create_server_pipe_now)); |
| 370 workers.push_back(new TwoStepClient(create_client_pipe_now)); | 361 workers.push_back(new TwoStepClient(create_client_pipe_now)); |
| 371 RunTest(workers); | 362 RunTest(workers); |
| 372 } | 363 } |
| 373 | 364 |
| 374 } // namespace | |
| 375 | |
| 376 // Tests basic two-step initialization, where you call the lightweight | 365 // Tests basic two-step initialization, where you call the lightweight |
| 377 // constructor then Init. | 366 // constructor then Init. |
| 378 TEST_F(IPCSyncChannelTest, TwoStepInitialization) { | 367 TEST_F(IPCSyncChannelTest, TwoStepInitialization) { |
| 379 TwoStep(false, false); | 368 TwoStep(false, false); |
| 380 TwoStep(false, true); | 369 TwoStep(false, true); |
| 381 TwoStep(true, false); | 370 TwoStep(true, false); |
| 382 TwoStep(true, true); | 371 TwoStep(true, true); |
| 383 } | 372 } |
| 384 | 373 |
| 385 | 374 //------------------------------------------------------------------------------ |
| 386 //----------------------------------------------------------------------------- | |
| 387 | |
| 388 namespace { | |
| 389 | 375 |
| 390 class DelayClient : public Worker { | 376 class DelayClient : public Worker { |
| 391 public: | 377 public: |
| 392 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } | 378 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
| 393 | 379 |
| 394 void OnAnswerDelay(Message* reply_msg) { | 380 void OnAnswerDelay(Message* reply_msg) { |
| 395 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); | 381 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 396 Send(reply_msg); | 382 Send(reply_msg); |
| 397 Done(); | 383 Done(); |
| 398 } | 384 } |
| 399 }; | 385 }; |
| 400 | 386 |
| 401 void DelayReply(bool pump_during_send) { | 387 void DelayReply(bool pump_during_send) { |
| 402 std::vector<Worker*> workers; | 388 std::vector<Worker*> workers; |
| 403 workers.push_back(new SimpleServer(pump_during_send)); | 389 workers.push_back(new SimpleServer(pump_during_send)); |
| 404 workers.push_back(new DelayClient()); | 390 workers.push_back(new DelayClient()); |
| 405 RunTest(workers); | 391 RunTest(workers); |
| 406 } | 392 } |
| 407 | 393 |
| 408 } // namespace | |
| 409 | |
| 410 // Tests that asynchronous replies work | 394 // Tests that asynchronous replies work |
| 411 TEST_F(IPCSyncChannelTest, DelayReply) { | 395 TEST_F(IPCSyncChannelTest, DelayReply) { |
| 412 DelayReply(false); | 396 DelayReply(false); |
| 413 DelayReply(true); | 397 DelayReply(true); |
| 414 } | 398 } |
| 415 | 399 |
| 416 //----------------------------------------------------------------------------- | 400 //------------------------------------------------------------------------------ |
| 417 | |
| 418 namespace { | |
| 419 | 401 |
| 420 class NoHangServer : public Worker { | 402 class NoHangServer : public Worker { |
| 421 public: | 403 public: |
| 422 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) | 404 NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) |
| 423 : Worker(Channel::MODE_SERVER, "no_hang_server"), | 405 : Worker(Channel::MODE_SERVER, "no_hang_server"), |
| 424 got_first_reply_(got_first_reply), | 406 got_first_reply_(got_first_reply), |
| 425 pump_during_send_(pump_during_send) { } | 407 pump_during_send_(pump_during_send) { } |
| 426 void Run() { | 408 void Run() { |
| 427 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); | 409 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 428 got_first_reply_->Signal(); | 410 got_first_reply_->Signal(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 455 }; | 437 }; |
| 456 | 438 |
| 457 void NoHang(bool pump_during_send) { | 439 void NoHang(bool pump_during_send) { |
| 458 WaitableEvent got_first_reply(false, false); | 440 WaitableEvent got_first_reply(false, false); |
| 459 std::vector<Worker*> workers; | 441 std::vector<Worker*> workers; |
| 460 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); | 442 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); |
| 461 workers.push_back(new NoHangClient(&got_first_reply)); | 443 workers.push_back(new NoHangClient(&got_first_reply)); |
| 462 RunTest(workers); | 444 RunTest(workers); |
| 463 } | 445 } |
| 464 | 446 |
| 465 } // namespace | |
| 466 | |
| 467 // Tests that caller doesn't hang if receiver dies | 447 // Tests that caller doesn't hang if receiver dies |
| 468 TEST_F(IPCSyncChannelTest, NoHang) { | 448 TEST_F(IPCSyncChannelTest, NoHang) { |
| 469 NoHang(false); | 449 NoHang(false); |
| 470 NoHang(true); | 450 NoHang(true); |
| 471 } | 451 } |
| 472 | 452 |
| 473 //----------------------------------------------------------------------------- | 453 //------------------------------------------------------------------------------ |
| 474 | |
| 475 namespace { | |
| 476 | 454 |
| 477 class UnblockServer : public Worker { | 455 class UnblockServer : public Worker { |
| 478 public: | 456 public: |
| 479 UnblockServer(bool pump_during_send, bool delete_during_send) | 457 UnblockServer(bool pump_during_send, bool delete_during_send) |
| 480 : Worker(Channel::MODE_SERVER, "unblock_server"), | 458 : Worker(Channel::MODE_SERVER, "unblock_server"), |
| 481 pump_during_send_(pump_during_send), | 459 pump_during_send_(pump_during_send), |
| 482 delete_during_send_(delete_during_send) { } | 460 delete_during_send_(delete_during_send) { } |
| 483 void Run() { | 461 void Run() { |
| 484 if (delete_during_send_) { | 462 if (delete_during_send_) { |
| 485 // Use custom code since race conditions mean the answer may or may not be | 463 // Use custom code since race conditions mean the answer may or may not be |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 bool pump_during_send_; | 499 bool pump_during_send_; |
| 522 }; | 500 }; |
| 523 | 501 |
| 524 void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { | 502 void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { |
| 525 std::vector<Worker*> workers; | 503 std::vector<Worker*> workers; |
| 526 workers.push_back(new UnblockServer(server_pump, delete_during_send)); | 504 workers.push_back(new UnblockServer(server_pump, delete_during_send)); |
| 527 workers.push_back(new UnblockClient(client_pump)); | 505 workers.push_back(new UnblockClient(client_pump)); |
| 528 RunTest(workers); | 506 RunTest(workers); |
| 529 } | 507 } |
| 530 | 508 |
| 531 } // namespace | |
| 532 | |
| 533 // Tests that the caller unblocks to answer a sync message from the receiver. | 509 // Tests that the caller unblocks to answer a sync message from the receiver. |
| 534 TEST_F(IPCSyncChannelTest, Unblock) { | 510 TEST_F(IPCSyncChannelTest, Unblock) { |
| 535 Unblock(false, false, false); | 511 Unblock(false, false, false); |
| 536 Unblock(false, true, false); | 512 Unblock(false, true, false); |
| 537 Unblock(true, false, false); | 513 Unblock(true, false, false); |
| 538 Unblock(true, true, false); | 514 Unblock(true, true, false); |
| 539 } | 515 } |
| 540 | 516 |
| 541 //----------------------------------------------------------------------------- | 517 //------------------------------------------------------------------------------ |
| 542 | 518 |
| 543 // Tests that the the SyncChannel object can be deleted during a Send. | 519 // Tests that the the SyncChannel object can be deleted during a Send. |
| 544 TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { | 520 TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { |
| 545 Unblock(false, false, true); | 521 Unblock(false, false, true); |
| 546 Unblock(false, true, true); | 522 Unblock(false, true, true); |
| 547 Unblock(true, false, true); | 523 Unblock(true, false, true); |
| 548 Unblock(true, true, true); | 524 Unblock(true, true, true); |
| 549 } | 525 } |
| 550 | 526 |
| 551 //----------------------------------------------------------------------------- | 527 //------------------------------------------------------------------------------ |
| 552 | |
| 553 namespace { | |
| 554 | 528 |
| 555 class RecursiveServer : public Worker { | 529 class RecursiveServer : public Worker { |
| 556 public: | 530 public: |
| 557 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second) | 531 RecursiveServer(bool expected_send_result, bool pump_first, bool pump_second) |
| 558 : Worker(Channel::MODE_SERVER, "recursive_server"), | 532 : Worker(Channel::MODE_SERVER, "recursive_server"), |
| 559 expected_send_result_(expected_send_result), | 533 expected_send_result_(expected_send_result), |
| 560 pump_first_(pump_first), pump_second_(pump_second) {} | 534 pump_first_(pump_first), pump_second_(pump_second) {} |
| 561 void Run() { | 535 void Run() { |
| 562 SendDouble(pump_first_, expected_send_result_); | 536 SendDouble(pump_first_, expected_send_result_); |
| 563 Done(); | 537 Done(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 | 577 |
| 604 void Recursive( | 578 void Recursive( |
| 605 bool server_pump_first, bool server_pump_second, bool client_pump) { | 579 bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 606 std::vector<Worker*> workers; | 580 std::vector<Worker*> workers; |
| 607 workers.push_back( | 581 workers.push_back( |
| 608 new RecursiveServer(true, server_pump_first, server_pump_second)); | 582 new RecursiveServer(true, server_pump_first, server_pump_second)); |
| 609 workers.push_back(new RecursiveClient(client_pump, false)); | 583 workers.push_back(new RecursiveClient(client_pump, false)); |
| 610 RunTest(workers); | 584 RunTest(workers); |
| 611 } | 585 } |
| 612 | 586 |
| 613 } // namespace | |
| 614 | |
| 615 // Tests a server calling Send while another Send is pending. | 587 // Tests a server calling Send while another Send is pending. |
| 616 TEST_F(IPCSyncChannelTest, Recursive) { | 588 TEST_F(IPCSyncChannelTest, Recursive) { |
| 617 Recursive(false, false, false); | 589 Recursive(false, false, false); |
| 618 Recursive(false, false, true); | 590 Recursive(false, false, true); |
| 619 Recursive(false, true, false); | 591 Recursive(false, true, false); |
| 620 Recursive(false, true, true); | 592 Recursive(false, true, true); |
| 621 Recursive(true, false, false); | 593 Recursive(true, false, false); |
| 622 Recursive(true, false, true); | 594 Recursive(true, false, true); |
| 623 Recursive(true, true, false); | 595 Recursive(true, true, false); |
| 624 Recursive(true, true, true); | 596 Recursive(true, true, true); |
| 625 } | 597 } |
| 626 | 598 |
| 627 //----------------------------------------------------------------------------- | 599 //------------------------------------------------------------------------------ |
| 628 | |
| 629 namespace { | |
| 630 | 600 |
| 631 void RecursiveNoHang( | 601 void RecursiveNoHang( |
| 632 bool server_pump_first, bool server_pump_second, bool client_pump) { | 602 bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 633 std::vector<Worker*> workers; | 603 std::vector<Worker*> workers; |
| 634 workers.push_back( | 604 workers.push_back( |
| 635 new RecursiveServer(false, server_pump_first, server_pump_second)); | 605 new RecursiveServer(false, server_pump_first, server_pump_second)); |
| 636 workers.push_back(new RecursiveClient(client_pump, true)); | 606 workers.push_back(new RecursiveClient(client_pump, true)); |
| 637 RunTest(workers); | 607 RunTest(workers); |
| 638 } | 608 } |
| 639 | 609 |
| 640 } // namespace | |
| 641 | |
| 642 // Tests that if a caller makes a sync call during an existing sync call and | 610 // Tests that if a caller makes a sync call during an existing sync call and |
| 643 // the receiver dies, neither of the Send() calls hang. | 611 // the receiver dies, neither of the Send() calls hang. |
| 644 TEST_F(IPCSyncChannelTest, RecursiveNoHang) { | 612 TEST_F(IPCSyncChannelTest, RecursiveNoHang) { |
| 645 RecursiveNoHang(false, false, false); | 613 RecursiveNoHang(false, false, false); |
| 646 RecursiveNoHang(false, false, true); | 614 RecursiveNoHang(false, false, true); |
| 647 RecursiveNoHang(false, true, false); | 615 RecursiveNoHang(false, true, false); |
| 648 RecursiveNoHang(false, true, true); | 616 RecursiveNoHang(false, true, true); |
| 649 RecursiveNoHang(true, false, false); | 617 RecursiveNoHang(true, false, false); |
| 650 RecursiveNoHang(true, false, true); | 618 RecursiveNoHang(true, false, true); |
| 651 RecursiveNoHang(true, true, false); | 619 RecursiveNoHang(true, true, false); |
| 652 RecursiveNoHang(true, true, true); | 620 RecursiveNoHang(true, true, true); |
| 653 } | 621 } |
| 654 | 622 |
| 655 //----------------------------------------------------------------------------- | 623 //------------------------------------------------------------------------------ |
| 656 | |
| 657 namespace { | |
| 658 | 624 |
| 659 class MultipleServer1 : public Worker { | 625 class MultipleServer1 : public Worker { |
| 660 public: | 626 public: |
| 661 explicit MultipleServer1(bool pump_during_send) | 627 explicit MultipleServer1(bool pump_during_send) |
| 662 : Worker("test_channel1", Channel::MODE_SERVER), | 628 : Worker("test_channel1", Channel::MODE_SERVER), |
| 663 pump_during_send_(pump_during_send) { } | 629 pump_during_send_(pump_during_send) { } |
| 664 | 630 |
| 665 void Run() { | 631 void Run() { |
| 666 SendDouble(pump_during_send_, true); | 632 SendDouble(pump_during_send_, true); |
| 667 Done(); | 633 Done(); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 worker->OverrideThread(&worker_thread); | 714 worker->OverrideThread(&worker_thread); |
| 749 workers.push_back(worker); | 715 workers.push_back(worker); |
| 750 | 716 |
| 751 worker = new MultipleClient1( | 717 worker = new MultipleClient1( |
| 752 &client1_msg_received, &client1_can_reply); | 718 &client1_msg_received, &client1_can_reply); |
| 753 workers.push_back(worker); | 719 workers.push_back(worker); |
| 754 | 720 |
| 755 RunTest(workers); | 721 RunTest(workers); |
| 756 } | 722 } |
| 757 | 723 |
| 758 } // namespace | |
| 759 | |
| 760 // Tests that multiple SyncObjects on the same listener thread can unblock each | 724 // Tests that multiple SyncObjects on the same listener thread can unblock each |
| 761 // other. | 725 // other. |
| 762 TEST_F(IPCSyncChannelTest, Multiple) { | 726 TEST_F(IPCSyncChannelTest, Multiple) { |
| 763 Multiple(false, false); | 727 Multiple(false, false); |
| 764 Multiple(false, true); | 728 Multiple(false, true); |
| 765 Multiple(true, false); | 729 Multiple(true, false); |
| 766 Multiple(true, true); | 730 Multiple(true, true); |
| 767 } | 731 } |
| 768 | 732 |
| 769 //----------------------------------------------------------------------------- | 733 //------------------------------------------------------------------------------ |
| 770 | |
| 771 namespace { | |
| 772 | 734 |
| 773 // This class provides server side functionality to test the case where | 735 // This class provides server side functionality to test the case where |
| 774 // multiple sync channels are in use on the same thread on the client and | 736 // multiple sync channels are in use on the same thread on the client and |
| 775 // nested calls are issued. | 737 // nested calls are issued. |
| 776 class QueuedReplyServer : public Worker { | 738 class QueuedReplyServer : public Worker { |
| 777 public: | 739 public: |
| 778 QueuedReplyServer(base::Thread* listener_thread, | 740 QueuedReplyServer(base::Thread* listener_thread, |
| 779 const std::string& channel_name, | 741 const std::string& channel_name, |
| 780 const std::string& reply_text) | 742 const std::string& reply_text) |
| 781 : Worker(channel_name, Channel::MODE_SERVER), | 743 : Worker(channel_name, Channel::MODE_SERVER), |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 860 | 822 |
| 861 worker = new QueuedReplyClient(&client_worker_thread, | 823 worker = new QueuedReplyClient(&client_worker_thread, |
| 862 "QueuedReply_Server2", | 824 "QueuedReply_Server2", |
| 863 "Got second message", | 825 "Got second message", |
| 864 client_pump); | 826 client_pump); |
| 865 workers.push_back(worker); | 827 workers.push_back(worker); |
| 866 | 828 |
| 867 RunTest(workers); | 829 RunTest(workers); |
| 868 } | 830 } |
| 869 | 831 |
| 870 } // namespace | |
| 871 | |
| 872 // While a blocking send is in progress, the listener thread might answer other | 832 // While a blocking send is in progress, the listener thread might answer other |
| 873 // synchronous messages. This tests that if during the response to another | 833 // synchronous messages. This tests that if during the response to another |
| 874 // message the reply to the original messages comes, it is queued up correctly | 834 // message the reply to the original messages comes, it is queued up correctly |
| 875 // and the original Send is unblocked later. | 835 // and the original Send is unblocked later. |
| 876 // We also test that the send call stacks unwind correctly when the channel | 836 // We also test that the send call stacks unwind correctly when the channel |
| 877 // pumps messages while waiting for a response. | 837 // pumps messages while waiting for a response. |
| 878 TEST_F(IPCSyncChannelTest, QueuedReply) { | 838 TEST_F(IPCSyncChannelTest, QueuedReply) { |
| 879 QueuedReply(false); | 839 QueuedReply(false); |
| 880 QueuedReply(true); | 840 QueuedReply(true); |
| 881 } | 841 } |
| 882 | 842 |
| 883 //----------------------------------------------------------------------------- | 843 //------------------------------------------------------------------------------ |
| 884 | |
| 885 namespace { | |
| 886 | 844 |
| 887 class ChattyClient : public Worker { | 845 class ChattyClient : public Worker { |
| 888 public: | 846 public: |
| 889 ChattyClient() : | 847 ChattyClient() : |
| 890 Worker(Channel::MODE_CLIENT, "chatty_client") { } | 848 Worker(Channel::MODE_CLIENT, "chatty_client") { } |
| 891 | 849 |
| 892 void OnAnswer(int* answer) { | 850 void OnAnswer(int* answer) { |
| 893 // The PostMessage limit is 10k. Send 20% more than that. | 851 // The PostMessage limit is 10k. Send 20% more than that. |
| 894 const int kMessageLimit = 10000; | 852 const int kMessageLimit = 10000; |
| 895 const int kMessagesToSend = kMessageLimit * 120 / 100; | 853 const int kMessagesToSend = kMessageLimit * 120 / 100; |
| 896 for (int i = 0; i < kMessagesToSend; ++i) { | 854 for (int i = 0; i < kMessagesToSend; ++i) { |
| 897 if (!SendDouble(false, true)) | 855 if (!SendDouble(false, true)) |
| 898 break; | 856 break; |
| 899 } | 857 } |
| 900 *answer = 42; | 858 *answer = 42; |
| 901 Done(); | 859 Done(); |
| 902 } | 860 } |
| 903 }; | 861 }; |
| 904 | 862 |
| 905 void ChattyServer(bool pump_during_send) { | 863 void ChattyServer(bool pump_during_send) { |
| 906 std::vector<Worker*> workers; | 864 std::vector<Worker*> workers; |
| 907 workers.push_back(new UnblockServer(pump_during_send, false)); | 865 workers.push_back(new UnblockServer(pump_during_send, false)); |
| 908 workers.push_back(new ChattyClient()); | 866 workers.push_back(new ChattyClient()); |
| 909 RunTest(workers); | 867 RunTest(workers); |
| 910 } | 868 } |
| 911 | 869 |
| 912 } // namespace | |
| 913 | |
| 914 // Tests http://b/1093251 - that sending lots of sync messages while | 870 // Tests http://b/1093251 - that sending lots of sync messages while |
| 915 // the receiver is waiting for a sync reply does not overflow the PostMessage | 871 // the receiver is waiting for a sync reply does not overflow the PostMessage |
| 916 // queue. | 872 // queue. |
| 917 TEST_F(IPCSyncChannelTest, ChattyServer) { | 873 TEST_F(IPCSyncChannelTest, ChattyServer) { |
| 918 ChattyServer(false); | 874 ChattyServer(false); |
| 919 ChattyServer(true); | 875 ChattyServer(true); |
| 920 } | 876 } |
| 921 | 877 |
| 922 //------------------------------------------------------------------------------ | 878 //------------------------------------------------------------------------------ |
| 923 | 879 |
| 924 namespace { | |
| 925 | |
| 926 class TimeoutServer : public Worker { | 880 class TimeoutServer : public Worker { |
| 927 public: | 881 public: |
| 928 TimeoutServer(int timeout_ms, | 882 TimeoutServer(int timeout_ms, |
| 929 std::vector<bool> timeout_seq, | 883 std::vector<bool> timeout_seq, |
| 930 bool pump_during_send) | 884 bool pump_during_send) |
| 931 : Worker(Channel::MODE_SERVER, "timeout_server"), | 885 : Worker(Channel::MODE_SERVER, "timeout_server"), |
| 932 timeout_ms_(timeout_ms), | 886 timeout_ms_(timeout_ms), |
| 933 timeout_seq_(timeout_seq), | 887 timeout_seq_(timeout_seq), |
| 934 pump_during_send_(pump_during_send) { | 888 pump_during_send_(pump_during_send) { |
| 935 } | 889 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1002 timeout_seq.push_back(true); | 956 timeout_seq.push_back(true); |
| 1003 timeout_seq.push_back(false); | 957 timeout_seq.push_back(false); |
| 1004 timeout_seq.push_back(false); | 958 timeout_seq.push_back(false); |
| 1005 timeout_seq.push_back(true); | 959 timeout_seq.push_back(true); |
| 1006 timeout_seq.push_back(false); | 960 timeout_seq.push_back(false); |
| 1007 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); | 961 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 1008 workers.push_back(new UnresponsiveClient(timeout_seq)); | 962 workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 1009 RunTest(workers); | 963 RunTest(workers); |
| 1010 } | 964 } |
| 1011 | 965 |
| 1012 } // namespace | |
| 1013 | |
| 1014 // Tests that SendWithTimeout does not time-out if the response comes back fast | 966 // Tests that SendWithTimeout does not time-out if the response comes back fast |
| 1015 // enough. | 967 // enough. |
| 1016 TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { | 968 TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { |
| 1017 SendWithTimeoutOK(false); | 969 SendWithTimeoutOK(false); |
| 1018 SendWithTimeoutOK(true); | 970 SendWithTimeoutOK(true); |
| 1019 } | 971 } |
| 1020 | 972 |
| 1021 // Tests that SendWithTimeout does time-out. | 973 // Tests that SendWithTimeout does time-out. |
| 1022 TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { | 974 TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
| 1023 SendWithTimeoutTimeout(false); | 975 SendWithTimeoutTimeout(false); |
| 1024 SendWithTimeoutTimeout(true); | 976 SendWithTimeoutTimeout(true); |
| 1025 } | 977 } |
| 1026 | 978 |
| 1027 // Sends some message that time-out and some that succeed. | 979 // Sends some message that time-out and some that succeed. |
| 1028 // Crashes flakily, http://crbug.com/70075. | 980 // Crashes flakily, http://crbug.com/70075. |
| 1029 TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) { | 981 TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) { |
| 1030 SendWithTimeoutMixedOKAndTimeout(false); | 982 SendWithTimeoutMixedOKAndTimeout(false); |
| 1031 SendWithTimeoutMixedOKAndTimeout(true); | 983 SendWithTimeoutMixedOKAndTimeout(true); |
| 1032 } | 984 } |
| 1033 | 985 |
| 1034 //------------------------------------------------------------------------------ | 986 //------------------------------------------------------------------------------ |
| 1035 | 987 |
| 1036 namespace { | |
| 1037 | |
| 1038 void NestedCallback(Worker* server) { | 988 void NestedCallback(Worker* server) { |
| 1039 // Sleep a bit so that we wake up after the reply has been received. | 989 // Sleep a bit so that we wake up after the reply has been received. |
| 1040 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(250)); | 990 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(250)); |
| 1041 server->SendAnswerToLife(true, base::kNoTimeout, true); | 991 server->SendAnswerToLife(true, base::kNoTimeout, true); |
| 1042 } | 992 } |
| 1043 | 993 |
| 1044 bool timeout_occurred = false; | 994 bool timeout_occurred = false; |
| 1045 | 995 |
| 1046 void TimeoutCallback() { | 996 void TimeoutCallback() { |
| 1047 timeout_occurred = true; | 997 timeout_occurred = true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1062 // Even though we have a timeout on the Send, it will succeed since for this | 1012 // Even though we have a timeout on the Send, it will succeed since for this |
| 1063 // bug, the reply message comes back and is deserialized, however the done | 1013 // bug, the reply message comes back and is deserialized, however the done |
| 1064 // event wasn't set. So we indirectly use the timeout task to notice if a | 1014 // event wasn't set. So we indirectly use the timeout task to notice if a |
| 1065 // timeout occurred. | 1015 // timeout occurred. |
| 1066 SendAnswerToLife(true, 10000, true); | 1016 SendAnswerToLife(true, 10000, true); |
| 1067 DCHECK(!timeout_occurred); | 1017 DCHECK(!timeout_occurred); |
| 1068 Done(); | 1018 Done(); |
| 1069 } | 1019 } |
| 1070 }; | 1020 }; |
| 1071 | 1021 |
| 1072 } // namespace | |
| 1073 | |
| 1074 // Tests http://b/1474092 - that if after the done_event is set but before | 1022 // Tests http://b/1474092 - that if after the done_event is set but before |
| 1075 // OnObjectSignaled is called another message is sent out, then after its | 1023 // OnObjectSignaled is called another message is sent out, then after its |
| 1076 // reply comes back OnObjectSignaled will be called for the first message. | 1024 // reply comes back OnObjectSignaled will be called for the first message. |
| 1077 TEST_F(IPCSyncChannelTest, DoneEventRace) { | 1025 TEST_F(IPCSyncChannelTest, DoneEventRace) { |
| 1078 std::vector<Worker*> workers; | 1026 std::vector<Worker*> workers; |
| 1079 workers.push_back(new DoneEventRaceServer()); | 1027 workers.push_back(new DoneEventRaceServer()); |
| 1080 workers.push_back(new SimpleClient()); | 1028 workers.push_back(new SimpleClient()); |
| 1081 RunTest(workers); | 1029 RunTest(workers); |
| 1082 } | 1030 } |
| 1083 | 1031 |
| 1084 //----------------------------------------------------------------------------- | 1032 //------------------------------------------------------------------------------ |
| 1085 | |
| 1086 namespace { | |
| 1087 | 1033 |
| 1088 class TestSyncMessageFilter : public SyncMessageFilter { | 1034 class TestSyncMessageFilter : public SyncMessageFilter { |
| 1089 public: | 1035 public: |
| 1090 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, | 1036 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, |
| 1091 Worker* worker, | 1037 Worker* worker, |
| 1092 scoped_refptr<base::MessageLoopProxy> message_loop) | 1038 scoped_refptr<base::MessageLoopProxy> message_loop) |
| 1093 : SyncMessageFilter(shutdown_event), | 1039 : SyncMessageFilter(shutdown_event), |
| 1094 worker_(worker), | 1040 worker_(worker), |
| 1095 message_loop_(message_loop) { | 1041 message_loop_(message_loop) { |
| 1096 } | 1042 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1166 | 1112 |
| 1167 bool Send(Message* msg) { | 1113 bool Send(Message* msg) { |
| 1168 send_result_ = Worker::Send(msg); | 1114 send_result_ = Worker::Send(msg); |
| 1169 Done(); | 1115 Done(); |
| 1170 return send_result_; | 1116 return send_result_; |
| 1171 } | 1117 } |
| 1172 | 1118 |
| 1173 bool send_result_; | 1119 bool send_result_; |
| 1174 }; | 1120 }; |
| 1175 | 1121 |
| 1176 } // namespace | |
| 1177 | |
| 1178 // Tests basic synchronous call | 1122 // Tests basic synchronous call |
| 1179 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { | 1123 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1180 std::vector<Worker*> workers; | 1124 std::vector<Worker*> workers; |
| 1181 workers.push_back(new SyncMessageFilterServer()); | 1125 workers.push_back(new SyncMessageFilterServer()); |
| 1182 workers.push_back(new SimpleClient()); | 1126 workers.push_back(new SimpleClient()); |
| 1183 RunTest(workers); | 1127 RunTest(workers); |
| 1184 } | 1128 } |
| 1185 | 1129 |
| 1186 // Test the case when the channel is closed and a Send is attempted after that. | 1130 // Test the case when the channel is closed and a Send is attempted after that. |
| 1187 TEST_F(IPCSyncChannelTest, SendAfterClose) { | 1131 TEST_F(IPCSyncChannelTest, SendAfterClose) { |
| 1188 ServerSendAfterClose server; | 1132 ServerSendAfterClose server; |
| 1189 server.Start(); | 1133 server.Start(); |
| 1190 | 1134 |
| 1191 server.done_event()->Wait(); | 1135 server.done_event()->Wait(); |
| 1192 server.done_event()->Reset(); | 1136 server.done_event()->Reset(); |
| 1193 | 1137 |
| 1194 server.SendDummy(); | 1138 server.SendDummy(); |
| 1195 server.done_event()->Wait(); | 1139 server.done_event()->Wait(); |
| 1196 | 1140 |
| 1197 EXPECT_FALSE(server.send_result()); | 1141 EXPECT_FALSE(server.send_result()); |
| 1198 | 1142 |
| 1199 server.Shutdown(); | 1143 server.Shutdown(); |
| 1200 } | 1144 } |
| 1201 | 1145 |
| 1202 //----------------------------------------------------------------------------- | 1146 //------------------------------------------------------------------------------ |
| 1203 | |
| 1204 namespace { | |
| 1205 | 1147 |
| 1206 class RestrictedDispatchServer : public Worker { | 1148 class RestrictedDispatchServer : public Worker { |
| 1207 public: | 1149 public: |
| 1208 RestrictedDispatchServer(WaitableEvent* sent_ping_event, | 1150 RestrictedDispatchServer(WaitableEvent* sent_ping_event, |
| 1209 WaitableEvent* wait_event) | 1151 WaitableEvent* wait_event) |
| 1210 : Worker("restricted_channel", Channel::MODE_SERVER), | 1152 : Worker("restricted_channel", Channel::MODE_SERVER), |
| 1211 sent_ping_event_(sent_ping_event), | 1153 sent_ping_event_(sent_ping_event), |
| 1212 wait_event_(wait_event) { } | 1154 wait_event_(wait_event) { } |
| 1213 | 1155 |
| 1214 void OnDoPing(int ping) { | 1156 void OnDoPing(int ping) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1369 } | 1311 } |
| 1370 | 1312 |
| 1371 int ping_; | 1313 int ping_; |
| 1372 RestrictedDispatchServer* server_; | 1314 RestrictedDispatchServer* server_; |
| 1373 NonRestrictedDispatchServer* server2_; | 1315 NonRestrictedDispatchServer* server2_; |
| 1374 int* success_; | 1316 int* success_; |
| 1375 WaitableEvent* sent_ping_event_; | 1317 WaitableEvent* sent_ping_event_; |
| 1376 scoped_ptr<SyncChannel> non_restricted_channel_; | 1318 scoped_ptr<SyncChannel> non_restricted_channel_; |
| 1377 }; | 1319 }; |
| 1378 | 1320 |
| 1379 } // namespace | |
| 1380 | |
| 1381 TEST_F(IPCSyncChannelTest, RestrictedDispatch) { | 1321 TEST_F(IPCSyncChannelTest, RestrictedDispatch) { |
| 1382 WaitableEvent sent_ping_event(false, false); | 1322 WaitableEvent sent_ping_event(false, false); |
| 1383 WaitableEvent wait_event(false, false); | 1323 WaitableEvent wait_event(false, false); |
| 1384 RestrictedDispatchServer* server = | 1324 RestrictedDispatchServer* server = |
| 1385 new RestrictedDispatchServer(&sent_ping_event, &wait_event); | 1325 new RestrictedDispatchServer(&sent_ping_event, &wait_event); |
| 1386 NonRestrictedDispatchServer* server2 = | 1326 NonRestrictedDispatchServer* server2 = |
| 1387 new NonRestrictedDispatchServer(&wait_event); | 1327 new NonRestrictedDispatchServer(&wait_event); |
| 1388 | 1328 |
| 1389 int success = 0; | 1329 int success = 0; |
| 1390 std::vector<Worker*> workers; | 1330 std::vector<Worker*> workers; |
| 1391 workers.push_back(server); | 1331 workers.push_back(server); |
| 1392 workers.push_back(server2); | 1332 workers.push_back(server2); |
| 1393 workers.push_back(new RestrictedDispatchClient( | 1333 workers.push_back(new RestrictedDispatchClient( |
| 1394 &sent_ping_event, server, server2, &success)); | 1334 &sent_ping_event, server, server2, &success)); |
| 1395 RunTest(workers); | 1335 RunTest(workers); |
| 1396 EXPECT_EQ(4, success); | 1336 EXPECT_EQ(4, success); |
| 1397 } | 1337 } |
| 1398 | 1338 |
| 1399 //----------------------------------------------------------------------------- | 1339 //------------------------------------------------------------------------------ |
| 1400 | 1340 |
| 1401 // This test case inspired by crbug.com/108491 | 1341 // This test case inspired by crbug.com/108491 |
| 1402 // We create two servers that use the same ListenerThread but have | 1342 // We create two servers that use the same ListenerThread but have |
| 1403 // SetRestrictDispatchToSameChannel set to true. | 1343 // SetRestrictDispatchToSameChannel set to true. |
| 1404 // We create clients, then use some specific WaitableEvent wait/signalling to | 1344 // We create clients, then use some specific WaitableEvent wait/signalling to |
| 1405 // ensure that messages get dispatched in a way that causes a deadlock due to | 1345 // ensure that messages get dispatched in a way that causes a deadlock due to |
| 1406 // a nested dispatch and an eligible message in a higher-level dispatch's | 1346 // a nested dispatch and an eligible message in a higher-level dispatch's |
| 1407 // delayed_queue. Specifically, we start with client1 about so send an | 1347 // delayed_queue. Specifically, we start with client1 about so send an |
| 1408 // unblocking message to server1, while the shared listener thread for the | 1348 // unblocking message to server1, while the shared listener thread for the |
| 1409 // servers server1 and server2 is about to send a non-unblocking message to | 1349 // servers server1 and server2 is about to send a non-unblocking message to |
| 1410 // client1. At the same time, client2 will be about to send an unblocking | 1350 // client1. At the same time, client2 will be about to send an unblocking |
| 1411 // message to server2. Server1 will handle the client1->server1 message by | 1351 // message to server2. Server1 will handle the client1->server1 message by |
| 1412 // telling server2 to send a non-unblocking message to client2. | 1352 // telling server2 to send a non-unblocking message to client2. |
| 1413 // What should happen is that the send to server2 should find the pending, | 1353 // What should happen is that the send to server2 should find the pending, |
| 1414 // same-context client2->server2 message to dispatch, causing client2 to | 1354 // same-context client2->server2 message to dispatch, causing client2 to |
| 1415 // unblock then handle the server2->client2 message, so that the shared | 1355 // unblock then handle the server2->client2 message, so that the shared |
| 1416 // servers' listener thread can then respond to the client1->server1 message. | 1356 // servers' listener thread can then respond to the client1->server1 message. |
| 1417 // Then client1 can handle the non-unblocking server1->client1 message. | 1357 // Then client1 can handle the non-unblocking server1->client1 message. |
| 1418 // The old code would end up in a state where the server2->client2 message is | 1358 // The old code would end up in a state where the server2->client2 message is |
| 1419 // sent, but the client2->server2 message (which is eligible for dispatch, and | 1359 // sent, but the client2->server2 message (which is eligible for dispatch, and |
| 1420 // which is what client2 is waiting for) is stashed in a local delayed_queue | 1360 // which is what client2 is waiting for) is stashed in a local delayed_queue |
| 1421 // that has server1's channel context, causing a deadlock. | 1361 // that has server1's channel context, causing a deadlock. |
| 1422 // WaitableEvents in the events array are used to: | 1362 // WaitableEvents in the events array are used to: |
| 1423 // event 0: indicate to client1 that server listener is in OnDoServerTask | 1363 // event 0: indicate to client1 that server listener is in OnDoServerTask |
| 1424 // event 1: indicate to client1 that client2 listener is in OnDoClient2Task | 1364 // event 1: indicate to client1 that client2 listener is in OnDoClient2Task |
| 1425 // event 2: indicate to server1 that client2 listener is in OnDoClient2Task | 1365 // event 2: indicate to server1 that client2 listener is in OnDoClient2Task |
| 1426 // event 3: indicate to client2 that server listener is in OnDoServerTask | 1366 // event 3: indicate to client2 that server listener is in OnDoServerTask |
| 1427 | 1367 |
| 1428 namespace { | |
| 1429 | |
| 1430 class RestrictedDispatchDeadlockServer : public Worker { | 1368 class RestrictedDispatchDeadlockServer : public Worker { |
| 1431 public: | 1369 public: |
| 1432 RestrictedDispatchDeadlockServer(int server_num, | 1370 RestrictedDispatchDeadlockServer(int server_num, |
| 1433 WaitableEvent* server_ready_event, | 1371 WaitableEvent* server_ready_event, |
| 1434 WaitableEvent** events, | 1372 WaitableEvent** events, |
| 1435 RestrictedDispatchDeadlockServer* peer) | 1373 RestrictedDispatchDeadlockServer* peer) |
| 1436 : Worker(server_num == 1 ? "channel1" : "channel2", Channel::MODE_SERVER), | 1374 : Worker(server_num == 1 ? "channel1" : "channel2", Channel::MODE_SERVER), |
| 1437 server_num_(server_num), | 1375 server_num_(server_num), |
| 1438 server_ready_event_(server_ready_event), | 1376 server_ready_event_(server_ready_event), |
| 1439 events_(events), | 1377 events_(events), |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1599 | 1537 |
| 1600 RestrictedDispatchDeadlockServer* server_; | 1538 RestrictedDispatchDeadlockServer* server_; |
| 1601 RestrictedDispatchDeadlockClient2* peer_; | 1539 RestrictedDispatchDeadlockClient2* peer_; |
| 1602 WaitableEvent* server_ready_event_; | 1540 WaitableEvent* server_ready_event_; |
| 1603 WaitableEvent** events_; | 1541 WaitableEvent** events_; |
| 1604 bool received_msg_; | 1542 bool received_msg_; |
| 1605 bool received_noarg_reply_; | 1543 bool received_noarg_reply_; |
| 1606 bool done_issued_; | 1544 bool done_issued_; |
| 1607 }; | 1545 }; |
| 1608 | 1546 |
| 1609 } // namespace | |
| 1610 | |
| 1611 TEST_F(IPCSyncChannelTest, RestrictedDispatchDeadlock) { | 1547 TEST_F(IPCSyncChannelTest, RestrictedDispatchDeadlock) { |
| 1612 std::vector<Worker*> workers; | 1548 std::vector<Worker*> workers; |
| 1613 | 1549 |
| 1614 // A shared worker thread so that server1 and server2 run on one thread. | 1550 // A shared worker thread so that server1 and server2 run on one thread. |
| 1615 base::Thread worker_thread("RestrictedDispatchDeadlock"); | 1551 base::Thread worker_thread("RestrictedDispatchDeadlock"); |
| 1616 ASSERT_TRUE(worker_thread.Start()); | 1552 ASSERT_TRUE(worker_thread.Start()); |
| 1617 | 1553 |
| 1618 WaitableEvent server1_ready(false, false); | 1554 WaitableEvent server1_ready(false, false); |
| 1619 WaitableEvent server2_ready(false, false); | 1555 WaitableEvent server2_ready(false, false); |
| 1620 | 1556 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1643 server1->OverrideThread(&worker_thread); | 1579 server1->OverrideThread(&worker_thread); |
| 1644 workers.push_back(server1); | 1580 workers.push_back(server1); |
| 1645 | 1581 |
| 1646 client1 = new RestrictedDispatchDeadlockClient1(server1, client2, | 1582 client1 = new RestrictedDispatchDeadlockClient1(server1, client2, |
| 1647 &server1_ready, events); | 1583 &server1_ready, events); |
| 1648 workers.push_back(client1); | 1584 workers.push_back(client1); |
| 1649 | 1585 |
| 1650 RunTest(workers); | 1586 RunTest(workers); |
| 1651 } | 1587 } |
| 1652 | 1588 |
| 1653 //----------------------------------------------------------------------------- | 1589 //------------------------------------------------------------------------------ |
| 1654 | 1590 |
| 1655 // This test case inspired by crbug.com/120530 | 1591 // This test case inspired by crbug.com/120530 |
| 1656 // We create 4 workers that pipe to each other W1->W2->W3->W4->W1 then we send a | 1592 // We create 4 workers that pipe to each other W1->W2->W3->W4->W1 then we send a |
| 1657 // message that recurses through 3, 4 or 5 steps to make sure, say, W1 can | 1593 // message that recurses through 3, 4 or 5 steps to make sure, say, W1 can |
| 1658 // re-enter when called from W4 while it's sending a message to W2. | 1594 // re-enter when called from W4 while it's sending a message to W2. |
| 1659 // The first worker drives the whole test so it must be treated specially. | 1595 // The first worker drives the whole test so it must be treated specially. |
| 1660 namespace { | |
| 1661 | 1596 |
| 1662 class RestrictedDispatchPipeWorker : public Worker { | 1597 class RestrictedDispatchPipeWorker : public Worker { |
| 1663 public: | 1598 public: |
| 1664 RestrictedDispatchPipeWorker( | 1599 RestrictedDispatchPipeWorker( |
| 1665 const std::string &channel1, | 1600 const std::string &channel1, |
| 1666 WaitableEvent* event1, | 1601 WaitableEvent* event1, |
| 1667 const std::string &channel2, | 1602 const std::string &channel2, |
| 1668 WaitableEvent* event2, | 1603 WaitableEvent* event2, |
| 1669 int group, | 1604 int group, |
| 1670 int* success) | 1605 int* success) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1730 } | 1665 } |
| 1731 | 1666 |
| 1732 scoped_ptr<SyncChannel> other_channel_; | 1667 scoped_ptr<SyncChannel> other_channel_; |
| 1733 WaitableEvent* event1_; | 1668 WaitableEvent* event1_; |
| 1734 WaitableEvent* event2_; | 1669 WaitableEvent* event2_; |
| 1735 std::string other_channel_name_; | 1670 std::string other_channel_name_; |
| 1736 int group_; | 1671 int group_; |
| 1737 int* success_; | 1672 int* success_; |
| 1738 }; | 1673 }; |
| 1739 | 1674 |
| 1740 } // namespace | |
| 1741 | |
| 1742 TEST_F(IPCSyncChannelTest, RestrictedDispatch4WayDeadlock) { | 1675 TEST_F(IPCSyncChannelTest, RestrictedDispatch4WayDeadlock) { |
| 1743 int success = 0; | 1676 int success = 0; |
| 1744 std::vector<Worker*> workers; | 1677 std::vector<Worker*> workers; |
| 1745 WaitableEvent event0(true, false); | 1678 WaitableEvent event0(true, false); |
| 1746 WaitableEvent event1(true, false); | 1679 WaitableEvent event1(true, false); |
| 1747 WaitableEvent event2(true, false); | 1680 WaitableEvent event2(true, false); |
| 1748 WaitableEvent event3(true, false); | 1681 WaitableEvent event3(true, false); |
| 1749 workers.push_back(new RestrictedDispatchPipeWorker( | 1682 workers.push_back(new RestrictedDispatchPipeWorker( |
| 1750 "channel0", &event0, "channel1", &event1, 1, &success)); | 1683 "channel0", &event0, "channel1", &event1, 1, &success)); |
| 1751 workers.push_back(new RestrictedDispatchPipeWorker( | 1684 workers.push_back(new RestrictedDispatchPipeWorker( |
| 1752 "channel1", &event1, "channel2", &event2, 2, NULL)); | 1685 "channel1", &event1, "channel2", &event2, 2, NULL)); |
| 1753 workers.push_back(new RestrictedDispatchPipeWorker( | 1686 workers.push_back(new RestrictedDispatchPipeWorker( |
| 1754 "channel2", &event2, "channel3", &event3, 3, NULL)); | 1687 "channel2", &event2, "channel3", &event3, 3, NULL)); |
| 1755 workers.push_back(new RestrictedDispatchPipeWorker( | 1688 workers.push_back(new RestrictedDispatchPipeWorker( |
| 1756 "channel3", &event3, "channel0", &event0, 4, NULL)); | 1689 "channel3", &event3, "channel0", &event0, 4, NULL)); |
| 1757 RunTest(workers); | 1690 RunTest(workers); |
| 1758 EXPECT_EQ(3, success); | 1691 EXPECT_EQ(3, success); |
| 1759 } | 1692 } |
| 1760 | 1693 |
| 1694 //------------------------------------------------------------------------------ |
| 1761 | 1695 |
| 1762 //----------------------------------------------------------------------------- | |
| 1763 // | |
| 1764 // This test case inspired by crbug.com/122443 | 1696 // This test case inspired by crbug.com/122443 |
| 1765 // We want to make sure a reply message with the unblock flag set correctly | 1697 // We want to make sure a reply message with the unblock flag set correctly |
| 1766 // behaves as a reply, not a regular message. | 1698 // behaves as a reply, not a regular message. |
| 1767 // We have 3 workers. Server1 will send a message to Server2 (which will block), | 1699 // We have 3 workers. Server1 will send a message to Server2 (which will block), |
| 1768 // during which it will dispatch a message comming from Client, at which point | 1700 // during which it will dispatch a message comming from Client, at which point |
| 1769 // it will send another message to Server2. While sending that second message it | 1701 // it will send another message to Server2. While sending that second message it |
| 1770 // will receive a reply from Server1 with the unblock flag. | 1702 // will receive a reply from Server1 with the unblock flag. |
| 1771 | 1703 |
| 1772 namespace { | |
| 1773 | |
| 1774 class ReentrantReplyServer1 : public Worker { | 1704 class ReentrantReplyServer1 : public Worker { |
| 1775 public: | 1705 public: |
| 1776 ReentrantReplyServer1(WaitableEvent* server_ready) | 1706 ReentrantReplyServer1(WaitableEvent* server_ready) |
| 1777 : Worker("reentrant_reply1", Channel::MODE_SERVER), | 1707 : Worker("reentrant_reply1", Channel::MODE_SERVER), |
| 1778 server_ready_(server_ready) { } | 1708 server_ready_(server_ready) { } |
| 1779 | 1709 |
| 1780 void Run() { | 1710 void Run() { |
| 1781 server2_channel_.reset(new SyncChannel( | 1711 server2_channel_.reset(new SyncChannel( |
| 1782 "reentrant_reply2", Channel::MODE_CLIENT, this, | 1712 "reentrant_reply2", Channel::MODE_CLIENT, this, |
| 1783 ipc_thread().message_loop_proxy(), true, shutdown_event())); | 1713 ipc_thread().message_loop_proxy(), true, shutdown_event())); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1854 void Run() { | 1784 void Run() { |
| 1855 server_ready_->Wait(); | 1785 server_ready_->Wait(); |
| 1856 Send(new SyncChannelTestMsg_Reentrant2()); | 1786 Send(new SyncChannelTestMsg_Reentrant2()); |
| 1857 Done(); | 1787 Done(); |
| 1858 } | 1788 } |
| 1859 | 1789 |
| 1860 private: | 1790 private: |
| 1861 WaitableEvent* server_ready_; | 1791 WaitableEvent* server_ready_; |
| 1862 }; | 1792 }; |
| 1863 | 1793 |
| 1864 } // namespace | |
| 1865 | |
| 1866 TEST_F(IPCSyncChannelTest, ReentrantReply) { | 1794 TEST_F(IPCSyncChannelTest, ReentrantReply) { |
| 1867 std::vector<Worker*> workers; | 1795 std::vector<Worker*> workers; |
| 1868 WaitableEvent server_ready(false, false); | 1796 WaitableEvent server_ready(false, false); |
| 1869 workers.push_back(new ReentrantReplyServer2()); | 1797 workers.push_back(new ReentrantReplyServer2()); |
| 1870 workers.push_back(new ReentrantReplyServer1(&server_ready)); | 1798 workers.push_back(new ReentrantReplyServer1(&server_ready)); |
| 1871 workers.push_back(new ReentrantReplyClient(&server_ready)); | 1799 workers.push_back(new ReentrantReplyClient(&server_ready)); |
| 1872 RunTest(workers); | 1800 RunTest(workers); |
| 1873 } | 1801 } |
| 1874 | 1802 |
| 1875 //----------------------------------------------------------------------------- | 1803 //------------------------------------------------------------------------------ |
| 1876 | 1804 |
| 1877 // Generate a validated channel ID using Channel::GenerateVerifiedChannelID(). | 1805 // Generate a validated channel ID using Channel::GenerateVerifiedChannelID(). |
| 1878 namespace { | |
| 1879 | 1806 |
| 1880 class VerifiedServer : public Worker { | 1807 class VerifiedServer : public Worker { |
| 1881 public: | 1808 public: |
| 1882 VerifiedServer(base::Thread* listener_thread, | 1809 VerifiedServer(base::Thread* listener_thread, |
| 1883 const std::string& channel_name, | 1810 const std::string& channel_name, |
| 1884 const std::string& reply_text) | 1811 const std::string& reply_text) |
| 1885 : Worker(channel_name, Channel::MODE_SERVER), | 1812 : Worker(channel_name, Channel::MODE_SERVER), |
| 1886 reply_text_(reply_text) { | 1813 reply_text_(reply_text) { |
| 1887 Worker::OverrideThread(listener_thread); | 1814 Worker::OverrideThread(listener_thread); |
| 1888 } | 1815 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1950 channel_id, | 1877 channel_id, |
| 1951 "Got first message"); | 1878 "Got first message"); |
| 1952 workers.push_back(worker); | 1879 workers.push_back(worker); |
| 1953 | 1880 |
| 1954 RunTest(workers); | 1881 RunTest(workers); |
| 1955 | 1882 |
| 1956 #if defined(OS_WIN) | 1883 #if defined(OS_WIN) |
| 1957 #endif | 1884 #endif |
| 1958 } | 1885 } |
| 1959 | 1886 |
| 1960 } // namespace | |
| 1961 | |
| 1962 // Windows needs to send an out-of-band secret to verify the client end of the | 1887 // Windows needs to send an out-of-band secret to verify the client end of the |
| 1963 // channel. Test that we still connect correctly in that case. | 1888 // channel. Test that we still connect correctly in that case. |
| 1964 TEST_F(IPCSyncChannelTest, Verified) { | 1889 TEST_F(IPCSyncChannelTest, Verified) { |
| 1965 Verified(); | 1890 Verified(); |
| 1966 } | 1891 } |
| 1967 | 1892 |
| 1893 } // namespace |
| 1968 } // namespace IPC | 1894 } // namespace IPC |
| OLD | NEW |