| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "net/quic/congestion_control/rtt_stats.h" |
| 9 #include "net/quic/congestion_control/time_loss_algorithm.h" | 10 #include "net/quic/congestion_control/time_loss_algorithm.h" |
| 10 #include "net/quic/quic_unacked_packet_map.h" | 11 #include "net/quic/quic_unacked_packet_map.h" |
| 11 #include "net/quic/test_tools/mock_clock.h" | 12 #include "net/quic/test_tools/mock_clock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace net { | 15 namespace net { |
| 15 namespace test { | 16 namespace test { |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 class TimeLossAlgorithmTest : public ::testing::Test { | 19 class TimeLossAlgorithmTest : public ::testing::Test { |
| 19 protected: | 20 protected: |
| 20 TimeLossAlgorithmTest() | 21 TimeLossAlgorithmTest() |
| 21 : unacked_packets_(true), | 22 : unacked_packets_() { |
| 22 srtt_(QuicTime::Delta::FromMilliseconds(100)) { } | 23 rtt_stats_.UpdateRtt(QuicTime::Delta::FromMilliseconds(100), |
| 24 QuicTime::Delta::Zero()); |
| 25 } |
| 23 | 26 |
| 24 void SendDataPacket(QuicPacketSequenceNumber sequence_number) { | 27 void SendDataPacket(QuicPacketSequenceNumber sequence_number) { |
| 25 SerializedPacket packet(sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, | 28 SerializedPacket packet(sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, |
| 26 NULL, 0, new RetransmittableFrames()); | 29 NULL, 0, new RetransmittableFrames()); |
| 27 unacked_packets_.AddPacket(packet); | 30 unacked_packets_.AddPacket(packet); |
| 28 unacked_packets_.SetPending(sequence_number, clock_.Now(), 1000); | 31 unacked_packets_.SetPending(sequence_number, clock_.Now(), 1000); |
| 29 } | 32 } |
| 30 | 33 |
| 31 void VerifyLosses(QuicPacketSequenceNumber largest_observed, | 34 void VerifyLosses(QuicPacketSequenceNumber largest_observed, |
| 32 QuicPacketSequenceNumber* losses_expected, | 35 QuicPacketSequenceNumber* losses_expected, |
| 33 size_t num_losses) { | 36 size_t num_losses) { |
| 34 SequenceNumberSet lost_packets = | 37 SequenceNumberSet lost_packets = |
| 35 loss_algorithm_.DetectLostPackets( | 38 loss_algorithm_.DetectLostPackets( |
| 36 unacked_packets_, clock_.Now(), largest_observed, srtt_, srtt_); | 39 unacked_packets_, clock_.Now(), largest_observed, rtt_stats_); |
| 37 EXPECT_EQ(num_losses, lost_packets.size()); | 40 EXPECT_EQ(num_losses, lost_packets.size()); |
| 38 for (size_t i = 0; i < num_losses; ++i) { | 41 for (size_t i = 0; i < num_losses; ++i) { |
| 39 EXPECT_TRUE(ContainsKey(lost_packets, losses_expected[i])); | 42 EXPECT_TRUE(ContainsKey(lost_packets, losses_expected[i])); |
| 40 } | 43 } |
| 41 } | 44 } |
| 42 | 45 |
| 43 QuicUnackedPacketMap unacked_packets_; | 46 QuicUnackedPacketMap unacked_packets_; |
| 44 TimeLossAlgorithm loss_algorithm_; | 47 TimeLossAlgorithm loss_algorithm_; |
| 45 QuicTime::Delta srtt_; | 48 RttStats rtt_stats_; |
| 46 MockClock clock_; | 49 MockClock clock_; |
| 47 }; | 50 }; |
| 48 | 51 |
| 49 TEST_F(TimeLossAlgorithmTest, NoLossFor500Nacks) { | 52 TEST_F(TimeLossAlgorithmTest, NoLossFor500Nacks) { |
| 50 const size_t kNumSentPackets = 5; | 53 const size_t kNumSentPackets = 5; |
| 51 // Transmit 5 packets. | 54 // Transmit 5 packets. |
| 52 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 55 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 53 SendDataPacket(i); | 56 SendDataPacket(i); |
| 54 } | 57 } |
| 55 unacked_packets_.SetNotPending(2); | 58 unacked_packets_.SetNotPending(2); |
| 56 for (size_t i = 0; i < 500; ++i) { | 59 for (size_t i = 0; i < 500; ++i) { |
| 57 unacked_packets_.NackPacket(1, i); | 60 unacked_packets_.NackPacket(1, i); |
| 58 VerifyLosses(2, NULL, 0); | 61 VerifyLosses(2, NULL, 0); |
| 59 } | 62 } |
| 60 EXPECT_EQ(srtt_.Multiply(1.25), | 63 EXPECT_EQ(rtt_stats_.SmoothedRtt().Multiply(1.25), |
| 61 loss_algorithm_.GetLossTimeout().Subtract(clock_.Now())); | 64 loss_algorithm_.GetLossTimeout().Subtract(clock_.Now())); |
| 62 } | 65 } |
| 63 | 66 |
| 64 TEST_F(TimeLossAlgorithmTest, NoLossUntilTimeout) { | 67 TEST_F(TimeLossAlgorithmTest, NoLossUntilTimeout) { |
| 65 const size_t kNumSentPackets = 10; | 68 const size_t kNumSentPackets = 10; |
| 66 // Transmit 10 packets at 1/10th an RTT interval. | 69 // Transmit 10 packets at 1/10th an RTT interval. |
| 67 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 70 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 68 SendDataPacket(i); | 71 SendDataPacket(i); |
| 69 clock_.AdvanceTime(srtt_.Multiply(0.1)); | 72 clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.1)); |
| 70 } | 73 } |
| 71 // Expect the timer to not be set. | 74 // Expect the timer to not be set. |
| 72 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 75 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
| 73 // The packet should not be lost until 1.25 RTTs pass. | 76 // The packet should not be lost until 1.25 RTTs pass. |
| 74 unacked_packets_.NackPacket(1, 1); | 77 unacked_packets_.NackPacket(1, 1); |
| 75 unacked_packets_.SetNotPending(2); | 78 unacked_packets_.SetNotPending(2); |
| 76 VerifyLosses(2, NULL, 0); | 79 VerifyLosses(2, NULL, 0); |
| 77 // Expect the timer to be set to 0.25 RTT's in the future. | 80 // Expect the timer to be set to 0.25 RTT's in the future. |
| 78 EXPECT_EQ(srtt_.Multiply(0.25), | 81 EXPECT_EQ(rtt_stats_.SmoothedRtt().Multiply(0.25), |
| 79 loss_algorithm_.GetLossTimeout().Subtract(clock_.Now())); | 82 loss_algorithm_.GetLossTimeout().Subtract(clock_.Now())); |
| 80 unacked_packets_.NackPacket(1, 5); | 83 unacked_packets_.NackPacket(1, 5); |
| 81 VerifyLosses(2, NULL, 0); | 84 VerifyLosses(2, NULL, 0); |
| 82 clock_.AdvanceTime(srtt_.Multiply(0.25)); | 85 clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25)); |
| 83 QuicPacketSequenceNumber lost[] = { 1 }; | 86 QuicPacketSequenceNumber lost[] = { 1 }; |
| 84 VerifyLosses(2, lost, arraysize(lost)); | 87 VerifyLosses(2, lost, arraysize(lost)); |
| 85 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 88 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
| 86 } | 89 } |
| 87 | 90 |
| 88 TEST_F(TimeLossAlgorithmTest, NoLossWithoutNack) { | 91 TEST_F(TimeLossAlgorithmTest, NoLossWithoutNack) { |
| 89 const size_t kNumSentPackets = 10; | 92 const size_t kNumSentPackets = 10; |
| 90 // Transmit 10 packets at 1/10th an RTT interval. | 93 // Transmit 10 packets at 1/10th an RTT interval. |
| 91 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 94 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 92 SendDataPacket(i); | 95 SendDataPacket(i); |
| 93 clock_.AdvanceTime(srtt_.Multiply(0.1)); | 96 clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.1)); |
| 94 } | 97 } |
| 95 // Expect the timer to not be set. | 98 // Expect the timer to not be set. |
| 96 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 99 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
| 97 // The packet should not be lost without a nack. | 100 // The packet should not be lost without a nack. |
| 98 unacked_packets_.SetNotPending(1); | 101 unacked_packets_.SetNotPending(1); |
| 99 VerifyLosses(1, NULL, 0); | 102 VerifyLosses(1, NULL, 0); |
| 100 // The timer should still not be set. | 103 // The timer should still not be set. |
| 101 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 104 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
| 102 clock_.AdvanceTime(srtt_.Multiply(0.25)); | 105 clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25)); |
| 103 VerifyLosses(1, NULL, 0); | 106 VerifyLosses(1, NULL, 0); |
| 104 clock_.AdvanceTime(srtt_); | 107 clock_.AdvanceTime(rtt_stats_.SmoothedRtt()); |
| 105 VerifyLosses(1, NULL, 0); | 108 VerifyLosses(1, NULL, 0); |
| 106 | 109 |
| 107 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 110 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
| 108 } | 111 } |
| 109 | 112 |
| 110 TEST_F(TimeLossAlgorithmTest, MultipleLossesAtOnce) { | 113 TEST_F(TimeLossAlgorithmTest, MultipleLossesAtOnce) { |
| 111 const size_t kNumSentPackets = 10; | 114 const size_t kNumSentPackets = 10; |
| 112 // Transmit 10 packets at once and then go forward an RTT. | 115 // Transmit 10 packets at once and then go forward an RTT. |
| 113 for (size_t i = 1; i <= kNumSentPackets; ++i) { | 116 for (size_t i = 1; i <= kNumSentPackets; ++i) { |
| 114 SendDataPacket(i); | 117 SendDataPacket(i); |
| 115 } | 118 } |
| 116 clock_.AdvanceTime(srtt_); | 119 clock_.AdvanceTime(rtt_stats_.SmoothedRtt()); |
| 117 // Expect the timer to not be set. | 120 // Expect the timer to not be set. |
| 118 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 121 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
| 119 // The packet should not be lost until 1.25 RTTs pass. | 122 // The packet should not be lost until 1.25 RTTs pass. |
| 120 for (size_t i = 1; i < kNumSentPackets; ++i) { | 123 for (size_t i = 1; i < kNumSentPackets; ++i) { |
| 121 unacked_packets_.NackPacket(i, 1); | 124 unacked_packets_.NackPacket(i, 1); |
| 122 } | 125 } |
| 123 unacked_packets_.SetNotPending(10); | 126 unacked_packets_.SetNotPending(10); |
| 124 VerifyLosses(10, NULL, 0); | 127 VerifyLosses(10, NULL, 0); |
| 125 // Expect the timer to be set to 0.25 RTT's in the future. | 128 // Expect the timer to be set to 0.25 RTT's in the future. |
| 126 EXPECT_EQ(srtt_.Multiply(0.25), | 129 EXPECT_EQ(rtt_stats_.SmoothedRtt().Multiply(0.25), |
| 127 loss_algorithm_.GetLossTimeout().Subtract(clock_.Now())); | 130 loss_algorithm_.GetLossTimeout().Subtract(clock_.Now())); |
| 128 clock_.AdvanceTime(srtt_.Multiply(0.25)); | 131 clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25)); |
| 129 QuicPacketSequenceNumber lost[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | 132 QuicPacketSequenceNumber lost[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 130 VerifyLosses(10, lost, arraysize(lost)); | 133 VerifyLosses(10, lost, arraysize(lost)); |
| 131 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); | 134 EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout()); |
| 132 } | 135 } |
| 133 | 136 |
| 134 } // namespace | 137 } // namespace |
| 135 } // namespace test | 138 } // namespace test |
| 136 } // namespace net | 139 } // namespace net |
| OLD | NEW |