Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / tools / quic / quic_time_wait_list_manager_test.cc
blobd3b273883a9836e035c5b2c8784dd3bebc1eb85d
1 // Copyright 2013 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/tools/quic/quic_time_wait_list_manager.h"
7 #include <errno.h>
9 #include "net/quic/crypto/crypto_protocol.h"
10 #include "net/quic/crypto/null_encrypter.h"
11 #include "net/quic/crypto/quic_decrypter.h"
12 #include "net/quic/crypto/quic_encrypter.h"
13 #include "net/quic/quic_connection_helper.h"
14 #include "net/quic/quic_data_reader.h"
15 #include "net/quic/quic_flags.h"
16 #include "net/quic/quic_framer.h"
17 #include "net/quic/quic_packet_writer.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/quic/quic_utils.h"
20 #include "net/quic/test_tools/quic_test_utils.h"
21 #include "net/tools/quic/quic_epoll_connection_helper.h"
22 #include "net/tools/quic/test_tools/mock_epoll_server.h"
23 #include "net/tools/quic/test_tools/mock_quic_server_session_visitor.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using net::test::kTestPort;
28 using net::test::BuildUnsizedDataPacket;
29 using net::test::NoOpFramerVisitor;
30 using net::test::QuicVersionMax;
31 using net::test::QuicVersionMin;
32 using net::test::ValueRestore;
33 using net::test::MockPacketWriter;
35 using testing::Args;
36 using testing::Assign;
37 using testing::DoAll;
38 using testing::Matcher;
39 using testing::MatcherInterface;
40 using testing::NiceMock;
41 using testing::Return;
42 using testing::ReturnPointee;
43 using testing::SetArgPointee;
44 using testing::StrictMock;
45 using testing::Truly;
46 using testing::_;
48 namespace net {
49 namespace tools {
50 namespace test {
52 class FramerVisitorCapturingPublicReset : public NoOpFramerVisitor {
53 public:
54 FramerVisitorCapturingPublicReset() {}
55 ~FramerVisitorCapturingPublicReset() override {}
57 void OnPublicResetPacket(const QuicPublicResetPacket& public_reset) override {
58 public_reset_packet_ = public_reset;
61 const QuicPublicResetPacket public_reset_packet() {
62 return public_reset_packet_;
65 private:
66 QuicPublicResetPacket public_reset_packet_;
69 class QuicTimeWaitListManagerPeer {
70 public:
71 static bool ShouldSendResponse(QuicTimeWaitListManager* manager,
72 int received_packet_count) {
73 return manager->ShouldSendResponse(received_packet_count);
76 static QuicTime::Delta time_wait_period(QuicTimeWaitListManager* manager) {
77 return manager->time_wait_period_;
80 static QuicVersion GetQuicVersionFromConnectionId(
81 QuicTimeWaitListManager* manager,
82 QuicConnectionId connection_id) {
83 return manager->GetQuicVersionFromConnectionId(connection_id);
87 namespace {
89 class MockFakeTimeEpollServer : public FakeTimeEpollServer {
90 public:
91 MOCK_METHOD2(RegisterAlarm, void(int64 timeout_in_us,
92 EpollAlarmCallbackInterface* alarm));
95 class QuicTimeWaitListManagerTest : public ::testing::Test {
96 protected:
97 QuicTimeWaitListManagerTest()
98 : helper_(&epoll_server_),
99 time_wait_list_manager_(&writer_,
100 &visitor_,
101 &helper_,
102 QuicSupportedVersions()),
103 framer_(QuicSupportedVersions(),
104 QuicTime::Zero(),
105 Perspective::IS_SERVER),
106 connection_id_(45),
107 client_address_(net::test::TestPeerIPAddress(), kTestPort),
108 writer_is_blocked_(false) {}
110 ~QuicTimeWaitListManagerTest() override {}
112 void SetUp() override {
113 EXPECT_CALL(writer_, IsWriteBlocked())
114 .WillRepeatedly(ReturnPointee(&writer_is_blocked_));
115 EXPECT_CALL(writer_, IsWriteBlockedDataBuffered())
116 .WillRepeatedly(Return(false));
119 void AddConnectionId(QuicConnectionId connection_id) {
120 AddConnectionId(connection_id, QuicVersionMax(),
121 /*connection_rejected_statelessly=*/false, nullptr);
124 void AddStatelessConnectionId(QuicConnectionId connection_id) {
125 time_wait_list_manager_.AddConnectionIdToTimeWait(
126 connection_id, QuicVersionMax(),
127 /*connection_rejected_statelessly=*/true, nullptr);
130 void AddConnectionId(QuicConnectionId connection_id,
131 QuicVersion version,
132 bool connection_rejected_statelessly,
133 QuicEncryptedPacket* packet) {
134 time_wait_list_manager_.AddConnectionIdToTimeWait(
135 connection_id, version, connection_rejected_statelessly, packet);
138 bool IsConnectionIdInTimeWait(QuicConnectionId connection_id) {
139 return time_wait_list_manager_.IsConnectionIdInTimeWait(connection_id);
142 void ProcessPacket(QuicConnectionId connection_id,
143 QuicPacketSequenceNumber sequence_number) {
144 QuicEncryptedPacket packet(nullptr, 0);
145 time_wait_list_manager_.ProcessPacket(server_address_,
146 client_address_,
147 connection_id,
148 sequence_number,
149 packet);
152 QuicEncryptedPacket* ConstructEncryptedPacket(
153 EncryptionLevel level,
154 QuicConnectionId connection_id,
155 QuicPacketSequenceNumber sequence_number) {
156 QuicPacketHeader header;
157 header.public_header.connection_id = connection_id;
158 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
159 header.public_header.version_flag = false;
160 header.public_header.reset_flag = false;
161 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
162 header.packet_sequence_number = sequence_number;
163 header.entropy_flag = false;
164 header.entropy_hash = 0;
165 header.fec_flag = false;
166 header.is_in_fec_group = NOT_IN_FEC_GROUP;
167 header.fec_group = 0;
168 QuicStreamFrame stream_frame(1, false, 0, StringPiece("data"));
169 QuicFrame frame(&stream_frame);
170 QuicFrames frames;
171 frames.push_back(frame);
172 scoped_ptr<QuicPacket> packet(
173 BuildUnsizedDataPacket(&framer_, header, frames));
174 EXPECT_TRUE(packet != nullptr);
175 char buffer[kMaxPacketSize];
176 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPayload(
177 ENCRYPTION_NONE, sequence_number, *packet, buffer, kMaxPacketSize));
178 EXPECT_TRUE(encrypted != nullptr);
179 return encrypted->Clone();
182 NiceMock<MockFakeTimeEpollServer> epoll_server_;
183 QuicEpollConnectionHelper helper_;
184 StrictMock<MockPacketWriter> writer_;
185 StrictMock<MockQuicServerSessionVisitor> visitor_;
186 QuicTimeWaitListManager time_wait_list_manager_;
187 QuicFramer framer_;
188 QuicConnectionId connection_id_;
189 IPEndPoint server_address_;
190 IPEndPoint client_address_;
191 bool writer_is_blocked_;
194 class ValidatePublicResetPacketPredicate
195 : public MatcherInterface<const std::tr1::tuple<const char*, int> > {
196 public:
197 explicit ValidatePublicResetPacketPredicate(QuicConnectionId connection_id,
198 QuicPacketSequenceNumber number)
199 : connection_id_(connection_id), sequence_number_(number) {
202 bool MatchAndExplain(
203 const std::tr1::tuple<const char*, int> packet_buffer,
204 testing::MatchResultListener* /* listener */) const override {
205 FramerVisitorCapturingPublicReset visitor;
206 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(),
207 Perspective::IS_CLIENT);
208 framer.set_visitor(&visitor);
209 QuicEncryptedPacket encrypted(std::tr1::get<0>(packet_buffer),
210 std::tr1::get<1>(packet_buffer));
211 framer.ProcessPacket(encrypted);
212 QuicPublicResetPacket packet = visitor.public_reset_packet();
213 return connection_id_ == packet.public_header.connection_id &&
214 packet.public_header.reset_flag && !packet.public_header.version_flag &&
215 sequence_number_ == packet.rejected_sequence_number &&
216 net::test::TestPeerIPAddress() == packet.client_address.address() &&
217 kTestPort == packet.client_address.port();
220 void DescribeTo(::std::ostream* os) const override {}
222 void DescribeNegationTo(::std::ostream* os) const override {}
224 private:
225 QuicConnectionId connection_id_;
226 QuicPacketSequenceNumber sequence_number_;
229 Matcher<const std::tr1::tuple<const char*, int>> PublicResetPacketEq(
230 QuicConnectionId connection_id,
231 QuicPacketSequenceNumber sequence_number) {
232 return MakeMatcher(new ValidatePublicResetPacketPredicate(connection_id,
233 sequence_number));
236 TEST_F(QuicTimeWaitListManagerTest, CheckConnectionIdInTimeWait) {
237 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id_));
238 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_));
239 AddConnectionId(connection_id_);
240 EXPECT_EQ(1u, time_wait_list_manager_.num_connections());
241 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_));
244 TEST_F(QuicTimeWaitListManagerTest, CheckStatelessConnectionIdInTimeWait) {
245 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id_));
246 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_));
247 AddStatelessConnectionId(connection_id_);
248 EXPECT_EQ(1u, time_wait_list_manager_.num_connections());
249 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_));
252 TEST_F(QuicTimeWaitListManagerTest, SendConnectionClose) {
253 const size_t kConnectionCloseLength = 100;
254 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_));
255 AddConnectionId(connection_id_, QuicVersionMax(),
256 /*connection_rejected_statelessly=*/false,
257 new QuicEncryptedPacket(new char[kConnectionCloseLength],
258 kConnectionCloseLength, true));
259 const int kRandomSequenceNumber = 1;
260 EXPECT_CALL(writer_, WritePacket(_, kConnectionCloseLength,
261 server_address_.address(),
262 client_address_))
263 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 1)));
265 ProcessPacket(connection_id_, kRandomSequenceNumber);
268 TEST_F(QuicTimeWaitListManagerTest, SendPublicReset) {
269 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_));
270 AddConnectionId(connection_id_);
271 const int kRandomSequenceNumber = 1;
272 EXPECT_CALL(writer_, WritePacket(_, _,
273 server_address_.address(),
274 client_address_))
275 .With(Args<0, 1>(PublicResetPacketEq(connection_id_,
276 kRandomSequenceNumber)))
277 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 0)));
279 ProcessPacket(connection_id_, kRandomSequenceNumber);
282 TEST_F(QuicTimeWaitListManagerTest, SendPublicResetWithExponentialBackOff) {
283 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_));
284 AddConnectionId(connection_id_);
285 EXPECT_EQ(1u, time_wait_list_manager_.num_connections());
286 for (int sequence_number = 1; sequence_number < 101; ++sequence_number) {
287 if ((sequence_number & (sequence_number - 1)) == 0) {
288 EXPECT_CALL(writer_, WritePacket(_, _, _, _))
289 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 1)));
291 ProcessPacket(connection_id_, sequence_number);
292 // Send public reset with exponential back off.
293 if ((sequence_number & (sequence_number - 1)) == 0) {
294 EXPECT_TRUE(QuicTimeWaitListManagerPeer::ShouldSendResponse(
295 &time_wait_list_manager_, sequence_number));
296 } else {
297 EXPECT_FALSE(QuicTimeWaitListManagerPeer::ShouldSendResponse(
298 &time_wait_list_manager_, sequence_number));
303 TEST_F(QuicTimeWaitListManagerTest, NoPublicResetForStatelessConnections) {
304 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_));
305 AddStatelessConnectionId(connection_id_);
306 const int kRandomSequenceNumber = 1;
307 ProcessPacket(connection_id_, kRandomSequenceNumber);
310 TEST_F(QuicTimeWaitListManagerTest, CleanUpOldConnectionIds) {
311 const size_t kConnectionIdCount = 100;
312 const size_t kOldConnectionIdCount = 31;
314 // Add connection_ids such that their expiry time is time_wait_period_.
315 epoll_server_.set_now_in_usec(0);
316 for (size_t connection_id = 1; connection_id <= kOldConnectionIdCount;
317 ++connection_id) {
318 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id));
319 AddConnectionId(connection_id);
321 EXPECT_EQ(kOldConnectionIdCount, time_wait_list_manager_.num_connections());
323 // Add remaining connection_ids such that their add time is
324 // 2 * time_wait_period_.
325 const QuicTime::Delta time_wait_period =
326 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_);
327 epoll_server_.set_now_in_usec(time_wait_period.ToMicroseconds());
328 for (size_t connection_id = kOldConnectionIdCount + 1;
329 connection_id <= kConnectionIdCount; ++connection_id) {
330 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id));
331 AddConnectionId(connection_id);
333 EXPECT_EQ(kConnectionIdCount, time_wait_list_manager_.num_connections());
335 QuicTime::Delta offset = QuicTime::Delta::FromMicroseconds(39);
336 // Now set the current time as time_wait_period + offset usecs.
337 epoll_server_.set_now_in_usec(time_wait_period.Add(offset).ToMicroseconds());
338 // After all the old connection_ids are cleaned up, check the next alarm
339 // interval.
340 int64 next_alarm_time = epoll_server_.ApproximateNowInUsec() +
341 time_wait_period.Subtract(offset).ToMicroseconds();
342 EXPECT_CALL(epoll_server_, RegisterAlarm(next_alarm_time, _));
344 for (size_t connection_id = 1; connection_id <= kConnectionIdCount;
345 ++connection_id) {
346 if (connection_id <= kOldConnectionIdCount) {
347 EXPECT_CALL(visitor_, OnConnectionRemovedFromTimeWaitList(connection_id));
350 time_wait_list_manager_.CleanUpOldConnectionIds();
351 for (size_t connection_id = 1; connection_id <= kConnectionIdCount;
352 ++connection_id) {
353 EXPECT_EQ(connection_id > kOldConnectionIdCount,
354 IsConnectionIdInTimeWait(connection_id))
355 << "kOldConnectionIdCount: " << kOldConnectionIdCount
356 << " connection_id: " << connection_id;
358 EXPECT_EQ(kConnectionIdCount - kOldConnectionIdCount,
359 time_wait_list_manager_.num_connections());
362 TEST_F(QuicTimeWaitListManagerTest, SendQueuedPackets) {
363 QuicConnectionId connection_id = 1;
364 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id));
365 AddConnectionId(connection_id);
366 QuicPacketSequenceNumber sequence_number = 234;
367 scoped_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket(
368 ENCRYPTION_NONE, connection_id, sequence_number));
369 // Let first write through.
370 EXPECT_CALL(writer_, WritePacket(_, _,
371 server_address_.address(),
372 client_address_))
373 .With(Args<0, 1>(PublicResetPacketEq(connection_id,
374 sequence_number)))
375 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, packet->length())));
376 ProcessPacket(connection_id, sequence_number);
378 // write block for the next packet.
379 EXPECT_CALL(writer_, WritePacket(_, _,
380 server_address_.address(),
381 client_address_))
382 .With(Args<0, 1>(PublicResetPacketEq(connection_id,
383 sequence_number)))
384 .WillOnce(DoAll(
385 Assign(&writer_is_blocked_, true),
386 Return(WriteResult(WRITE_STATUS_BLOCKED, EAGAIN))));
387 EXPECT_CALL(visitor_, OnWriteBlocked(&time_wait_list_manager_));
388 ProcessPacket(connection_id, sequence_number);
389 // 3rd packet. No public reset should be sent;
390 ProcessPacket(connection_id, sequence_number);
392 // write packet should not be called since we are write blocked but the
393 // should be queued.
394 QuicConnectionId other_connection_id = 2;
395 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(other_connection_id));
396 AddConnectionId(other_connection_id);
397 QuicPacketSequenceNumber other_sequence_number = 23423;
398 scoped_ptr<QuicEncryptedPacket> other_packet(
399 ConstructEncryptedPacket(
400 ENCRYPTION_NONE, other_connection_id, other_sequence_number));
401 EXPECT_CALL(writer_, WritePacket(_, _, _, _))
402 .Times(0);
403 EXPECT_CALL(visitor_, OnWriteBlocked(&time_wait_list_manager_));
404 ProcessPacket(other_connection_id, other_sequence_number);
405 EXPECT_EQ(2u, time_wait_list_manager_.num_connections());
407 // Now expect all the write blocked public reset packets to be sent again.
408 writer_is_blocked_ = false;
409 EXPECT_CALL(writer_, WritePacket(_, _,
410 server_address_.address(),
411 client_address_))
412 .With(Args<0, 1>(PublicResetPacketEq(connection_id,
413 sequence_number)))
414 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, packet->length())));
415 EXPECT_CALL(writer_, WritePacket(_, _,
416 server_address_.address(),
417 client_address_))
418 .With(Args<0, 1>(PublicResetPacketEq(other_connection_id,
419 other_sequence_number)))
420 .WillOnce(Return(WriteResult(WRITE_STATUS_OK,
421 other_packet->length())));
422 time_wait_list_manager_.OnCanWrite();
425 TEST_F(QuicTimeWaitListManagerTest, GetQuicVersionFromMap) {
426 const int kConnectionId1 = 123;
427 const int kConnectionId2 = 456;
428 const int kConnectionId3 = 789;
430 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(kConnectionId1));
431 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(kConnectionId2));
432 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(kConnectionId3));
433 AddConnectionId(kConnectionId1, QuicVersionMin(),
434 /*connection_rejected_statelessly=*/false, nullptr);
435 AddConnectionId(kConnectionId2, QuicVersionMax(),
436 /*connection_rejected_statelessly=*/false, nullptr);
437 AddConnectionId(kConnectionId3, QuicVersionMax(),
438 /*connection_rejected_statelessly=*/false, nullptr);
440 EXPECT_EQ(QuicVersionMin(),
441 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId(
442 &time_wait_list_manager_, kConnectionId1));
443 EXPECT_EQ(QuicVersionMax(),
444 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId(
445 &time_wait_list_manager_, kConnectionId2));
446 EXPECT_EQ(QuicVersionMax(),
447 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId(
448 &time_wait_list_manager_, kConnectionId3));
451 TEST_F(QuicTimeWaitListManagerTest, AddConnectionIdTwice) {
452 // Add connection_ids such that their expiry time is time_wait_period_.
453 epoll_server_.set_now_in_usec(0);
454 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_));
455 AddConnectionId(connection_id_);
456 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_));
457 const size_t kConnectionCloseLength = 100;
458 AddConnectionId(connection_id_, QuicVersionMax(),
459 /*connection_rejected_statelessly=*/false,
460 new QuicEncryptedPacket(new char[kConnectionCloseLength],
461 kConnectionCloseLength, true));
462 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_));
463 EXPECT_EQ(1u, time_wait_list_manager_.num_connections());
465 EXPECT_CALL(writer_, WritePacket(_,
466 kConnectionCloseLength,
467 server_address_.address(),
468 client_address_))
469 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 1)));
471 const int kRandomSequenceNumber = 1;
472 ProcessPacket(connection_id_, kRandomSequenceNumber);
474 const QuicTime::Delta time_wait_period =
475 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_);
477 QuicTime::Delta offset = QuicTime::Delta::FromMicroseconds(39);
478 // Now set the current time as time_wait_period + offset usecs.
479 epoll_server_.set_now_in_usec(time_wait_period.Add(offset).ToMicroseconds());
480 // After the connection_ids are cleaned up, check the next alarm interval.
481 int64 next_alarm_time = epoll_server_.ApproximateNowInUsec() +
482 time_wait_period.ToMicroseconds();
484 EXPECT_CALL(epoll_server_, RegisterAlarm(next_alarm_time, _));
485 EXPECT_CALL(visitor_, OnConnectionRemovedFromTimeWaitList(connection_id_));
486 time_wait_list_manager_.CleanUpOldConnectionIds();
487 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id_));
488 EXPECT_EQ(0u, time_wait_list_manager_.num_connections());
491 TEST_F(QuicTimeWaitListManagerTest, ConnectionIdsOrderedByTime) {
492 // Simple randomization: the values of connection_ids are swapped based on the
493 // current seconds on the clock. If the container is broken, the test will be
494 // 50% flaky.
495 int odd_second = static_cast<int>(epoll_server_.ApproximateNowInUsec()) % 2;
496 EXPECT_TRUE(odd_second == 0 || odd_second == 1);
497 const QuicConnectionId connection_id1 = odd_second;
498 const QuicConnectionId connection_id2 = 1 - odd_second;
500 // 1 will hash lower than 2, but we add it later. They should come out in the
501 // add order, not hash order.
502 epoll_server_.set_now_in_usec(0);
503 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id1));
504 AddConnectionId(connection_id1);
505 epoll_server_.set_now_in_usec(10);
506 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id2));
507 AddConnectionId(connection_id2);
508 EXPECT_EQ(2u, time_wait_list_manager_.num_connections());
510 const QuicTime::Delta time_wait_period =
511 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_);
512 epoll_server_.set_now_in_usec(time_wait_period.ToMicroseconds() + 1);
514 EXPECT_CALL(epoll_server_, RegisterAlarm(_, _));
516 EXPECT_CALL(visitor_, OnConnectionRemovedFromTimeWaitList(connection_id1));
517 time_wait_list_manager_.CleanUpOldConnectionIds();
518 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id1));
519 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id2));
520 EXPECT_EQ(1u, time_wait_list_manager_.num_connections());
523 TEST_F(QuicTimeWaitListManagerTest, MaxConnectionsTest) {
524 // Basically, shut off time-based eviction.
525 FLAGS_quic_time_wait_list_seconds = 10000000000;
526 FLAGS_quic_time_wait_list_max_connections = 5;
528 QuicConnectionId current_connection_id = 0;
529 // Add exactly the maximum number of connections
530 for (int64 i = 0; i < FLAGS_quic_time_wait_list_max_connections; ++i) {
531 ++current_connection_id;
532 EXPECT_FALSE(IsConnectionIdInTimeWait(current_connection_id));
533 EXPECT_CALL(visitor_,
534 OnConnectionAddedToTimeWaitList(current_connection_id));
535 AddConnectionId(current_connection_id);
536 EXPECT_EQ(current_connection_id, time_wait_list_manager_.num_connections());
537 EXPECT_TRUE(IsConnectionIdInTimeWait(current_connection_id));
540 // Now keep adding. Since we're already at the max, every new connection-id
541 // will evict the oldest one.
542 for (int64 i = 0; i < FLAGS_quic_time_wait_list_max_connections; ++i) {
543 ++current_connection_id;
544 const QuicConnectionId id_to_evict =
545 current_connection_id - FLAGS_quic_time_wait_list_max_connections;
546 EXPECT_TRUE(IsConnectionIdInTimeWait(id_to_evict));
547 EXPECT_FALSE(IsConnectionIdInTimeWait(current_connection_id));
548 EXPECT_CALL(visitor_, OnConnectionRemovedFromTimeWaitList(id_to_evict));
549 EXPECT_CALL(visitor_,
550 OnConnectionAddedToTimeWaitList(current_connection_id));
551 AddConnectionId(current_connection_id);
552 EXPECT_EQ(static_cast<size_t>(FLAGS_quic_time_wait_list_max_connections),
553 time_wait_list_manager_.num_connections());
554 EXPECT_FALSE(IsConnectionIdInTimeWait(id_to_evict));
555 EXPECT_TRUE(IsConnectionIdInTimeWait(current_connection_id));
559 } // namespace
560 } // namespace test
561 } // namespace tools
562 } // namespace net