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 3 // found in the LICENSE file. 4 5 // Test for FixRate sender and receiver. 6 7 #include "base/logging.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "net/quic/congestion_control/fix_rate_receiver.h" 10 #include "net/quic/congestion_control/fix_rate_sender.h" 11 #include "net/quic/quic_protocol.h" 12 #include "net/quic/test_tools/mock_clock.h" 13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace net { 17 namespace test { 18 19 class FixRateReceiverPeer : public FixRateReceiver { 20 public: 21 FixRateReceiverPeer() 22 : FixRateReceiver() { 23 } 24 void SetBitrate(QuicBandwidth fix_rate) { 25 FixRateReceiver::configured_rate_ = fix_rate; 26 } 27 }; 28 29 class FixRateTest : public ::testing::Test { 30 protected: 31 FixRateTest() 32 : rtt_(QuicTime::Delta::FromMilliseconds(30)), 33 sender_(new FixRateSender(&clock_)), 34 receiver_(new FixRateReceiverPeer()), 35 start_(clock_.Now()) { 36 // Make sure clock does not start at 0. 37 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2)); 38 } 39 const QuicTime::Delta rtt_; 40 MockClock clock_; 41 SendAlgorithmInterface::SentPacketsMap unused_packet_map_; 42 scoped_ptr<FixRateSender> sender_; 43 scoped_ptr<FixRateReceiverPeer> receiver_; 44 const QuicTime start_; 45 }; 46 47 TEST_F(FixRateTest, ReceiverAPI) { 48 QuicCongestionFeedbackFrame feedback; 49 QuicTime timestamp(QuicTime::Zero()); 50 receiver_->SetBitrate(QuicBandwidth::FromKBytesPerSecond(300)); 51 receiver_->RecordIncomingPacket(1, 1, timestamp, false); 52 ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback)); 53 EXPECT_EQ(kFixRate, feedback.type); 54 EXPECT_EQ(300000u, feedback.fix_rate.bitrate.ToBytesPerSecond()); 55 } 56 57 TEST_F(FixRateTest, SenderAPI) { 58 QuicCongestionFeedbackFrame feedback; 59 feedback.type = kFixRate; 60 feedback.fix_rate.bitrate = QuicBandwidth::FromKBytesPerSecond(300); 61 sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now(), 62 unused_packet_map_); 63 EXPECT_EQ(300000, sender_->BandwidthEstimate().ToBytesPerSecond()); 64 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(), 65 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero()); 66 sender_->OnPacketSent(clock_.Now(), 1, kDefaultMaxPacketSize, 67 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA); 68 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(), 69 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero()); 70 sender_->OnPacketSent(clock_.Now(), 2, kDefaultMaxPacketSize, 71 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA); 72 sender_->OnPacketSent(clock_.Now(), 3, 600, NOT_RETRANSMISSION, 73 HAS_RETRANSMITTABLE_DATA); 74 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10), 75 sender_->TimeUntilSend(clock_.Now(), 76 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE)); 77 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2)); 78 EXPECT_EQ(QuicTime::Delta::Infinite(), sender_->TimeUntilSend(clock_.Now(), 79 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE)); 80 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8)); 81 sender_->OnPacketAcked(1, kDefaultMaxPacketSize, rtt_); 82 sender_->OnPacketAcked(2, kDefaultMaxPacketSize, rtt_); 83 sender_->OnPacketAcked(3, 600, rtt_); 84 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(), 85 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE).IsZero()); 86 } 87 88 TEST_F(FixRateTest, FixRatePacing) { 89 const QuicByteCount packet_size = 1200; 90 const QuicBandwidth bitrate = QuicBandwidth::FromKBytesPerSecond(240); 91 const int64 num_packets = 200; 92 QuicCongestionFeedbackFrame feedback; 93 receiver_->SetBitrate(QuicBandwidth::FromKBytesPerSecond(240)); 94 ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback)); 95 sender_->OnIncomingQuicCongestionFeedbackFrame(feedback, clock_.Now(), 96 unused_packet_map_); 97 QuicTime acc_advance_time(QuicTime::Zero()); 98 QuicPacketSequenceNumber sequence_number = 0; 99 for (int i = 0; i < num_packets; i += 2) { 100 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(), 101 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, 102 NOT_HANDSHAKE).IsZero()); 103 sender_->OnPacketSent(clock_.Now(), sequence_number++, packet_size, 104 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA); 105 EXPECT_TRUE(sender_->TimeUntilSend(clock_.Now(), 106 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, 107 NOT_HANDSHAKE).IsZero()); 108 sender_->OnPacketSent(clock_.Now(), sequence_number++, packet_size, 109 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA); 110 QuicTime::Delta advance_time = sender_->TimeUntilSend(clock_.Now(), 111 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE); 112 clock_.AdvanceTime(advance_time); 113 sender_->OnPacketAcked(sequence_number - 1, packet_size, rtt_); 114 sender_->OnPacketAcked(sequence_number - 2, packet_size, rtt_); 115 acc_advance_time = acc_advance_time.Add(advance_time); 116 } 117 EXPECT_EQ(num_packets * packet_size * 1000000 / bitrate.ToBytesPerSecond(), 118 static_cast<uint64>(acc_advance_time.Subtract(start_) 119 .ToMicroseconds())); 120 } 121 122 } // namespace test 123 } // namespace net 124