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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 bool done_; | 456 bool done_; |
457 }; | 457 }; |
458 | 458 |
459 class TCPChannelTester : public ChannelTesterBase { | 459 class TCPChannelTester : public ChannelTesterBase { |
460 public: | 460 public: |
461 TCPChannelTester(Session* host_session, | 461 TCPChannelTester(Session* host_session, |
462 Session* client_session, | 462 Session* client_session, |
463 int message_size, | 463 int message_size, |
464 int message_count) | 464 int message_count) |
465 : ChannelTesterBase(host_session, client_session), | 465 : ChannelTesterBase(host_session, client_session), |
466 ALLOW_THIS_IN_INITIALIZER_LIST( | |
467 write_cb_(this, &TCPChannelTester::OnWritten)), | |
468 ALLOW_THIS_IN_INITIALIZER_LIST( | |
469 read_cb_(this, &TCPChannelTester::OnRead)), | |
470 write_errors_(0), | 466 write_errors_(0), |
471 read_errors_(0), | 467 read_errors_(0), |
472 message_size_(message_size), | 468 message_size_(message_size), |
473 message_count_(message_count), | 469 message_count_(message_count), |
474 test_data_size_(message_size * message_count) { | 470 test_data_size_(message_size * message_count) { |
475 } | 471 } |
476 | 472 |
477 virtual ~TCPChannelTester() { } | 473 virtual ~TCPChannelTester() { } |
478 | 474 |
479 virtual bool did_initialization_fail() { | 475 virtual bool did_initialization_fail() { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 input_buffer_ = new net::GrowableIOBuffer(); | 529 input_buffer_ = new net::GrowableIOBuffer(); |
534 } | 530 } |
535 | 531 |
536 virtual void DoWrite() { | 532 virtual void DoWrite() { |
537 int result = 1; | 533 int result = 1; |
538 while (result > 0) { | 534 while (result > 0) { |
539 if (output_buffer_->BytesRemaining() == 0) | 535 if (output_buffer_->BytesRemaining() == 0) |
540 break; | 536 break; |
541 int bytes_to_write = std::min(output_buffer_->BytesRemaining(), | 537 int bytes_to_write = std::min(output_buffer_->BytesRemaining(), |
542 message_size_); | 538 message_size_); |
543 result = sockets_[0]->Write(output_buffer_, bytes_to_write, &write_cb_); | 539 result = sockets_[0]->Write(output_buffer_, bytes_to_write, |
| 540 base::Bind(&TCPChannelTester::OnWritten, |
| 541 base::Unretained(this))); |
544 HandleWriteResult(result); | 542 HandleWriteResult(result); |
545 }; | 543 }; |
546 } | 544 } |
547 | 545 |
548 void OnWritten(int result) { | 546 void OnWritten(int result) { |
549 HandleWriteResult(result); | 547 HandleWriteResult(result); |
550 DoWrite(); | 548 DoWrite(); |
551 } | 549 } |
552 | 550 |
553 void HandleWriteResult(int result) { | 551 void HandleWriteResult(int result) { |
554 if (result <= 0 && result != net::ERR_IO_PENDING) { | 552 if (result <= 0 && result != net::ERR_IO_PENDING) { |
555 LOG(ERROR) << "Received error " << result << " when trying to write"; | 553 LOG(ERROR) << "Received error " << result << " when trying to write"; |
556 write_errors_++; | 554 write_errors_++; |
557 Done(); | 555 Done(); |
558 } else if (result > 0) { | 556 } else if (result > 0) { |
559 output_buffer_->DidConsume(result); | 557 output_buffer_->DidConsume(result); |
560 } | 558 } |
561 } | 559 } |
562 | 560 |
563 virtual void DoRead() { | 561 virtual void DoRead() { |
564 int result = 1; | 562 int result = 1; |
565 while (result > 0) { | 563 while (result > 0) { |
566 input_buffer_->SetCapacity(input_buffer_->offset() + message_size_); | 564 input_buffer_->SetCapacity(input_buffer_->offset() + message_size_); |
567 result = sockets_[1]->Read(input_buffer_, message_size_, &read_cb_); | 565 result = sockets_[1]->Read(input_buffer_, message_size_, |
| 566 base::Bind(&TCPChannelTester::OnRead, |
| 567 base::Unretained(this))); |
568 HandleReadResult(result); | 568 HandleReadResult(result); |
569 }; | 569 }; |
570 } | 570 } |
571 | 571 |
572 void OnRead(int result) { | 572 void OnRead(int result) { |
573 HandleReadResult(result); | 573 HandleReadResult(result); |
574 if (!done_) | 574 if (!done_) |
575 DoRead(); // Don't try to read again when we are done reading. | 575 DoRead(); // Don't try to read again when we are done reading. |
576 } | 576 } |
577 | 577 |
578 void HandleReadResult(int result) { | 578 void HandleReadResult(int result) { |
579 if (result <= 0 && result != net::ERR_IO_PENDING) { | 579 if (result <= 0 && result != net::ERR_IO_PENDING) { |
580 if (!done_) { | 580 if (!done_) { |
581 LOG(ERROR) << "Received error " << result << " when trying to read"; | 581 LOG(ERROR) << "Received error " << result << " when trying to read"; |
582 read_errors_++; | 582 read_errors_++; |
583 Done(); | 583 Done(); |
584 } | 584 } |
585 } else if (result > 0) { | 585 } else if (result > 0) { |
586 // Allocate memory for the next read. | 586 // Allocate memory for the next read. |
587 input_buffer_->set_offset(input_buffer_->offset() + result); | 587 input_buffer_->set_offset(input_buffer_->offset() + result); |
588 if (input_buffer_->offset() == test_data_size_) | 588 if (input_buffer_->offset() == test_data_size_) |
589 Done(); | 589 Done(); |
590 } | 590 } |
591 } | 591 } |
592 | 592 |
593 scoped_refptr<net::DrainableIOBuffer> output_buffer_; | 593 scoped_refptr<net::DrainableIOBuffer> output_buffer_; |
594 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | 594 scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
595 | 595 |
596 net::OldCompletionCallbackImpl<TCPChannelTester> write_cb_; | |
597 net::OldCompletionCallbackImpl<TCPChannelTester> read_cb_; | |
598 int write_errors_; | 596 int write_errors_; |
599 int read_errors_; | 597 int read_errors_; |
600 int message_size_; | 598 int message_size_; |
601 int message_count_; | 599 int message_count_; |
602 int test_data_size_; | 600 int test_data_size_; |
603 }; | 601 }; |
604 | 602 |
605 class ChannelSpeedTester : public TCPChannelTester { | 603 class ChannelSpeedTester : public TCPChannelTester { |
606 public: | 604 public: |
607 ChannelSpeedTester(Session* host_session, | 605 ChannelSpeedTester(Session* host_session, |
(...skipping 20 matching lines...) Expand all Loading... |
628 } | 626 } |
629 | 627 |
630 base::Time start_time_; | 628 base::Time start_time_; |
631 }; | 629 }; |
632 | 630 |
633 class UDPChannelTester : public ChannelTesterBase { | 631 class UDPChannelTester : public ChannelTesterBase { |
634 public: | 632 public: |
635 UDPChannelTester(Session* host_session, | 633 UDPChannelTester(Session* host_session, |
636 Session* client_session) | 634 Session* client_session) |
637 : ChannelTesterBase(host_session, client_session), | 635 : ChannelTesterBase(host_session, client_session), |
638 ALLOW_THIS_IN_INITIALIZER_LIST( | |
639 write_cb_(this, &UDPChannelTester::OnWritten)), | |
640 ALLOW_THIS_IN_INITIALIZER_LIST( | |
641 read_cb_(this, &UDPChannelTester::OnRead)), | |
642 write_errors_(0), | 636 write_errors_(0), |
643 read_errors_(0), | 637 read_errors_(0), |
644 packets_sent_(0), | 638 packets_sent_(0), |
645 packets_received_(0), | 639 packets_received_(0), |
646 broken_packets_(0) { | 640 broken_packets_(0) { |
647 } | 641 } |
648 | 642 |
649 virtual ~UDPChannelTester() { } | 643 virtual ~UDPChannelTester() { } |
650 | 644 |
651 virtual void CheckResults() { | 645 virtual void CheckResults() { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 Done(); | 692 Done(); |
699 return; | 693 return; |
700 } | 694 } |
701 | 695 |
702 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(kMessageSize)); | 696 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(kMessageSize)); |
703 memset(packet->data(), 123, kMessageSize); | 697 memset(packet->data(), 123, kMessageSize); |
704 sent_packets_[packets_sent_] = packet; | 698 sent_packets_[packets_sent_] = packet; |
705 // Put index of this packet in the beginning of the packet body. | 699 // Put index of this packet in the beginning of the packet body. |
706 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); | 700 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); |
707 | 701 |
708 int result = sockets_[0]->Write(packet, kMessageSize, &write_cb_); | 702 int result = sockets_[0]->Write(packet, kMessageSize, |
| 703 base::Bind(&UDPChannelTester::OnWritten, |
| 704 base::Unretained(this))); |
709 HandleWriteResult(result); | 705 HandleWriteResult(result); |
710 } | 706 } |
711 | 707 |
712 void OnWritten(int result) { | 708 void OnWritten(int result) { |
713 HandleWriteResult(result); | 709 HandleWriteResult(result); |
714 } | 710 } |
715 | 711 |
716 void HandleWriteResult(int result) { | 712 void HandleWriteResult(int result) { |
717 if (result <= 0 && result != net::ERR_IO_PENDING) { | 713 if (result <= 0 && result != net::ERR_IO_PENDING) { |
718 LOG(ERROR) << "Received error " << result << " when trying to write"; | 714 LOG(ERROR) << "Received error " << result << " when trying to write"; |
719 write_errors_++; | 715 write_errors_++; |
720 Done(); | 716 Done(); |
721 } else if (result > 0) { | 717 } else if (result > 0) { |
722 EXPECT_EQ(kMessageSize, result); | 718 EXPECT_EQ(kMessageSize, result); |
723 packets_sent_++; | 719 packets_sent_++; |
724 MessageLoop::current()->PostDelayedTask( | 720 MessageLoop::current()->PostDelayedTask( |
725 FROM_HERE, base::Bind(&UDPChannelTester::DoWrite, this), | 721 FROM_HERE, base::Bind(&UDPChannelTester::DoWrite, this), |
726 kUdpWriteDelayMs); | 722 kUdpWriteDelayMs); |
727 } | 723 } |
728 } | 724 } |
729 | 725 |
730 virtual void DoRead() { | 726 virtual void DoRead() { |
731 int result = 1; | 727 int result = 1; |
732 while (result > 0) { | 728 while (result > 0) { |
733 int kReadSize = kMessageSize * 2; | 729 int kReadSize = kMessageSize * 2; |
734 read_buffer_ = new net::IOBuffer(kReadSize); | 730 read_buffer_ = new net::IOBuffer(kReadSize); |
735 | 731 |
736 result = sockets_[1]->Read(read_buffer_, kReadSize, &read_cb_); | 732 result = sockets_[1]->Read(read_buffer_, kReadSize, |
| 733 base::Bind(&UDPChannelTester::OnRead, |
| 734 base::Unretained(this))); |
737 HandleReadResult(result); | 735 HandleReadResult(result); |
738 }; | 736 }; |
739 } | 737 } |
740 | 738 |
741 void OnRead(int result) { | 739 void OnRead(int result) { |
742 HandleReadResult(result); | 740 HandleReadResult(result); |
743 DoRead(); | 741 DoRead(); |
744 } | 742 } |
745 | 743 |
746 void HandleReadResult(int result) { | 744 void HandleReadResult(int result) { |
(...skipping 21 matching lines...) Expand all Loading... |
768 broken_packets_++; | 766 broken_packets_++; |
769 } | 767 } |
770 } | 768 } |
771 } | 769 } |
772 } | 770 } |
773 | 771 |
774 private: | 772 private: |
775 scoped_refptr<net::IOBuffer> sent_packets_[kMessages]; | 773 scoped_refptr<net::IOBuffer> sent_packets_[kMessages]; |
776 scoped_refptr<net::IOBuffer> read_buffer_; | 774 scoped_refptr<net::IOBuffer> read_buffer_; |
777 | 775 |
778 net::OldCompletionCallbackImpl<UDPChannelTester> write_cb_; | |
779 net::OldCompletionCallbackImpl<UDPChannelTester> read_cb_; | |
780 int write_errors_; | 776 int write_errors_; |
781 int read_errors_; | 777 int read_errors_; |
782 int packets_sent_; | 778 int packets_sent_; |
783 int packets_received_; | 779 int packets_received_; |
784 int broken_packets_; | 780 int broken_packets_; |
785 }; | 781 }; |
786 | 782 |
787 // Verify that we can create and destory server objects without a connection. | 783 // Verify that we can create and destory server objects without a connection. |
788 TEST_F(JingleSessionTest, CreateAndDestoy) { | 784 TEST_F(JingleSessionTest, CreateAndDestoy) { |
789 CreateServerPair(false); | 785 CreateServerPair(false); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
913 ASSERT_TRUE(tester->WaitFinished()); | 909 ASSERT_TRUE(tester->WaitFinished()); |
914 LOG(INFO) << "Time for 500k bytes " | 910 LOG(INFO) << "Time for 500k bytes " |
915 << tester->GetElapsedTime().InMilliseconds() << " ms."; | 911 << tester->GetElapsedTime().InMilliseconds() << " ms."; |
916 | 912 |
917 // Connections must be closed while |tester| still exists. | 913 // Connections must be closed while |tester| still exists. |
918 CloseSessions(); | 914 CloseSessions(); |
919 } | 915 } |
920 | 916 |
921 } // namespace protocol | 917 } // namespace protocol |
922 } // namespace remoting | 918 } // namespace remoting |
OLD | NEW |