1 // Copyright (c) 2015 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_ack_notifier_manager.h"
7 #include "net/quic/quic_ack_notifier.h"
8 #include "net/quic/quic_protocol.h"
9 #include "net/quic/test_tools/quic_ack_notifier_manager_peer.h"
10 #include "net/quic/test_tools/quic_test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // Test fixture for testing AckNotifierManager. Instantiates a manager and
18 // provides shared code for adding notifiers and verifying the contents of the
20 class QuicAckNotifierManagerTest
: public ::testing::Test
{
22 AckNotifierManager manager_
;
23 scoped_refptr
<MockAckNotifierDelegate
> delegate_
;
24 QuicTime::Delta zero_
;
26 QuicAckNotifierManagerTest() : zero_(QuicTime::Delta::Zero()) {
27 delegate_
= new MockAckNotifierDelegate
;
30 ~QuicAckNotifierManagerTest() override
{}
32 size_t CountPackets() const {
33 return AckNotifierManagerPeer::GetNumberOfRegisteredPackets(&manager_
);
36 // Add a mock packet with specified parameters. The packet with given
37 // sequence number must not exist in the map before.
38 void AddPacket(QuicPacketSequenceNumber sequence_number
,
39 bool retransmittable
) {
40 // Create a mock packet.
41 RetransmittableFrames
frames(ENCRYPTION_NONE
);
42 SerializedPacket
packet(sequence_number
, PACKET_4BYTE_SEQUENCE_NUMBER
,
45 retransmittable
? &frames
: nullptr,
47 /*has_stop_waiting=*/false);
49 // Create and register a notifier. Normally, this would be created by
50 // QuicPacketGenerator.
51 QuicAckNotifier
* notifier
= new QuicAckNotifier(delegate_
.get());
52 packet
.notifiers
.push_back(notifier
);
54 // Ensure that exactly one packet is added.
55 const size_t old_packet_count
= CountPackets();
57 // Actually add the packet.
58 manager_
.OnSerializedPacket(packet
);
60 // Ensure the change in the number of packets.
61 EXPECT_EQ(old_packet_count
+ 1, CountPackets());
65 // This test verifies that QuicAckNotifierManager can handle the trivial case of
66 // received packet notification.
67 TEST_F(QuicAckNotifierManagerTest
, SimpleAck
) {
71 EXPECT_CALL(*delegate_
, OnAckNotification(0, 0, zero_
)).Times(2);
72 manager_
.OnPacketAcked(1, zero_
);
73 EXPECT_EQ(1u, CountPackets());
74 manager_
.OnPacketAcked(2, zero_
);
75 EXPECT_EQ(0u, CountPackets());
77 manager_
.OnPacketRemoved(1);
78 manager_
.OnPacketRemoved(2);
79 EXPECT_EQ(0u, CountPackets());
82 // This test verifies that QuicAckNotifierManager can correctly handle the case
83 // when some of the packets are lost, which causes retransmission and removal
84 // from the unacked packet map.
85 TEST_F(QuicAckNotifierManagerTest
, AckWithLosses
) {
86 const size_t retransmitted_packet_size
= kDefaultMaxPacketSize
;
88 // Here, we simulate the following scenario:
89 // 1. We send packets 1 to 5, where only odd-numbered packets are
91 // 2. The peer acks 1, 2 and 5, but not 3 and 4.
92 // 3. We retransmit 3 as 6.
93 // 4. We remove 1 and 2, since we no longer care about them.
94 // 4. The peer acks 6.
95 // 5. We remove packets 3 to 6.
97 // Step 1: send five packets.
104 // Step 2: handle acks from peer.
105 EXPECT_CALL(*delegate_
, OnAckNotification(0, 0, zero_
)).Times(3);
106 manager_
.OnPacketAcked(1, zero_
);
107 EXPECT_EQ(4u, CountPackets());
108 manager_
.OnPacketAcked(2, zero_
);
109 EXPECT_EQ(3u, CountPackets());
110 manager_
.OnPacketAcked(5, zero_
);
111 EXPECT_EQ(2u, CountPackets());
113 // Step 3: retransmit 3 as 6.
114 manager_
.OnPacketRetransmitted(3, 6, retransmitted_packet_size
);
115 EXPECT_EQ(2u, CountPackets());
117 // Step 4: remove 1 and 2.
118 manager_
.OnPacketRemoved(1);
119 manager_
.OnPacketRemoved(2);
120 EXPECT_EQ(2u, CountPackets());
122 // Step 4: ack packet 6.
123 EXPECT_CALL(*delegate_
,
124 OnAckNotification(1, retransmitted_packet_size
, zero_
)).Times(1);
125 manager_
.OnPacketAcked(6, zero_
);
126 EXPECT_EQ(1u, CountPackets());
128 // Step 5: remove all packets. This causes packet 4 to be dropped from the
130 manager_
.OnPacketRemoved(3);
131 manager_
.OnPacketRemoved(4);
132 manager_
.OnPacketRemoved(5);
133 manager_
.OnPacketRemoved(6);
134 EXPECT_EQ(0u, CountPackets());
137 // This test verifies that the QuicAckNotifierManager behaves correctly when
138 // there are many retransmissions.
139 TEST_F(QuicAckNotifierManagerTest
, RepeatedRetransmission
) {
142 const size_t packet_size
= kDefaultMaxPacketSize
;
143 const size_t times_lost
= 100;
144 const size_t total_size_lost
= packet_size
* times_lost
;
145 const QuicPacketSequenceNumber last_packet
= times_lost
+ 1;
147 // Retransmit the packet many times.
148 for (size_t sequence_number
= 1; sequence_number
< last_packet
;
150 manager_
.OnPacketRetransmitted(sequence_number
, sequence_number
+ 1,
152 EXPECT_EQ(1u, CountPackets());
155 // Remove all lost packets.
156 for (QuicPacketSequenceNumber packet
= 1; packet
< last_packet
; packet
++) {
157 manager_
.OnPacketRemoved(packet
);
159 EXPECT_EQ(1u, CountPackets());
161 // Finally get the packet acknowledged.
162 EXPECT_CALL(*delegate_
, OnAckNotification(times_lost
, total_size_lost
, zero_
))
164 manager_
.OnPacketAcked(last_packet
, zero_
);
165 EXPECT_EQ(0u, CountPackets());
167 // Remove the last packet.
168 manager_
.OnPacketRemoved(last_packet
);
169 EXPECT_EQ(0u, CountPackets());