OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 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/stream_sequencer_buffer.h" | 5 #include "net/quic/stream_sequencer_buffer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "net/quic/test_tools/mock_clock.h" | 10 #include "net/quic/test_tools/mock_clock.h" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 std::list<Gap> GetGaps() { return buffer_->gaps_; } | 128 std::list<Gap> GetGaps() { return buffer_->gaps_; } |
129 | 129 |
130 size_t max_buffer_capacity() { return buffer_->max_buffer_capacity_bytes_; } | 130 size_t max_buffer_capacity() { return buffer_->max_buffer_capacity_bytes_; } |
131 | 131 |
132 size_t ReadableBytes() { return buffer_->ReadableBytes(); } | 132 size_t ReadableBytes() { return buffer_->ReadableBytes(); } |
133 | 133 |
134 std::map<QuicStreamOffset, FrameInfo>* frame_arrival_time_map() { | 134 std::map<QuicStreamOffset, FrameInfo>* frame_arrival_time_map() { |
135 return &(buffer_->frame_arrival_time_map_); | 135 return &(buffer_->frame_arrival_time_map_); |
136 } | 136 } |
137 | 137 |
| 138 void set_total_bytes_read(QuicStreamOffset total_bytes_read) { |
| 139 buffer_->total_bytes_read_ = total_bytes_read; |
| 140 } |
| 141 |
| 142 void set_gaps(const std::list<Gap>& gaps) { buffer_->gaps_ = gaps; } |
| 143 |
138 private: | 144 private: |
139 StreamSequencerBuffer* buffer_; | 145 StreamSequencerBuffer* buffer_; |
140 }; | 146 }; |
141 | 147 |
142 namespace { | 148 namespace { |
143 | 149 |
144 class StreamSequencerBufferTest : public testing::Test { | 150 class StreamSequencerBufferTest : public testing::Test { |
145 public: | 151 public: |
146 void SetUp() override { Initialize(); } | 152 void SetUp() override { Initialize(); } |
147 | 153 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 source = std::string(100, 'b'); | 276 source = std::string(100, 'b'); |
271 // Write something into [kBlockSizeBytes * 2 - 20, kBlockSizeBytes * 2 + 80). | 277 // Write something into [kBlockSizeBytes * 2 - 20, kBlockSizeBytes * 2 + 80). |
272 EXPECT_EQ(QUIC_NO_ERROR, | 278 EXPECT_EQ(QUIC_NO_ERROR, |
273 buffer_->OnStreamData(kBlockSizeBytes * 2 - 20, source, | 279 buffer_->OnStreamData(kBlockSizeBytes * 2 - 20, source, |
274 clock_.ApproximateNow(), &written)); | 280 clock_.ApproximateNow(), &written)); |
275 EXPECT_EQ(3, helper_->GapSize()); | 281 EXPECT_EQ(3, helper_->GapSize()); |
276 EXPECT_EQ(1024u + 100u, buffer_->BytesBuffered()); | 282 EXPECT_EQ(1024u + 100u, buffer_->BytesBuffered()); |
277 EXPECT_TRUE(helper_->CheckBufferInvariants()); | 283 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
278 } | 284 } |
279 | 285 |
| 286 TEST_F(StreamSequencerBufferTest, OnStreamDataInLongStreamWithOverlap) { |
| 287 // Assume a stream has already buffered almost 4GB. |
| 288 uint64_t total_bytes_read = pow(2, 32) - 1; |
| 289 helper_->set_total_bytes_read(total_bytes_read); |
| 290 helper_->set_gaps(std::list<Gap>( |
| 291 1, Gap(total_bytes_read, std::numeric_limits<QuicStreamOffset>::max()))); |
| 292 |
| 293 // Three new out of order frames arrive. |
| 294 const size_t kBytesToWrite = 100; |
| 295 string source(kBytesToWrite, 'a'); |
| 296 size_t written; |
| 297 // Frame [2^32 + 500, 2^32 + 600). |
| 298 QuicStreamOffset offset = pow(2, 32) + 500; |
| 299 EXPECT_EQ( |
| 300 QUIC_NO_ERROR, |
| 301 buffer_->OnStreamData(offset, source, clock_.ApproximateNow(), &written)); |
| 302 EXPECT_EQ(2, helper_->GapSize()); |
| 303 |
| 304 // Frame [2^32 + 700, 2^32 + 800). |
| 305 offset = pow(2, 32) + 700; |
| 306 EXPECT_EQ( |
| 307 QUIC_NO_ERROR, |
| 308 buffer_->OnStreamData(offset, source, clock_.ApproximateNow(), &written)); |
| 309 EXPECT_EQ(3, helper_->GapSize()); |
| 310 |
| 311 // Another frame [2^32 + 300, 2^32 + 400). |
| 312 offset = pow(2, 32) + 300; |
| 313 EXPECT_EQ( |
| 314 QUIC_NO_ERROR, |
| 315 buffer_->OnStreamData(offset, source, clock_.ApproximateNow(), &written)); |
| 316 EXPECT_EQ(4, helper_->GapSize()); |
| 317 } |
| 318 |
280 TEST_F(StreamSequencerBufferTest, OnStreamDataTillEnd) { | 319 TEST_F(StreamSequencerBufferTest, OnStreamDataTillEnd) { |
281 // Write 50 bytes to the end. | 320 // Write 50 bytes to the end. |
282 const size_t kBytesToWrite = 50; | 321 const size_t kBytesToWrite = 50; |
283 std::string source(kBytesToWrite, 'a'); | 322 std::string source(kBytesToWrite, 'a'); |
284 size_t written; | 323 size_t written; |
285 EXPECT_EQ(QUIC_NO_ERROR, | 324 EXPECT_EQ(QUIC_NO_ERROR, |
286 buffer_->OnStreamData(max_capacity_bytes_ - kBytesToWrite, source, | 325 buffer_->OnStreamData(max_capacity_bytes_ - kBytesToWrite, source, |
287 clock_.ApproximateNow(), &written)); | 326 clock_.ApproximateNow(), &written)); |
288 EXPECT_EQ(50u, buffer_->BytesBuffered()); | 327 EXPECT_EQ(50u, buffer_->BytesBuffered()); |
289 EXPECT_TRUE(helper_->CheckBufferInvariants()); | 328 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
972 EXPECT_LE(bytes_to_buffer_, total_bytes_read_) << "iterations: " | 1011 EXPECT_LE(bytes_to_buffer_, total_bytes_read_) << "iterations: " |
973 << iterations; | 1012 << iterations; |
974 EXPECT_LE(bytes_to_buffer_, total_bytes_written_); | 1013 EXPECT_LE(bytes_to_buffer_, total_bytes_written_); |
975 } | 1014 } |
976 | 1015 |
977 } // anonymous namespace | 1016 } // anonymous namespace |
978 | 1017 |
979 } // namespace test | 1018 } // namespace test |
980 | 1019 |
981 } // namespace net | 1020 } // namespace net |
OLD | NEW |