Home | History | Annotate | Download | only in quic
      1 // Copyright 2013 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 #include "net/quic/quic_sent_packet_manager.h"
      6 
      7 #include "base/stl_util.h"
      8 #include "net/quic/quic_flags.h"
      9 #include "net/quic/test_tools/quic_config_peer.h"
     10 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
     11 #include "net/quic/test_tools/quic_test_utils.h"
     12 #include "testing/gmock/include/gmock/gmock.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 using std::vector;
     16 using testing::AnyNumber;
     17 using testing::ElementsAre;
     18 using testing::Pair;
     19 using testing::Pointwise;
     20 using testing::Return;
     21 using testing::StrictMock;
     22 using testing::_;
     23 
     24 namespace net {
     25 namespace test {
     26 namespace {
     27 
     28 // Default packet length.
     29 const uint32 kDefaultLength = 1000;
     30 
     31 // Matcher to check the key of the key-value pair it receives as first argument
     32 // equals its second argument.
     33 MATCHER(KeyEq, "") {
     34   return std::tr1::get<0>(arg).first == std::tr1::get<1>(arg);
     35 }
     36 
     37 class MockDebugDelegate : public QuicSentPacketManager::DebugDelegate {
     38  public:
     39   MOCK_METHOD2(OnSpuriousPacketRetransmition,
     40                void(TransmissionType transmission_type,
     41                     QuicByteCount byte_size));
     42 };
     43 
     44 class QuicSentPacketManagerTest : public ::testing::TestWithParam<bool> {
     45  protected:
     46   QuicSentPacketManagerTest()
     47       : manager_(true, &clock_, &stats_, kCubic, kNack),
     48         send_algorithm_(new StrictMock<MockSendAlgorithm>),
     49         network_change_visitor_(new StrictMock<MockNetworkChangeVisitor>) {
     50     QuicSentPacketManagerPeer::SetSendAlgorithm(&manager_, send_algorithm_);
     51     // Disable tail loss probes for most tests.
     52     QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 0);
     53     // Advance the time 1s so the send times are never QuicTime::Zero.
     54     clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
     55     manager_.set_network_change_visitor(network_change_visitor_.get());
     56 
     57     EXPECT_CALL(*send_algorithm_, HasReliableBandwidthEstimate())
     58         .Times(AnyNumber());
     59     EXPECT_CALL(*send_algorithm_, BandwidthEstimate())
     60         .Times(AnyNumber())
     61         .WillRepeatedly(Return(QuicBandwidth::Zero()));
     62     EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber());
     63     EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber());
     64   }
     65 
     66   virtual ~QuicSentPacketManagerTest() OVERRIDE {
     67     STLDeleteElements(&packets_);
     68   }
     69 
     70   QuicByteCount BytesInFlight() {
     71     return QuicSentPacketManagerPeer::GetBytesInFlight(&manager_);
     72   }
     73   void VerifyUnackedPackets(QuicPacketSequenceNumber* packets,
     74                             size_t num_packets) {
     75     if (num_packets == 0) {
     76       EXPECT_FALSE(manager_.HasUnackedPackets());
     77       EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
     78           &manager_));
     79       return;
     80     }
     81 
     82     EXPECT_TRUE(manager_.HasUnackedPackets());
     83     EXPECT_EQ(packets[0], manager_.GetLeastUnacked());
     84     for (size_t i = 0; i < num_packets; ++i) {
     85       EXPECT_TRUE(manager_.IsUnacked(packets[i])) << packets[i];
     86     }
     87   }
     88 
     89   void VerifyRetransmittablePackets(QuicPacketSequenceNumber* packets,
     90                                     size_t num_packets) {
     91     EXPECT_EQ(num_packets,
     92               QuicSentPacketManagerPeer::GetNumRetransmittablePackets(
     93                   &manager_));
     94     for (size_t i = 0; i < num_packets; ++i) {
     95       EXPECT_TRUE(manager_.HasRetransmittableFrames(packets[i]))
     96           << " packets[" << i << "]:" << packets[i];
     97     }
     98   }
     99 
    100   void ExpectAck(QuicPacketSequenceNumber largest_observed) {
    101     EXPECT_CALL(*send_algorithm_, OnCongestionEvent(
    102         true, _, ElementsAre(Pair(largest_observed, _)), _));
    103     EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
    104         .WillOnce(Return(100 * kDefaultTCPMSS));
    105     EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
    106   }
    107 
    108   void ExpectUpdatedRtt(QuicPacketSequenceNumber largest_observed) {
    109     EXPECT_CALL(*send_algorithm_,
    110                 OnCongestionEvent(true, _, _, _));
    111     EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
    112         .WillOnce(Return(100 * kDefaultTCPMSS));
    113     EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
    114   }
    115 
    116   void ExpectAckAndLoss(bool rtt_updated,
    117                         QuicPacketSequenceNumber largest_observed,
    118                         QuicPacketSequenceNumber lost_packet) {
    119     EXPECT_CALL(*send_algorithm_, OnCongestionEvent(
    120         rtt_updated, _, ElementsAre(Pair(largest_observed, _)),
    121         ElementsAre(Pair(lost_packet, _))));
    122     EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
    123         .WillOnce(Return(100 * kDefaultTCPMSS));
    124     EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
    125   }
    126 
    127   // |packets_acked| and |packets_lost| should be in sequence number order.
    128   void ExpectAcksAndLosses(bool rtt_updated,
    129                            QuicPacketSequenceNumber* packets_acked,
    130                            size_t num_packets_acked,
    131                            QuicPacketSequenceNumber* packets_lost,
    132                            size_t num_packets_lost) {
    133     vector<QuicPacketSequenceNumber> ack_vector;
    134     for (size_t i = 0; i < num_packets_acked; ++i) {
    135       ack_vector.push_back(packets_acked[i]);
    136     }
    137     vector<QuicPacketSequenceNumber> lost_vector;
    138     for (size_t i = 0; i < num_packets_lost; ++i) {
    139       lost_vector.push_back(packets_lost[i]);
    140     }
    141     EXPECT_CALL(*send_algorithm_,
    142                 OnCongestionEvent(rtt_updated, _,
    143                                   Pointwise(KeyEq(), ack_vector),
    144                                   Pointwise(KeyEq(), lost_vector)));
    145     EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
    146         .WillRepeatedly(Return(100 * kDefaultTCPMSS));
    147     EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_)).
    148         Times(AnyNumber());
    149   }
    150 
    151   // Retransmits a packet as though it was a TLP retransmission, because TLP
    152   // leaves the |old_sequence_number| pending.
    153   // TODO(ianswett): Test with transmission types besides TLP.
    154   void RetransmitPacket(QuicPacketSequenceNumber old_sequence_number,
    155                         QuicPacketSequenceNumber new_sequence_number) {
    156     QuicSentPacketManagerPeer::MarkForRetransmission(
    157         &manager_, old_sequence_number, TLP_RETRANSMISSION);
    158     EXPECT_TRUE(manager_.HasPendingRetransmissions());
    159     QuicSentPacketManager::PendingRetransmission next_retransmission =
    160         manager_.NextPendingRetransmission();
    161     EXPECT_EQ(old_sequence_number, next_retransmission.sequence_number);
    162     EXPECT_EQ(TLP_RETRANSMISSION,
    163               next_retransmission.transmission_type);
    164     manager_.OnRetransmittedPacket(old_sequence_number,
    165                                    new_sequence_number);
    166     EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(
    167         &manager_, new_sequence_number));
    168   }
    169 
    170   void RetransmitAndSendPacket(QuicPacketSequenceNumber old_sequence_number,
    171                                QuicPacketSequenceNumber new_sequence_number) {
    172     RetransmitPacket(old_sequence_number, new_sequence_number);
    173 
    174     EXPECT_CALL(*send_algorithm_,
    175                 OnPacketSent(_, BytesInFlight(), new_sequence_number,
    176                              kDefaultLength, HAS_RETRANSMITTABLE_DATA))
    177         .WillOnce(Return(true));
    178     manager_.OnPacketSent(new_sequence_number,
    179                           clock_.Now(),
    180                           kDefaultLength,
    181                           LOSS_RETRANSMISSION,
    182                           HAS_RETRANSMITTABLE_DATA);
    183   }
    184 
    185   SerializedPacket CreateDataPacket(QuicPacketSequenceNumber sequence_number) {
    186     return CreatePacket(sequence_number, true);
    187   }
    188 
    189   SerializedPacket CreatePacket(QuicPacketSequenceNumber sequence_number,
    190                                 bool retransmittable) {
    191     packets_.push_back(QuicPacket::NewDataPacket(
    192         NULL, kDefaultLength, false, PACKET_8BYTE_CONNECTION_ID, false,
    193         PACKET_6BYTE_SEQUENCE_NUMBER));
    194     return SerializedPacket(
    195         sequence_number, PACKET_6BYTE_SEQUENCE_NUMBER,
    196         packets_.back(), 0u,
    197         retransmittable ? new RetransmittableFrames() : NULL);
    198   }
    199 
    200   SerializedPacket CreateFecPacket(QuicPacketSequenceNumber sequence_number) {
    201     packets_.push_back(QuicPacket::NewFecPacket(
    202         NULL, kDefaultLength, false, PACKET_8BYTE_CONNECTION_ID, false,
    203         PACKET_6BYTE_SEQUENCE_NUMBER));
    204     return SerializedPacket(sequence_number, PACKET_6BYTE_SEQUENCE_NUMBER,
    205                             packets_.back(), 0u, NULL);
    206   }
    207 
    208   void SendDataPacket(QuicPacketSequenceNumber sequence_number) {
    209     EXPECT_CALL(*send_algorithm_,
    210                 OnPacketSent(_, BytesInFlight(), sequence_number, _, _))
    211                     .Times(1).WillOnce(Return(true));
    212     SerializedPacket packet(CreateDataPacket(sequence_number));
    213     manager_.OnSerializedPacket(packet);
    214     manager_.OnPacketSent(sequence_number, clock_.Now(),
    215                           packet.packet->length(), NOT_RETRANSMISSION,
    216                           HAS_RETRANSMITTABLE_DATA);
    217   }
    218 
    219   void SendCryptoPacket(QuicPacketSequenceNumber sequence_number) {
    220     EXPECT_CALL(*send_algorithm_,
    221                 OnPacketSent(_, BytesInFlight(), sequence_number,
    222                              kDefaultLength, HAS_RETRANSMITTABLE_DATA))
    223                     .Times(1).WillOnce(Return(true));
    224     SerializedPacket packet(CreateDataPacket(sequence_number));
    225     packet.retransmittable_frames->AddStreamFrame(
    226         new QuicStreamFrame(1, false, 0, IOVector()));
    227     packet.retransmittable_frames->set_encryption_level(ENCRYPTION_NONE);
    228     manager_.OnSerializedPacket(packet);
    229     manager_.OnPacketSent(sequence_number, clock_.ApproximateNow(),
    230                           packet.packet->length(), NOT_RETRANSMISSION,
    231                           HAS_RETRANSMITTABLE_DATA);
    232   }
    233 
    234   void SendFecPacket(QuicPacketSequenceNumber sequence_number) {
    235     EXPECT_CALL(*send_algorithm_,
    236                 OnPacketSent(_, BytesInFlight(), sequence_number,
    237                              kDefaultLength, NO_RETRANSMITTABLE_DATA))
    238                     .Times(1).WillOnce(Return(true));
    239     SerializedPacket packet(CreateFecPacket(sequence_number));
    240     manager_.OnSerializedPacket(packet);
    241     manager_.OnPacketSent(sequence_number, clock_.ApproximateNow(),
    242                           packet.packet->length(), NOT_RETRANSMISSION,
    243                           NO_RETRANSMITTABLE_DATA);
    244   }
    245 
    246   void SendAckPacket(QuicPacketSequenceNumber sequence_number) {
    247     EXPECT_CALL(*send_algorithm_,
    248                 OnPacketSent(_, BytesInFlight(), sequence_number,
    249                              kDefaultLength, NO_RETRANSMITTABLE_DATA))
    250                     .Times(1).WillOnce(Return(false));
    251     SerializedPacket packet(CreatePacket(sequence_number, false));
    252     manager_.OnSerializedPacket(packet);
    253     manager_.OnPacketSent(sequence_number, clock_.Now(),
    254                           packet.packet->length(), NOT_RETRANSMISSION,
    255                           NO_RETRANSMITTABLE_DATA);
    256   }
    257 
    258   // Based on QuicConnection's WritePendingRetransmissions.
    259   void RetransmitNextPacket(
    260       QuicPacketSequenceNumber retransmission_sequence_number) {
    261     EXPECT_TRUE(manager_.HasPendingRetransmissions());
    262     EXPECT_CALL(*send_algorithm_,
    263                 OnPacketSent(_, _, retransmission_sequence_number,
    264                              kDefaultLength, HAS_RETRANSMITTABLE_DATA))
    265                     .Times(1).WillOnce(Return(true));
    266     const QuicSentPacketManager::PendingRetransmission pending =
    267         manager_.NextPendingRetransmission();
    268     manager_.OnRetransmittedPacket(pending.sequence_number,
    269                                    retransmission_sequence_number);
    270     manager_.OnPacketSent(retransmission_sequence_number, clock_.Now(),
    271                           kDefaultLength, pending.transmission_type,
    272                           HAS_RETRANSMITTABLE_DATA);
    273   }
    274 
    275   QuicSentPacketManager manager_;
    276   vector<QuicPacket*> packets_;
    277   MockClock clock_;
    278   QuicConnectionStats stats_;
    279   MockSendAlgorithm* send_algorithm_;
    280   scoped_ptr<MockNetworkChangeVisitor> network_change_visitor_;
    281 };
    282 
    283 TEST_F(QuicSentPacketManagerTest, IsUnacked) {
    284   VerifyUnackedPackets(NULL, 0);
    285 
    286   SerializedPacket serialized_packet(CreateDataPacket(1));
    287 
    288   manager_.OnSerializedPacket(serialized_packet);
    289 
    290   QuicPacketSequenceNumber unacked[] = { 1 };
    291   VerifyUnackedPackets(unacked, arraysize(unacked));
    292   QuicPacketSequenceNumber retransmittable[] = { 1 };
    293   VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
    294 }
    295 
    296 TEST_F(QuicSentPacketManagerTest, IsUnAckedRetransmit) {
    297   SendDataPacket(1);
    298   RetransmitPacket(1, 2);
    299 
    300   EXPECT_TRUE(QuicSentPacketManagerPeer::IsRetransmission(&manager_, 2));
    301   QuicPacketSequenceNumber unacked[] = { 1, 2 };
    302   VerifyUnackedPackets(unacked, arraysize(unacked));
    303   QuicPacketSequenceNumber retransmittable[] = { 2 };
    304   VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
    305 }
    306 
    307 TEST_F(QuicSentPacketManagerTest, RetransmitThenAck) {
    308   SendDataPacket(1);
    309   RetransmitAndSendPacket(1, 2);
    310 
    311   // Ack 2 but not 1.
    312   QuicAckFrame ack_frame;
    313   ack_frame.largest_observed = 2;
    314   ack_frame.missing_packets.insert(1);
    315   ExpectAck(2);
    316   manager_.OnIncomingAck(ack_frame, clock_.Now());
    317 
    318   // Packet 1 is unacked, pending, but not retransmittable.
    319   QuicPacketSequenceNumber unacked[] = { 1 };
    320   VerifyUnackedPackets(unacked, arraysize(unacked));
    321   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    322   VerifyRetransmittablePackets(NULL, 0);
    323 }
    324 
    325 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckBeforeSend) {
    326   SendDataPacket(1);
    327   QuicSentPacketManagerPeer::MarkForRetransmission(
    328       &manager_, 1, TLP_RETRANSMISSION);
    329   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    330 
    331   // Ack 1.
    332   QuicAckFrame ack_frame;
    333   ack_frame.largest_observed = 1;
    334   ExpectAck(1);
    335   manager_.OnIncomingAck(ack_frame, clock_.Now());
    336 
    337   // There should no longer be a pending retransmission.
    338   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    339 
    340   // No unacked packets remain.
    341   VerifyUnackedPackets(NULL, 0);
    342   VerifyRetransmittablePackets(NULL, 0);
    343   EXPECT_EQ(0u, stats_.packets_spuriously_retransmitted);
    344 }
    345 
    346 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPrevious) {
    347   SendDataPacket(1);
    348   RetransmitPacket(1, 2);
    349   QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
    350   clock_.AdvanceTime(rtt);
    351 
    352   // Ack 1 but not 2.
    353   ExpectAck(1);
    354   QuicAckFrame ack_frame;
    355   ack_frame.largest_observed = 1;
    356   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    357 
    358   // 2 should be unacked, since it may provide an RTT measurement.
    359   QuicPacketSequenceNumber unacked[] = { 2 };
    360   VerifyUnackedPackets(unacked, arraysize(unacked));
    361   EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    362   VerifyRetransmittablePackets(NULL, 0);
    363 
    364   // Verify that the retransmission alarm would not fire,
    365   // since there is no retransmittable data outstanding.
    366   EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
    367   EXPECT_EQ(1u, stats_.packets_spuriously_retransmitted);
    368 }
    369 
    370 TEST_F(QuicSentPacketManagerTest, RetransmitAndSendThenAckPrevious) {
    371   SendDataPacket(1);
    372   RetransmitAndSendPacket(1, 2);
    373   QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
    374   clock_.AdvanceTime(rtt);
    375 
    376   // Ack 1 but not 2.
    377   ExpectAck(1);
    378   QuicAckFrame ack_frame;
    379   ack_frame.largest_observed = 1;
    380   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    381 
    382   // 2 remains unacked, but no packets have retransmittable data.
    383   QuicPacketSequenceNumber unacked[] = { 2 };
    384   VerifyUnackedPackets(unacked, arraysize(unacked));
    385   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    386   VerifyRetransmittablePackets(NULL, 0);
    387 
    388   EXPECT_EQ(1u, stats_.packets_spuriously_retransmitted);
    389 }
    390 
    391 TEST_F(QuicSentPacketManagerTest, RetransmitThenAckPreviousThenNackRetransmit) {
    392   SendDataPacket(1);
    393   RetransmitPacket(1, 2);
    394   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2, _, _))
    395       .WillOnce(Return(true));
    396   manager_.OnPacketSent(2, clock_.ApproximateNow(), kDefaultLength,
    397                         LOSS_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA);
    398   QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
    399   clock_.AdvanceTime(rtt);
    400 
    401   // First, ACK packet 1 which makes packet 2 non-retransmittable.
    402   ExpectAck(1);
    403   QuicAckFrame ack_frame;
    404   ack_frame.largest_observed = 1;
    405   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    406 
    407   SendDataPacket(3);
    408   SendDataPacket(4);
    409   SendDataPacket(5);
    410   clock_.AdvanceTime(rtt);
    411 
    412   // Next, NACK packet 2 three times.
    413   ack_frame.largest_observed = 3;
    414   ack_frame.missing_packets.insert(2);
    415   ExpectAck(3);
    416   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    417 
    418   ack_frame.largest_observed = 4;
    419   ExpectAck(4);
    420   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    421 
    422   ack_frame.largest_observed = 5;
    423   ExpectAckAndLoss(true, 5, 2);
    424   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    425 
    426   // No packets remain unacked.
    427   VerifyUnackedPackets(NULL, 0);
    428   EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    429   VerifyRetransmittablePackets(NULL, 0);
    430 
    431   // Verify that the retransmission alarm would not fire,
    432   // since there is no retransmittable data outstanding.
    433   EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
    434 }
    435 
    436 TEST_F(QuicSentPacketManagerTest, RetransmitTwiceThenAckPreviousBeforeSend) {
    437   SendDataPacket(1);
    438   RetransmitAndSendPacket(1, 2);
    439 
    440   // Fire the RTO, which will mark 2 for retransmission (but will not send it).
    441   EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
    442   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
    443   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
    444       .WillOnce(Return(2 * kDefaultTCPMSS));
    445   manager_.OnRetransmissionTimeout();
    446   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    447 
    448   // Ack 1 but not 2, before 2 is able to be sent.
    449   // Since 1 has been retransmitted, it has already been lost, and so the
    450   // send algorithm is not informed that it has been ACK'd.
    451   QuicAckFrame ack_frame;
    452   ack_frame.largest_observed = 1;
    453   ExpectUpdatedRtt(1);
    454   EXPECT_CALL(*send_algorithm_, RevertRetransmissionTimeout());
    455   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    456 
    457   // Since 2 was marked for retransmit, when 1 is acked, 2 is kept for RTT.
    458   QuicPacketSequenceNumber unacked[] = { 2 };
    459   VerifyUnackedPackets(unacked, arraysize(unacked));
    460   EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    461   VerifyRetransmittablePackets(NULL, 0);
    462 
    463   // Verify that the retransmission alarm would not fire,
    464   // since there is no retransmittable data outstanding.
    465   EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
    466 }
    467 
    468 TEST_F(QuicSentPacketManagerTest, RetransmitTwiceThenAckFirst) {
    469   StrictMock<MockDebugDelegate> debug_delegate;
    470   EXPECT_CALL(debug_delegate, OnSpuriousPacketRetransmition(
    471       TLP_RETRANSMISSION, kDefaultLength)).Times(2);
    472   manager_.set_debug_delegate(&debug_delegate);
    473 
    474   SendDataPacket(1);
    475   RetransmitAndSendPacket(1, 2);
    476   RetransmitAndSendPacket(2, 3);
    477   QuicTime::Delta rtt = QuicTime::Delta::FromMilliseconds(15);
    478   clock_.AdvanceTime(rtt);
    479 
    480   // Ack 1 but not 2 or 3.
    481   ExpectAck(1);
    482   QuicAckFrame ack_frame;
    483   ack_frame.largest_observed = 1;
    484   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    485 
    486   // 2 and 3 remain unacked, but no packets have retransmittable data.
    487   QuicPacketSequenceNumber unacked[] = { 2, 3 };
    488   VerifyUnackedPackets(unacked, arraysize(unacked));
    489   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    490   VerifyRetransmittablePackets(NULL, 0);
    491 
    492   // Ensure packet 2 is lost when 4 is sent and 3 and 4 are acked.
    493   SendDataPacket(4);
    494   ack_frame.largest_observed = 4;
    495   ack_frame.missing_packets.insert(2);
    496   QuicPacketSequenceNumber acked[] = { 3, 4 };
    497   ExpectAcksAndLosses(true, acked, arraysize(acked), NULL, 0);
    498   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    499 
    500   QuicPacketSequenceNumber unacked2[] = { 2 };
    501   VerifyUnackedPackets(unacked2, arraysize(unacked2));
    502   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    503 
    504   SendDataPacket(5);
    505   ack_frame.largest_observed = 5;
    506   ExpectAckAndLoss(true, 5, 2);
    507   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    508 
    509   VerifyUnackedPackets(NULL, 0);
    510   EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    511   EXPECT_EQ(2u, stats_.packets_spuriously_retransmitted);
    512 }
    513 
    514 TEST_F(QuicSentPacketManagerTest, LoseButDontRetransmitRevivedPacket) {
    515   SendDataPacket(1);
    516   SendDataPacket(2);
    517   SendFecPacket(3);
    518   SendDataPacket(4);
    519 
    520   // Ack 2 and 3, and mark 1 as revived.
    521   QuicAckFrame ack_frame;
    522   ack_frame.largest_observed = 3;
    523   ack_frame.missing_packets.insert(1);
    524   ack_frame.revived_packets.insert(1);
    525   QuicPacketSequenceNumber acked[] = { 2, 3 };
    526   ExpectAcksAndLosses(true, acked, arraysize(acked), NULL, 0);
    527   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    528 
    529   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    530   QuicPacketSequenceNumber unacked[] = { 1, 4 };
    531   VerifyUnackedPackets(unacked, arraysize(unacked));
    532   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    533   QuicPacketSequenceNumber retransmittable[] = { 4 };
    534   VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
    535 
    536   // Ack the 4th packet and expect the 1st to be considered lost.
    537   ack_frame.largest_observed = 4;
    538   ExpectAckAndLoss(true, 4, 1);
    539   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    540 
    541   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    542   VerifyRetransmittablePackets(NULL, 0);
    543 }
    544 
    545 TEST_F(QuicSentPacketManagerTest, MarkLostThenReviveAndDontRetransmitPacket) {
    546   SendDataPacket(1);
    547   SendDataPacket(2);
    548   SendDataPacket(3);
    549   SendDataPacket(4);
    550   SendFecPacket(5);
    551 
    552   // Ack 2, 3, and 4, and expect the 1st to be considered lost.
    553   QuicAckFrame ack_frame;
    554   ack_frame.largest_observed = 4;
    555   ack_frame.missing_packets.insert(1);
    556   QuicPacketSequenceNumber acked[] = { 2, 3, 4 };
    557   QuicPacketSequenceNumber lost[] = { 1 };
    558   ExpectAcksAndLosses(true, acked, arraysize(acked), lost, arraysize(lost));
    559   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    560 
    561   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    562   QuicPacketSequenceNumber unacked[] = { 1, 5 };
    563   VerifyUnackedPackets(unacked, arraysize(unacked));
    564   QuicPacketSequenceNumber retransmittable[] = { 1 };
    565   VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
    566 
    567   // Ack 5th packet (FEC) and revive 1st packet. 1st packet should now be
    568   // removed from pending retransmissions map.
    569   ack_frame.largest_observed = 5;
    570   ack_frame.revived_packets.insert(1);
    571   ExpectAck(5);
    572   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    573 
    574   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    575   VerifyRetransmittablePackets(NULL, 0);
    576 }
    577 
    578 TEST_F(QuicSentPacketManagerTest, TruncatedAck) {
    579   SendDataPacket(1);
    580   RetransmitAndSendPacket(1, 2);
    581   RetransmitAndSendPacket(2, 3);
    582   RetransmitAndSendPacket(3, 4);
    583   RetransmitAndSendPacket(4, 5);
    584 
    585   // Truncated ack with 4 NACKs, so the first packet is lost.
    586   QuicAckFrame ack_frame;
    587   ack_frame.largest_observed = 4;
    588   ack_frame.missing_packets.insert(1);
    589   ack_frame.missing_packets.insert(2);
    590   ack_frame.missing_packets.insert(3);
    591   ack_frame.missing_packets.insert(4);
    592   ack_frame.is_truncated = true;
    593 
    594   QuicPacketSequenceNumber lost[] = { 1 };
    595   ExpectAcksAndLosses(true, NULL, 0, lost, arraysize(lost));
    596   manager_.OnIncomingAck(ack_frame, clock_.Now());
    597 
    598   // High water mark will be raised.
    599   QuicPacketSequenceNumber unacked[] = { 2, 3, 4, 5 };
    600   VerifyUnackedPackets(unacked, arraysize(unacked));
    601   QuicPacketSequenceNumber retransmittable[] = { 5 };
    602   VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
    603 }
    604 
    605 TEST_F(QuicSentPacketManagerTest, AckPreviousTransmissionThenTruncatedAck) {
    606   SendDataPacket(1);
    607   RetransmitAndSendPacket(1, 2);
    608   RetransmitAndSendPacket(2, 3);
    609   RetransmitAndSendPacket(3, 4);
    610   SendDataPacket(5);
    611   SendDataPacket(6);
    612   SendDataPacket(7);
    613   SendDataPacket(8);
    614   SendDataPacket(9);
    615 
    616   // Ack previous transmission
    617   {
    618     QuicAckFrame ack_frame;
    619     ack_frame.largest_observed = 2;
    620     ack_frame.missing_packets.insert(1);
    621     ExpectAck(2);
    622     manager_.OnIncomingAck(ack_frame, clock_.Now());
    623     EXPECT_TRUE(manager_.IsUnacked(4));
    624   }
    625 
    626   // Truncated ack with 4 NACKs
    627   {
    628     QuicAckFrame ack_frame;
    629     ack_frame.largest_observed = 6;
    630     ack_frame.missing_packets.insert(3);
    631     ack_frame.missing_packets.insert(4);
    632     ack_frame.missing_packets.insert(5);
    633     ack_frame.missing_packets.insert(6);
    634     ack_frame.is_truncated = true;
    635     ExpectAckAndLoss(true, 1, 3);
    636     manager_.OnIncomingAck(ack_frame, clock_.Now());
    637   }
    638 
    639   // High water mark will be raised.
    640   QuicPacketSequenceNumber unacked[] = { 4, 5, 6, 7, 8, 9 };
    641   VerifyUnackedPackets(unacked, arraysize(unacked));
    642   QuicPacketSequenceNumber retransmittable[] = { 5, 6, 7, 8, 9 };
    643   VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
    644 }
    645 
    646 TEST_F(QuicSentPacketManagerTest, GetLeastUnacked) {
    647   EXPECT_EQ(1u, manager_.GetLeastUnacked());
    648 }
    649 
    650 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedUnacked) {
    651   SerializedPacket serialized_packet(CreateDataPacket(1));
    652 
    653   manager_.OnSerializedPacket(serialized_packet);
    654   EXPECT_EQ(1u, manager_.GetLeastUnacked());
    655 }
    656 
    657 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedUnackedFec) {
    658   SerializedPacket serialized_packet(CreateFecPacket(1));
    659 
    660   manager_.OnSerializedPacket(serialized_packet);
    661   EXPECT_EQ(1u, manager_.GetLeastUnacked());
    662 }
    663 
    664 TEST_F(QuicSentPacketManagerTest, GetLeastUnackedAndDiscard) {
    665   VerifyUnackedPackets(NULL, 0);
    666 
    667   SendFecPacket(1);
    668   EXPECT_EQ(1u, manager_.GetLeastUnacked());
    669 
    670   SendFecPacket(2);
    671   EXPECT_EQ(1u, manager_.GetLeastUnacked());
    672 
    673   SendFecPacket(3);
    674   EXPECT_EQ(1u, manager_.GetLeastUnacked());
    675 
    676   QuicPacketSequenceNumber unacked[] = { 1, 2, 3 };
    677   VerifyUnackedPackets(unacked, arraysize(unacked));
    678   VerifyRetransmittablePackets(NULL, 0);
    679 
    680   // Ack 2, so there's an rtt update.
    681   ExpectAck(2);
    682   QuicAckFrame ack_frame;
    683   ack_frame.largest_observed = 2;
    684   ack_frame.missing_packets.insert(1);
    685   manager_.OnIncomingAck(ack_frame, clock_.Now());
    686 
    687   EXPECT_EQ(1u, manager_.GetLeastUnacked());
    688 }
    689 
    690 TEST_F(QuicSentPacketManagerTest, GetSentTime) {
    691   VerifyUnackedPackets(NULL, 0);
    692 
    693   SerializedPacket serialized_packet(CreateFecPacket(1));
    694   manager_.OnSerializedPacket(serialized_packet);
    695   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _))
    696                   .Times(1).WillOnce(Return(true));
    697   manager_.OnPacketSent(1, QuicTime::Zero(), kDefaultLength, NOT_RETRANSMISSION,
    698                         NO_RETRANSMITTABLE_DATA);
    699 
    700   SerializedPacket serialized_packet2(CreateFecPacket(2));
    701   QuicTime sent_time = QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(1));
    702   manager_.OnSerializedPacket(serialized_packet2);
    703   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2, _, _))
    704                   .Times(1).WillOnce(Return(true));
    705   manager_.OnPacketSent(2, sent_time, kDefaultLength, NOT_RETRANSMISSION,
    706                         NO_RETRANSMITTABLE_DATA);
    707 
    708   QuicPacketSequenceNumber unacked[] = { 1, 2 };
    709   VerifyUnackedPackets(unacked, arraysize(unacked));
    710   VerifyRetransmittablePackets(NULL, 0);
    711 
    712   EXPECT_TRUE(manager_.HasUnackedPackets());
    713   EXPECT_EQ(QuicTime::Zero(),
    714             QuicSentPacketManagerPeer::GetSentTime(&manager_, 1));
    715   EXPECT_EQ(sent_time, QuicSentPacketManagerPeer::GetSentTime(&manager_, 2));
    716 }
    717 
    718 TEST_F(QuicSentPacketManagerTest, AckAckAndUpdateRtt) {
    719   SendDataPacket(1);
    720   SendAckPacket(2);
    721 
    722   // Now ack the ack and expect an RTT update.
    723   QuicAckFrame ack_frame;
    724   ack_frame.largest_observed = 2;
    725   ack_frame.delta_time_largest_observed =
    726       QuicTime::Delta::FromMilliseconds(5);
    727 
    728   ExpectAck(1);
    729   manager_.OnIncomingAck(ack_frame, clock_.Now());
    730 
    731   SendAckPacket(3);
    732 
    733   // Now ack the ack and expect only an RTT update.
    734   ack_frame.largest_observed = 3;
    735   ExpectUpdatedRtt(3);
    736   manager_.OnIncomingAck(ack_frame, clock_.Now());
    737 }
    738 
    739 TEST_F(QuicSentPacketManagerTest, Rtt) {
    740   QuicPacketSequenceNumber sequence_number = 1;
    741   QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(15);
    742   SendDataPacket(sequence_number);
    743   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
    744 
    745   ExpectAck(sequence_number);
    746   QuicAckFrame ack_frame;
    747   ack_frame.largest_observed = sequence_number;
    748   ack_frame.delta_time_largest_observed =
    749       QuicTime::Delta::FromMilliseconds(5);
    750   manager_.OnIncomingAck(ack_frame, clock_.Now());
    751   EXPECT_EQ(expected_rtt,
    752             QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
    753 }
    754 
    755 TEST_F(QuicSentPacketManagerTest, RttWithInvalidDelta) {
    756   // Expect that the RTT is equal to the local time elapsed, since the
    757   // delta_time_largest_observed is larger than the local time elapsed
    758   // and is hence invalid.
    759   QuicPacketSequenceNumber sequence_number = 1;
    760   QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
    761   SendDataPacket(sequence_number);
    762   clock_.AdvanceTime(expected_rtt);
    763 
    764   ExpectAck(sequence_number);
    765   QuicAckFrame ack_frame;
    766   ack_frame.largest_observed = sequence_number;
    767   ack_frame.delta_time_largest_observed =
    768       QuicTime::Delta::FromMilliseconds(11);
    769   manager_.OnIncomingAck(ack_frame, clock_.Now());
    770   EXPECT_EQ(expected_rtt,
    771             QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
    772 }
    773 
    774 TEST_F(QuicSentPacketManagerTest, RttWithInfiniteDelta) {
    775   // Expect that the RTT is equal to the local time elapsed, since the
    776   // delta_time_largest_observed is infinite, and is hence invalid.
    777   QuicPacketSequenceNumber sequence_number = 1;
    778   QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
    779   SendDataPacket(sequence_number);
    780   clock_.AdvanceTime(expected_rtt);
    781 
    782   ExpectAck(sequence_number);
    783   QuicAckFrame ack_frame;
    784   ack_frame.largest_observed = sequence_number;
    785   ack_frame.delta_time_largest_observed = QuicTime::Delta::Infinite();
    786   manager_.OnIncomingAck(ack_frame, clock_.Now());
    787   EXPECT_EQ(expected_rtt,
    788             QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
    789 }
    790 
    791 TEST_F(QuicSentPacketManagerTest, RttZeroDelta) {
    792   // Expect that the RTT is the time between send and receive since the
    793   // delta_time_largest_observed is zero.
    794   QuicPacketSequenceNumber sequence_number = 1;
    795   QuicTime::Delta expected_rtt = QuicTime::Delta::FromMilliseconds(10);
    796   SendDataPacket(sequence_number);
    797   clock_.AdvanceTime(expected_rtt);
    798 
    799   ExpectAck(sequence_number);
    800   QuicAckFrame ack_frame;
    801   ack_frame.largest_observed = sequence_number;
    802   ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
    803   manager_.OnIncomingAck(ack_frame, clock_.Now());
    804   EXPECT_EQ(expected_rtt,
    805             QuicSentPacketManagerPeer::GetRttStats(&manager_)->latest_rtt());
    806 }
    807 
    808 TEST_F(QuicSentPacketManagerTest, TailLossProbeTimeout) {
    809   QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
    810 
    811   // Send 1 packet.
    812   QuicPacketSequenceNumber sequence_number = 1;
    813   SendDataPacket(sequence_number);
    814 
    815   // The first tail loss probe retransmits 1 packet.
    816   manager_.OnRetransmissionTimeout();
    817   EXPECT_EQ(QuicTime::Delta::Zero(),
    818             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    819   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    820   manager_.MaybeRetransmitTailLossProbe();
    821   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    822   RetransmitNextPacket(2);
    823   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    824 
    825   // The second tail loss probe retransmits 1 packet.
    826   manager_.OnRetransmissionTimeout();
    827   EXPECT_EQ(QuicTime::Delta::Zero(),
    828             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    829   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    830   manager_.MaybeRetransmitTailLossProbe();
    831   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    832   RetransmitNextPacket(3);
    833   EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
    834       QuicTime::Delta::Infinite()));
    835   EXPECT_EQ(QuicTime::Delta::Infinite(),
    836             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    837   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    838 
    839   // Ack the third and ensure the first two are still pending.
    840   ExpectAck(3);
    841   QuicAckFrame ack_frame;
    842   ack_frame.largest_observed = 3;
    843   ack_frame.missing_packets.insert(1);
    844   ack_frame.missing_packets.insert(2);
    845   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    846 
    847   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    848 
    849   // Acking two more packets will lose both of them due to nacks.
    850   ack_frame.largest_observed = 5;
    851   QuicPacketSequenceNumber lost[] = { 1, 2 };
    852   ExpectAcksAndLosses(false, NULL, 0, lost, arraysize(lost));
    853   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    854 
    855   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    856   EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
    857   EXPECT_EQ(2u, stats_.tlp_count);
    858   EXPECT_EQ(0u, stats_.rto_count);
    859 }
    860 
    861 TEST_F(QuicSentPacketManagerTest, TailLossProbeThenRTO) {
    862   QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
    863 
    864   // Send 100 packets.
    865   const size_t kNumSentPackets = 100;
    866   for (size_t i = 1; i <= kNumSentPackets; ++i) {
    867     SendDataPacket(i);
    868   }
    869 
    870   // The first tail loss probe retransmits 1 packet.
    871   manager_.OnRetransmissionTimeout();
    872   EXPECT_EQ(QuicTime::Delta::Zero(),
    873             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    874   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    875   manager_.MaybeRetransmitTailLossProbe();
    876   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    877   RetransmitNextPacket(101);
    878   EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
    879       QuicTime::Delta::Infinite()));
    880   EXPECT_EQ(QuicTime::Delta::Infinite(),
    881             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    882   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    883 
    884   // The second tail loss probe retransmits 1 packet.
    885   manager_.OnRetransmissionTimeout();
    886   EXPECT_EQ(QuicTime::Delta::Zero(),
    887             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    888   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    889   EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
    890   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    891   RetransmitNextPacket(102);
    892   EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
    893       QuicTime::Delta::Infinite()));
    894   EXPECT_EQ(QuicTime::Delta::Infinite(),
    895             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    896 
    897   // Advance the time enough to ensure all packets are RTO'd.
    898   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1000));
    899 
    900   // The final RTO abandons all of them.
    901   EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
    902   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
    903   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
    904       .WillOnce(Return(2 * kDefaultTCPMSS));
    905   manager_.OnRetransmissionTimeout();
    906   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    907   EXPECT_EQ(2u, stats_.tlp_count);
    908   EXPECT_EQ(1u, stats_.rto_count);
    909 }
    910 
    911 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeout) {
    912   // Send 2 crypto packets and 3 data packets.
    913   const size_t kNumSentCryptoPackets = 2;
    914   for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
    915     SendCryptoPacket(i);
    916   }
    917   const size_t kNumSentDataPackets = 3;
    918   for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
    919     SendDataPacket(kNumSentCryptoPackets + i);
    920   }
    921   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
    922 
    923   // The first retransmits 2 packets.
    924   manager_.OnRetransmissionTimeout();
    925   EXPECT_EQ(QuicTime::Delta::Zero(),
    926             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    927   RetransmitNextPacket(6);
    928   RetransmitNextPacket(7);
    929   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    930   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
    931 
    932   // The second retransmits 2 packets.
    933   manager_.OnRetransmissionTimeout();
    934   EXPECT_EQ(QuicTime::Delta::Zero(),
    935             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
    936   RetransmitNextPacket(8);
    937   RetransmitNextPacket(9);
    938   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    939   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
    940 
    941   // Now ack the two crypto packets and the speculatively encrypted request,
    942   // and ensure the first four crypto packets get abandoned, but not lost.
    943   QuicPacketSequenceNumber acked[] = { 3, 4, 5, 8, 9 };
    944   ExpectAcksAndLosses(true, acked, arraysize(acked), NULL, 0);
    945   QuicAckFrame ack_frame;
    946   ack_frame.largest_observed = 9;
    947   ack_frame.missing_packets.insert(1);
    948   ack_frame.missing_packets.insert(2);
    949   ack_frame.missing_packets.insert(6);
    950   ack_frame.missing_packets.insert(7);
    951   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
    952 
    953   EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
    954 }
    955 
    956 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutVersionNegotiation) {
    957   // Send 2 crypto packets and 3 data packets.
    958   const size_t kNumSentCryptoPackets = 2;
    959   for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
    960     SendCryptoPacket(i);
    961   }
    962   const size_t kNumSentDataPackets = 3;
    963   for (size_t i = 1; i <= kNumSentDataPackets; ++i) {
    964     SendDataPacket(kNumSentCryptoPackets + i);
    965   }
    966   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
    967 
    968   // The first retransmission timeout retransmits 2 crypto packets.
    969   manager_.OnRetransmissionTimeout();
    970   RetransmitNextPacket(6);
    971   RetransmitNextPacket(7);
    972   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    973   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
    974 
    975   // Now act like a version negotiation packet arrived, which would cause all
    976   // unacked packets to be retransmitted.
    977   manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
    978 
    979   // Ensure the first two pending packets are the crypto retransmits.
    980   ASSERT_TRUE(manager_.HasPendingRetransmissions());
    981   EXPECT_EQ(6u, manager_.NextPendingRetransmission().sequence_number);
    982   RetransmitNextPacket(8);
    983   EXPECT_EQ(7u, manager_.NextPendingRetransmission().sequence_number);
    984   RetransmitNextPacket(9);
    985 
    986   EXPECT_TRUE(manager_.HasPendingRetransmissions());
    987   // Send 3 more data packets and ensure the least unacked is raised.
    988   RetransmitNextPacket(10);
    989   RetransmitNextPacket(11);
    990   RetransmitNextPacket(12);
    991   EXPECT_FALSE(manager_.HasPendingRetransmissions());
    992 
    993   EXPECT_EQ(8u, manager_.GetLeastUnacked());
    994 }
    995 
    996 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeSpuriousRetransmission) {
    997   // Send 1 crypto packet.
    998   SendCryptoPacket(1);
    999   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1000 
   1001   // Retransmit the crypto packet as 2.
   1002   manager_.OnRetransmissionTimeout();
   1003   RetransmitNextPacket(2);
   1004 
   1005   // Retransmit the crypto packet as 3.
   1006   manager_.OnRetransmissionTimeout();
   1007   RetransmitNextPacket(3);
   1008 
   1009   // Now ack the second crypto packet, and ensure the first gets removed, but
   1010   // the third does not.
   1011   ExpectUpdatedRtt(2);
   1012   QuicAckFrame ack_frame;
   1013   ack_frame.largest_observed = 2;
   1014   ack_frame.missing_packets.insert(1);
   1015   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
   1016 
   1017   EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1018   QuicPacketSequenceNumber unacked[] = { 3 };
   1019   VerifyUnackedPackets(unacked, arraysize(unacked));
   1020 }
   1021 
   1022 TEST_F(QuicSentPacketManagerTest, CryptoHandshakeTimeoutUnsentDataPacket) {
   1023   // Send 2 crypto packets and serialize 1 data packet.
   1024   const size_t kNumSentCryptoPackets = 2;
   1025   for (size_t i = 1; i <= kNumSentCryptoPackets; ++i) {
   1026     SendCryptoPacket(i);
   1027   }
   1028   SerializedPacket packet(CreateDataPacket(3));
   1029   manager_.OnSerializedPacket(packet);
   1030   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1031 
   1032   // Retransmit 2 crypto packets, but not the serialized packet.
   1033   manager_.OnRetransmissionTimeout();
   1034   RetransmitNextPacket(4);
   1035   RetransmitNextPacket(5);
   1036   EXPECT_FALSE(manager_.HasPendingRetransmissions());
   1037   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1038 }
   1039 
   1040 TEST_F(QuicSentPacketManagerTest,
   1041        CryptoHandshakeRetransmissionThenRetransmitAll) {
   1042   // Send 1 crypto packet.
   1043   SendCryptoPacket(1);
   1044   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1045 
   1046   // Retransmit the crypto packet as 2.
   1047   manager_.OnRetransmissionTimeout();
   1048   RetransmitNextPacket(2);
   1049 
   1050   // Now retransmit all the unacked packets, which occurs when there is a
   1051   // version negotiation.
   1052   manager_.RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
   1053   QuicPacketSequenceNumber unacked[] = { 1, 2 };
   1054   VerifyUnackedPackets(unacked, arraysize(unacked));
   1055   EXPECT_TRUE(manager_.HasPendingRetransmissions());
   1056   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1057   EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
   1058 }
   1059 
   1060 TEST_F(QuicSentPacketManagerTest,
   1061        CryptoHandshakeRetransmissionThenNeuterAndAck) {
   1062   // Send 1 crypto packet.
   1063   SendCryptoPacket(1);
   1064   EXPECT_TRUE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1065 
   1066   // Retransmit the crypto packet as 2.
   1067   manager_.OnRetransmissionTimeout();
   1068   RetransmitNextPacket(2);
   1069 
   1070   // Retransmit the crypto packet as 3.
   1071   manager_.OnRetransmissionTimeout();
   1072   RetransmitNextPacket(3);
   1073 
   1074   // Now neuter all unacked unencrypted packets, which occurs when the
   1075   // connection goes forward secure.
   1076   manager_.NeuterUnencryptedPackets();
   1077   QuicPacketSequenceNumber unacked[] = { 1, 2, 3};
   1078   VerifyUnackedPackets(unacked, arraysize(unacked));
   1079   VerifyRetransmittablePackets(NULL, 0);
   1080   EXPECT_FALSE(manager_.HasPendingRetransmissions());
   1081   EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1082   EXPECT_FALSE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
   1083 
   1084   // Ensure both packets get discarded when packet 2 is acked.
   1085   QuicAckFrame ack_frame;
   1086   ack_frame.largest_observed = 3;
   1087   ack_frame.missing_packets.insert(1);
   1088   ack_frame.missing_packets.insert(2);
   1089   ExpectUpdatedRtt(3);
   1090   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
   1091   VerifyUnackedPackets(NULL, 0);
   1092   VerifyRetransmittablePackets(NULL, 0);
   1093 }
   1094 
   1095 TEST_F(QuicSentPacketManagerTest, TailLossProbeTimeoutUnsentDataPacket) {
   1096   QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
   1097   // Serialize two data packets and send the latter.
   1098   SerializedPacket packet(CreateDataPacket(1));
   1099   manager_.OnSerializedPacket(packet);
   1100   SendDataPacket(2);
   1101   EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1102   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
   1103 
   1104   // Retransmit 1 unacked packets, but not the first serialized packet.
   1105   manager_.OnRetransmissionTimeout();
   1106   EXPECT_EQ(QuicTime::Delta::Zero(),
   1107             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
   1108   EXPECT_FALSE(manager_.HasPendingRetransmissions());
   1109   manager_.MaybeRetransmitTailLossProbe();
   1110   EXPECT_TRUE(manager_.HasPendingRetransmissions());
   1111   RetransmitNextPacket(3);
   1112   EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
   1113       QuicTime::Delta::Infinite()));
   1114   EXPECT_EQ(QuicTime::Delta::Infinite(),
   1115             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
   1116   EXPECT_FALSE(manager_.HasPendingRetransmissions());
   1117   EXPECT_FALSE(QuicSentPacketManagerPeer::HasUnackedCryptoPackets(&manager_));
   1118   EXPECT_TRUE(QuicSentPacketManagerPeer::HasPendingPackets(&manager_));
   1119 }
   1120 
   1121 TEST_F(QuicSentPacketManagerTest, ResetRecentMinRTTWithEmptyWindow) {
   1122   QuicTime::Delta min_rtt = QuicTime::Delta::FromMilliseconds(50);
   1123   QuicSentPacketManagerPeer::GetRttStats(&manager_)->UpdateRtt(
   1124       min_rtt, QuicTime::Delta::Zero(), QuicTime::Zero());
   1125   EXPECT_EQ(min_rtt,
   1126             QuicSentPacketManagerPeer::GetRttStats(&manager_)->min_rtt());
   1127   EXPECT_EQ(min_rtt,
   1128             QuicSentPacketManagerPeer::GetRttStats(
   1129                 &manager_)->recent_min_rtt());
   1130 
   1131   // Send two packets with no prior bytes in flight.
   1132   SendDataPacket(1);
   1133   SendDataPacket(2);
   1134 
   1135   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
   1136   // Ack two packets with 100ms RTT observations.
   1137   QuicAckFrame ack_frame;
   1138   ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
   1139   ack_frame.largest_observed = 1;
   1140   ExpectAck(1);
   1141   manager_.OnIncomingAck(ack_frame, clock_.Now());
   1142 
   1143   // First ack does not change recent min rtt.
   1144   EXPECT_EQ(min_rtt,
   1145             QuicSentPacketManagerPeer::GetRttStats(
   1146                 &manager_)->recent_min_rtt());
   1147 
   1148   ack_frame.largest_observed = 2;
   1149   ExpectAck(2);
   1150   manager_.OnIncomingAck(ack_frame, clock_.Now());
   1151 
   1152   EXPECT_EQ(min_rtt,
   1153             QuicSentPacketManagerPeer::GetRttStats(&manager_)->min_rtt());
   1154   EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100),
   1155             QuicSentPacketManagerPeer::GetRttStats(
   1156                 &manager_)->recent_min_rtt());
   1157 }
   1158 
   1159 TEST_F(QuicSentPacketManagerTest, RetransmissionTimeout) {
   1160   // Send 100 packets and then ensure all are abandoned when the RTO fires.
   1161   const size_t kNumSentPackets = 100;
   1162   for (size_t i = 1; i <= kNumSentPackets; ++i) {
   1163     SendDataPacket(i);
   1164   }
   1165 
   1166   EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   1167   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1168   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1169       .WillOnce(Return(2 * kDefaultTCPMSS));
   1170   EXPECT_FALSE(manager_.MaybeRetransmitTailLossProbe());
   1171   manager_.OnRetransmissionTimeout();
   1172 }
   1173 
   1174 TEST_F(QuicSentPacketManagerTest, GetTransmissionTime) {
   1175   EXPECT_EQ(QuicTime::Zero(), manager_.GetRetransmissionTime());
   1176 }
   1177 
   1178 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeCryptoHandshake) {
   1179   SendCryptoPacket(1);
   1180 
   1181   // Check the min.
   1182   QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
   1183       1 * base::Time::kMicrosecondsPerMillisecond);
   1184   EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
   1185             manager_.GetRetransmissionTime());
   1186 
   1187   // Test with a standard smoothed RTT.
   1188   QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
   1189       100 * base::Time::kMicrosecondsPerMillisecond);
   1190 
   1191   QuicTime::Delta srtt = manager_.GetRttStats()->SmoothedRtt();
   1192   QuicTime expected_time = clock_.Now().Add(srtt.Multiply(1.5));
   1193   EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
   1194 
   1195   // Retransmit the packet by invoking the retransmission timeout.
   1196   clock_.AdvanceTime(srtt.Multiply(1.5));
   1197   manager_.OnRetransmissionTimeout();
   1198   RetransmitNextPacket(2);
   1199 
   1200   // The retransmission time should now be twice as far in the future.
   1201   expected_time = clock_.Now().Add(srtt.Multiply(2).Multiply(1.5));
   1202   EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
   1203 }
   1204 
   1205 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeTailLossProbe) {
   1206   QuicSentPacketManagerPeer::SetMaxTailLossProbes(&manager_, 2);
   1207   SendDataPacket(1);
   1208   SendDataPacket(2);
   1209 
   1210   // Check the min.
   1211   QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
   1212       1 * base::Time::kMicrosecondsPerMillisecond);
   1213   EXPECT_EQ(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)),
   1214             manager_.GetRetransmissionTime());
   1215 
   1216   // Test with a standard smoothed RTT.
   1217   QuicSentPacketManagerPeer::GetRttStats(&manager_)->set_initial_rtt_us(
   1218       100 * base::Time::kMicrosecondsPerMillisecond);
   1219   QuicTime::Delta srtt = manager_.GetRttStats()->SmoothedRtt();
   1220   QuicTime::Delta expected_tlp_delay = srtt.Multiply(2);
   1221   QuicTime expected_time = clock_.Now().Add(expected_tlp_delay);
   1222   EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
   1223 
   1224   // Retransmit the packet by invoking the retransmission timeout.
   1225   clock_.AdvanceTime(expected_tlp_delay);
   1226   manager_.OnRetransmissionTimeout();
   1227   EXPECT_EQ(QuicTime::Delta::Zero(),
   1228             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
   1229   EXPECT_FALSE(manager_.HasPendingRetransmissions());
   1230   EXPECT_TRUE(manager_.MaybeRetransmitTailLossProbe());
   1231   EXPECT_TRUE(manager_.HasPendingRetransmissions());
   1232   RetransmitNextPacket(3);
   1233   EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)).WillOnce(Return(
   1234       QuicTime::Delta::Infinite()));
   1235   EXPECT_EQ(QuicTime::Delta::Infinite(),
   1236             manager_.TimeUntilSend(clock_.Now(), HAS_RETRANSMITTABLE_DATA));
   1237   EXPECT_FALSE(manager_.HasPendingRetransmissions());
   1238 
   1239   expected_time = clock_.Now().Add(expected_tlp_delay);
   1240   EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
   1241 }
   1242 
   1243 TEST_F(QuicSentPacketManagerTest, GetTransmissionTimeRTO) {
   1244   QuicSentPacketManagerPeer::GetRttStats(&manager_)->UpdateRtt(
   1245       QuicTime::Delta::FromMilliseconds(100),
   1246       QuicTime::Delta::Zero(),
   1247       QuicTime::Zero());
   1248 
   1249   SendDataPacket(1);
   1250   SendDataPacket(2);
   1251   SendDataPacket(3);
   1252   SendDataPacket(4);
   1253 
   1254   QuicTime::Delta expected_rto_delay = QuicTime::Delta::FromMilliseconds(500);
   1255   EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
   1256       .WillRepeatedly(Return(expected_rto_delay));
   1257   QuicTime expected_time = clock_.Now().Add(expected_rto_delay);
   1258   EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
   1259 
   1260   // Retransmit the packet by invoking the retransmission timeout.
   1261   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1262   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1263       .WillOnce(Return(2 * kDefaultTCPMSS));
   1264   EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   1265   clock_.AdvanceTime(expected_rto_delay);
   1266   manager_.OnRetransmissionTimeout();
   1267   EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
   1268   RetransmitNextPacket(5);
   1269   RetransmitNextPacket(6);
   1270   EXPECT_EQ(2 * kDefaultLength,
   1271             QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
   1272   EXPECT_TRUE(manager_.HasPendingRetransmissions());
   1273 
   1274   // The delay should double the second time.
   1275   expected_time = clock_.Now().Add(expected_rto_delay).Add(expected_rto_delay);
   1276   EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
   1277 
   1278   // Ack a packet and ensure the RTO goes back to the original value.
   1279   QuicAckFrame ack_frame;
   1280   ack_frame.largest_observed = 2;
   1281   ack_frame.missing_packets.insert(1);
   1282   ExpectUpdatedRtt(2);
   1283   EXPECT_CALL(*send_algorithm_, RevertRetransmissionTimeout());
   1284   manager_.OnIncomingAck(ack_frame, clock_.ApproximateNow());
   1285   EXPECT_FALSE(manager_.HasPendingRetransmissions());
   1286   EXPECT_EQ(4 * kDefaultLength,
   1287             QuicSentPacketManagerPeer::GetBytesInFlight(&manager_));
   1288 
   1289   // Wait 2RTTs from now for the RTO, since it's the max of the RTO time
   1290   // and the TLP time.  In production, there would always be two TLP's first.
   1291   expected_time = clock_.Now().Add(QuicTime::Delta::FromMilliseconds(200));
   1292   EXPECT_EQ(expected_time, manager_.GetRetransmissionTime());
   1293 }
   1294 
   1295 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMin) {
   1296   SendDataPacket(1);
   1297   EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
   1298       .WillRepeatedly(Return(QuicTime::Delta::FromMilliseconds(1)));
   1299   QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(200);
   1300 
   1301   // If the delay is smaller than the min, ensure it exponentially backs off
   1302   // from the min.
   1303   for (int i = 0; i < 5; ++i) {
   1304     EXPECT_EQ(delay,
   1305               QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
   1306     delay = delay.Add(delay);
   1307     EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1308     EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1309         .WillOnce(Return(2 * kDefaultTCPMSS));
   1310     EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   1311     manager_.OnRetransmissionTimeout();
   1312     RetransmitNextPacket(i + 2);
   1313   }
   1314 }
   1315 
   1316 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelayMax) {
   1317   EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
   1318       .WillOnce(Return(QuicTime::Delta::FromSeconds(500)));
   1319 
   1320   EXPECT_EQ(QuicTime::Delta::FromSeconds(60),
   1321             QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
   1322 }
   1323 
   1324 TEST_F(QuicSentPacketManagerTest, GetTransmissionDelay) {
   1325   SendDataPacket(1);
   1326   QuicTime::Delta delay = QuicTime::Delta::FromMilliseconds(500);
   1327   EXPECT_CALL(*send_algorithm_, RetransmissionDelay())
   1328       .WillRepeatedly(Return(delay));
   1329 
   1330   // Delay should back off exponentially.
   1331   for (int i = 0; i < 5; ++i) {
   1332     EXPECT_EQ(delay,
   1333               QuicSentPacketManagerPeer::GetRetransmissionDelay(&manager_));
   1334     delay = delay.Add(delay);
   1335     EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1336     EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1337         .WillOnce(Return(2 * kDefaultTCPMSS));
   1338     EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   1339     manager_.OnRetransmissionTimeout();
   1340     RetransmitNextPacket(i + 2);
   1341   }
   1342 }
   1343 
   1344 TEST_F(QuicSentPacketManagerTest, GetLossDelay) {
   1345   MockLossAlgorithm* loss_algorithm = new MockLossAlgorithm();
   1346   QuicSentPacketManagerPeer::SetLossAlgorithm(&manager_, loss_algorithm);
   1347 
   1348   EXPECT_CALL(*loss_algorithm, GetLossTimeout())
   1349       .WillRepeatedly(Return(QuicTime::Zero()));
   1350   SendDataPacket(1);
   1351   SendDataPacket(2);
   1352 
   1353   // Handle an ack which causes the loss algorithm to be evaluated and
   1354   // set the loss timeout.
   1355   ExpectAck(2);
   1356   EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
   1357       .WillOnce(Return(SequenceNumberSet()));
   1358   QuicAckFrame ack_frame;
   1359   ack_frame.largest_observed = 2;
   1360   ack_frame.missing_packets.insert(1);
   1361   manager_.OnIncomingAck(ack_frame, clock_.Now());
   1362 
   1363   QuicTime timeout(clock_.Now().Add(QuicTime::Delta::FromMilliseconds(10)));
   1364   EXPECT_CALL(*loss_algorithm, GetLossTimeout())
   1365       .WillRepeatedly(Return(timeout));
   1366   EXPECT_EQ(timeout, manager_.GetRetransmissionTime());
   1367 
   1368   // Fire the retransmission timeout and ensure the loss detection algorithm
   1369   // is invoked.
   1370   EXPECT_CALL(*loss_algorithm, DetectLostPackets(_, _, _, _))
   1371       .WillOnce(Return(SequenceNumberSet()));
   1372   manager_.OnRetransmissionTimeout();
   1373 }
   1374 
   1375 TEST_F(QuicSentPacketManagerTest, NegotiateTimeLossDetection) {
   1376   EXPECT_EQ(kNack,
   1377             QuicSentPacketManagerPeer::GetLossAlgorithm(
   1378                 &manager_)->GetLossDetectionType());
   1379 
   1380   QuicConfig config;
   1381   QuicConfigPeer::SetReceivedLossDetection(&config, kTIME);
   1382   EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
   1383   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1384   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1385       .WillOnce(Return(100 * kDefaultTCPMSS));
   1386   manager_.SetFromConfig(config);
   1387 
   1388   EXPECT_EQ(kTime,
   1389             QuicSentPacketManagerPeer::GetLossAlgorithm(
   1390                 &manager_)->GetLossDetectionType());
   1391 }
   1392 
   1393 TEST_F(QuicSentPacketManagerTest, NegotiateTimeLossDetectionFromOptions) {
   1394   EXPECT_EQ(kNack,
   1395             QuicSentPacketManagerPeer::GetLossAlgorithm(
   1396                 &manager_)->GetLossDetectionType());
   1397 
   1398   QuicConfig config;
   1399   QuicTagVector options;
   1400   options.push_back(kTIME);
   1401   QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
   1402   EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
   1403   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1404   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1405       .WillOnce(Return(100 * kDefaultTCPMSS));
   1406   manager_.SetFromConfig(config);
   1407 
   1408   EXPECT_EQ(kTime,
   1409             QuicSentPacketManagerPeer::GetLossAlgorithm(
   1410                 &manager_)->GetLossDetectionType());
   1411 }
   1412 
   1413 TEST_F(QuicSentPacketManagerTest, NegotiateCongestionControlFromOptions) {
   1414   QuicConfig config;
   1415   QuicTagVector options;
   1416 
   1417   options.push_back(kRENO);
   1418   QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
   1419   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1420   manager_.SetFromConfig(config);
   1421   EXPECT_EQ(kReno, QuicSentPacketManagerPeer::GetCongestionControlAlgorithm(
   1422       manager_)->GetCongestionControlType());
   1423 
   1424   // TODO(rtenneti): Enable the following code after BBR code is checked in.
   1425 #if 0
   1426   options.clear();
   1427   options.push_back(kTBBR);
   1428   QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
   1429   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1430   manager_.SetFromConfig(config);
   1431   EXPECT_EQ(kBBR, QuicSentPacketManagerPeer::GetCongestionControlAlgorithm(
   1432       manager_)->GetCongestionControlType());
   1433 #endif
   1434 }
   1435 
   1436 TEST_F(QuicSentPacketManagerTest, NegotiatePacingFromOptions) {
   1437   EXPECT_FALSE(manager_.using_pacing());
   1438 
   1439   QuicConfig config;
   1440   QuicTagVector options;
   1441   options.push_back(kPACE);
   1442   QuicConfigPeer::SetReceivedConnectionOptions(&config, options);
   1443   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1444   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1445       .WillOnce(Return(100 * kDefaultTCPMSS));
   1446   EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
   1447   manager_.SetFromConfig(config);
   1448 
   1449   EXPECT_TRUE(manager_.using_pacing());
   1450 }
   1451 
   1452 TEST_F(QuicSentPacketManagerTest, UseInitialRoundTripTimeToSend) {
   1453   uint32 initial_rtt_us = 325000;
   1454   EXPECT_NE(initial_rtt_us,
   1455             manager_.GetRttStats()->SmoothedRtt().ToMicroseconds());
   1456 
   1457   QuicConfig config;
   1458   config.SetInitialRoundTripTimeUsToSend(initial_rtt_us);
   1459   EXPECT_CALL(*send_algorithm_, SetFromConfig(_, _));
   1460   EXPECT_CALL(*network_change_visitor_, OnCongestionWindowChange(_));
   1461   EXPECT_CALL(*send_algorithm_, GetCongestionWindow())
   1462       .WillOnce(Return(100 * kDefaultTCPMSS));
   1463   manager_.SetFromConfig(config);
   1464 
   1465   EXPECT_EQ(initial_rtt_us,
   1466             manager_.GetRttStats()->SmoothedRtt().ToMicroseconds());
   1467 }
   1468 
   1469 }  // namespace
   1470 }  // namespace test
   1471 }  // namespace net
   1472