Snap pinch zoom gestures near the screen edge.
[chromium-blink-merge.git] / net / quic / quic_packet_generator_test.cc
blobbf601e6d12cbb2013ec344d6653028b59e816b0f
1 // Copyright (c) 2012 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_packet_generator.h"
7 #include <string>
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_flags.h"
14 #include "net/quic/quic_utils.h"
15 #include "net/quic/test_tools/quic_packet_creator_peer.h"
16 #include "net/quic/test_tools/quic_packet_generator_peer.h"
17 #include "net/quic/test_tools/quic_test_utils.h"
18 #include "net/quic/test_tools/simple_quic_framer.h"
19 #include "net/test/gtest_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using base::StringPiece;
24 using std::string;
25 using std::vector;
26 using testing::InSequence;
27 using testing::Return;
28 using testing::SaveArg;
29 using testing::StrictMock;
30 using testing::_;
32 namespace net {
33 namespace test {
34 namespace {
36 const int64 kMinFecTimeoutMs = 5u;
38 static const FecSendPolicy kFecSendPolicyList[] = {
39 FEC_ANY_TRIGGER,
40 FEC_ALARM_TRIGGER,
43 class MockDelegate : public QuicPacketGenerator::DelegateInterface {
44 public:
45 MockDelegate() {}
46 ~MockDelegate() override {}
48 MOCK_METHOD2(ShouldGeneratePacket,
49 bool(HasRetransmittableData retransmittable,
50 IsHandshake handshake));
51 MOCK_METHOD1(PopulateAckFrame, void(QuicAckFrame*));
52 MOCK_METHOD1(PopulateStopWaitingFrame, void(QuicStopWaitingFrame*));
53 MOCK_METHOD1(OnSerializedPacket, void(const SerializedPacket& packet));
54 MOCK_METHOD2(CloseConnection, void(QuicErrorCode, bool));
55 MOCK_METHOD0(OnResetFecGroup, void());
57 void SetCanWriteAnything() {
58 EXPECT_CALL(*this, ShouldGeneratePacket(_, _)).WillRepeatedly(Return(true));
59 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _))
60 .WillRepeatedly(Return(true));
63 void SetCanNotWrite() {
64 EXPECT_CALL(*this, ShouldGeneratePacket(_, _))
65 .WillRepeatedly(Return(false));
66 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _))
67 .WillRepeatedly(Return(false));
70 // Use this when only ack frames should be allowed to be written.
71 void SetCanWriteOnlyNonRetransmittable() {
72 EXPECT_CALL(*this, ShouldGeneratePacket(_, _))
73 .WillRepeatedly(Return(false));
74 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _))
75 .WillRepeatedly(Return(true));
78 private:
79 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
82 // Simple struct for describing the contents of a packet.
83 // Useful in conjunction with a SimpleQuicFrame for validating that a packet
84 // contains the expected frames.
85 struct PacketContents {
86 PacketContents()
87 : num_ack_frames(0),
88 num_connection_close_frames(0),
89 num_goaway_frames(0),
90 num_rst_stream_frames(0),
91 num_stop_waiting_frames(0),
92 num_stream_frames(0),
93 fec_group(0) {
96 size_t num_ack_frames;
97 size_t num_connection_close_frames;
98 size_t num_goaway_frames;
99 size_t num_rst_stream_frames;
100 size_t num_stop_waiting_frames;
101 size_t num_stream_frames;
103 QuicFecGroupNumber fec_group;
106 } // namespace
108 class QuicPacketGeneratorTest : public ::testing::TestWithParam<FecSendPolicy> {
109 public:
110 QuicPacketGeneratorTest()
111 : framer_(QuicSupportedVersions(),
112 QuicTime::Zero(),
113 Perspective::IS_CLIENT),
114 generator_(42, &framer_, &random_, &delegate_),
115 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_)) {
116 generator_.set_fec_send_policy(GetParam());
119 ~QuicPacketGeneratorTest() override {
120 for (SerializedPacket& packet : packets_) {
121 delete packet.packet;
122 delete packet.retransmittable_frames;
126 void SavePacket(const SerializedPacket& packet) {
127 packets_.push_back(packet);
128 ASSERT_FALSE(packet.packet->owns_buffer());
129 scoped_ptr<QuicEncryptedPacket> encrypted_deleter(packets_.back().packet);
130 packets_.back().packet = packets_.back().packet->Clone();
133 protected:
134 QuicRstStreamFrame* CreateRstStreamFrame() {
135 return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR, 0);
138 QuicGoAwayFrame* CreateGoAwayFrame() {
139 return new QuicGoAwayFrame(QUIC_NO_ERROR, 1, string());
142 void CheckPacketContains(const PacketContents& contents,
143 size_t packet_index) {
144 ASSERT_GT(packets_.size(), packet_index);
145 const SerializedPacket& packet = packets_[packet_index];
146 size_t num_retransmittable_frames = contents.num_connection_close_frames +
147 contents.num_goaway_frames + contents.num_rst_stream_frames +
148 contents.num_stream_frames;
149 size_t num_frames = contents.num_ack_frames +
150 contents.num_stop_waiting_frames +
151 num_retransmittable_frames;
153 if (num_retransmittable_frames == 0) {
154 ASSERT_TRUE(packet.retransmittable_frames == nullptr);
155 } else {
156 ASSERT_TRUE(packet.retransmittable_frames != nullptr);
157 EXPECT_EQ(num_retransmittable_frames,
158 packet.retransmittable_frames->frames().size());
161 ASSERT_TRUE(packet.packet != nullptr);
162 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
163 EXPECT_EQ(num_frames, simple_framer_.num_frames());
164 EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size());
165 EXPECT_EQ(contents.num_connection_close_frames,
166 simple_framer_.connection_close_frames().size());
167 EXPECT_EQ(contents.num_goaway_frames,
168 simple_framer_.goaway_frames().size());
169 EXPECT_EQ(contents.num_rst_stream_frames,
170 simple_framer_.rst_stream_frames().size());
171 EXPECT_EQ(contents.num_stream_frames,
172 simple_framer_.stream_frames().size());
173 EXPECT_EQ(contents.num_stop_waiting_frames,
174 simple_framer_.stop_waiting_frames().size());
175 EXPECT_EQ(contents.fec_group, simple_framer_.header().fec_group);
178 void CheckPacketHasSingleStreamFrame(size_t packet_index) {
179 ASSERT_GT(packets_.size(), packet_index);
180 const SerializedPacket& packet = packets_[packet_index];
181 ASSERT_TRUE(packet.retransmittable_frames != nullptr);
182 EXPECT_EQ(1u, packet.retransmittable_frames->frames().size());
183 ASSERT_TRUE(packet.packet != nullptr);
184 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
185 EXPECT_EQ(1u, simple_framer_.num_frames());
186 EXPECT_EQ(1u, simple_framer_.stream_frames().size());
189 void CheckAllPacketsHaveSingleStreamFrame() {
190 for (size_t i = 0; i < packets_.size(); i++) {
191 CheckPacketHasSingleStreamFrame(i);
195 void CheckPacketIsFec(size_t packet_index,
196 QuicPacketSequenceNumber fec_group) {
197 ASSERT_GT(packets_.size(), packet_index);
198 const SerializedPacket& packet = packets_[packet_index];
199 ASSERT_TRUE(packet.retransmittable_frames == nullptr);
200 ASSERT_TRUE(packet.packet != nullptr);
201 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
202 EXPECT_TRUE(simple_framer_.header().fec_flag);
203 EXPECT_EQ(fec_group, simple_framer_.fec_data().fec_group);
206 QuicIOVector CreateData(size_t len) {
207 data_array_.reset(new char[len]);
208 memset(data_array_.get(), '?', len);
209 iov_.iov_base = data_array_.get();
210 iov_.iov_len = len;
211 return QuicIOVector(&iov_, 1, len);
214 QuicIOVector MakeIOVector(StringPiece s) {
215 return ::net::MakeIOVector(s, &iov_);
218 QuicFramer framer_;
219 MockRandom random_;
220 StrictMock<MockDelegate> delegate_;
221 QuicPacketGenerator generator_;
222 QuicPacketCreator* creator_;
223 SimpleQuicFramer simple_framer_;
224 vector<SerializedPacket> packets_;
226 private:
227 scoped_ptr<char[]> data_array_;
228 struct iovec iov_;
231 class MockDebugDelegate : public QuicPacketGenerator::DebugDelegate {
232 public:
233 MOCK_METHOD1(OnFrameAddedToPacket,
234 void(const QuicFrame&));
237 // Run all end to end tests with all supported FEC send polocies.
238 INSTANTIATE_TEST_CASE_P(FecSendPolicy,
239 QuicPacketGeneratorTest,
240 ::testing::ValuesIn(kFecSendPolicyList));
242 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_NotWritable) {
243 delegate_.SetCanNotWrite();
245 generator_.SetShouldSendAck(false);
246 EXPECT_TRUE(generator_.HasQueuedFrames());
249 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldNotFlush) {
250 StrictMock<MockDebugDelegate> debug_delegate;
252 generator_.set_debug_delegate(&debug_delegate);
253 delegate_.SetCanWriteOnlyNonRetransmittable();
254 generator_.StartBatchOperations();
256 EXPECT_CALL(delegate_, PopulateAckFrame(_));
257 EXPECT_CALL(debug_delegate, OnFrameAddedToPacket(_)).Times(1);
259 generator_.SetShouldSendAck(false);
260 EXPECT_TRUE(generator_.HasQueuedFrames());
263 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldFlush) {
264 delegate_.SetCanWriteOnlyNonRetransmittable();
266 EXPECT_CALL(delegate_, PopulateAckFrame(_));
267 EXPECT_CALL(delegate_, OnSerializedPacket(_))
268 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
270 generator_.SetShouldSendAck(false);
271 EXPECT_FALSE(generator_.HasQueuedFrames());
273 PacketContents contents;
274 contents.num_ack_frames = 1;
275 CheckPacketContains(contents, 0);
278 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_MultipleCalls) {
279 // Make sure that calling SetShouldSendAck multiple times does not result in a
280 // crash. Previously this would result in multiple QuicFrames queued in the
281 // packet generator, with all but the last with internal pointers to freed
282 // memory.
283 delegate_.SetCanWriteAnything();
285 // Only one AckFrame should be created.
286 EXPECT_CALL(delegate_, PopulateAckFrame(_)).Times(1);
287 EXPECT_CALL(delegate_, OnSerializedPacket(_))
288 .Times(1)
289 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
291 generator_.StartBatchOperations();
292 generator_.SetShouldSendAck(false);
293 generator_.SetShouldSendAck(false);
294 generator_.FinishBatchOperations();
297 TEST_P(QuicPacketGeneratorTest, AddControlFrame_NotWritable) {
298 delegate_.SetCanNotWrite();
300 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
301 EXPECT_TRUE(generator_.HasQueuedFrames());
304 TEST_P(QuicPacketGeneratorTest, AddControlFrame_OnlyAckWritable) {
305 delegate_.SetCanWriteOnlyNonRetransmittable();
307 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
308 EXPECT_TRUE(generator_.HasQueuedFrames());
311 TEST_P(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldNotFlush) {
312 delegate_.SetCanWriteAnything();
313 generator_.StartBatchOperations();
315 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
316 EXPECT_TRUE(generator_.HasQueuedFrames());
319 TEST_P(QuicPacketGeneratorTest, AddControlFrame_NotWritableBatchThenFlush) {
320 delegate_.SetCanNotWrite();
321 generator_.StartBatchOperations();
323 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
324 EXPECT_TRUE(generator_.HasQueuedFrames());
325 generator_.FinishBatchOperations();
326 EXPECT_TRUE(generator_.HasQueuedFrames());
328 EXPECT_CALL(delegate_, OnSerializedPacket(_))
329 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
330 generator_.FlushAllQueuedFrames();
331 EXPECT_FALSE(generator_.HasQueuedFrames());
333 PacketContents contents;
334 contents.num_rst_stream_frames = 1;
335 CheckPacketContains(contents, 0);
338 TEST_P(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldFlush) {
339 delegate_.SetCanWriteAnything();
341 EXPECT_CALL(delegate_, OnSerializedPacket(_))
342 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
344 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
345 EXPECT_FALSE(generator_.HasQueuedFrames());
347 PacketContents contents;
348 contents.num_rst_stream_frames = 1;
349 CheckPacketContains(contents, 0);
352 TEST_P(QuicPacketGeneratorTest, ConsumeData_NotWritable) {
353 delegate_.SetCanNotWrite();
355 QuicConsumedData consumed = generator_.ConsumeData(
356 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
357 EXPECT_EQ(0u, consumed.bytes_consumed);
358 EXPECT_FALSE(consumed.fin_consumed);
359 EXPECT_FALSE(generator_.HasQueuedFrames());
362 TEST_P(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) {
363 delegate_.SetCanWriteAnything();
364 generator_.StartBatchOperations();
366 QuicConsumedData consumed = generator_.ConsumeData(
367 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
368 EXPECT_EQ(3u, consumed.bytes_consumed);
369 EXPECT_TRUE(consumed.fin_consumed);
370 EXPECT_TRUE(generator_.HasQueuedFrames());
373 TEST_P(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) {
374 delegate_.SetCanWriteAnything();
376 EXPECT_CALL(delegate_, OnSerializedPacket(_))
377 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
378 QuicConsumedData consumed = generator_.ConsumeData(
379 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
380 EXPECT_EQ(3u, consumed.bytes_consumed);
381 EXPECT_TRUE(consumed.fin_consumed);
382 EXPECT_FALSE(generator_.HasQueuedFrames());
384 PacketContents contents;
385 contents.num_stream_frames = 1;
386 CheckPacketContains(contents, 0);
389 // Test the behavior of ConsumeData when the data consumed is for the crypto
390 // handshake stream. Ensure that the packet is always sent and padded even if
391 // the generator operates in batch mode.
392 TEST_P(QuicPacketGeneratorTest, ConsumeData_Handshake) {
393 delegate_.SetCanWriteAnything();
394 generator_.StartBatchOperations();
396 EXPECT_CALL(delegate_, OnSerializedPacket(_))
397 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
398 QuicConsumedData consumed = generator_.ConsumeData(
399 kCryptoStreamId, MakeIOVector("foo"), 0, false, MAY_FEC_PROTECT, nullptr);
400 EXPECT_EQ(3u, consumed.bytes_consumed);
401 EXPECT_FALSE(generator_.HasQueuedFrames());
403 PacketContents contents;
404 contents.num_stream_frames = 1;
405 CheckPacketContains(contents, 0);
407 ASSERT_EQ(1u, packets_.size());
408 ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetMaxPacketLength());
409 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].packet->length());
412 TEST_P(QuicPacketGeneratorTest, ConsumeData_EmptyData) {
413 EXPECT_DFATAL(generator_.ConsumeData(kHeadersStreamId, MakeIOVector(""), 0,
414 false, MAY_FEC_PROTECT, nullptr),
415 "Attempt to consume empty data without FIN.");
418 TEST_P(QuicPacketGeneratorTest,
419 ConsumeDataMultipleTimes_WritableAndShouldNotFlush) {
420 delegate_.SetCanWriteAnything();
421 generator_.StartBatchOperations();
423 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
424 MAY_FEC_PROTECT, nullptr);
425 QuicConsumedData consumed = generator_.ConsumeData(
426 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, nullptr);
427 EXPECT_EQ(4u, consumed.bytes_consumed);
428 EXPECT_FALSE(consumed.fin_consumed);
429 EXPECT_TRUE(generator_.HasQueuedFrames());
432 TEST_P(QuicPacketGeneratorTest, ConsumeData_BatchOperations) {
433 delegate_.SetCanWriteAnything();
434 generator_.StartBatchOperations();
436 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
437 MAY_FEC_PROTECT, nullptr);
438 QuicConsumedData consumed = generator_.ConsumeData(
439 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, nullptr);
440 EXPECT_EQ(4u, consumed.bytes_consumed);
441 EXPECT_FALSE(consumed.fin_consumed);
442 EXPECT_TRUE(generator_.HasQueuedFrames());
444 // Now both frames will be flushed out.
445 EXPECT_CALL(delegate_, OnSerializedPacket(_))
446 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
447 generator_.FinishBatchOperations();
448 EXPECT_FALSE(generator_.HasQueuedFrames());
450 PacketContents contents;
451 contents.num_stream_frames = 2;
452 CheckPacketContains(contents, 0);
455 TEST_P(QuicPacketGeneratorTest, ConsumeDataFecOnMaxGroupSize) {
456 delegate_.SetCanWriteAnything();
458 // Send FEC every two packets.
459 creator_->set_max_packets_per_fec_group(2);
462 InSequence dummy;
463 EXPECT_CALL(delegate_, OnSerializedPacket(_))
464 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
465 EXPECT_CALL(delegate_, OnSerializedPacket(_))
466 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
467 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
468 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER, but FEC
469 // group is closed.
470 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
471 } else {
472 EXPECT_CALL(delegate_, OnSerializedPacket(_))
473 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
475 EXPECT_CALL(delegate_, OnSerializedPacket(_))
476 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
479 // Send enough data to create 3 packets: two full and one partial. Send with
480 // MUST_FEC_PROTECT flag.
481 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
482 QuicConsumedData consumed = generator_.ConsumeData(
483 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
484 EXPECT_EQ(data_len, consumed.bytes_consumed);
485 EXPECT_TRUE(consumed.fin_consumed);
486 EXPECT_FALSE(generator_.HasQueuedFrames());
488 CheckPacketHasSingleStreamFrame(0);
489 CheckPacketHasSingleStreamFrame(1);
490 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
491 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER.
492 CheckPacketHasSingleStreamFrame(2);
493 } else {
494 CheckPacketIsFec(2, 1);
495 CheckPacketHasSingleStreamFrame(3);
497 EXPECT_TRUE(creator_->IsFecProtected());
499 // If FEC send policy is FEC_ANY_TRIGGER, then the FEC packet under
500 // construction will be sent when one more packet is sent (since FEC group
501 // size is 2), or when OnFecTimeout is called. Send more data with
502 // MAY_FEC_PROTECT. This packet should also be protected, and FEC packet is
503 // sent since FEC group size is reached.
505 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed when the group
506 // size is reached. FEC packet is not sent.
508 InSequence dummy;
509 EXPECT_CALL(delegate_, OnSerializedPacket(_))
510 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
511 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
512 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
513 } else {
514 EXPECT_CALL(delegate_, OnSerializedPacket(_))
515 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
518 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
519 nullptr);
520 EXPECT_EQ(1u, consumed.bytes_consumed);
521 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
522 CheckPacketHasSingleStreamFrame(3);
523 } else {
524 CheckPacketHasSingleStreamFrame(4);
525 CheckPacketIsFec(5, 4);
527 EXPECT_FALSE(creator_->IsFecProtected());
530 TEST_P(QuicPacketGeneratorTest, ConsumeDataSendsFecOnTimeout) {
531 delegate_.SetCanWriteAnything();
532 creator_->set_max_packets_per_fec_group(1000);
534 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
535 // creator FEC protects all data.
536 EXPECT_CALL(delegate_, OnSerializedPacket(_))
537 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
538 QuicConsumedData consumed = generator_.ConsumeData(3, CreateData(1u), 0, true,
539 MUST_FEC_PROTECT, nullptr);
540 EXPECT_EQ(1u, consumed.bytes_consumed);
541 EXPECT_TRUE(consumed.fin_consumed);
542 CheckPacketHasSingleStreamFrame(0);
543 EXPECT_TRUE(creator_->IsFecProtected());
545 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
546 // and FEC packet is not yet sent.
547 EXPECT_CALL(delegate_, OnSerializedPacket(_))
548 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
549 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
550 nullptr);
551 EXPECT_EQ(1u, consumed.bytes_consumed);
552 CheckPacketHasSingleStreamFrame(1);
553 EXPECT_TRUE(creator_->IsFecProtected());
555 // Calling OnFecTimeout should cause the FEC packet to be emitted.
556 EXPECT_CALL(delegate_, OnSerializedPacket(_))
557 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
558 generator_.OnFecTimeout();
559 CheckPacketIsFec(2, 1);
560 EXPECT_FALSE(creator_->IsFecProtected());
562 // Subsequent data is protected under the next FEC group. Send enough data to
563 // create 2 more packets: one full and one partial.
565 InSequence dummy;
566 EXPECT_CALL(delegate_, OnSerializedPacket(_))
567 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
568 EXPECT_CALL(delegate_, OnSerializedPacket(_))
569 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
571 size_t data_len = kDefaultMaxPacketSize + 1;
572 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
573 MUST_FEC_PROTECT, nullptr);
574 EXPECT_EQ(data_len, consumed.bytes_consumed);
575 EXPECT_TRUE(consumed.fin_consumed);
576 CheckPacketHasSingleStreamFrame(3);
577 CheckPacketHasSingleStreamFrame(4);
578 EXPECT_TRUE(creator_->IsFecProtected());
580 // Calling OnFecTimeout should cause the FEC packet to be emitted.
581 EXPECT_CALL(delegate_, OnSerializedPacket(_))
582 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
583 generator_.OnFecTimeout();
584 CheckPacketIsFec(5, 4);
585 EXPECT_FALSE(creator_->IsFecProtected());
588 TEST_P(QuicPacketGeneratorTest, GetFecTimeoutFiniteOnlyOnFirstPacketInGroup) {
589 delegate_.SetCanWriteAnything();
590 creator_->set_max_packets_per_fec_group(6);
592 // Send enough data to create 2 packets: one full and one partial. Send with
593 // MUST_FEC_PROTECT flag. No FEC packet is emitted yet, but the creator FEC
594 // protects all data.
596 InSequence dummy;
597 EXPECT_CALL(delegate_, OnSerializedPacket(_))
598 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
599 EXPECT_CALL(delegate_, OnSerializedPacket(_))
600 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
602 size_t data_len = 1 * kDefaultMaxPacketSize + 100;
603 QuicConsumedData consumed = generator_.ConsumeData(
604 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
605 EXPECT_EQ(data_len, consumed.bytes_consumed);
606 EXPECT_TRUE(consumed.fin_consumed);
607 EXPECT_FALSE(generator_.HasQueuedFrames());
608 CheckPacketHasSingleStreamFrame(0);
609 CheckPacketHasSingleStreamFrame(1);
610 EXPECT_TRUE(creator_->IsFecProtected());
612 // GetFecTimeout returns finite timeout only for first packet in group.
613 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
614 generator_.GetFecTimeout(/*sequence_number=*/1u));
615 EXPECT_EQ(QuicTime::Delta::Infinite(),
616 generator_.GetFecTimeout(/*sequence_number=*/2u));
618 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
619 // and FEC packet is not yet sent.
620 EXPECT_CALL(delegate_, OnSerializedPacket(_))
621 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
622 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
623 nullptr);
624 CheckPacketHasSingleStreamFrame(2);
625 EXPECT_TRUE(creator_->IsFecProtected());
627 // GetFecTimeout returns finite timeout only for first packet in group.
628 EXPECT_EQ(QuicTime::Delta::Infinite(),
629 generator_.GetFecTimeout(/*sequence_number=*/3u));
631 // Calling OnFecTimeout should cause the FEC packet to be emitted.
632 EXPECT_CALL(delegate_, OnSerializedPacket(_))
633 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
634 generator_.OnFecTimeout();
635 CheckPacketIsFec(3, /*fec_group=*/1u);
636 EXPECT_FALSE(creator_->IsFecProtected());
638 // Subsequent data is protected under the next FEC group. Send enough data to
639 // create 2 more packets: one full and one partial.
641 InSequence dummy;
642 EXPECT_CALL(delegate_, OnSerializedPacket(_))
643 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
644 EXPECT_CALL(delegate_, OnSerializedPacket(_))
645 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
647 data_len = kDefaultMaxPacketSize + 1u;
648 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
649 MUST_FEC_PROTECT, nullptr);
650 EXPECT_EQ(data_len, consumed.bytes_consumed);
651 EXPECT_TRUE(consumed.fin_consumed);
652 CheckPacketHasSingleStreamFrame(4);
653 CheckPacketHasSingleStreamFrame(5);
654 EXPECT_TRUE(creator_->IsFecProtected());
656 // GetFecTimeout returns finite timeout for first packet in the new group.
657 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
658 generator_.GetFecTimeout(/*sequence_number=*/5u));
659 EXPECT_EQ(QuicTime::Delta::Infinite(),
660 generator_.GetFecTimeout(/*sequence_number=*/6u));
662 // Calling OnFecTimeout should cause the FEC packet to be emitted.
663 EXPECT_CALL(delegate_, OnSerializedPacket(_))
664 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
665 generator_.OnFecTimeout();
666 CheckPacketIsFec(6, /*fec_group=*/5u);
667 EXPECT_FALSE(creator_->IsFecProtected());
669 // Send more data with MAY_FEC_PROTECT. No FEC protection, so GetFecTimeout
670 // returns infinite.
671 EXPECT_CALL(delegate_, OnSerializedPacket(_))
672 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
673 consumed = generator_.ConsumeData(9, CreateData(1u), 0, true, MAY_FEC_PROTECT,
674 nullptr);
675 CheckPacketHasSingleStreamFrame(7);
676 EXPECT_FALSE(creator_->IsFecProtected());
677 EXPECT_EQ(QuicTime::Delta::Infinite(),
678 generator_.GetFecTimeout(/*sequence_number=*/8u));
681 TEST_P(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) {
682 // Set the packet size be enough for two stream frames with 0 stream offset,
683 // but not enough for a stream frame of 0 offset and one with non-zero offset.
684 size_t length =
685 NullEncrypter().GetCiphertextSize(0) +
686 GetPacketHeaderSize(
687 creator_->connection_id_length(), true,
688 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_),
689 NOT_IN_FEC_GROUP) +
690 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
691 // than the GetMinStreamFrameSize.
692 QuicFramer::GetMinStreamFrameSize(1, 0, false, NOT_IN_FEC_GROUP) + 3 +
693 QuicFramer::GetMinStreamFrameSize(1, 0, true, NOT_IN_FEC_GROUP) + 1;
694 generator_.SetMaxPacketLength(length, /*force=*/false);
695 delegate_.SetCanWriteAnything();
697 InSequence dummy;
698 EXPECT_CALL(delegate_, OnSerializedPacket(_))
699 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
700 EXPECT_CALL(delegate_, OnSerializedPacket(_))
701 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
703 generator_.StartBatchOperations();
704 // Queue enough data to prevent a stream frame with a non-zero offset from
705 // fitting.
706 QuicConsumedData consumed =
707 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 0, false,
708 MAY_FEC_PROTECT, nullptr);
709 EXPECT_EQ(3u, consumed.bytes_consumed);
710 EXPECT_FALSE(consumed.fin_consumed);
711 EXPECT_TRUE(generator_.HasQueuedFrames());
713 // This frame will not fit with the existing frame, causing the queued frame
714 // to be serialized, and it will not fit with another frame like it, so it is
715 // serialized by itself.
716 consumed = generator_.ConsumeData(kHeadersStreamId, MakeIOVector("bar"), 3,
717 true, MAY_FEC_PROTECT, nullptr);
718 EXPECT_EQ(3u, consumed.bytes_consumed);
719 EXPECT_TRUE(consumed.fin_consumed);
720 EXPECT_FALSE(generator_.HasQueuedFrames());
722 PacketContents contents;
723 contents.num_stream_frames = 1;
724 CheckPacketContains(contents, 0);
725 CheckPacketContains(contents, 1);
728 TEST_P(QuicPacketGeneratorTest, NoFecPacketSentWhenBatchEnds) {
729 delegate_.SetCanWriteAnything();
730 creator_->set_max_packets_per_fec_group(6);
732 generator_.StartBatchOperations();
734 generator_.ConsumeData(3, MakeIOVector("foo"), 2, true, MUST_FEC_PROTECT,
735 nullptr);
736 QuicConsumedData consumed = generator_.ConsumeData(
737 5, MakeIOVector("quux"), 7, false, MUST_FEC_PROTECT, nullptr);
738 EXPECT_EQ(4u, consumed.bytes_consumed);
739 EXPECT_FALSE(consumed.fin_consumed);
740 EXPECT_TRUE(generator_.HasQueuedFrames());
742 // Now both frames will be flushed out, but FEC packet is not yet sent.
743 EXPECT_CALL(delegate_, OnSerializedPacket(_))
744 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
745 generator_.FinishBatchOperations();
746 EXPECT_FALSE(generator_.HasQueuedFrames());
748 PacketContents contents;
749 contents.num_stream_frames = 2u;
750 contents.fec_group = 1u;
751 CheckPacketContains(contents, 0);
753 // Forcing FEC timeout causes FEC packet to be emitted.
754 EXPECT_CALL(delegate_, OnSerializedPacket(_))
755 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
756 generator_.OnFecTimeout();
757 CheckPacketIsFec(1, /*fec_group=*/1u);
760 TEST_P(QuicPacketGeneratorTest, FecTimeoutOnRttChange) {
761 EXPECT_EQ(QuicTime::Delta::Zero(),
762 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
763 generator_.OnRttChange(QuicTime::Delta::FromMilliseconds(300));
764 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(150),
765 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
768 TEST_P(QuicPacketGeneratorTest, FecGroupSizeOnCongestionWindowChange) {
769 delegate_.SetCanWriteAnything();
770 creator_->set_max_packets_per_fec_group(50);
771 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
772 EXPECT_FALSE(creator_->IsFecGroupOpen());
774 // On reduced cwnd.
775 generator_.OnCongestionWindowChange(7);
776 EXPECT_EQ(3u, creator_->max_packets_per_fec_group());
778 // On increased cwnd.
779 generator_.OnCongestionWindowChange(100);
780 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
782 // On collapsed cwnd.
783 generator_.OnCongestionWindowChange(1);
784 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
787 TEST_P(QuicPacketGeneratorTest, FecGroupSizeChangeWithOpenGroup) {
788 delegate_.SetCanWriteAnything();
789 generator_.StartBatchOperations();
790 creator_->set_max_packets_per_fec_group(50);
791 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
792 EXPECT_FALSE(creator_->IsFecGroupOpen());
794 // Send enough data to create 4 packets with MUST_FEC_PROTECT flag. 3 packets
795 // are sent, one is queued in the creator.
797 InSequence dummy;
798 EXPECT_CALL(delegate_, OnSerializedPacket(_))
799 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
800 EXPECT_CALL(delegate_, OnSerializedPacket(_))
801 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
802 EXPECT_CALL(delegate_, OnSerializedPacket(_))
803 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
805 size_t data_len = 3 * kDefaultMaxPacketSize + 1;
806 QuicConsumedData consumed = generator_.ConsumeData(
807 7, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
808 EXPECT_EQ(data_len, consumed.bytes_consumed);
809 EXPECT_TRUE(creator_->IsFecGroupOpen());
811 // Change FEC groupsize.
812 generator_.OnCongestionWindowChange(2);
813 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
815 // If FEC send policy is FEC_ANY_TRIGGER, then send enough data to trigger one
816 // unprotected data packet, causing the FEC packet to also be sent.
818 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed and FEC packet
819 // is not sent.
821 InSequence dummy;
822 EXPECT_CALL(delegate_, OnSerializedPacket(_))
823 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
824 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
825 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
826 } else {
827 EXPECT_CALL(delegate_, OnSerializedPacket(_))
828 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
831 consumed = generator_.ConsumeData(7, CreateData(kDefaultMaxPacketSize), 0,
832 true, MAY_FEC_PROTECT, nullptr);
833 EXPECT_EQ(kDefaultMaxPacketSize, consumed.bytes_consumed);
834 if (generator_.fec_send_policy() == FEC_ANY_TRIGGER) {
835 // Verify that one FEC packet was sent.
836 CheckPacketIsFec(4, /*fec_group=*/1u);
838 EXPECT_FALSE(creator_->IsFecGroupOpen());
839 EXPECT_FALSE(creator_->IsFecProtected());
842 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOff) {
843 delegate_.SetCanWriteAnything();
844 creator_->set_max_packets_per_fec_group(2);
845 EXPECT_FALSE(creator_->IsFecProtected());
847 // Send one unprotected data packet.
848 EXPECT_CALL(delegate_, OnSerializedPacket(_))
849 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
850 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
851 MAY_FEC_PROTECT, nullptr);
852 EXPECT_EQ(1u, consumed.bytes_consumed);
853 EXPECT_FALSE(generator_.HasQueuedFrames());
854 EXPECT_FALSE(creator_->IsFecProtected());
855 // Verify that one data packet was sent.
856 PacketContents contents;
857 contents.num_stream_frames = 1;
858 CheckPacketContains(contents, 0);
861 InSequence dummy;
862 EXPECT_CALL(delegate_, OnSerializedPacket(_))
863 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
864 EXPECT_CALL(delegate_, OnSerializedPacket(_))
865 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
866 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
867 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed.
868 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
869 } else {
870 EXPECT_CALL(delegate_, OnSerializedPacket(_))
871 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
873 EXPECT_CALL(delegate_, OnSerializedPacket(_))
874 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
876 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
877 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
878 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
879 MUST_FEC_PROTECT, nullptr);
880 EXPECT_EQ(data_len, consumed.bytes_consumed);
881 EXPECT_FALSE(generator_.HasQueuedFrames());
883 // If FEC send policy is FEC_ANY_TRIGGER, verify that packets sent were 3 data
884 // and 1 FEC.
886 // If FEC send policy is FEC_ALARM_TRIGGER, verify that packets sent were 3
887 // data and FEC group is closed.
888 CheckPacketHasSingleStreamFrame(1);
889 CheckPacketHasSingleStreamFrame(2);
890 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
891 CheckPacketHasSingleStreamFrame(3);
892 } else {
893 CheckPacketIsFec(3, /*fec_group=*/2u);
894 CheckPacketHasSingleStreamFrame(4);
897 // Calling OnFecTimeout should emit the pending FEC packet.
898 EXPECT_CALL(delegate_, OnSerializedPacket(_))
899 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
900 generator_.OnFecTimeout();
901 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
902 CheckPacketIsFec(4, /*fec_group=*/4u);
903 } else {
904 CheckPacketIsFec(5, /*fec_group=*/5u);
907 // Send one unprotected data packet.
908 EXPECT_CALL(delegate_, OnSerializedPacket(_))
909 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
910 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true, MAY_FEC_PROTECT,
911 nullptr);
912 EXPECT_EQ(1u, consumed.bytes_consumed);
913 EXPECT_FALSE(generator_.HasQueuedFrames());
914 EXPECT_FALSE(creator_->IsFecProtected());
915 // Verify that one unprotected data packet was sent.
916 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
917 CheckPacketContains(contents, 5);
918 } else {
919 CheckPacketContains(contents, 6);
923 TEST_P(QuicPacketGeneratorTest, SwitchFecOnWithPendingFrameInCreator) {
924 delegate_.SetCanWriteAnything();
925 // Enable FEC.
926 creator_->set_max_packets_per_fec_group(2);
928 generator_.StartBatchOperations();
929 // Queue enough data to prevent a stream frame with a non-zero offset from
930 // fitting.
931 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
932 MAY_FEC_PROTECT, nullptr);
933 EXPECT_EQ(1u, consumed.bytes_consumed);
934 EXPECT_TRUE(creator_->HasPendingFrames());
936 // Queue protected data for sending. Should cause queued frames to be flushed.
937 EXPECT_CALL(delegate_, OnSerializedPacket(_))
938 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
939 EXPECT_FALSE(creator_->IsFecProtected());
940 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
941 MUST_FEC_PROTECT, nullptr);
942 EXPECT_EQ(1u, consumed.bytes_consumed);
943 PacketContents contents;
944 contents.num_stream_frames = 1;
945 // Transmitted packet was not FEC protected.
946 CheckPacketContains(contents, 0);
947 EXPECT_TRUE(creator_->IsFecProtected());
948 EXPECT_TRUE(creator_->HasPendingFrames());
951 TEST_P(QuicPacketGeneratorTest, SwitchFecOnWithPendingFramesInGenerator) {
952 // Enable FEC.
953 creator_->set_max_packets_per_fec_group(2);
955 // Queue control frames in generator.
956 delegate_.SetCanNotWrite();
957 generator_.SetShouldSendAck(true);
958 delegate_.SetCanWriteAnything();
959 generator_.StartBatchOperations();
961 // Set up frames to write into the creator when control frames are written.
962 EXPECT_CALL(delegate_, PopulateAckFrame(_));
963 EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_));
965 // Generator should have queued control frames, and creator should be empty.
966 EXPECT_TRUE(generator_.HasQueuedFrames());
967 EXPECT_FALSE(creator_->HasPendingFrames());
968 EXPECT_FALSE(creator_->IsFecProtected());
970 // Queue protected data for sending. Should cause queued frames to be flushed.
971 EXPECT_CALL(delegate_, OnSerializedPacket(_))
972 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
973 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
974 MUST_FEC_PROTECT, nullptr);
975 EXPECT_EQ(1u, consumed.bytes_consumed);
976 PacketContents contents;
977 contents.num_ack_frames = 1;
978 contents.num_stop_waiting_frames = 1;
979 CheckPacketContains(contents, 0);
981 // FEC protection should be on in creator.
982 EXPECT_TRUE(creator_->IsFecProtected());
985 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentFramesProtected) {
986 delegate_.SetCanWriteAnything();
988 // Enable FEC.
989 creator_->set_max_packets_per_fec_group(2);
990 EXPECT_FALSE(creator_->IsFecProtected());
992 // Queue stream frame to be protected in creator.
993 generator_.StartBatchOperations();
994 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
995 MUST_FEC_PROTECT, nullptr);
996 EXPECT_EQ(1u, consumed.bytes_consumed);
997 // Creator has a pending protected frame.
998 EXPECT_TRUE(creator_->HasPendingFrames());
999 EXPECT_TRUE(creator_->IsFecProtected());
1001 // Add enough unprotected data to exceed size of current packet, so that
1002 // current packet is sent. Both frames will be sent out in a single packet.
1003 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1004 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1005 size_t data_len = kDefaultMaxPacketSize;
1006 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
1007 MAY_FEC_PROTECT, nullptr);
1008 EXPECT_EQ(data_len, consumed.bytes_consumed);
1009 PacketContents contents;
1010 contents.num_stream_frames = 2u;
1011 contents.fec_group = 1u;
1012 CheckPacketContains(contents, 0);
1013 // FEC protection should still be on in creator.
1014 EXPECT_TRUE(creator_->IsFecProtected());
1017 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentPacketsProtected) {
1018 delegate_.SetCanWriteAnything();
1020 // Enable FEC.
1021 creator_->set_max_packets_per_fec_group(2);
1022 EXPECT_FALSE(creator_->IsFecProtected());
1024 // Send first packet, FEC protected.
1025 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1026 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1027 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
1028 MUST_FEC_PROTECT, nullptr);
1029 EXPECT_EQ(1u, consumed.bytes_consumed);
1030 PacketContents contents;
1031 contents.num_stream_frames = 1u;
1032 contents.fec_group = 1u;
1033 CheckPacketContains(contents, 0);
1035 // FEC should still be on in creator.
1036 EXPECT_TRUE(creator_->IsFecProtected());
1038 // Send unprotected data to cause second packet to be sent, which gets
1039 // protected because it happens to fall within an open FEC group. Data packet
1040 // will be followed by FEC packet.
1042 InSequence dummy;
1043 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1044 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1045 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1046 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1047 } else {
1048 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1049 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1052 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
1053 nullptr);
1054 EXPECT_EQ(1u, consumed.bytes_consumed);
1055 contents.num_stream_frames = 1u;
1056 CheckPacketContains(contents, 1);
1057 if (generator_.fec_send_policy() == FEC_ANY_TRIGGER) {
1058 // FEC packet is sent when send policy is FEC_ANY_TRIGGER.
1059 CheckPacketIsFec(2, /*fec_group=*/1u);
1062 // FEC protection should be off in creator.
1063 EXPECT_FALSE(creator_->IsFecProtected());
1066 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOffThenOnWithCreatorProtectionOn) {
1067 delegate_.SetCanWriteAnything();
1068 generator_.StartBatchOperations();
1070 // Enable FEC.
1071 creator_->set_max_packets_per_fec_group(2);
1072 EXPECT_FALSE(creator_->IsFecProtected());
1074 // Queue one byte of FEC protected data.
1075 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
1076 MUST_FEC_PROTECT, nullptr);
1077 EXPECT_TRUE(creator_->HasPendingFrames());
1079 // Add more unprotected data causing first packet to be sent, FEC protected.
1080 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1081 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1082 size_t data_len = kDefaultMaxPacketSize;
1083 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
1084 MAY_FEC_PROTECT, nullptr);
1085 EXPECT_EQ(data_len, consumed.bytes_consumed);
1086 PacketContents contents;
1087 contents.num_stream_frames = 2u;
1088 contents.fec_group = 1u;
1089 CheckPacketContains(contents, 0);
1091 // FEC group is still open in creator.
1092 EXPECT_TRUE(creator_->IsFecProtected());
1094 // Add data that should be protected, large enough to cause second packet to
1095 // be sent. Data packet should be followed by FEC packet.
1097 InSequence dummy;
1098 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1099 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1100 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1101 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1102 } else {
1103 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1104 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1107 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
1108 MUST_FEC_PROTECT, nullptr);
1109 EXPECT_EQ(data_len, consumed.bytes_consumed);
1110 CheckPacketContains(contents, 1);
1111 if (generator_.fec_send_policy() == FEC_ANY_TRIGGER) {
1112 // FEC packet is sent when send policy is FEC_ANY_TRIGGER.
1113 CheckPacketIsFec(2, /*fec_group=*/1u);
1116 // FEC protection should remain on in creator.
1117 EXPECT_TRUE(creator_->IsFecProtected());
1120 TEST_P(QuicPacketGeneratorTest, ResetFecGroupNoTimeout) {
1121 delegate_.SetCanWriteAnything();
1122 // Send FEC packet after 2 packets.
1123 creator_->set_max_packets_per_fec_group(2);
1124 EXPECT_FALSE(creator_->IsFecProtected());
1126 // Send two packets so that when this data is consumed, two packets are sent
1127 // out. In FEC_TRIGGER_ANY, this will cause an FEC packet to be sent out and
1128 // with FEC_TRIGGER_ALARM, this will cause a Reset to be called. In both
1129 // cases, the creator's fec protection will be turned off afterwards.
1131 InSequence dummy;
1132 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1133 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1134 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1135 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1136 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1137 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER, but FEC
1138 // group is closed.
1139 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1140 } else {
1141 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1142 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1144 // Fin Packet.
1145 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1146 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1148 size_t data_len = 2 * kDefaultMaxPacketSize;
1149 QuicConsumedData consumed = generator_.ConsumeData(
1150 5, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
1151 EXPECT_EQ(data_len, consumed.bytes_consumed);
1152 EXPECT_TRUE(consumed.fin_consumed);
1153 EXPECT_FALSE(generator_.HasQueuedFrames());
1154 CheckPacketHasSingleStreamFrame(0);
1155 CheckPacketHasSingleStreamFrame(1);
1156 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1157 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER.
1158 CheckPacketHasSingleStreamFrame(2);
1159 } else {
1160 // FEC packet is sent after 2 packets and when send policy is
1161 // FEC_ANY_TRIGGER.
1162 CheckPacketIsFec(2, 1);
1163 CheckPacketHasSingleStreamFrame(3);
1165 EXPECT_TRUE(creator_->IsFecProtected());
1167 // Do the same send (with MUST_FEC_PROTECT) on a different stream id.
1169 InSequence dummy;
1170 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1171 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1172 // FEC packet is sent after 2 packets and when send policy is
1173 // FEC_ANY_TRIGGER. When policy is FEC_ALARM_TRIGGER, FEC group is closed
1174 // and FEC packet is not sent.
1175 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1176 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1177 } else {
1178 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1179 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1181 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1182 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1183 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1184 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1185 // FEC packet is sent after 2 packets and when send policy is
1186 // FEC_ANY_TRIGGER. When policy is FEC_ALARM_TRIGGER, FEC group is closed
1187 // and FEC packet is not sent.
1188 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1189 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1190 } else {
1191 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1192 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1195 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
1196 MUST_FEC_PROTECT, nullptr);
1197 EXPECT_EQ(data_len, consumed.bytes_consumed);
1198 EXPECT_TRUE(consumed.fin_consumed);
1199 EXPECT_FALSE(generator_.HasQueuedFrames());
1200 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1201 CheckPacketHasSingleStreamFrame(3);
1202 CheckPacketHasSingleStreamFrame(4);
1203 CheckPacketHasSingleStreamFrame(5);
1204 } else {
1205 CheckPacketHasSingleStreamFrame(4);
1206 // FEC packet is sent after 2 packets and when send policy is
1207 // FEC_ANY_TRIGGER.
1208 CheckPacketIsFec(5, 4);
1209 CheckPacketHasSingleStreamFrame(6);
1210 CheckPacketHasSingleStreamFrame(7);
1211 // FEC packet is sent after 2 packets and when send policy is
1212 // FEC_ANY_TRIGGER.
1213 CheckPacketIsFec(8, 7);
1215 EXPECT_TRUE(creator_->IsFecProtected());
1218 // 1. Create and send one packet with MUST_FEC_PROTECT.
1219 // 2. Call FecTimeout, expect FEC packet is sent.
1220 // 3. Do the same thing over again, with a different stream id.
1221 TEST_P(QuicPacketGeneratorTest, FecPacketSentOnFecTimeout) {
1222 delegate_.SetCanWriteAnything();
1223 creator_->set_max_packets_per_fec_group(1000);
1224 EXPECT_FALSE(creator_->IsFecProtected());
1226 for (int i = 1; i < 4; i = i + 2) {
1227 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
1228 // creator FEC protects all data.
1229 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1230 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1231 QuicConsumedData consumed = generator_.ConsumeData(
1232 i + 2, CreateData(1u), 0, true, MUST_FEC_PROTECT, nullptr);
1233 EXPECT_EQ(1u, consumed.bytes_consumed);
1234 EXPECT_TRUE(consumed.fin_consumed);
1235 CheckPacketHasSingleStreamFrame(0);
1236 EXPECT_TRUE(creator_->IsFecProtected());
1238 // Calling OnFecTimeout should cause the FEC packet to be emitted.
1239 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1240 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1241 generator_.OnFecTimeout();
1242 CheckPacketIsFec(i, i);
1243 EXPECT_FALSE(creator_->IsFecProtected());
1247 TEST_P(QuicPacketGeneratorTest, NotWritableThenBatchOperations) {
1248 delegate_.SetCanNotWrite();
1250 generator_.SetShouldSendAck(false);
1251 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1252 EXPECT_TRUE(generator_.HasQueuedFrames());
1254 delegate_.SetCanWriteAnything();
1256 generator_.StartBatchOperations();
1258 // When the first write operation is invoked, the ack frame will be returned.
1259 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1261 // Send some data and a control frame
1262 generator_.ConsumeData(3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT,
1263 nullptr);
1264 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1266 // All five frames will be flushed out in a single packet.
1267 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1268 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1269 generator_.FinishBatchOperations();
1270 EXPECT_FALSE(generator_.HasQueuedFrames());
1272 PacketContents contents;
1273 contents.num_ack_frames = 1;
1274 contents.num_goaway_frames = 1;
1275 contents.num_rst_stream_frames = 1;
1276 contents.num_stream_frames = 1;
1277 CheckPacketContains(contents, 0);
1280 TEST_P(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) {
1281 delegate_.SetCanNotWrite();
1283 generator_.SetShouldSendAck(false);
1284 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1285 EXPECT_TRUE(generator_.HasQueuedFrames());
1287 delegate_.SetCanWriteAnything();
1289 generator_.StartBatchOperations();
1291 // When the first write operation is invoked, the ack frame will be returned.
1292 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1295 InSequence dummy;
1296 // All five frames will be flushed out in a single packet
1297 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1298 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1299 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1300 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1303 // Send enough data to exceed one packet
1304 size_t data_len = kDefaultMaxPacketSize + 100;
1305 QuicConsumedData consumed = generator_.ConsumeData(
1306 3, CreateData(data_len), 0, true, MAY_FEC_PROTECT, nullptr);
1307 EXPECT_EQ(data_len, consumed.bytes_consumed);
1308 EXPECT_TRUE(consumed.fin_consumed);
1309 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1311 generator_.FinishBatchOperations();
1312 EXPECT_FALSE(generator_.HasQueuedFrames());
1314 // The first packet should have the queued data and part of the stream data.
1315 PacketContents contents;
1316 contents.num_ack_frames = 1;
1317 contents.num_rst_stream_frames = 1;
1318 contents.num_stream_frames = 1;
1319 CheckPacketContains(contents, 0);
1321 // The second should have the remainder of the stream data.
1322 PacketContents contents2;
1323 contents2.num_goaway_frames = 1;
1324 contents2.num_stream_frames = 1;
1325 CheckPacketContains(contents2, 1);
1328 TEST_P(QuicPacketGeneratorTest, TestConnectionIdLength) {
1329 generator_.SetConnectionIdLength(0);
1330 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, creator_->connection_id_length());
1331 generator_.SetConnectionIdLength(1);
1332 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID, creator_->connection_id_length());
1333 generator_.SetConnectionIdLength(2);
1334 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1335 generator_.SetConnectionIdLength(3);
1336 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1337 generator_.SetConnectionIdLength(4);
1338 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1339 generator_.SetConnectionIdLength(5);
1340 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1341 generator_.SetConnectionIdLength(6);
1342 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1343 generator_.SetConnectionIdLength(7);
1344 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1345 generator_.SetConnectionIdLength(8);
1346 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1347 generator_.SetConnectionIdLength(9);
1348 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1351 // Test whether SetMaxPacketLength() works in the situation when the queue is
1352 // empty, and we send three packets worth of data.
1353 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_Initial) {
1354 delegate_.SetCanWriteAnything();
1356 // Send enough data for three packets.
1357 size_t data_len = 3 * kDefaultMaxPacketSize + 1;
1358 size_t packet_len = kDefaultMaxPacketSize + 100;
1359 ASSERT_LE(packet_len, kMaxPacketSize);
1360 generator_.SetMaxPacketLength(packet_len, /*force=*/false);
1361 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
1363 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1364 .Times(3)
1365 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1366 QuicConsumedData consumed =
1367 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1368 /*offset=*/2,
1369 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1370 EXPECT_EQ(data_len, consumed.bytes_consumed);
1371 EXPECT_TRUE(consumed.fin_consumed);
1372 EXPECT_FALSE(generator_.HasQueuedFrames());
1374 // We expect three packets, and first two of them have to be of packet_len
1375 // size. We check multiple packets (instead of just one) because we want to
1376 // ensure that |max_packet_length_| does not get changed incorrectly by the
1377 // generator after first packet is serialized.
1378 ASSERT_EQ(3u, packets_.size());
1379 EXPECT_EQ(packet_len, packets_[0].packet->length());
1380 EXPECT_EQ(packet_len, packets_[1].packet->length());
1381 CheckAllPacketsHaveSingleStreamFrame();
1384 // Test whether SetMaxPacketLength() works in the situation when we first write
1385 // data, then change packet size, then write data again.
1386 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_Middle) {
1387 delegate_.SetCanWriteAnything();
1389 // We send enough data to overflow default packet length, but not the altered
1390 // one.
1391 size_t data_len = kDefaultMaxPacketSize;
1392 size_t packet_len = kDefaultMaxPacketSize + 100;
1393 ASSERT_LE(packet_len, kMaxPacketSize);
1395 // We expect to see three packets in total.
1396 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1397 .Times(3)
1398 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1400 // Send two packets before packet size change.
1401 QuicConsumedData consumed =
1402 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1403 /*offset=*/2,
1404 /*fin=*/false, MAY_FEC_PROTECT, nullptr);
1405 EXPECT_EQ(data_len, consumed.bytes_consumed);
1406 EXPECT_FALSE(consumed.fin_consumed);
1407 EXPECT_FALSE(generator_.HasQueuedFrames());
1409 // Make sure we already have two packets.
1410 ASSERT_EQ(2u, packets_.size());
1412 // Increase packet size.
1413 generator_.SetMaxPacketLength(packet_len, /*force=*/false);
1414 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
1416 // Send a packet after packet size change.
1417 consumed = generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1418 2 + data_len,
1419 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1420 EXPECT_EQ(data_len, consumed.bytes_consumed);
1421 EXPECT_TRUE(consumed.fin_consumed);
1422 EXPECT_FALSE(generator_.HasQueuedFrames());
1424 // We expect first data chunk to get fragmented, but the second one to fit
1425 // into a single packet.
1426 ASSERT_EQ(3u, packets_.size());
1427 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].packet->length());
1428 EXPECT_LE(kDefaultMaxPacketSize, packets_[2].packet->length());
1429 CheckAllPacketsHaveSingleStreamFrame();
1432 // Test whether SetMaxPacketLength() works correctly when we change the packet
1433 // size in the middle of the batched packet.
1434 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_Midpacket) {
1435 delegate_.SetCanWriteAnything();
1436 generator_.StartBatchOperations();
1438 size_t first_write_len = kDefaultMaxPacketSize / 2;
1439 size_t second_write_len = kDefaultMaxPacketSize;
1440 size_t packet_len = kDefaultMaxPacketSize + 100;
1441 ASSERT_LE(packet_len, kMaxPacketSize);
1443 // First send half of the packet worth of data. We are in the batch mode, so
1444 // should not cause packet serialization.
1445 QuicConsumedData consumed =
1446 generator_.ConsumeData(kHeadersStreamId, CreateData(first_write_len),
1447 /*offset=*/2,
1448 /*fin=*/false, MAY_FEC_PROTECT, nullptr);
1449 EXPECT_EQ(first_write_len, consumed.bytes_consumed);
1450 EXPECT_FALSE(consumed.fin_consumed);
1451 EXPECT_TRUE(generator_.HasQueuedFrames());
1453 // Make sure we have no packets so far.
1454 ASSERT_EQ(0u, packets_.size());
1456 // Increase packet size. Ensure it's not immediately enacted.
1457 generator_.SetMaxPacketLength(packet_len, /*force=*/false);
1458 EXPECT_EQ(packet_len, generator_.GetMaxPacketLength());
1459 EXPECT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength());
1461 // We expect to see exactly one packet serialized after that, since we are in
1462 // batch mode and we have sent approximately 3/2 of our MTU.
1463 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1464 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1466 // Send a packet worth of data to the same stream. This should trigger
1467 // serialization of other packet.
1468 consumed =
1469 generator_.ConsumeData(kHeadersStreamId, CreateData(second_write_len),
1470 /*offset=*/2 + first_write_len,
1471 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1472 EXPECT_EQ(second_write_len, consumed.bytes_consumed);
1473 EXPECT_TRUE(consumed.fin_consumed);
1474 EXPECT_TRUE(generator_.HasQueuedFrames());
1476 // We expect the first packet to contain two frames, and to not reflect the
1477 // packet size change.
1478 ASSERT_EQ(1u, packets_.size());
1479 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].packet->length());
1481 PacketContents contents;
1482 contents.num_stream_frames = 2;
1483 CheckPacketContains(contents, 0);
1486 // Test whether SetMaxPacketLength() works correctly when we force the change of
1487 // the packet size in the middle of the batched packet.
1488 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_MidpacketFlush) {
1489 delegate_.SetCanWriteAnything();
1490 generator_.StartBatchOperations();
1492 size_t first_write_len = kDefaultMaxPacketSize / 2;
1493 size_t packet_len = kDefaultMaxPacketSize + 100;
1494 size_t second_write_len = packet_len + 1;
1495 ASSERT_LE(packet_len, kMaxPacketSize);
1497 // First send half of the packet worth of data. We are in the batch mode, so
1498 // should not cause packet serialization.
1499 QuicConsumedData consumed =
1500 generator_.ConsumeData(kHeadersStreamId, CreateData(first_write_len),
1501 /*offset=*/2,
1502 /*fin=*/false, MAY_FEC_PROTECT, nullptr);
1503 EXPECT_EQ(first_write_len, consumed.bytes_consumed);
1504 EXPECT_FALSE(consumed.fin_consumed);
1505 EXPECT_TRUE(generator_.HasQueuedFrames());
1507 // Make sure we have no packets so far.
1508 ASSERT_EQ(0u, packets_.size());
1510 // Expect a packet to be flushed.
1511 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1512 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1514 // Increase packet size. Ensure it's immediately enacted.
1515 generator_.SetMaxPacketLength(packet_len, /*force=*/true);
1516 EXPECT_EQ(packet_len, generator_.GetMaxPacketLength());
1517 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
1518 EXPECT_FALSE(generator_.HasQueuedFrames());
1520 // We expect to see exactly one packet serialized after that, because we send
1521 // a value somewhat exceeding new max packet size, and the tail data does not
1522 // get serialized because we are still in the batch mode.
1523 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1524 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1526 // Send a more than a packet worth of data to the same stream. This should
1527 // trigger serialization of one packet, and queue another one.
1528 consumed =
1529 generator_.ConsumeData(kHeadersStreamId, CreateData(second_write_len),
1530 /*offset=*/2 + first_write_len,
1531 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1532 EXPECT_EQ(second_write_len, consumed.bytes_consumed);
1533 EXPECT_TRUE(consumed.fin_consumed);
1534 EXPECT_TRUE(generator_.HasQueuedFrames());
1536 // We expect the first packet to be underfilled, and the second packet be up
1537 // to the new max packet size.
1538 ASSERT_EQ(2u, packets_.size());
1539 EXPECT_GT(kDefaultMaxPacketSize, packets_[0].packet->length());
1540 EXPECT_EQ(packet_len, packets_[1].packet->length());
1542 CheckAllPacketsHaveSingleStreamFrame();
1545 } // namespace test
1546 } // namespace net