OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_buffered_packet_store.h" | 5 #include "net/quic/quic_buffered_packet_store.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 BufferedPacketList last_expired_packet_queue_; | 52 BufferedPacketList last_expired_packet_queue_; |
53 }; | 53 }; |
54 | 54 |
55 class QuicBufferedPacketStoreTest : public ::testing::Test { | 55 class QuicBufferedPacketStoreTest : public ::testing::Test { |
56 public: | 56 public: |
57 QuicBufferedPacketStoreTest() | 57 QuicBufferedPacketStoreTest() |
58 : store_(&visitor_, &clock_, &alarm_factory_), | 58 : store_(&visitor_, &clock_, &alarm_factory_), |
59 server_address_(Loopback6(), 65535), | 59 server_address_(Loopback6(), 65535), |
60 client_address_(Loopback6(), 65535), | 60 client_address_(Loopback6(), 65535), |
61 packet_content_("some encrypted content"), | 61 packet_content_("some encrypted content"), |
62 packet_time_( | 62 packet_time_(QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(42)), |
63 QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(42))), | |
64 data_packet_(packet_content_.data(), | 63 data_packet_(packet_content_.data(), |
65 packet_content_.size(), | 64 packet_content_.size(), |
66 packet_time_) {} | 65 packet_time_) {} |
67 | 66 |
68 protected: | 67 protected: |
69 QuicBufferedPacketStoreVisitor visitor_; | 68 QuicBufferedPacketStoreVisitor visitor_; |
70 MockClock clock_; | 69 MockClock clock_; |
71 MockAlarmFactory alarm_factory_; | 70 MockAlarmFactory alarm_factory_; |
72 QuicBufferedPacketStore store_; | 71 QuicBufferedPacketStore store_; |
73 IPEndPoint server_address_; | 72 IPEndPoint server_address_; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 client_address_); | 182 client_address_); |
184 // Packet for another connection arrive 1ms later. | 183 // Packet for another connection arrive 1ms later. |
185 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); | 184 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); |
186 QuicConnectionId connection_id2 = 2; | 185 QuicConnectionId connection_id2 = 2; |
187 // Use different client address to differetiate packets from different | 186 // Use different client address to differetiate packets from different |
188 // connections. | 187 // connections. |
189 IPEndPoint another_client_address(Loopback4(), 255); | 188 IPEndPoint another_client_address(Loopback4(), 255); |
190 store_.EnqueuePacket(connection_id2, data_packet_, server_address_, | 189 store_.EnqueuePacket(connection_id2, data_packet_, server_address_, |
191 another_client_address); | 190 another_client_address); |
192 // Advance clock to the time when connection 1 expires. | 191 // Advance clock to the time when connection 1 expires. |
193 clock_.AdvanceTime(QuicBufferedPacketStorePeer::expiration_alarm(&store_) | 192 clock_.AdvanceTime( |
194 ->deadline() | 193 QuicBufferedPacketStorePeer::expiration_alarm(&store_)->deadline() - |
195 .Subtract(clock_.ApproximateNow())); | 194 clock_.ApproximateNow()); |
196 ASSERT_GE(clock_.ApproximateNow(), | 195 ASSERT_GE(clock_.ApproximateNow(), |
197 QuicBufferedPacketStorePeer::expiration_alarm(&store_)->deadline()); | 196 QuicBufferedPacketStorePeer::expiration_alarm(&store_)->deadline()); |
198 // Fire alarm to remove long-staying connection 1 packets. | 197 // Fire alarm to remove long-staying connection 1 packets. |
199 alarm_factory_.FireAlarm( | 198 alarm_factory_.FireAlarm( |
200 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); | 199 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); |
201 EXPECT_EQ(1u, visitor_.last_expired_packet_queue_.buffered_packets.size()); | 200 EXPECT_EQ(1u, visitor_.last_expired_packet_queue_.buffered_packets.size()); |
202 // Try to deliver packets, but packet queue has been removed so no | 201 // Try to deliver packets, but packet queue has been removed so no |
203 // packets can be returned. | 202 // packets can be returned. |
204 ASSERT_EQ(0u, store_.DeliverPackets(connection_id).size()); | 203 ASSERT_EQ(0u, store_.DeliverPackets(connection_id).size()); |
205 | 204 |
206 // Deliver packets on connection 2. And the queue for connection 2 should be | 205 // Deliver packets on connection 2. And the queue for connection 2 should be |
207 // returned. | 206 // returned. |
208 list<BufferedPacket> queue = store_.DeliverPackets(connection_id2); | 207 list<BufferedPacket> queue = store_.DeliverPackets(connection_id2); |
209 ASSERT_EQ(1u, queue.size()); | 208 ASSERT_EQ(1u, queue.size()); |
210 // Packets in connection 2 should use another client address. | 209 // Packets in connection 2 should use another client address. |
211 EXPECT_EQ(another_client_address, queue.front().client_address); | 210 EXPECT_EQ(another_client_address, queue.front().client_address); |
212 | 211 |
213 // Test the alarm is reset by enqueueing 2 packets for 3rd connection and wait | 212 // Test the alarm is reset by enqueueing 2 packets for 3rd connection and wait |
214 // for them to expire. | 213 // for them to expire. |
215 QuicConnectionId connection_id3 = 3; | 214 QuicConnectionId connection_id3 = 3; |
216 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, | 215 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, |
217 client_address_); | 216 client_address_); |
218 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, | 217 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, |
219 client_address_); | 218 client_address_); |
220 clock_.AdvanceTime(QuicBufferedPacketStorePeer::expiration_alarm(&store_) | 219 clock_.AdvanceTime( |
221 ->deadline() | 220 QuicBufferedPacketStorePeer::expiration_alarm(&store_)->deadline() - |
222 .Subtract(clock_.ApproximateNow())); | 221 clock_.ApproximateNow()); |
223 alarm_factory_.FireAlarm( | 222 alarm_factory_.FireAlarm( |
224 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); | 223 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); |
225 // |last_expired_packet_queue_| should be updated. | 224 // |last_expired_packet_queue_| should be updated. |
226 EXPECT_EQ(2u, visitor_.last_expired_packet_queue_.buffered_packets.size()); | 225 EXPECT_EQ(2u, visitor_.last_expired_packet_queue_.buffered_packets.size()); |
227 } | 226 } |
228 | 227 |
229 } // namespace | 228 } // namespace |
230 } // namespace test | 229 } // namespace test |
231 } // namespace net | 230 } // namespace net |
OLD | NEW |