This source file includes following definitions.
- SendDataPacket
- VerifyLosses
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <algorithm>
#include "base/logging.h"
#include "base/stl_util.h"
#include "net/quic/congestion_control/rtt_stats.h"
#include "net/quic/congestion_control/time_loss_algorithm.h"
#include "net/quic/quic_unacked_packet_map.h"
#include "net/quic/test_tools/mock_clock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace test {
class TimeLossAlgorithmTest : public ::testing::Test {
protected:
TimeLossAlgorithmTest()
: unacked_packets_() {
rtt_stats_.UpdateRtt(QuicTime::Delta::FromMilliseconds(100),
QuicTime::Delta::Zero());
}
void SendDataPacket(QuicPacketSequenceNumber sequence_number) {
SerializedPacket packet(sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER,
NULL, 0, new RetransmittableFrames());
unacked_packets_.AddPacket(packet);
unacked_packets_.SetSent(sequence_number, clock_.Now(), 1000, true);
}
void VerifyLosses(QuicPacketSequenceNumber largest_observed,
QuicPacketSequenceNumber* losses_expected,
size_t num_losses) {
SequenceNumberSet lost_packets =
loss_algorithm_.DetectLostPackets(
unacked_packets_, clock_.Now(), largest_observed, rtt_stats_);
EXPECT_EQ(num_losses, lost_packets.size());
for (size_t i = 0; i < num_losses; ++i) {
EXPECT_TRUE(ContainsKey(lost_packets, losses_expected[i]));
}
}
QuicUnackedPacketMap unacked_packets_;
TimeLossAlgorithm loss_algorithm_;
RttStats rtt_stats_;
MockClock clock_;
};
TEST_F(TimeLossAlgorithmTest, NoLossFor500Nacks) {
const size_t kNumSentPackets = 5;
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
}
unacked_packets_.SetNotPending(2);
for (size_t i = 1; i < 500; ++i) {
unacked_packets_.NackPacket(1, i);
VerifyLosses(2, NULL, 0);
}
EXPECT_EQ(rtt_stats_.SmoothedRtt().Multiply(1.25),
loss_algorithm_.GetLossTimeout().Subtract(clock_.Now()));
}
TEST_F(TimeLossAlgorithmTest, NoLossUntilTimeout) {
const size_t kNumSentPackets = 10;
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.1));
}
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
unacked_packets_.NackPacket(1, 1);
unacked_packets_.SetNotPending(2);
VerifyLosses(2, NULL, 0);
EXPECT_EQ(rtt_stats_.SmoothedRtt().Multiply(0.25),
loss_algorithm_.GetLossTimeout().Subtract(clock_.Now()));
unacked_packets_.NackPacket(1, 5);
VerifyLosses(2, NULL, 0);
clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25));
QuicPacketSequenceNumber lost[] = { 1 };
VerifyLosses(2, lost, arraysize(lost));
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
TEST_F(TimeLossAlgorithmTest, NoLossWithoutNack) {
const size_t kNumSentPackets = 10;
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.1));
}
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
unacked_packets_.SetNotPending(1);
VerifyLosses(1, NULL, 0);
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25));
VerifyLosses(1, NULL, 0);
clock_.AdvanceTime(rtt_stats_.SmoothedRtt());
VerifyLosses(1, NULL, 0);
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
TEST_F(TimeLossAlgorithmTest, MultipleLossesAtOnce) {
const size_t kNumSentPackets = 10;
for (size_t i = 1; i <= kNumSentPackets; ++i) {
SendDataPacket(i);
}
clock_.AdvanceTime(rtt_stats_.SmoothedRtt());
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
for (size_t i = 1; i < kNumSentPackets; ++i) {
unacked_packets_.NackPacket(i, 1);
}
unacked_packets_.SetNotPending(10);
VerifyLosses(10, NULL, 0);
EXPECT_EQ(rtt_stats_.SmoothedRtt().Multiply(0.25),
loss_algorithm_.GetLossTimeout().Subtract(clock_.Now()));
clock_.AdvanceTime(rtt_stats_.SmoothedRtt().Multiply(0.25));
QuicPacketSequenceNumber lost[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
VerifyLosses(10, lost, arraysize(lost));
EXPECT_EQ(QuicTime::Zero(), loss_algorithm_.GetLossTimeout());
}
}
}