Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_unacked_packet_map_test.cc
blobc5264fd16e906db066c02a598f6411dde8b5b1ce
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
3 // found in the LICENSE file.
5 #include "net/quic/quic_unacked_packet_map.h"
7 #include "net/quic/test_tools/quic_test_utils.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace net {
11 namespace test {
12 namespace {
14 // Default packet length.
15 const uint32 kDefaultAckLength = 50;
16 const uint32 kDefaultLength = 1000;
18 class QuicUnackedPacketMapTest : public ::testing::Test {
19 protected:
20 QuicUnackedPacketMapTest()
21 : now_(QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(1000))) {
24 SerializedPacket CreateRetransmittablePacket(
25 QuicPacketSequenceNumber sequence_number) {
26 return SerializedPacket(sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, NULL,
27 0, new RetransmittableFrames());
30 SerializedPacket CreateNonRetransmittablePacket(
31 QuicPacketSequenceNumber sequence_number) {
32 return SerializedPacket(
33 sequence_number, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL);
36 void VerifyPendingPackets(QuicPacketSequenceNumber* packets,
37 size_t num_packets) {
38 if (num_packets == 0) {
39 EXPECT_FALSE(unacked_packets_.HasInFlightPackets());
40 EXPECT_FALSE(unacked_packets_.HasMultipleInFlightPackets());
41 return;
43 if (num_packets == 1) {
44 EXPECT_TRUE(unacked_packets_.HasInFlightPackets());
45 EXPECT_FALSE(unacked_packets_.HasMultipleInFlightPackets());
47 for (size_t i = 0; i < num_packets; ++i) {
48 ASSERT_TRUE(unacked_packets_.IsUnacked(packets[i]));
49 EXPECT_TRUE(unacked_packets_.GetTransmissionInfo(packets[i]).in_flight);
53 void VerifyUnackedPackets(QuicPacketSequenceNumber* packets,
54 size_t num_packets) {
55 if (num_packets == 0) {
56 EXPECT_FALSE(unacked_packets_.HasUnackedPackets());
57 EXPECT_FALSE(unacked_packets_.HasUnackedRetransmittableFrames());
58 return;
60 EXPECT_TRUE(unacked_packets_.HasUnackedPackets());
61 for (size_t i = 0; i < num_packets; ++i) {
62 EXPECT_TRUE(unacked_packets_.IsUnacked(packets[i])) << packets[i];
66 void VerifyRetransmittablePackets(QuicPacketSequenceNumber* packets,
67 size_t num_packets) {
68 size_t num_retransmittable_packets = 0;
69 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
70 it != unacked_packets_.end(); ++it) {
71 if (it->second.retransmittable_frames != NULL) {
72 ++num_retransmittable_packets;
75 EXPECT_EQ(num_packets, num_retransmittable_packets);
76 for (size_t i = 0; i < num_packets; ++i) {
77 EXPECT_TRUE(unacked_packets_.HasRetransmittableFrames(packets[i]))
78 << " packets[" << i << "]:" << packets[i];
82 QuicUnackedPacketMap unacked_packets_;
83 QuicTime now_;
86 TEST_F(QuicUnackedPacketMapTest, RttOnly) {
87 // Acks are only tracked for RTT measurement purposes.
88 unacked_packets_.AddPacket(CreateNonRetransmittablePacket(1));
89 unacked_packets_.SetSent(1, now_, kDefaultAckLength, false);
91 QuicPacketSequenceNumber unacked[] = { 1 };
92 VerifyUnackedPackets(unacked, arraysize(unacked));
93 VerifyPendingPackets(NULL, 0);
94 VerifyRetransmittablePackets(NULL, 0);
96 unacked_packets_.IncreaseLargestObserved(1);
97 VerifyUnackedPackets(NULL, 0);
98 VerifyPendingPackets(NULL, 0);
99 VerifyRetransmittablePackets(NULL, 0);
102 TEST_F(QuicUnackedPacketMapTest, RetransmittableInflightAndRtt) {
103 // Simulate a retransmittable packet being sent and acked.
104 unacked_packets_.AddPacket(CreateRetransmittablePacket(1));
105 unacked_packets_.SetSent(1, now_, kDefaultLength, true);
107 QuicPacketSequenceNumber unacked[] = { 1 };
108 VerifyUnackedPackets(unacked, arraysize(unacked));
109 VerifyPendingPackets(unacked, arraysize(unacked));
110 VerifyRetransmittablePackets(unacked, arraysize(unacked));
112 unacked_packets_.RemoveRetransmittability(1);
113 VerifyUnackedPackets(unacked, arraysize(unacked));
114 VerifyPendingPackets(unacked, arraysize(unacked));
115 VerifyRetransmittablePackets(NULL, 0);
117 unacked_packets_.IncreaseLargestObserved(1);
118 VerifyUnackedPackets(unacked, arraysize(unacked));
119 VerifyPendingPackets(unacked, arraysize(unacked));
120 VerifyRetransmittablePackets(NULL, 0);
122 unacked_packets_.RemoveFromInFlight(1);
123 VerifyUnackedPackets(NULL, 0);
124 VerifyPendingPackets(NULL, 0);
125 VerifyRetransmittablePackets(NULL, 0);
128 TEST_F(QuicUnackedPacketMapTest, RetransmittedPacket) {
129 // Simulate a retransmittable packet being sent, retransmitted, and the first
130 // transmission being acked.
131 unacked_packets_.AddPacket(CreateRetransmittablePacket(1));
132 unacked_packets_.SetSent(1, now_, kDefaultLength, true);
133 unacked_packets_.OnRetransmittedPacket(1, 2, LOSS_RETRANSMISSION);
134 unacked_packets_.SetSent(2, now_, kDefaultLength, true);
136 QuicPacketSequenceNumber unacked[] = { 1, 2 };
137 VerifyUnackedPackets(unacked, arraysize(unacked));
138 VerifyPendingPackets(unacked, arraysize(unacked));
139 QuicPacketSequenceNumber retransmittable[] = { 2 };
140 VerifyRetransmittablePackets(retransmittable, arraysize(retransmittable));
142 unacked_packets_.RemoveRetransmittability(1);
143 VerifyUnackedPackets(unacked, arraysize(unacked));
144 VerifyPendingPackets(unacked, arraysize(unacked));
145 VerifyRetransmittablePackets(NULL, 0);
147 unacked_packets_.IncreaseLargestObserved(2);
148 VerifyUnackedPackets(unacked, arraysize(unacked));
149 VerifyPendingPackets(unacked, arraysize(unacked));
150 VerifyRetransmittablePackets(NULL, 0);
152 unacked_packets_.RemoveFromInFlight(2);
153 QuicPacketSequenceNumber unacked2[] = { 1 };
154 VerifyUnackedPackets(unacked, arraysize(unacked2));
155 VerifyPendingPackets(unacked, arraysize(unacked2));
156 VerifyRetransmittablePackets(NULL, 0);
158 unacked_packets_.RemoveFromInFlight(1);
159 VerifyUnackedPackets(NULL, 0);
160 VerifyPendingPackets(NULL, 0);
161 VerifyRetransmittablePackets(NULL, 0);
164 } // namespace
165 } // namespace test
166 } // namespace net