Landing Recent QUIC changes until 06/17/2015 14:59.
[chromium-blink-merge.git] / net / quic / quic_ack_notifier_manager_test.cc
blob9e6641041cb1b14117433dbae486acdc2aff4962
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"
13 namespace net {
14 namespace test {
15 namespace {
17 // Test fixture for testing AckNotifierManager. Instantiates a manager and
18 // provides shared code for adding notifiers and verifying the contents of the
19 // manager.
20 class QuicAckNotifierManagerTest : public ::testing::Test {
21 protected:
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,
43 /*packet=*/nullptr,
44 /*entropy_hash=*/0,
45 retransmittable ? &frames : nullptr);
47 // Create and register a notifier. Normally, this would be created by
48 // QuicPacketGenerator.
49 QuicAckNotifier* notifier = new QuicAckNotifier(delegate_.get());
50 packet.notifiers.push_back(notifier);
52 // Ensure that exactly one packet is added.
53 const size_t old_packet_count = CountPackets();
55 // Actually add the packet.
56 manager_.OnSerializedPacket(packet);
58 // Ensure the change in the number of packets.
59 EXPECT_EQ(old_packet_count + 1, CountPackets());
63 // This test verifies that QuicAckNotifierManager can handle the trivial case of
64 // received packet notification.
65 TEST_F(QuicAckNotifierManagerTest, SimpleAck) {
66 AddPacket(1, true);
67 AddPacket(2, true);
69 EXPECT_CALL(*delegate_, OnAckNotification(0, 0, zero_)).Times(2);
70 manager_.OnPacketAcked(1, zero_);
71 EXPECT_EQ(1u, CountPackets());
72 manager_.OnPacketAcked(2, zero_);
73 EXPECT_EQ(0u, CountPackets());
76 // This test verifies that the QuicAckNotifierManager behaves correctly when
77 // there are many retransmissions.
78 TEST_F(QuicAckNotifierManagerTest, RepeatedRetransmission) {
79 AddPacket(1, true);
81 const size_t packet_size = kDefaultMaxPacketSize;
82 const size_t times_lost = 100;
83 const size_t total_size_lost = packet_size * times_lost;
84 const QuicPacketSequenceNumber last_packet = times_lost + 1;
86 // Retransmit the packet many times.
87 for (size_t sequence_number = 1; sequence_number < last_packet;
88 sequence_number++) {
89 manager_.OnPacketRetransmitted(sequence_number, sequence_number + 1,
90 packet_size);
91 EXPECT_EQ(1u, CountPackets());
94 // Finally get the packet acknowledged.
95 EXPECT_CALL(*delegate_, OnAckNotification(times_lost, total_size_lost, zero_))
96 .Times(1);
97 manager_.OnPacketAcked(last_packet, zero_);
98 EXPECT_EQ(0u, CountPackets());
101 } // namespace
102 } // namespace test
103 } // namespace net