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 // packet number must not exist in the map before.
38 void AddPacket(QuicPacketNumber packet_number
, bool retransmittable
) {
39 // Create a mock packet.
40 RetransmittableFrames
frames(ENCRYPTION_NONE
);
41 SerializedPacket
packet(packet_number
, PACKET_4BYTE_PACKET_NUMBER
,
44 retransmittable
? &frames
: nullptr,
46 /*has_stop_waiting=*/false);
48 // Create and register a notifier. Normally, this would be created by
49 // QuicPacketGenerator.
50 QuicAckNotifier
* notifier
= new QuicAckNotifier(delegate_
.get());
51 packet
.notifiers
.push_back(notifier
);
53 // Ensure that exactly one packet is added.
54 const size_t old_packet_count
= CountPackets();
56 // Actually add the packet.
57 manager_
.OnSerializedPacket(packet
);
59 // Ensure the change in the number of packets.
60 EXPECT_EQ(old_packet_count
+ 1, CountPackets());
64 // This test verifies that QuicAckNotifierManager can handle the trivial case of
65 // received packet notification.
66 TEST_F(QuicAckNotifierManagerTest
, SimpleAck
) {
70 EXPECT_CALL(*delegate_
, OnAckNotification(0, 0, zero_
)).Times(2);
71 manager_
.OnPacketAcked(1, zero_
);
72 EXPECT_EQ(1u, CountPackets());
73 manager_
.OnPacketAcked(2, zero_
);
74 EXPECT_EQ(0u, CountPackets());
76 manager_
.OnPacketRemoved(1);
77 manager_
.OnPacketRemoved(2);
78 EXPECT_EQ(0u, CountPackets());
81 // This test verifies that QuicAckNotifierManager can correctly handle the case
82 // when some of the packets are lost, which causes retransmission and removal
83 // from the unacked packet map.
84 TEST_F(QuicAckNotifierManagerTest
, AckWithLosses
) {
85 const size_t retransmitted_packet_size
= kDefaultMaxPacketSize
;
87 // Here, we simulate the following scenario:
88 // 1. We send packets 1 to 5, where only odd-numbered packets are
90 // 2. The peer acks 1, 2 and 5, but not 3 and 4.
91 // 3. We retransmit 3 as 6.
92 // 4. We remove 1 and 2, since we no longer care about them.
93 // 4. The peer acks 6.
94 // 5. We remove packets 3 to 6.
96 // Step 1: send five packets.
103 // Step 2: handle acks from peer.
104 EXPECT_CALL(*delegate_
, OnAckNotification(0, 0, zero_
)).Times(3);
105 manager_
.OnPacketAcked(1, zero_
);
106 EXPECT_EQ(4u, CountPackets());
107 manager_
.OnPacketAcked(2, zero_
);
108 EXPECT_EQ(3u, CountPackets());
109 manager_
.OnPacketAcked(5, zero_
);
110 EXPECT_EQ(2u, CountPackets());
112 // Step 3: retransmit 3 as 6.
113 manager_
.OnPacketRetransmitted(3, 6, retransmitted_packet_size
);
114 EXPECT_EQ(2u, CountPackets());
116 // Step 4: remove 1 and 2.
117 manager_
.OnPacketRemoved(1);
118 manager_
.OnPacketRemoved(2);
119 EXPECT_EQ(2u, CountPackets());
121 // Step 4: ack packet 6.
122 EXPECT_CALL(*delegate_
,
123 OnAckNotification(1, retransmitted_packet_size
, zero_
)).Times(1);
124 manager_
.OnPacketAcked(6, zero_
);
125 EXPECT_EQ(1u, CountPackets());
127 // Step 5: remove all packets. This causes packet 4 to be dropped from the
129 manager_
.OnPacketRemoved(3);
130 manager_
.OnPacketRemoved(4);
131 manager_
.OnPacketRemoved(5);
132 manager_
.OnPacketRemoved(6);
133 EXPECT_EQ(0u, CountPackets());
136 // This test verifies that the QuicAckNotifierManager behaves correctly when
137 // there are many retransmissions.
138 TEST_F(QuicAckNotifierManagerTest
, RepeatedRetransmission
) {
141 const size_t packet_size
= kDefaultMaxPacketSize
;
142 const size_t times_lost
= 100;
143 const size_t total_size_lost
= packet_size
* times_lost
;
144 const QuicPacketNumber last_packet
= times_lost
+ 1;
146 // Retransmit the packet many times.
147 for (size_t packet_number
= 1; packet_number
< last_packet
; packet_number
++) {
148 manager_
.OnPacketRetransmitted(packet_number
, packet_number
+ 1,
150 EXPECT_EQ(1u, CountPackets());
153 // Remove all lost packets.
154 for (QuicPacketNumber packet
= 1; packet
< last_packet
; packet
++) {
155 manager_
.OnPacketRemoved(packet
);
157 EXPECT_EQ(1u, CountPackets());
159 // Finally get the packet acknowledged.
160 EXPECT_CALL(*delegate_
, OnAckNotification(times_lost
, total_size_lost
, zero_
))
162 manager_
.OnPacketAcked(last_packet
, zero_
);
163 EXPECT_EQ(0u, CountPackets());
165 // Remove the last packet.
166 manager_
.OnPacketRemoved(last_packet
);
167 EXPECT_EQ(0u, CountPackets());