Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/stl_util-inl.h" | 16 #include "base/stl_util-inl.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 19 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
| 20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
| 22 #include "ipc/ipc_message.h" | 22 #include "ipc/ipc_message.h" |
| 23 #include "ipc/ipc_sync_message_filter.h" | 23 #include "ipc/ipc_sync_message_filter.h" |
| 24 #include "ipc/ipc_sync_message_unittest.h" | 24 #include "ipc/ipc_sync_message_unittest.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 using namespace IPC; | |
| 28 using base::WaitableEvent; | 27 using base::WaitableEvent; |
| 29 | 28 |
| 30 namespace { | 29 namespace IPC { |
|
brettw
2011/05/13 19:13:40
I think we still want to keep the anonymous namesp
KushalP
2011/05/13 19:19:17
Done.
| |
| 31 | 30 |
| 32 // Base class for a "process" with listener and IPC threads. | 31 // Base class for a "process" with listener and IPC threads. |
| 33 class Worker : public Channel::Listener, public Message::Sender { | 32 class Worker : public Channel::Listener, public Message::Sender { |
| 34 public: | 33 public: |
| 35 // Will create a channel without a name. | 34 // Will create a channel without a name. |
| 36 Worker(Channel::Mode mode, const std::string& thread_name) | 35 Worker(Channel::Mode mode, const std::string& thread_name) |
| 37 : done_(new WaitableEvent(false, false)), | 36 : done_(new WaitableEvent(false, false)), |
| 38 channel_created_(new WaitableEvent(false, false)), | 37 channel_created_(new WaitableEvent(false, false)), |
| 39 mode_(mode), | 38 mode_(mode), |
| 40 ipc_thread_((thread_name + "_ipc").c_str()), | 39 ipc_thread_((thread_name + "_ipc").c_str()), |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 workers[i]->Start(); | 246 workers[i]->Start(); |
| 248 } | 247 } |
| 249 | 248 |
| 250 // wait for all the workers to finish | 249 // wait for all the workers to finish |
| 251 for (size_t i = 0; i < workers.size(); ++i) | 250 for (size_t i = 0; i < workers.size(); ++i) |
| 252 workers[i]->done_event()->Wait(); | 251 workers[i]->done_event()->Wait(); |
| 253 | 252 |
| 254 STLDeleteContainerPointers(workers.begin(), workers.end()); | 253 STLDeleteContainerPointers(workers.begin(), workers.end()); |
| 255 } | 254 } |
| 256 | 255 |
| 257 } // namespace | |
| 258 | |
| 259 class IPCSyncChannelTest : public testing::Test { | 256 class IPCSyncChannelTest : public testing::Test { |
| 260 private: | 257 private: |
| 261 MessageLoop message_loop_; | 258 MessageLoop message_loop_; |
| 262 }; | 259 }; |
| 263 | 260 |
| 264 //----------------------------------------------------------------------------- | 261 //----------------------------------------------------------------------------- |
| 265 | 262 |
| 266 namespace { | |
| 267 | |
| 268 class SimpleServer : public Worker { | 263 class SimpleServer : public Worker { |
| 269 public: | 264 public: |
| 270 explicit SimpleServer(bool pump_during_send) | 265 explicit SimpleServer(bool pump_during_send) |
| 271 : Worker(Channel::MODE_SERVER, "simpler_server"), | 266 : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 272 pump_during_send_(pump_during_send) { } | 267 pump_during_send_(pump_during_send) { } |
| 273 void Run() { | 268 void Run() { |
| 274 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); | 269 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 275 Done(); | 270 Done(); |
| 276 } | 271 } |
| 277 | 272 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 288 } | 283 } |
| 289 }; | 284 }; |
| 290 | 285 |
| 291 void Simple(bool pump_during_send) { | 286 void Simple(bool pump_during_send) { |
| 292 std::vector<Worker*> workers; | 287 std::vector<Worker*> workers; |
| 293 workers.push_back(new SimpleServer(pump_during_send)); | 288 workers.push_back(new SimpleServer(pump_during_send)); |
| 294 workers.push_back(new SimpleClient()); | 289 workers.push_back(new SimpleClient()); |
| 295 RunTest(workers); | 290 RunTest(workers); |
| 296 } | 291 } |
| 297 | 292 |
| 298 } // namespace | |
| 299 | |
| 300 // Tests basic synchronous call | 293 // Tests basic synchronous call |
| 301 TEST_F(IPCSyncChannelTest, Simple) { | 294 TEST_F(IPCSyncChannelTest, Simple) { |
| 302 Simple(false); | 295 Simple(false); |
| 303 Simple(true); | 296 Simple(true); |
| 304 } | 297 } |
| 305 | 298 |
| 306 //----------------------------------------------------------------------------- | 299 //----------------------------------------------------------------------------- |
| 307 | 300 |
| 308 namespace { | |
| 309 | |
| 310 class DelayClient : public Worker { | 301 class DelayClient : public Worker { |
| 311 public: | 302 public: |
| 312 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } | 303 DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |
| 313 | 304 |
| 314 void OnAnswerDelay(Message* reply_msg) { | 305 void OnAnswerDelay(Message* reply_msg) { |
| 315 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); | 306 SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42); |
| 316 Send(reply_msg); | 307 Send(reply_msg); |
| 317 Done(); | 308 Done(); |
| 318 } | 309 } |
| 319 }; | 310 }; |
| 320 | 311 |
| 321 void DelayReply(bool pump_during_send) { | 312 void DelayReply(bool pump_during_send) { |
| 322 std::vector<Worker*> workers; | 313 std::vector<Worker*> workers; |
| 323 workers.push_back(new SimpleServer(pump_during_send)); | 314 workers.push_back(new SimpleServer(pump_during_send)); |
| 324 workers.push_back(new DelayClient()); | 315 workers.push_back(new DelayClient()); |
| 325 RunTest(workers); | 316 RunTest(workers); |
| 326 } | 317 } |
| 327 | 318 |
| 328 } // namespace | |
| 329 | |
| 330 // Tests that asynchronous replies work | 319 // Tests that asynchronous replies work |
| 331 TEST_F(IPCSyncChannelTest, DelayReply) { | 320 TEST_F(IPCSyncChannelTest, DelayReply) { |
| 332 DelayReply(false); | 321 DelayReply(false); |
| 333 DelayReply(true); | 322 DelayReply(true); |
| 334 } | 323 } |
| 335 | 324 |
| 336 //----------------------------------------------------------------------------- | 325 //----------------------------------------------------------------------------- |
| 337 | 326 |
| 338 namespace { | |
| 339 | |
| 340 class NoHangServer : public Worker { | 327 class NoHangServer : public Worker { |
| 341 public: | 328 public: |
| 342 explicit NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) | 329 explicit NoHangServer(WaitableEvent* got_first_reply, bool pump_during_send) |
| 343 : Worker(Channel::MODE_SERVER, "no_hang_server"), | 330 : Worker(Channel::MODE_SERVER, "no_hang_server"), |
| 344 got_first_reply_(got_first_reply), | 331 got_first_reply_(got_first_reply), |
| 345 pump_during_send_(pump_during_send) { } | 332 pump_during_send_(pump_during_send) { } |
| 346 void Run() { | 333 void Run() { |
| 347 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); | 334 SendAnswerToLife(pump_during_send_, base::kNoTimeout, true); |
| 348 got_first_reply_->Signal(); | 335 got_first_reply_->Signal(); |
| 349 | 336 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 375 }; | 362 }; |
| 376 | 363 |
| 377 void NoHang(bool pump_during_send) { | 364 void NoHang(bool pump_during_send) { |
| 378 WaitableEvent got_first_reply(false, false); | 365 WaitableEvent got_first_reply(false, false); |
| 379 std::vector<Worker*> workers; | 366 std::vector<Worker*> workers; |
| 380 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); | 367 workers.push_back(new NoHangServer(&got_first_reply, pump_during_send)); |
| 381 workers.push_back(new NoHangClient(&got_first_reply)); | 368 workers.push_back(new NoHangClient(&got_first_reply)); |
| 382 RunTest(workers); | 369 RunTest(workers); |
| 383 } | 370 } |
| 384 | 371 |
| 385 } // namespace | |
| 386 | |
| 387 // Tests that caller doesn't hang if receiver dies | 372 // Tests that caller doesn't hang if receiver dies |
| 388 TEST_F(IPCSyncChannelTest, NoHang) { | 373 TEST_F(IPCSyncChannelTest, NoHang) { |
| 389 NoHang(false); | 374 NoHang(false); |
| 390 NoHang(true); | 375 NoHang(true); |
| 391 } | 376 } |
| 392 | 377 |
| 393 //----------------------------------------------------------------------------- | 378 //----------------------------------------------------------------------------- |
| 394 | 379 |
| 395 namespace { | |
| 396 | |
| 397 class UnblockServer : public Worker { | 380 class UnblockServer : public Worker { |
| 398 public: | 381 public: |
| 399 UnblockServer(bool pump_during_send, bool delete_during_send) | 382 UnblockServer(bool pump_during_send, bool delete_during_send) |
| 400 : Worker(Channel::MODE_SERVER, "unblock_server"), | 383 : Worker(Channel::MODE_SERVER, "unblock_server"), |
| 401 pump_during_send_(pump_during_send), | 384 pump_during_send_(pump_during_send), |
| 402 delete_during_send_(delete_during_send) { } | 385 delete_during_send_(delete_during_send) { } |
| 403 void Run() { | 386 void Run() { |
| 404 if (delete_during_send_) { | 387 if (delete_during_send_) { |
| 405 // Use custom code since race conditions mean the answer may or may not be | 388 // Use custom code since race conditions mean the answer may or may not be |
| 406 // available. | 389 // available. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 441 bool pump_during_send_; | 424 bool pump_during_send_; |
| 442 }; | 425 }; |
| 443 | 426 |
| 444 void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { | 427 void Unblock(bool server_pump, bool client_pump, bool delete_during_send) { |
| 445 std::vector<Worker*> workers; | 428 std::vector<Worker*> workers; |
| 446 workers.push_back(new UnblockServer(server_pump, delete_during_send)); | 429 workers.push_back(new UnblockServer(server_pump, delete_during_send)); |
| 447 workers.push_back(new UnblockClient(client_pump)); | 430 workers.push_back(new UnblockClient(client_pump)); |
| 448 RunTest(workers); | 431 RunTest(workers); |
| 449 } | 432 } |
| 450 | 433 |
| 451 } // namespace | |
| 452 | |
| 453 // Tests that the caller unblocks to answer a sync message from the receiver. | 434 // Tests that the caller unblocks to answer a sync message from the receiver. |
| 454 TEST_F(IPCSyncChannelTest, Unblock) { | 435 TEST_F(IPCSyncChannelTest, Unblock) { |
| 455 Unblock(false, false, false); | 436 Unblock(false, false, false); |
| 456 Unblock(false, true, false); | 437 Unblock(false, true, false); |
| 457 Unblock(true, false, false); | 438 Unblock(true, false, false); |
| 458 Unblock(true, true, false); | 439 Unblock(true, true, false); |
| 459 } | 440 } |
| 460 | 441 |
| 461 //----------------------------------------------------------------------------- | 442 //----------------------------------------------------------------------------- |
| 462 | 443 |
| 463 // Tests that the the IPC::SyncChannel object can be deleted during a Send. | 444 // Tests that the the SyncChannel object can be deleted during a Send. |
| 464 TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { | 445 TEST_F(IPCSyncChannelTest, ChannelDeleteDuringSend) { |
| 465 Unblock(false, false, true); | 446 Unblock(false, false, true); |
| 466 Unblock(false, true, true); | 447 Unblock(false, true, true); |
| 467 Unblock(true, false, true); | 448 Unblock(true, false, true); |
| 468 Unblock(true, true, true); | 449 Unblock(true, true, true); |
| 469 } | 450 } |
| 470 | 451 |
| 471 //----------------------------------------------------------------------------- | 452 //----------------------------------------------------------------------------- |
| 472 | 453 |
| 473 namespace { | |
| 474 | |
| 475 class RecursiveServer : public Worker { | 454 class RecursiveServer : public Worker { |
| 476 public: | 455 public: |
| 477 explicit RecursiveServer( | 456 explicit RecursiveServer( |
| 478 bool expected_send_result, bool pump_first, bool pump_second) | 457 bool expected_send_result, bool pump_first, bool pump_second) |
| 479 : Worker(Channel::MODE_SERVER, "recursive_server"), | 458 : Worker(Channel::MODE_SERVER, "recursive_server"), |
| 480 expected_send_result_(expected_send_result), | 459 expected_send_result_(expected_send_result), |
| 481 pump_first_(pump_first), pump_second_(pump_second) { } | 460 pump_first_(pump_first), pump_second_(pump_second) { } |
| 482 void Run() { | 461 void Run() { |
| 483 SendDouble(pump_first_, expected_send_result_); | 462 SendDouble(pump_first_, expected_send_result_); |
| 484 Done(); | 463 Done(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 | 503 |
| 525 void Recursive( | 504 void Recursive( |
| 526 bool server_pump_first, bool server_pump_second, bool client_pump) { | 505 bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 527 std::vector<Worker*> workers; | 506 std::vector<Worker*> workers; |
| 528 workers.push_back( | 507 workers.push_back( |
| 529 new RecursiveServer(true, server_pump_first, server_pump_second)); | 508 new RecursiveServer(true, server_pump_first, server_pump_second)); |
| 530 workers.push_back(new RecursiveClient(client_pump, false)); | 509 workers.push_back(new RecursiveClient(client_pump, false)); |
| 531 RunTest(workers); | 510 RunTest(workers); |
| 532 } | 511 } |
| 533 | 512 |
| 534 } // namespace | |
| 535 | |
| 536 // Tests a server calling Send while another Send is pending. | 513 // Tests a server calling Send while another Send is pending. |
| 537 TEST_F(IPCSyncChannelTest, Recursive) { | 514 TEST_F(IPCSyncChannelTest, Recursive) { |
| 538 Recursive(false, false, false); | 515 Recursive(false, false, false); |
| 539 Recursive(false, false, true); | 516 Recursive(false, false, true); |
| 540 Recursive(false, true, false); | 517 Recursive(false, true, false); |
| 541 Recursive(false, true, true); | 518 Recursive(false, true, true); |
| 542 Recursive(true, false, false); | 519 Recursive(true, false, false); |
| 543 Recursive(true, false, true); | 520 Recursive(true, false, true); |
| 544 Recursive(true, true, false); | 521 Recursive(true, true, false); |
| 545 Recursive(true, true, true); | 522 Recursive(true, true, true); |
| 546 } | 523 } |
| 547 | 524 |
| 548 //----------------------------------------------------------------------------- | 525 //----------------------------------------------------------------------------- |
| 549 | 526 |
| 550 namespace { | |
| 551 | |
| 552 void RecursiveNoHang( | 527 void RecursiveNoHang( |
| 553 bool server_pump_first, bool server_pump_second, bool client_pump) { | 528 bool server_pump_first, bool server_pump_second, bool client_pump) { |
| 554 std::vector<Worker*> workers; | 529 std::vector<Worker*> workers; |
| 555 workers.push_back( | 530 workers.push_back( |
| 556 new RecursiveServer(false, server_pump_first, server_pump_second)); | 531 new RecursiveServer(false, server_pump_first, server_pump_second)); |
| 557 workers.push_back(new RecursiveClient(client_pump, true)); | 532 workers.push_back(new RecursiveClient(client_pump, true)); |
| 558 RunTest(workers); | 533 RunTest(workers); |
| 559 } | 534 } |
| 560 | 535 |
| 561 } // namespace | |
| 562 | |
| 563 // Tests that if a caller makes a sync call during an existing sync call and | 536 // Tests that if a caller makes a sync call during an existing sync call and |
| 564 // the receiver dies, neither of the Send() calls hang. | 537 // the receiver dies, neither of the Send() calls hang. |
| 565 TEST_F(IPCSyncChannelTest, RecursiveNoHang) { | 538 TEST_F(IPCSyncChannelTest, RecursiveNoHang) { |
| 566 RecursiveNoHang(false, false, false); | 539 RecursiveNoHang(false, false, false); |
| 567 RecursiveNoHang(false, false, true); | 540 RecursiveNoHang(false, false, true); |
| 568 RecursiveNoHang(false, true, false); | 541 RecursiveNoHang(false, true, false); |
| 569 RecursiveNoHang(false, true, true); | 542 RecursiveNoHang(false, true, true); |
| 570 RecursiveNoHang(true, false, false); | 543 RecursiveNoHang(true, false, false); |
| 571 RecursiveNoHang(true, false, true); | 544 RecursiveNoHang(true, false, true); |
| 572 RecursiveNoHang(true, true, false); | 545 RecursiveNoHang(true, true, false); |
| 573 RecursiveNoHang(true, true, true); | 546 RecursiveNoHang(true, true, true); |
| 574 } | 547 } |
| 575 | 548 |
| 576 //----------------------------------------------------------------------------- | 549 //----------------------------------------------------------------------------- |
| 577 | 550 |
| 578 namespace { | |
| 579 | |
| 580 class MultipleServer1 : public Worker { | 551 class MultipleServer1 : public Worker { |
| 581 public: | 552 public: |
| 582 explicit MultipleServer1(bool pump_during_send) | 553 explicit MultipleServer1(bool pump_during_send) |
| 583 : Worker("test_channel1", Channel::MODE_SERVER), | 554 : Worker("test_channel1", Channel::MODE_SERVER), |
| 584 pump_during_send_(pump_during_send) { } | 555 pump_during_send_(pump_during_send) { } |
| 585 | 556 |
| 586 void Run() { | 557 void Run() { |
| 587 SendDouble(pump_during_send_, true); | 558 SendDouble(pump_during_send_, true); |
| 588 Done(); | 559 Done(); |
| 589 } | 560 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 669 worker->OverrideThread(&worker_thread); | 640 worker->OverrideThread(&worker_thread); |
| 670 workers.push_back(worker); | 641 workers.push_back(worker); |
| 671 | 642 |
| 672 worker = new MultipleClient1( | 643 worker = new MultipleClient1( |
| 673 &client1_msg_received, &client1_can_reply); | 644 &client1_msg_received, &client1_can_reply); |
| 674 workers.push_back(worker); | 645 workers.push_back(worker); |
| 675 | 646 |
| 676 RunTest(workers); | 647 RunTest(workers); |
| 677 } | 648 } |
| 678 | 649 |
| 679 } // namespace | |
| 680 | |
| 681 // Tests that multiple SyncObjects on the same listener thread can unblock each | 650 // Tests that multiple SyncObjects on the same listener thread can unblock each |
| 682 // other. | 651 // other. |
| 683 TEST_F(IPCSyncChannelTest, Multiple) { | 652 TEST_F(IPCSyncChannelTest, Multiple) { |
| 684 Multiple(false, false); | 653 Multiple(false, false); |
| 685 Multiple(false, true); | 654 Multiple(false, true); |
| 686 Multiple(true, false); | 655 Multiple(true, false); |
| 687 Multiple(true, true); | 656 Multiple(true, true); |
| 688 } | 657 } |
| 689 | 658 |
| 690 //----------------------------------------------------------------------------- | 659 //----------------------------------------------------------------------------- |
| 691 | 660 |
| 692 namespace { | |
| 693 | |
| 694 // This class provides server side functionality to test the case where | 661 // This class provides server side functionality to test the case where |
| 695 // multiple sync channels are in use on the same thread on the client and | 662 // multiple sync channels are in use on the same thread on the client and |
| 696 // nested calls are issued. | 663 // nested calls are issued. |
| 697 class QueuedReplyServer : public Worker { | 664 class QueuedReplyServer : public Worker { |
| 698 public: | 665 public: |
| 699 QueuedReplyServer(base::Thread* listener_thread, | 666 QueuedReplyServer(base::Thread* listener_thread, |
| 700 const std::string& channel_name, | 667 const std::string& channel_name, |
| 701 const std::string& reply_text) | 668 const std::string& reply_text) |
| 702 : Worker(channel_name, Channel::MODE_SERVER), | 669 : Worker(channel_name, Channel::MODE_SERVER), |
| 703 reply_text_(reply_text) { | 670 reply_text_(reply_text) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 | 748 |
| 782 worker = new QueuedReplyClient(&client_worker_thread, | 749 worker = new QueuedReplyClient(&client_worker_thread, |
| 783 "QueuedReply_Server2", | 750 "QueuedReply_Server2", |
| 784 "Got second message", | 751 "Got second message", |
| 785 client_pump); | 752 client_pump); |
| 786 workers.push_back(worker); | 753 workers.push_back(worker); |
| 787 | 754 |
| 788 RunTest(workers); | 755 RunTest(workers); |
| 789 } | 756 } |
| 790 | 757 |
| 791 } // namespace | |
| 792 | |
| 793 // While a blocking send is in progress, the listener thread might answer other | 758 // While a blocking send is in progress, the listener thread might answer other |
| 794 // synchronous messages. This tests that if during the response to another | 759 // synchronous messages. This tests that if during the response to another |
| 795 // message the reply to the original messages comes, it is queued up correctly | 760 // message the reply to the original messages comes, it is queued up correctly |
| 796 // and the original Send is unblocked later. | 761 // and the original Send is unblocked later. |
| 797 // We also test that the send call stacks unwind correctly when the channel | 762 // We also test that the send call stacks unwind correctly when the channel |
| 798 // pumps messages while waiting for a response. | 763 // pumps messages while waiting for a response. |
| 799 TEST_F(IPCSyncChannelTest, QueuedReply) { | 764 TEST_F(IPCSyncChannelTest, QueuedReply) { |
| 800 QueuedReply(false); | 765 QueuedReply(false); |
| 801 QueuedReply(true); | 766 QueuedReply(true); |
| 802 } | 767 } |
| 803 | 768 |
| 804 //----------------------------------------------------------------------------- | 769 //----------------------------------------------------------------------------- |
| 805 | 770 |
| 806 namespace { | |
| 807 | |
| 808 void DropAssert(const std::string&) {} | 771 void DropAssert(const std::string&) {} |
| 809 | 772 |
| 810 class BadServer : public Worker { | 773 class BadServer : public Worker { |
| 811 public: | 774 public: |
| 812 explicit BadServer(bool pump_during_send) | 775 explicit BadServer(bool pump_during_send) |
| 813 : Worker(Channel::MODE_SERVER, "simpler_server"), | 776 : Worker(Channel::MODE_SERVER, "simpler_server"), |
| 814 pump_during_send_(pump_during_send) { } | 777 pump_during_send_(pump_during_send) { } |
| 815 void Run() { | 778 void Run() { |
| 816 int answer = 0; | 779 int answer = 0; |
| 817 | 780 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 839 bool pump_during_send_; | 802 bool pump_during_send_; |
| 840 }; | 803 }; |
| 841 | 804 |
| 842 void BadMessage(bool pump_during_send) { | 805 void BadMessage(bool pump_during_send) { |
| 843 std::vector<Worker*> workers; | 806 std::vector<Worker*> workers; |
| 844 workers.push_back(new BadServer(pump_during_send)); | 807 workers.push_back(new BadServer(pump_during_send)); |
| 845 workers.push_back(new SimpleClient()); | 808 workers.push_back(new SimpleClient()); |
| 846 RunTest(workers); | 809 RunTest(workers); |
| 847 } | 810 } |
| 848 | 811 |
| 849 } // namespace | |
| 850 | |
| 851 // Tests that if a message is not serialized correctly, the Send() will fail. | 812 // Tests that if a message is not serialized correctly, the Send() will fail. |
| 852 TEST_F(IPCSyncChannelTest, BadMessage) { | 813 TEST_F(IPCSyncChannelTest, BadMessage) { |
| 853 BadMessage(false); | 814 BadMessage(false); |
| 854 BadMessage(true); | 815 BadMessage(true); |
| 855 } | 816 } |
| 856 | 817 |
| 857 //----------------------------------------------------------------------------- | 818 //----------------------------------------------------------------------------- |
| 858 | 819 |
| 859 namespace { | |
| 860 | |
| 861 class ChattyClient : public Worker { | 820 class ChattyClient : public Worker { |
| 862 public: | 821 public: |
| 863 ChattyClient() : | 822 ChattyClient() : |
| 864 Worker(Channel::MODE_CLIENT, "chatty_client") { } | 823 Worker(Channel::MODE_CLIENT, "chatty_client") { } |
| 865 | 824 |
| 866 void OnAnswer(int* answer) { | 825 void OnAnswer(int* answer) { |
| 867 // The PostMessage limit is 10k. Send 20% more than that. | 826 // The PostMessage limit is 10k. Send 20% more than that. |
| 868 const int kMessageLimit = 10000; | 827 const int kMessageLimit = 10000; |
| 869 const int kMessagesToSend = kMessageLimit * 120 / 100; | 828 const int kMessagesToSend = kMessageLimit * 120 / 100; |
| 870 for (int i = 0; i < kMessagesToSend; ++i) { | 829 for (int i = 0; i < kMessagesToSend; ++i) { |
| 871 if (!SendDouble(false, true)) | 830 if (!SendDouble(false, true)) |
| 872 break; | 831 break; |
| 873 } | 832 } |
| 874 *answer = 42; | 833 *answer = 42; |
| 875 Done(); | 834 Done(); |
| 876 } | 835 } |
| 877 }; | 836 }; |
| 878 | 837 |
| 879 void ChattyServer(bool pump_during_send) { | 838 void ChattyServer(bool pump_during_send) { |
| 880 std::vector<Worker*> workers; | 839 std::vector<Worker*> workers; |
| 881 workers.push_back(new UnblockServer(pump_during_send, false)); | 840 workers.push_back(new UnblockServer(pump_during_send, false)); |
| 882 workers.push_back(new ChattyClient()); | 841 workers.push_back(new ChattyClient()); |
| 883 RunTest(workers); | 842 RunTest(workers); |
| 884 } | 843 } |
| 885 | 844 |
| 886 } // namespace | |
| 887 | |
| 888 // Tests http://b/1093251 - that sending lots of sync messages while | 845 // Tests http://b/1093251 - that sending lots of sync messages while |
| 889 // the receiver is waiting for a sync reply does not overflow the PostMessage | 846 // the receiver is waiting for a sync reply does not overflow the PostMessage |
| 890 // queue. | 847 // queue. |
| 891 TEST_F(IPCSyncChannelTest, ChattyServer) { | 848 TEST_F(IPCSyncChannelTest, ChattyServer) { |
| 892 ChattyServer(false); | 849 ChattyServer(false); |
| 893 ChattyServer(true); | 850 ChattyServer(true); |
| 894 } | 851 } |
| 895 | 852 |
| 896 //------------------------------------------------------------------------------ | 853 //------------------------------------------------------------------------------ |
| 897 | 854 |
| 898 namespace { | |
| 899 | |
| 900 class TimeoutServer : public Worker { | 855 class TimeoutServer : public Worker { |
| 901 public: | 856 public: |
| 902 TimeoutServer(int timeout_ms, | 857 TimeoutServer(int timeout_ms, |
| 903 std::vector<bool> timeout_seq, | 858 std::vector<bool> timeout_seq, |
| 904 bool pump_during_send) | 859 bool pump_during_send) |
| 905 : Worker(Channel::MODE_SERVER, "timeout_server"), | 860 : Worker(Channel::MODE_SERVER, "timeout_server"), |
| 906 timeout_ms_(timeout_ms), | 861 timeout_ms_(timeout_ms), |
| 907 timeout_seq_(timeout_seq), | 862 timeout_seq_(timeout_seq), |
| 908 pump_during_send_(pump_during_send) { | 863 pump_during_send_(pump_during_send) { |
| 909 } | 864 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 976 timeout_seq.push_back(true); | 931 timeout_seq.push_back(true); |
| 977 timeout_seq.push_back(false); | 932 timeout_seq.push_back(false); |
| 978 timeout_seq.push_back(false); | 933 timeout_seq.push_back(false); |
| 979 timeout_seq.push_back(true); | 934 timeout_seq.push_back(true); |
| 980 timeout_seq.push_back(false); | 935 timeout_seq.push_back(false); |
| 981 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); | 936 workers.push_back(new TimeoutServer(100, timeout_seq, pump_during_send)); |
| 982 workers.push_back(new UnresponsiveClient(timeout_seq)); | 937 workers.push_back(new UnresponsiveClient(timeout_seq)); |
| 983 RunTest(workers); | 938 RunTest(workers); |
| 984 } | 939 } |
| 985 | 940 |
| 986 } // namespace | |
| 987 | |
| 988 // Tests that SendWithTimeout does not time-out if the response comes back fast | 941 // Tests that SendWithTimeout does not time-out if the response comes back fast |
| 989 // enough. | 942 // enough. |
| 990 TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { | 943 TEST_F(IPCSyncChannelTest, SendWithTimeoutOK) { |
| 991 SendWithTimeoutOK(false); | 944 SendWithTimeoutOK(false); |
| 992 SendWithTimeoutOK(true); | 945 SendWithTimeoutOK(true); |
| 993 } | 946 } |
| 994 | 947 |
| 995 // Tests that SendWithTimeout does time-out. | 948 // Tests that SendWithTimeout does time-out. |
| 996 TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { | 949 TEST_F(IPCSyncChannelTest, SendWithTimeoutTimeout) { |
| 997 SendWithTimeoutTimeout(false); | 950 SendWithTimeoutTimeout(false); |
| 998 SendWithTimeoutTimeout(true); | 951 SendWithTimeoutTimeout(true); |
| 999 } | 952 } |
| 1000 | 953 |
| 1001 // Sends some message that time-out and some that succeed. | 954 // Sends some message that time-out and some that succeed. |
| 1002 // Crashes flakily, http://crbug.com/70075. | 955 // Crashes flakily, http://crbug.com/70075. |
| 1003 TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) { | 956 TEST_F(IPCSyncChannelTest, DISABLED_SendWithTimeoutMixedOKAndTimeout) { |
| 1004 SendWithTimeoutMixedOKAndTimeout(false); | 957 SendWithTimeoutMixedOKAndTimeout(false); |
| 1005 SendWithTimeoutMixedOKAndTimeout(true); | 958 SendWithTimeoutMixedOKAndTimeout(true); |
| 1006 } | 959 } |
| 1007 | 960 |
| 1008 //------------------------------------------------------------------------------ | 961 //------------------------------------------------------------------------------ |
| 1009 | 962 |
| 1010 namespace { | |
| 1011 | |
| 1012 class NestedTask : public Task { | 963 class NestedTask : public Task { |
| 1013 public: | 964 public: |
| 1014 explicit NestedTask(Worker* server) : server_(server) { } | 965 explicit NestedTask(Worker* server) : server_(server) { } |
| 1015 void Run() { | 966 void Run() { |
| 1016 // Sleep a bit so that we wake up after the reply has been received. | 967 // Sleep a bit so that we wake up after the reply has been received. |
| 1017 base::PlatformThread::Sleep(250); | 968 base::PlatformThread::Sleep(250); |
| 1018 server_->SendAnswerToLife(true, base::kNoTimeout, true); | 969 server_->SendAnswerToLife(true, base::kNoTimeout, true); |
| 1019 } | 970 } |
| 1020 | 971 |
| 1021 Worker* server_; | 972 Worker* server_; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 1041 // Even though we have a timeout on the Send, it will succeed since for this | 992 // Even though we have a timeout on the Send, it will succeed since for this |
| 1042 // bug, the reply message comes back and is deserialized, however the done | 993 // bug, the reply message comes back and is deserialized, however the done |
| 1043 // event wasn't set. So we indirectly use the timeout task to notice if a | 994 // event wasn't set. So we indirectly use the timeout task to notice if a |
| 1044 // timeout occurred. | 995 // timeout occurred. |
| 1045 SendAnswerToLife(true, 10000, true); | 996 SendAnswerToLife(true, 10000, true); |
| 1046 DCHECK(!timeout_occured); | 997 DCHECK(!timeout_occured); |
| 1047 Done(); | 998 Done(); |
| 1048 } | 999 } |
| 1049 }; | 1000 }; |
| 1050 | 1001 |
| 1051 } // namespace | |
| 1052 | |
| 1053 // Tests http://b/1474092 - that if after the done_event is set but before | 1002 // Tests http://b/1474092 - that if after the done_event is set but before |
| 1054 // OnObjectSignaled is called another message is sent out, then after its | 1003 // OnObjectSignaled is called another message is sent out, then after its |
| 1055 // reply comes back OnObjectSignaled will be called for the first message. | 1004 // reply comes back OnObjectSignaled will be called for the first message. |
| 1056 TEST_F(IPCSyncChannelTest, DoneEventRace) { | 1005 TEST_F(IPCSyncChannelTest, DoneEventRace) { |
| 1057 std::vector<Worker*> workers; | 1006 std::vector<Worker*> workers; |
| 1058 workers.push_back(new DoneEventRaceServer()); | 1007 workers.push_back(new DoneEventRaceServer()); |
| 1059 workers.push_back(new SimpleClient()); | 1008 workers.push_back(new SimpleClient()); |
| 1060 RunTest(workers); | 1009 RunTest(workers); |
| 1061 } | 1010 } |
| 1062 | 1011 |
| 1063 //----------------------------------------------------------------------------- | 1012 //----------------------------------------------------------------------------- |
| 1064 | 1013 |
| 1065 namespace { | 1014 class TestSyncMessageFilter : public SyncMessageFilter { |
| 1066 | |
| 1067 class TestSyncMessageFilter : public IPC::SyncMessageFilter { | |
| 1068 public: | 1015 public: |
| 1069 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) | 1016 TestSyncMessageFilter(base::WaitableEvent* shutdown_event, Worker* worker) |
| 1070 : SyncMessageFilter(shutdown_event), | 1017 : SyncMessageFilter(shutdown_event), |
| 1071 worker_(worker), | 1018 worker_(worker), |
| 1072 thread_("helper_thread") { | 1019 thread_("helper_thread") { |
| 1073 base::Thread::Options options; | 1020 base::Thread::Options options; |
| 1074 options.message_loop_type = MessageLoop::TYPE_DEFAULT; | 1021 options.message_loop_type = MessageLoop::TYPE_DEFAULT; |
| 1075 thread_.StartWithOptions(options); | 1022 thread_.StartWithOptions(options); |
| 1076 } | 1023 } |
| 1077 | 1024 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1135 | 1082 |
| 1136 bool Send(Message* msg) { | 1083 bool Send(Message* msg) { |
| 1137 send_result_ = Worker::Send(msg); | 1084 send_result_ = Worker::Send(msg); |
| 1138 Done(); | 1085 Done(); |
| 1139 return send_result_; | 1086 return send_result_; |
| 1140 } | 1087 } |
| 1141 | 1088 |
| 1142 bool send_result_; | 1089 bool send_result_; |
| 1143 }; | 1090 }; |
| 1144 | 1091 |
| 1145 } // namespace | |
| 1146 | |
| 1147 // Tests basic synchronous call | 1092 // Tests basic synchronous call |
| 1148 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { | 1093 TEST_F(IPCSyncChannelTest, SyncMessageFilter) { |
| 1149 std::vector<Worker*> workers; | 1094 std::vector<Worker*> workers; |
| 1150 workers.push_back(new SyncMessageFilterServer()); | 1095 workers.push_back(new SyncMessageFilterServer()); |
| 1151 workers.push_back(new SimpleClient()); | 1096 workers.push_back(new SimpleClient()); |
| 1152 RunTest(workers); | 1097 RunTest(workers); |
| 1153 } | 1098 } |
| 1154 | 1099 |
| 1155 // Test the case when the channel is closed and a Send is attempted after that. | 1100 // Test the case when the channel is closed and a Send is attempted after that. |
| 1156 TEST_F(IPCSyncChannelTest, SendAfterClose) { | 1101 TEST_F(IPCSyncChannelTest, SendAfterClose) { |
| 1157 ServerSendAfterClose server; | 1102 ServerSendAfterClose server; |
| 1158 server.Start(); | 1103 server.Start(); |
| 1159 | 1104 |
| 1160 server.done_event()->Wait(); | 1105 server.done_event()->Wait(); |
| 1161 server.done_event()->Reset(); | 1106 server.done_event()->Reset(); |
| 1162 | 1107 |
| 1163 server.SendDummy(); | 1108 server.SendDummy(); |
| 1164 server.done_event()->Wait(); | 1109 server.done_event()->Wait(); |
| 1165 | 1110 |
| 1166 EXPECT_FALSE(server.send_result()); | 1111 EXPECT_FALSE(server.send_result()); |
| 1167 } | 1112 } |
| 1168 | 1113 |
| 1169 //----------------------------------------------------------------------------- | 1114 //----------------------------------------------------------------------------- |
| 1170 | 1115 |
| 1171 namespace { | |
| 1172 | |
| 1173 class RestrictedDispatchServer : public Worker { | 1116 class RestrictedDispatchServer : public Worker { |
| 1174 public: | 1117 public: |
| 1175 RestrictedDispatchServer(WaitableEvent* sent_ping_event) | 1118 RestrictedDispatchServer(WaitableEvent* sent_ping_event) |
| 1176 : Worker("restricted_channel", Channel::MODE_SERVER), | 1119 : Worker("restricted_channel", Channel::MODE_SERVER), |
| 1177 sent_ping_event_(sent_ping_event) { } | 1120 sent_ping_event_(sent_ping_event) { } |
| 1178 | 1121 |
| 1179 void OnDoPing(int ping) { | 1122 void OnDoPing(int ping) { |
| 1180 // Send an asynchronous message that unblocks the caller. | 1123 // Send an asynchronous message that unblocks the caller. |
| 1181 IPC::Message* msg = new SyncChannelTestMsg_Ping(ping); | 1124 Message* msg = new SyncChannelTestMsg_Ping(ping); |
| 1182 msg->set_unblock(true); | 1125 msg->set_unblock(true); |
| 1183 Send(msg); | 1126 Send(msg); |
| 1184 // Signal the event after the message has been sent on the channel, on the | 1127 // Signal the event after the message has been sent on the channel, on the |
| 1185 // IPC thread. | 1128 // IPC thread. |
| 1186 ipc_thread().message_loop()->PostTask(FROM_HERE, | 1129 ipc_thread().message_loop()->PostTask(FROM_HERE, |
| 1187 NewRunnableMethod(this, &RestrictedDispatchServer::OnPingSent)); | 1130 NewRunnableMethod(this, &RestrictedDispatchServer::OnPingSent)); |
| 1188 } | 1131 } |
| 1189 | 1132 |
| 1190 base::Thread* ListenerThread() { return Worker::ListenerThread(); } | 1133 base::Thread* ListenerThread() { return Worker::ListenerThread(); } |
| 1191 | 1134 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1292 void OnPing(int ping) { | 1235 void OnPing(int ping) { |
| 1293 ping_ = ping; | 1236 ping_ = ping; |
| 1294 } | 1237 } |
| 1295 | 1238 |
| 1296 int ping_; | 1239 int ping_; |
| 1297 RestrictedDispatchServer* server_; | 1240 RestrictedDispatchServer* server_; |
| 1298 int* success_; | 1241 int* success_; |
| 1299 WaitableEvent* sent_ping_event_; | 1242 WaitableEvent* sent_ping_event_; |
| 1300 }; | 1243 }; |
| 1301 | 1244 |
| 1302 } // namespace | |
| 1303 | |
| 1304 TEST_F(IPCSyncChannelTest, RestrictedDispatch) { | 1245 TEST_F(IPCSyncChannelTest, RestrictedDispatch) { |
| 1305 WaitableEvent sent_ping_event(false, false); | 1246 WaitableEvent sent_ping_event(false, false); |
| 1306 | 1247 |
| 1307 RestrictedDispatchServer* server = | 1248 RestrictedDispatchServer* server = |
| 1308 new RestrictedDispatchServer(&sent_ping_event); | 1249 new RestrictedDispatchServer(&sent_ping_event); |
| 1309 int success = 0; | 1250 int success = 0; |
| 1310 std::vector<Worker*> workers; | 1251 std::vector<Worker*> workers; |
| 1311 workers.push_back(new NonRestrictedDispatchServer); | 1252 workers.push_back(new NonRestrictedDispatchServer); |
| 1312 workers.push_back(server); | 1253 workers.push_back(server); |
| 1313 workers.push_back( | 1254 workers.push_back( |
| 1314 new RestrictedDispatchClient(&sent_ping_event, server, &success)); | 1255 new RestrictedDispatchClient(&sent_ping_event, server, &success)); |
| 1315 RunTest(workers); | 1256 RunTest(workers); |
| 1316 EXPECT_EQ(3, success); | 1257 EXPECT_EQ(3, success); |
| 1317 } | 1258 } |
| 1259 | |
| 1260 } // namespace IPC | |
| OLD | NEW |