OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 #include "net/quic/quic_stream_sequencer_buffer.h" | |
5 | |
6 #include <algorithm> | |
7 #include <limits> | |
8 #include <map> | |
9 #include <string> | |
10 #include <utility> | |
11 | |
12 #include "base/logging.h" | |
13 #include "base/macros.h" | |
14 #include "base/rand_util.h" | |
15 #include "net/quic/test_tools/mock_clock.h" | |
16 #include "net/quic/test_tools/quic_test_utils.h" | |
17 #include "net/test/gtest_util.h" | |
18 #include "testing/gmock/include/gmock/gmock.h" | |
19 #include "testing/gmock_mutant.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using std::min; | |
23 using std::string; | |
24 | |
25 namespace net { | |
26 | |
27 namespace test { | |
28 | |
29 char GetCharFromIOVecs(size_t offset, iovec iov[], size_t count) { | |
30 size_t start_offset = 0; | |
31 for (size_t i = 0; i < count; i++) { | |
32 if (iov[i].iov_len == 0) { | |
33 continue; | |
34 } | |
35 size_t end_offset = start_offset + iov[i].iov_len - 1; | |
36 if (offset >= start_offset && offset <= end_offset) { | |
37 const char* buf = reinterpret_cast<const char*>(iov[i].iov_base); | |
38 return buf[offset - start_offset]; | |
39 } | |
40 start_offset += iov[i].iov_len; | |
41 } | |
42 LOG(ERROR) << "Could not locate char at offset " << offset << " in " << count | |
43 << " iovecs"; | |
44 for (size_t i = 0; i < count; ++i) { | |
45 LOG(ERROR) << " iov[" << i << "].iov_len = " << iov[i].iov_len; | |
46 } | |
47 return '\0'; | |
48 } | |
49 | |
50 static const size_t kBlockSizeBytes = | |
51 QuicStreamSequencerBuffer::kBlockSizeBytes; | |
52 typedef QuicStreamSequencerBuffer::BufferBlock BufferBlock; | |
53 typedef QuicStreamSequencerBuffer::Gap Gap; | |
54 typedef QuicStreamSequencerBuffer::FrameInfo FrameInfo; | |
55 | |
56 class QuicStreamSequencerBufferPeer { | |
57 public: | |
58 explicit QuicStreamSequencerBufferPeer(QuicStreamSequencerBuffer* buffer) | |
59 : buffer_(buffer) {} | |
60 | |
61 // Read from this buffer_->into the given destination buffer_-> up to the | |
62 // size of the destination. Returns the number of bytes read. Reading from | |
63 // an empty buffer_->returns 0. | |
64 size_t Read(char* dest_buffer, size_t size) { | |
65 iovec dest; | |
66 dest.iov_base = dest_buffer, dest.iov_len = size; | |
67 return buffer_->Readv(&dest, 1); | |
68 } | |
69 | |
70 // If buffer is empty, the blocks_ array must be empty, which means all | |
71 // blocks are deallocated. | |
72 bool CheckEmptyInvariants() { | |
73 return !buffer_->Empty() || IsBlockArrayEmpty(); | |
74 } | |
75 | |
76 bool IsBlockArrayEmpty() { | |
77 size_t count = buffer_->blocks_count_; | |
78 for (size_t i = 0; i < count; i++) { | |
79 if (buffer_->blocks_[i] != nullptr) { | |
80 return false; | |
81 } | |
82 } | |
83 return true; | |
84 } | |
85 | |
86 bool CheckInitialState() { | |
87 EXPECT_TRUE(buffer_->Empty() && buffer_->total_bytes_read_ == 0 && | |
88 buffer_->num_bytes_buffered_ == 0); | |
89 return CheckBufferInvariants(); | |
90 } | |
91 | |
92 bool CheckBufferInvariants() { | |
93 QuicStreamOffset data_span = | |
94 buffer_->gaps_.back().begin_offset - buffer_->total_bytes_read_; | |
95 bool capacity_sane = data_span <= buffer_->max_buffer_capacity_bytes_ && | |
96 data_span >= buffer_->num_bytes_buffered_; | |
97 if (!capacity_sane) { | |
98 LOG(ERROR) << "data span is larger than capacity."; | |
99 LOG(ERROR) << "total read: " << buffer_->total_bytes_read_ | |
100 << " last byte: " << buffer_->gaps_.back().begin_offset; | |
101 } | |
102 bool total_read_sane = | |
103 buffer_->gaps_.front().begin_offset >= buffer_->total_bytes_read_; | |
104 if (!total_read_sane) { | |
105 LOG(ERROR) << "read across 1st gap."; | |
106 } | |
107 bool read_offset_sane = buffer_->ReadOffset() < kBlockSizeBytes; | |
108 if (!capacity_sane) { | |
109 LOG(ERROR) << "read offset go beyond 1st block"; | |
110 } | |
111 bool block_match_capacity = | |
112 (buffer_->max_buffer_capacity_bytes_ <= | |
113 buffer_->blocks_count_ * kBlockSizeBytes) && | |
114 (buffer_->max_buffer_capacity_bytes_ > | |
115 (buffer_->blocks_count_ - 1) * kBlockSizeBytes); | |
116 if (!capacity_sane) { | |
117 LOG(ERROR) << "block number not match capcaity."; | |
118 } | |
119 bool block_retired_when_empty = CheckEmptyInvariants(); | |
120 if (!block_retired_when_empty) { | |
121 LOG(ERROR) << "block is not retired after use."; | |
122 } | |
123 return capacity_sane && total_read_sane && read_offset_sane && | |
124 block_match_capacity && block_retired_when_empty; | |
125 } | |
126 | |
127 size_t GetInBlockOffset(QuicStreamOffset offset) { | |
128 return buffer_->GetInBlockOffset(offset); | |
129 } | |
130 | |
131 BufferBlock* GetBlock(size_t index) { return buffer_->blocks_[index]; } | |
132 | |
133 int GapSize() { return buffer_->gaps_.size(); } | |
134 | |
135 std::list<Gap> GetGaps() { return buffer_->gaps_; } | |
136 | |
137 size_t max_buffer_capacity() { return buffer_->max_buffer_capacity_bytes_; } | |
138 | |
139 size_t ReadableBytes() { return buffer_->ReadableBytes(); } | |
140 | |
141 std::map<QuicStreamOffset, FrameInfo>* frame_arrival_time_map() { | |
142 return &(buffer_->frame_arrival_time_map_); | |
143 } | |
144 | |
145 void set_total_bytes_read(QuicStreamOffset total_bytes_read) { | |
146 buffer_->total_bytes_read_ = total_bytes_read; | |
147 } | |
148 | |
149 void set_gaps(const std::list<Gap>& gaps) { buffer_->gaps_ = gaps; } | |
150 | |
151 private: | |
152 QuicStreamSequencerBuffer* buffer_; | |
153 }; | |
154 | |
155 namespace { | |
156 | |
157 class QuicStreamSequencerBufferTest : public testing::Test { | |
158 public: | |
159 void SetUp() override { Initialize(); } | |
160 | |
161 void ResetMaxCapacityBytes(size_t max_capacity_bytes) { | |
162 max_capacity_bytes_ = max_capacity_bytes; | |
163 Initialize(); | |
164 } | |
165 | |
166 protected: | |
167 void Initialize() { | |
168 buffer_.reset(new QuicStreamSequencerBuffer(max_capacity_bytes_)); | |
169 helper_.reset(new QuicStreamSequencerBufferPeer(buffer_.get())); | |
170 } | |
171 | |
172 // Use 2.5 here to make sure the buffer has more than one block and its end | |
173 // doesn't align with the end of a block in order to test all the offset | |
174 // calculation. | |
175 size_t max_capacity_bytes_ = 2.5 * kBlockSizeBytes; | |
176 | |
177 MockClock clock_; | |
178 std::unique_ptr<QuicStreamSequencerBuffer> buffer_; | |
179 std::unique_ptr<QuicStreamSequencerBufferPeer> helper_; | |
180 string error_details_; | |
181 }; | |
182 | |
183 TEST_F(QuicStreamSequencerBufferTest, InitializationWithDifferentSizes) { | |
184 const size_t kCapacity = 2 * QuicStreamSequencerBuffer::kBlockSizeBytes; | |
185 ResetMaxCapacityBytes(kCapacity); | |
186 EXPECT_EQ(max_capacity_bytes_, helper_->max_buffer_capacity()); | |
187 EXPECT_TRUE(helper_->CheckInitialState()); | |
188 | |
189 const size_t kCapacity1 = 8 * QuicStreamSequencerBuffer::kBlockSizeBytes; | |
190 ResetMaxCapacityBytes(kCapacity1); | |
191 EXPECT_EQ(kCapacity1, helper_->max_buffer_capacity()); | |
192 EXPECT_TRUE(helper_->CheckInitialState()); | |
193 } | |
194 | |
195 TEST_F(QuicStreamSequencerBufferTest, ClearOnEmpty) { | |
196 buffer_->Clear(); | |
197 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
198 } | |
199 | |
200 TEST_F(QuicStreamSequencerBufferTest, OnStreamData0length) { | |
201 size_t written; | |
202 QuicErrorCode error = buffer_->OnStreamData(800, "", clock_.ApproximateNow(), | |
203 &written, &error_details_); | |
204 EXPECT_EQ(error, QUIC_EMPTY_STREAM_FRAME_NO_FIN); | |
205 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
206 } | |
207 | |
208 TEST_F(QuicStreamSequencerBufferTest, OnStreamDataWithinBlock) { | |
209 string source(1024, 'a'); | |
210 size_t written; | |
211 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
212 QuicTime t = clock_.ApproximateNow(); | |
213 EXPECT_EQ(QUIC_NO_ERROR, | |
214 buffer_->OnStreamData(800, source, t, &written, &error_details_)); | |
215 BufferBlock* block_ptr = helper_->GetBlock(0); | |
216 for (size_t i = 0; i < source.size(); ++i) { | |
217 ASSERT_EQ('a', block_ptr->buffer[helper_->GetInBlockOffset(800) + i]); | |
218 } | |
219 EXPECT_EQ(2, helper_->GapSize()); | |
220 std::list<Gap> gaps = helper_->GetGaps(); | |
221 EXPECT_EQ(800u, gaps.front().end_offset); | |
222 EXPECT_EQ(1824u, gaps.back().begin_offset); | |
223 auto* frame_map = helper_->frame_arrival_time_map(); | |
224 EXPECT_EQ(1u, frame_map->size()); | |
225 EXPECT_EQ(800u, frame_map->begin()->first); | |
226 EXPECT_EQ(t, (*frame_map)[800].timestamp); | |
227 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
228 } | |
229 | |
230 TEST_F(QuicStreamSequencerBufferTest, OnStreamDataWithOverlap) { | |
231 string source(1024, 'a'); | |
232 // Write something into [800, 1824) | |
233 size_t written; | |
234 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
235 QuicTime t1 = clock_.ApproximateNow(); | |
236 EXPECT_EQ(QUIC_NO_ERROR, | |
237 buffer_->OnStreamData(800, source, t1, &written, &error_details_)); | |
238 // Try to write to [0, 1024) and [1024, 2048). | |
239 // But no byte will be written since overlap. | |
240 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
241 QuicTime t2 = clock_.ApproximateNow(); | |
242 EXPECT_EQ(QUIC_OVERLAPPING_STREAM_DATA, | |
243 buffer_->OnStreamData(0, source, t2, &written, &error_details_)); | |
244 EXPECT_EQ(QUIC_OVERLAPPING_STREAM_DATA, | |
245 buffer_->OnStreamData(1024, source, t2, &written, &error_details_)); | |
246 auto* frame_map = helper_->frame_arrival_time_map(); | |
247 EXPECT_EQ(1u, frame_map->size()); | |
248 EXPECT_EQ(t1, (*frame_map)[800].timestamp); | |
249 } | |
250 | |
251 TEST_F(QuicStreamSequencerBufferTest, | |
252 OnStreamDataOverlapAndDuplicateCornerCases) { | |
253 string source(1024, 'a'); | |
254 // Write something into [800, 1824) | |
255 size_t written; | |
256 buffer_->OnStreamData(800, source, clock_.ApproximateNow(), &written, | |
257 &error_details_); | |
258 source = string(800, 'b'); | |
259 // Try to write to [1, 801), but should fail due to overlapping | |
260 EXPECT_EQ(QUIC_OVERLAPPING_STREAM_DATA, | |
261 buffer_->OnStreamData(1, source, clock_.ApproximateNow(), &written, | |
262 &error_details_)); | |
263 // write to [0, 800) | |
264 EXPECT_EQ(QUIC_NO_ERROR, | |
265 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
266 &error_details_)); | |
267 // Try to write one byte to [1823, 1824), but should count as duplicate | |
268 string one_byte = "c"; | |
269 EXPECT_EQ(QUIC_NO_ERROR, | |
270 buffer_->OnStreamData(1823, one_byte, clock_.ApproximateNow(), | |
271 &written, &error_details_)); | |
272 EXPECT_EQ(0u, written); | |
273 // write one byte to [1824, 1825) | |
274 EXPECT_EQ(QUIC_NO_ERROR, | |
275 buffer_->OnStreamData(1824, one_byte, clock_.ApproximateNow(), | |
276 &written, &error_details_)); | |
277 auto* frame_map = helper_->frame_arrival_time_map(); | |
278 EXPECT_EQ(3u, frame_map->size()); | |
279 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
280 } | |
281 | |
282 TEST_F(QuicStreamSequencerBufferTest, OnStreamDataWithoutOverlap) { | |
283 string source(1024, 'a'); | |
284 // Write something into [800, 1824). | |
285 size_t written; | |
286 EXPECT_EQ(QUIC_NO_ERROR, | |
287 buffer_->OnStreamData(800, source, clock_.ApproximateNow(), | |
288 &written, &error_details_)); | |
289 source = string(100, 'b'); | |
290 // Write something into [kBlockSizeBytes * 2 - 20, kBlockSizeBytes * 2 + 80). | |
291 EXPECT_EQ(QUIC_NO_ERROR, | |
292 buffer_->OnStreamData(kBlockSizeBytes * 2 - 20, source, | |
293 clock_.ApproximateNow(), &written, | |
294 &error_details_)); | |
295 EXPECT_EQ(3, helper_->GapSize()); | |
296 EXPECT_EQ(1024u + 100u, buffer_->BytesBuffered()); | |
297 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
298 } | |
299 | |
300 TEST_F(QuicStreamSequencerBufferTest, OnStreamDataInLongStreamWithOverlap) { | |
301 // Assume a stream has already buffered almost 4GB. | |
302 uint64_t total_bytes_read = pow(2, 32) - 1; | |
303 helper_->set_total_bytes_read(total_bytes_read); | |
304 helper_->set_gaps(std::list<Gap>( | |
305 1, Gap(total_bytes_read, std::numeric_limits<QuicStreamOffset>::max()))); | |
306 | |
307 // Three new out of order frames arrive. | |
308 const size_t kBytesToWrite = 100; | |
309 string source(kBytesToWrite, 'a'); | |
310 size_t written; | |
311 // Frame [2^32 + 500, 2^32 + 600). | |
312 QuicStreamOffset offset = pow(2, 32) + 500; | |
313 EXPECT_EQ(QUIC_NO_ERROR, | |
314 buffer_->OnStreamData(offset, source, clock_.ApproximateNow(), | |
315 &written, &error_details_)); | |
316 EXPECT_EQ(2, helper_->GapSize()); | |
317 | |
318 // Frame [2^32 + 700, 2^32 + 800). | |
319 offset = pow(2, 32) + 700; | |
320 EXPECT_EQ(QUIC_NO_ERROR, | |
321 buffer_->OnStreamData(offset, source, clock_.ApproximateNow(), | |
322 &written, &error_details_)); | |
323 EXPECT_EQ(3, helper_->GapSize()); | |
324 | |
325 // Another frame [2^32 + 300, 2^32 + 400). | |
326 offset = pow(2, 32) + 300; | |
327 EXPECT_EQ(QUIC_NO_ERROR, | |
328 buffer_->OnStreamData(offset, source, clock_.ApproximateNow(), | |
329 &written, &error_details_)); | |
330 EXPECT_EQ(4, helper_->GapSize()); | |
331 } | |
332 | |
333 TEST_F(QuicStreamSequencerBufferTest, OnStreamDataTillEnd) { | |
334 // Write 50 bytes to the end. | |
335 const size_t kBytesToWrite = 50; | |
336 string source(kBytesToWrite, 'a'); | |
337 size_t written; | |
338 EXPECT_EQ(QUIC_NO_ERROR, | |
339 buffer_->OnStreamData(max_capacity_bytes_ - kBytesToWrite, source, | |
340 clock_.ApproximateNow(), &written, | |
341 &error_details_)); | |
342 EXPECT_EQ(50u, buffer_->BytesBuffered()); | |
343 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
344 } | |
345 | |
346 TEST_F(QuicStreamSequencerBufferTest, OnStreamDataTillEndCorner) { | |
347 // Write 1 byte to the end. | |
348 const size_t kBytesToWrite = 1; | |
349 string source(kBytesToWrite, 'a'); | |
350 size_t written; | |
351 EXPECT_EQ(QUIC_NO_ERROR, | |
352 buffer_->OnStreamData(max_capacity_bytes_ - kBytesToWrite, source, | |
353 clock_.ApproximateNow(), &written, | |
354 &error_details_)); | |
355 EXPECT_EQ(1u, buffer_->BytesBuffered()); | |
356 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
357 } | |
358 | |
359 TEST_F(QuicStreamSequencerBufferTest, OnStreamDataBeyondCapacity) { | |
360 string source(60, 'a'); | |
361 size_t written; | |
362 EXPECT_EQ(QUIC_INTERNAL_ERROR, | |
363 buffer_->OnStreamData(max_capacity_bytes_ - 50, source, | |
364 clock_.ApproximateNow(), &written, | |
365 &error_details_)); | |
366 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
367 | |
368 source = "b"; | |
369 EXPECT_EQ(QUIC_INTERNAL_ERROR, | |
370 buffer_->OnStreamData(max_capacity_bytes_, source, | |
371 clock_.ApproximateNow(), &written, | |
372 &error_details_)); | |
373 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
374 | |
375 EXPECT_EQ(QUIC_INTERNAL_ERROR, | |
376 buffer_->OnStreamData(max_capacity_bytes_ * 1000, source, | |
377 clock_.ApproximateNow(), &written, | |
378 &error_details_)); | |
379 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
380 EXPECT_EQ(0u, buffer_->BytesBuffered()); | |
381 } | |
382 | |
383 TEST_F(QuicStreamSequencerBufferTest, Readv100Bytes) { | |
384 string source(1024, 'a'); | |
385 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
386 QuicTime t1 = clock_.ApproximateNow(); | |
387 // Write something into [kBlockSizeBytes, kBlockSizeBytes + 1024). | |
388 size_t written; | |
389 buffer_->OnStreamData(kBlockSizeBytes, source, t1, &written, &error_details_); | |
390 EXPECT_FALSE(buffer_->HasBytesToRead()); | |
391 source = string(100, 'b'); | |
392 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
393 QuicTime t2 = clock_.ApproximateNow(); | |
394 // Write something into [0, 100). | |
395 buffer_->OnStreamData(0, source, t2, &written, &error_details_); | |
396 EXPECT_TRUE(buffer_->HasBytesToRead()); | |
397 EXPECT_EQ(2u, helper_->frame_arrival_time_map()->size()); | |
398 // Read into a iovec array with total capacity of 120 bytes. | |
399 char dest[120]; | |
400 iovec iovecs[3]{iovec{dest, 40}, iovec{dest + 40, 40}, iovec{dest + 80, 40}}; | |
401 size_t read = buffer_->Readv(iovecs, 3); | |
402 EXPECT_EQ(100u, read); | |
403 EXPECT_EQ(100u, buffer_->BytesConsumed()); | |
404 EXPECT_EQ(source, string(dest, read)); | |
405 EXPECT_EQ(1u, helper_->frame_arrival_time_map()->size()); | |
406 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
407 } | |
408 | |
409 TEST_F(QuicStreamSequencerBufferTest, ReadvAcrossBlocks) { | |
410 string source(kBlockSizeBytes + 50, 'a'); | |
411 // Write 1st block to full and extand 50 bytes to next block. | |
412 size_t written; | |
413 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
414 &error_details_); | |
415 EXPECT_EQ(source.size(), helper_->ReadableBytes()); | |
416 // Iteratively read 512 bytes from buffer_-> Overwrite dest[] each time. | |
417 char dest[512]; | |
418 while (helper_->ReadableBytes()) { | |
419 std::fill(dest, dest + 512, 0); | |
420 iovec iovecs[2]{iovec{dest, 256}, iovec{dest + 256, 256}}; | |
421 buffer_->Readv(iovecs, 2); | |
422 } | |
423 // The last read only reads the rest 50 bytes in 2nd block. | |
424 EXPECT_EQ(string(50, 'a'), string(dest, 50)); | |
425 EXPECT_EQ(0, dest[50]) << "Dest[50] shouln't be filled."; | |
426 EXPECT_EQ(source.size(), buffer_->BytesConsumed()); | |
427 EXPECT_TRUE(buffer_->Empty()); | |
428 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
429 } | |
430 | |
431 TEST_F(QuicStreamSequencerBufferTest, ClearAfterRead) { | |
432 string source(kBlockSizeBytes + 50, 'a'); | |
433 // Write 1st block to full with 'a'. | |
434 size_t written; | |
435 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
436 &error_details_); | |
437 // Read first 512 bytes from buffer to make space at the beginning. | |
438 char dest[512]{0}; | |
439 const iovec iov{dest, 512}; | |
440 buffer_->Readv(&iov, 1); | |
441 // Clear() should make buffer empty while preserving BytesConsumed() | |
442 buffer_->Clear(); | |
443 EXPECT_TRUE(buffer_->Empty()); | |
444 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
445 } | |
446 | |
447 TEST_F(QuicStreamSequencerBufferTest, | |
448 OnStreamDataAcrossLastBlockAndFillCapacity) { | |
449 string source(kBlockSizeBytes + 50, 'a'); | |
450 // Write 1st block to full with 'a'. | |
451 size_t written; | |
452 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
453 &error_details_); | |
454 // Read first 512 bytes from buffer to make space at the beginning. | |
455 char dest[512]{0}; | |
456 const iovec iov{dest, 512}; | |
457 buffer_->Readv(&iov, 1); | |
458 EXPECT_EQ(source.size(), written); | |
459 | |
460 // Write more than half block size of bytes in the last block with 'b', which | |
461 // will wrap to the beginning and reaches the full capacity. | |
462 source = string(0.5 * kBlockSizeBytes + 512, 'b'); | |
463 EXPECT_EQ(QUIC_NO_ERROR, buffer_->OnStreamData(2 * kBlockSizeBytes, source, | |
464 clock_.ApproximateNow(), | |
465 &written, &error_details_)); | |
466 EXPECT_EQ(source.size(), written); | |
467 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
468 } | |
469 | |
470 TEST_F(QuicStreamSequencerBufferTest, | |
471 OnStreamDataAcrossLastBlockAndExceedCapacity) { | |
472 string source(kBlockSizeBytes + 50, 'a'); | |
473 // Write 1st block to full. | |
474 size_t written; | |
475 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
476 &error_details_); | |
477 // Read first 512 bytes from buffer to make space at the beginning. | |
478 char dest[512]{0}; | |
479 const iovec iov{dest, 512}; | |
480 buffer_->Readv(&iov, 1); | |
481 | |
482 // Try to write from [max_capacity_bytes_ - 0.5 * kBlockSizeBytes, | |
483 // max_capacity_bytes_ + 512 + 1). But last bytes exceeds current capacity. | |
484 source = string(0.5 * kBlockSizeBytes + 512 + 1, 'b'); | |
485 EXPECT_EQ(QUIC_INTERNAL_ERROR, | |
486 buffer_->OnStreamData(2 * kBlockSizeBytes, source, | |
487 clock_.ApproximateNow(), &written, | |
488 &error_details_)); | |
489 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
490 } | |
491 | |
492 TEST_F(QuicStreamSequencerBufferTest, ReadvAcrossLastBlock) { | |
493 // Write to full capacity and read out 512 bytes at beginning and continue | |
494 // appending 256 bytes. | |
495 string source(max_capacity_bytes_, 'a'); | |
496 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
497 QuicTime t = clock_.ApproximateNow(); | |
498 size_t written; | |
499 buffer_->OnStreamData(0, source, t, &written, &error_details_); | |
500 char dest[512]{0}; | |
501 const iovec iov{dest, 512}; | |
502 buffer_->Readv(&iov, 1); | |
503 source = string(256, 'b'); | |
504 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
505 QuicTime t2 = clock_.ApproximateNow(); | |
506 buffer_->OnStreamData(max_capacity_bytes_, source, t2, &written, | |
507 &error_details_); | |
508 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
509 EXPECT_EQ(2u, helper_->frame_arrival_time_map()->size()); | |
510 | |
511 // Read all data out. | |
512 std::unique_ptr<char[]> dest1{new char[max_capacity_bytes_]}; | |
513 dest1[0] = 0; | |
514 const iovec iov1{dest1.get(), max_capacity_bytes_}; | |
515 EXPECT_EQ(max_capacity_bytes_ - 512 + 256, buffer_->Readv(&iov1, 1)); | |
516 EXPECT_EQ(max_capacity_bytes_ + 256, buffer_->BytesConsumed()); | |
517 EXPECT_TRUE(buffer_->Empty()); | |
518 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
519 EXPECT_EQ(0u, helper_->frame_arrival_time_map()->size()); | |
520 } | |
521 | |
522 TEST_F(QuicStreamSequencerBufferTest, ReadvEmpty) { | |
523 char dest[512]{0}; | |
524 iovec iov{dest, 512}; | |
525 size_t read = buffer_->Readv(&iov, 1); | |
526 EXPECT_EQ(0u, read); | |
527 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
528 } | |
529 | |
530 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionsEmpty) { | |
531 iovec iovs[2]; | |
532 int iov_count = buffer_->GetReadableRegions(iovs, 2); | |
533 EXPECT_EQ(0, iov_count); | |
534 EXPECT_EQ(nullptr, iovs[iov_count].iov_base); | |
535 EXPECT_EQ(0u, iovs[iov_count].iov_len); | |
536 } | |
537 | |
538 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionsBlockedByGap) { | |
539 // Write into [1, 1024). | |
540 string source(1023, 'a'); | |
541 size_t written; | |
542 buffer_->OnStreamData(1, source, clock_.ApproximateNow(), &written, | |
543 &error_details_); | |
544 // Try to get readable regions, but none is there. | |
545 iovec iovs[2]; | |
546 int iov_count = buffer_->GetReadableRegions(iovs, 2); | |
547 EXPECT_EQ(0, iov_count); | |
548 } | |
549 | |
550 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionsTillEndOfBlock) { | |
551 // Write first block to full with [0, 256) 'a' and the rest 'b' then read out | |
552 // [0, 256) | |
553 string source(kBlockSizeBytes, 'a'); | |
554 size_t written; | |
555 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
556 &error_details_); | |
557 char dest[256]; | |
558 helper_->Read(dest, 256); | |
559 // Get readable region from [256, 1024) | |
560 iovec iovs[2]; | |
561 int iov_count = buffer_->GetReadableRegions(iovs, 2); | |
562 EXPECT_EQ(1, iov_count); | |
563 EXPECT_EQ( | |
564 string(kBlockSizeBytes - 256, 'a'), | |
565 string(reinterpret_cast<const char*>(iovs[0].iov_base), iovs[0].iov_len)); | |
566 } | |
567 | |
568 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionsWithinOneBlock) { | |
569 // Write into [0, 1024) and then read out [0, 256) | |
570 string source(1024, 'a'); | |
571 size_t written; | |
572 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
573 &error_details_); | |
574 char dest[256]; | |
575 helper_->Read(dest, 256); | |
576 // Get readable region from [256, 1024) | |
577 iovec iovs[2]; | |
578 int iov_count = buffer_->GetReadableRegions(iovs, 2); | |
579 EXPECT_EQ(1, iov_count); | |
580 EXPECT_EQ( | |
581 string(1024 - 256, 'a'), | |
582 string(reinterpret_cast<const char*>(iovs[0].iov_base), iovs[0].iov_len)); | |
583 } | |
584 | |
585 TEST_F(QuicStreamSequencerBufferTest, | |
586 GetReadableRegionsAcrossBlockWithLongIOV) { | |
587 // Write into [0, 2 * kBlockSizeBytes + 1024) and then read out [0, 1024) | |
588 string source(2 * kBlockSizeBytes + 1024, 'a'); | |
589 size_t written; | |
590 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
591 &error_details_); | |
592 char dest[1024]; | |
593 helper_->Read(dest, 1024); | |
594 | |
595 iovec iovs[4]; | |
596 int iov_count = buffer_->GetReadableRegions(iovs, 4); | |
597 EXPECT_EQ(3, iov_count); | |
598 EXPECT_EQ(kBlockSizeBytes - 1024, iovs[0].iov_len); | |
599 EXPECT_EQ(kBlockSizeBytes, iovs[1].iov_len); | |
600 EXPECT_EQ(1024u, iovs[2].iov_len); | |
601 } | |
602 | |
603 TEST_F(QuicStreamSequencerBufferTest, | |
604 GetReadableRegionsWithMultipleIOVsAcrossEnd) { | |
605 // Write into [0, 2 * kBlockSizeBytes + 1024) and then read out [0, 1024) | |
606 // and then append 1024 + 512 bytes. | |
607 string source(2.5 * kBlockSizeBytes - 1024, 'a'); | |
608 size_t written; | |
609 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
610 &error_details_); | |
611 char dest[1024]; | |
612 helper_->Read(dest, 1024); | |
613 // Write across the end. | |
614 source = string(1024 + 512, 'b'); | |
615 buffer_->OnStreamData(2.5 * kBlockSizeBytes - 1024, source, | |
616 clock_.ApproximateNow(), &written, &error_details_); | |
617 // Use short iovec's. | |
618 iovec iovs[2]; | |
619 int iov_count = buffer_->GetReadableRegions(iovs, 2); | |
620 EXPECT_EQ(2, iov_count); | |
621 EXPECT_EQ(kBlockSizeBytes - 1024, iovs[0].iov_len); | |
622 EXPECT_EQ(kBlockSizeBytes, iovs[1].iov_len); | |
623 // Use long iovec's and wrap the end of buffer. | |
624 iovec iovs1[5]; | |
625 EXPECT_EQ(4, buffer_->GetReadableRegions(iovs1, 5)); | |
626 EXPECT_EQ(0.5 * kBlockSizeBytes, iovs1[2].iov_len); | |
627 EXPECT_EQ(512u, iovs1[3].iov_len); | |
628 EXPECT_EQ(string(512, 'b'), | |
629 string(reinterpret_cast<const char*>(iovs1[3].iov_base), | |
630 iovs1[3].iov_len)); | |
631 } | |
632 | |
633 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionEmpty) { | |
634 iovec iov; | |
635 QuicTime t = QuicTime::Zero(); | |
636 EXPECT_FALSE(buffer_->GetReadableRegion(&iov, &t)); | |
637 EXPECT_EQ(nullptr, iov.iov_base); | |
638 EXPECT_EQ(0u, iov.iov_len); | |
639 } | |
640 | |
641 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionBeforeGap) { | |
642 // Write into [1, 1024). | |
643 string source(1023, 'a'); | |
644 size_t written; | |
645 buffer_->OnStreamData(1, source, clock_.ApproximateNow(), &written, | |
646 &error_details_); | |
647 // GetReadableRegion should return false because range [0,1) hasn't been | |
648 // filled yet. | |
649 iovec iov; | |
650 QuicTime t = QuicTime::Zero(); | |
651 EXPECT_FALSE(buffer_->GetReadableRegion(&iov, &t)); | |
652 } | |
653 | |
654 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionTillEndOfBlock) { | |
655 // Write into [0, kBlockSizeBytes + 1) and then read out [0, 256) | |
656 string source(kBlockSizeBytes + 1, 'a'); | |
657 size_t written; | |
658 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
659 QuicTime t = clock_.ApproximateNow(); | |
660 buffer_->OnStreamData(0, source, t, &written, &error_details_); | |
661 char dest[256]; | |
662 helper_->Read(dest, 256); | |
663 // Get readable region from [256, 1024) | |
664 iovec iov; | |
665 QuicTime t2 = QuicTime::Zero(); | |
666 EXPECT_TRUE(buffer_->GetReadableRegion(&iov, &t2)); | |
667 EXPECT_EQ(t, t2); | |
668 EXPECT_EQ(string(kBlockSizeBytes - 256, 'a'), | |
669 string(reinterpret_cast<const char*>(iov.iov_base), iov.iov_len)); | |
670 } | |
671 | |
672 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionTillGap) { | |
673 // Write into [0, kBlockSizeBytes - 1) and then read out [0, 256) | |
674 string source(kBlockSizeBytes - 1, 'a'); | |
675 size_t written; | |
676 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
677 QuicTime t = clock_.ApproximateNow(); | |
678 buffer_->OnStreamData(0, source, t, &written, &error_details_); | |
679 char dest[256]; | |
680 helper_->Read(dest, 256); | |
681 // Get readable region from [256, 1023) | |
682 iovec iov; | |
683 QuicTime t2 = QuicTime::Zero(); | |
684 EXPECT_TRUE(buffer_->GetReadableRegion(&iov, &t2)); | |
685 EXPECT_EQ(t, t2); | |
686 EXPECT_EQ(string(kBlockSizeBytes - 1 - 256, 'a'), | |
687 string(reinterpret_cast<const char*>(iov.iov_base), iov.iov_len)); | |
688 } | |
689 | |
690 TEST_F(QuicStreamSequencerBufferTest, GetReadableRegionByArrivalTime) { | |
691 // Write into [0, kBlockSizeBytes - 100) and then read out [0, 256) | |
692 string source(kBlockSizeBytes - 100, 'a'); | |
693 size_t written; | |
694 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
695 QuicTime t = clock_.ApproximateNow(); | |
696 buffer_->OnStreamData(0, source, t, &written, &error_details_); | |
697 char dest[256]; | |
698 helper_->Read(dest, 256); | |
699 // Write into [kBlockSizeBytes - 100, kBlockSizeBytes - 50)] in same time | |
700 string source2(50, 'b'); | |
701 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
702 buffer_->OnStreamData(kBlockSizeBytes - 100, source2, t, &written, | |
703 &error_details_); | |
704 | |
705 // Write into [kBlockSizeBytes - 50, kBlockSizeBytes)] in another time | |
706 string source3(50, 'c'); | |
707 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); | |
708 QuicTime t3 = clock_.ApproximateNow(); | |
709 buffer_->OnStreamData(kBlockSizeBytes - 50, source3, t3, &written, | |
710 &error_details_); | |
711 | |
712 // Get readable region from [256, 1024 - 50) | |
713 iovec iov; | |
714 QuicTime t4 = QuicTime::Zero(); | |
715 EXPECT_TRUE(buffer_->GetReadableRegion(&iov, &t4)); | |
716 EXPECT_EQ(t, t4); | |
717 EXPECT_EQ(string(kBlockSizeBytes - 100 - 256, 'a') + source2, | |
718 string(reinterpret_cast<const char*>(iov.iov_base), iov.iov_len)); | |
719 } | |
720 | |
721 TEST_F(QuicStreamSequencerBufferTest, MarkConsumedInOneBlock) { | |
722 // Write into [0, 1024) and then read out [0, 256) | |
723 string source(1024, 'a'); | |
724 size_t written; | |
725 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
726 &error_details_); | |
727 char dest[256]; | |
728 helper_->Read(dest, 256); | |
729 | |
730 EXPECT_TRUE(buffer_->MarkConsumed(512)); | |
731 EXPECT_EQ(256u + 512u, buffer_->BytesConsumed()); | |
732 EXPECT_EQ(256u, helper_->ReadableBytes()); | |
733 EXPECT_EQ(1u, helper_->frame_arrival_time_map()->size()); | |
734 buffer_->MarkConsumed(256); | |
735 EXPECT_EQ(0u, helper_->frame_arrival_time_map()->size()); | |
736 EXPECT_TRUE(buffer_->Empty()); | |
737 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
738 } | |
739 | |
740 TEST_F(QuicStreamSequencerBufferTest, MarkConsumedNotEnoughBytes) { | |
741 // Write into [0, 1024) and then read out [0, 256) | |
742 string source(1024, 'a'); | |
743 size_t written; | |
744 QuicTime t = clock_.ApproximateNow(); | |
745 buffer_->OnStreamData(0, source, t, &written, &error_details_); | |
746 char dest[256]; | |
747 helper_->Read(dest, 256); | |
748 | |
749 // Consume 1st 512 bytes | |
750 EXPECT_TRUE(buffer_->MarkConsumed(512)); | |
751 EXPECT_EQ(256u + 512u, buffer_->BytesConsumed()); | |
752 EXPECT_EQ(256u, helper_->ReadableBytes()); | |
753 // Try to consume one bytes more than available. Should return false. | |
754 EXPECT_FALSE(buffer_->MarkConsumed(257)); | |
755 EXPECT_EQ(256u + 512u, buffer_->BytesConsumed()); | |
756 QuicTime t2 = QuicTime::Zero(); | |
757 iovec iov; | |
758 EXPECT_TRUE(buffer_->GetReadableRegion(&iov, &t2)); | |
759 EXPECT_EQ(t, t2); | |
760 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
761 } | |
762 | |
763 TEST_F(QuicStreamSequencerBufferTest, MarkConsumedAcrossBlock) { | |
764 // Write into [0, 2 * kBlockSizeBytes + 1024) and then read out [0, 1024) | |
765 string source(2 * kBlockSizeBytes + 1024, 'a'); | |
766 size_t written; | |
767 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
768 &error_details_); | |
769 char dest[1024]; | |
770 helper_->Read(dest, 1024); | |
771 | |
772 buffer_->MarkConsumed(2 * kBlockSizeBytes); | |
773 EXPECT_EQ(source.size(), buffer_->BytesConsumed()); | |
774 EXPECT_TRUE(buffer_->Empty()); | |
775 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
776 } | |
777 | |
778 TEST_F(QuicStreamSequencerBufferTest, MarkConsumedAcrossEnd) { | |
779 // Write into [0, 2.5 * kBlockSizeBytes - 1024) and then read out [0, 1024) | |
780 // and then append 1024 + 512 bytes. | |
781 string source(2.5 * kBlockSizeBytes - 1024, 'a'); | |
782 size_t written; | |
783 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
784 &error_details_); | |
785 char dest[1024]; | |
786 helper_->Read(dest, 1024); | |
787 source = string(1024 + 512, 'b'); | |
788 buffer_->OnStreamData(2.5 * kBlockSizeBytes - 1024, source, | |
789 clock_.ApproximateNow(), &written, &error_details_); | |
790 EXPECT_EQ(1024u, buffer_->BytesConsumed()); | |
791 | |
792 // Consume to the end of 2nd block. | |
793 buffer_->MarkConsumed(2 * kBlockSizeBytes - 1024); | |
794 EXPECT_EQ(2 * kBlockSizeBytes, buffer_->BytesConsumed()); | |
795 // Consume across the physical end of buffer | |
796 buffer_->MarkConsumed(0.5 * kBlockSizeBytes + 500); | |
797 EXPECT_EQ(max_capacity_bytes_ + 500, buffer_->BytesConsumed()); | |
798 EXPECT_EQ(12u, helper_->ReadableBytes()); | |
799 // Consume to the logical end of buffer | |
800 buffer_->MarkConsumed(12); | |
801 EXPECT_EQ(max_capacity_bytes_ + 512, buffer_->BytesConsumed()); | |
802 EXPECT_TRUE(buffer_->Empty()); | |
803 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
804 } | |
805 | |
806 TEST_F(QuicStreamSequencerBufferTest, FlushBufferedFrames) { | |
807 // Write into [0, 2.5 * kBlockSizeBytes - 1024) and then read out [0, 1024). | |
808 string source(max_capacity_bytes_ - 1024, 'a'); | |
809 size_t written; | |
810 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written, | |
811 &error_details_); | |
812 char dest[1024]; | |
813 helper_->Read(dest, 1024); | |
814 EXPECT_EQ(1024u, buffer_->BytesConsumed()); | |
815 // Write [1024, 512) to the physical beginning. | |
816 source = string(512, 'b'); | |
817 buffer_->OnStreamData(max_capacity_bytes_, source, clock_.ApproximateNow(), | |
818 &written, &error_details_); | |
819 EXPECT_EQ(512u, written); | |
820 EXPECT_EQ(max_capacity_bytes_ - 1024 + 512, buffer_->FlushBufferedFrames()); | |
821 EXPECT_EQ(max_capacity_bytes_ + 512, buffer_->BytesConsumed()); | |
822 EXPECT_TRUE(buffer_->Empty()); | |
823 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
824 // Clear buffer at this point should still preserve BytesConsumed(). | |
825 buffer_->Clear(); | |
826 EXPECT_EQ(max_capacity_bytes_ + 512, buffer_->BytesConsumed()); | |
827 EXPECT_TRUE(helper_->CheckBufferInvariants()); | |
828 } | |
829 | |
830 class QuicStreamSequencerBufferRandomIOTest | |
831 : public QuicStreamSequencerBufferTest { | |
832 public: | |
833 typedef std::pair<QuicStreamOffset, size_t> OffsetSizePair; | |
834 | |
835 void SetUp() override { | |
836 // Test against a larger capacity then above tests. Also make sure the last | |
837 // block is partially available to use. | |
838 max_capacity_bytes_ = 6.25 * kBlockSizeBytes; | |
839 // Stream to be buffered should be larger than the capacity to test wrap | |
840 // around. | |
841 bytes_to_buffer_ = 2 * max_capacity_bytes_; | |
842 Initialize(); | |
843 | |
844 uint32_t seed = base::RandInt(0, std::numeric_limits<int32_t>::max()); | |
845 LOG(INFO) << "RandomWriteAndProcessInPlace test seed is " << seed; | |
846 rng_.set_seed(seed); | |
847 } | |
848 | |
849 // Create an out-of-order source stream with given size to populate | |
850 // shuffled_buf_. | |
851 void CreateSourceAndShuffle(size_t max_chunk_size_bytes) { | |
852 max_chunk_size_bytes_ = max_chunk_size_bytes; | |
853 std::unique_ptr<OffsetSizePair[]> chopped_stream( | |
854 new OffsetSizePair[bytes_to_buffer_]); | |
855 | |
856 // Split stream into small chunks with random length. chopped_stream will be | |
857 // populated with segmented stream chunks. | |
858 size_t start_chopping_offset = 0; | |
859 size_t iterations = 0; | |
860 while (start_chopping_offset < bytes_to_buffer_) { | |
861 size_t max_chunk = min<size_t>(max_chunk_size_bytes_, | |
862 bytes_to_buffer_ - start_chopping_offset); | |
863 size_t chunk_size = rng_.RandUint64() % max_chunk + 1; | |
864 chopped_stream[iterations] = | |
865 OffsetSizePair(start_chopping_offset, chunk_size); | |
866 start_chopping_offset += chunk_size; | |
867 ++iterations; | |
868 } | |
869 DCHECK(start_chopping_offset == bytes_to_buffer_); | |
870 size_t chunk_num = iterations; | |
871 | |
872 // Randomly change the sequence of in-ordered OffsetSizePairs to make a | |
873 // out-of-order array of OffsetSizePairs. | |
874 for (int i = chunk_num - 1; i >= 0; --i) { | |
875 size_t random_idx = rng_.RandUint64() % (i + 1); | |
876 DVLOG(1) << "chunk offset " << chopped_stream[random_idx].first | |
877 << " size " << chopped_stream[random_idx].second; | |
878 shuffled_buf_.push_front(chopped_stream[random_idx]); | |
879 chopped_stream[random_idx] = chopped_stream[i]; | |
880 } | |
881 } | |
882 | |
883 // Write the currently first chunk of data in the out-of-order stream into | |
884 // QuicStreamSequencerBuffer. If current chuck cannot be written into buffer | |
885 // because it goes beyond current capacity, move it to the end of | |
886 // shuffled_buf_ and write it later. | |
887 void WriteNextChunkToBuffer() { | |
888 OffsetSizePair& chunk = shuffled_buf_.front(); | |
889 QuicStreamOffset offset = chunk.first; | |
890 const size_t num_to_write = chunk.second; | |
891 std::unique_ptr<char[]> write_buf{new char[max_chunk_size_bytes_]}; | |
892 for (size_t i = 0; i < num_to_write; ++i) { | |
893 write_buf[i] = (offset + i) % 256; | |
894 } | |
895 base::StringPiece string_piece_w(write_buf.get(), num_to_write); | |
896 size_t written; | |
897 auto result = | |
898 buffer_->OnStreamData(offset, string_piece_w, clock_.ApproximateNow(), | |
899 &written, &error_details_); | |
900 if (result == QUIC_NO_ERROR) { | |
901 shuffled_buf_.pop_front(); | |
902 total_bytes_written_ += num_to_write; | |
903 } else { | |
904 // This chunk offset exceeds window size. | |
905 shuffled_buf_.push_back(chunk); | |
906 shuffled_buf_.pop_front(); | |
907 } | |
908 DVLOG(1) << " write at offset: " << offset | |
909 << " len to write: " << num_to_write << " write result: " << result | |
910 << " left over: " << shuffled_buf_.size(); | |
911 } | |
912 | |
913 protected: | |
914 std::list<OffsetSizePair> shuffled_buf_; | |
915 size_t max_chunk_size_bytes_; | |
916 QuicStreamOffset bytes_to_buffer_; | |
917 size_t total_bytes_written_ = 0; | |
918 size_t total_bytes_read_ = 0; | |
919 SimpleRandom rng_; | |
920 }; | |
921 | |
922 TEST_F(QuicStreamSequencerBufferRandomIOTest, RandomWriteAndReadv) { | |
923 // Set kMaxReadSize larger than kBlockSizeBytes to test both small and large | |
924 // read. | |
925 const size_t kMaxReadSize = kBlockSizeBytes * 2; | |
926 // kNumReads is larger than 1 to test how multiple read destinations work. | |
927 const size_t kNumReads = 2; | |
928 // Since write and read operation have equal possibility to be called. Bytes | |
929 // to be written into and read out of should roughly the same. | |
930 const size_t kMaxWriteSize = kNumReads * kMaxReadSize; | |
931 size_t iterations = 0; | |
932 | |
933 CreateSourceAndShuffle(kMaxWriteSize); | |
934 | |
935 while ((!shuffled_buf_.empty() || total_bytes_read_ < bytes_to_buffer_) && | |
936 iterations <= 2 * bytes_to_buffer_) { | |
937 uint8_t next_action = | |
938 shuffled_buf_.empty() ? uint8_t{1} : rng_.RandUint64() % 2; | |
939 DVLOG(1) << "iteration: " << iterations; | |
940 switch (next_action) { | |
941 case 0: { // write | |
942 WriteNextChunkToBuffer(); | |
943 ASSERT_TRUE(helper_->CheckBufferInvariants()); | |
944 break; | |
945 } | |
946 case 1: { // readv | |
947 std::unique_ptr<char[][kMaxReadSize]> read_buf{ | |
948 new char[kNumReads][kMaxReadSize]}; | |
949 iovec dest_iov[kNumReads]; | |
950 size_t num_to_read = 0; | |
951 for (size_t i = 0; i < kNumReads; ++i) { | |
952 dest_iov[i].iov_base = | |
953 reinterpret_cast<void*>(const_cast<char*>(read_buf[i])); | |
954 dest_iov[i].iov_len = rng_.RandUint64() % kMaxReadSize; | |
955 num_to_read += dest_iov[i].iov_len; | |
956 } | |
957 size_t actually_read = buffer_->Readv(dest_iov, kNumReads); | |
958 ASSERT_LE(actually_read, num_to_read); | |
959 DVLOG(1) << " read from offset: " << total_bytes_read_ | |
960 << " size: " << num_to_read | |
961 << " actual read: " << actually_read; | |
962 for (size_t i = 0; i < actually_read; ++i) { | |
963 char ch = (i + total_bytes_read_) % 256; | |
964 ASSERT_EQ(ch, GetCharFromIOVecs(i, dest_iov, kNumReads)) | |
965 << " at iteration " << iterations; | |
966 } | |
967 total_bytes_read_ += actually_read; | |
968 ASSERT_EQ(total_bytes_read_, buffer_->BytesConsumed()); | |
969 ASSERT_TRUE(helper_->CheckBufferInvariants()); | |
970 break; | |
971 } | |
972 } | |
973 ++iterations; | |
974 ASSERT_LE(total_bytes_read_, total_bytes_written_); | |
975 } | |
976 EXPECT_LT(iterations, bytes_to_buffer_) << "runaway test"; | |
977 EXPECT_LE(bytes_to_buffer_, total_bytes_read_) << "iterations: " | |
978 << iterations; | |
979 EXPECT_LE(bytes_to_buffer_, total_bytes_written_); | |
980 } | |
981 | |
982 TEST_F(QuicStreamSequencerBufferRandomIOTest, RandomWriteAndConsumeInPlace) { | |
983 // The value 4 is chosen such that the max write size is no larger than the | |
984 // maximum buffer capacity. | |
985 const size_t kMaxNumReads = 4; | |
986 // Adjust write amount be roughly equal to that GetReadableRegions() can get. | |
987 const size_t kMaxWriteSize = kMaxNumReads * kBlockSizeBytes; | |
988 ASSERT_LE(kMaxWriteSize, max_capacity_bytes_); | |
989 size_t iterations = 0; | |
990 | |
991 CreateSourceAndShuffle(kMaxWriteSize); | |
992 | |
993 while ((!shuffled_buf_.empty() || total_bytes_read_ < bytes_to_buffer_) && | |
994 iterations <= 2 * bytes_to_buffer_) { | |
995 uint8_t next_action = | |
996 shuffled_buf_.empty() ? uint8_t{1} : rng_.RandUint64() % 2; | |
997 DVLOG(1) << "iteration: " << iterations; | |
998 switch (next_action) { | |
999 case 0: { // write | |
1000 WriteNextChunkToBuffer(); | |
1001 ASSERT_TRUE(helper_->CheckBufferInvariants()); | |
1002 break; | |
1003 } | |
1004 case 1: { // GetReadableRegions and then MarkConsumed | |
1005 size_t num_read = rng_.RandUint64() % kMaxNumReads + 1; | |
1006 iovec dest_iov[kMaxNumReads]; | |
1007 ASSERT_TRUE(helper_->CheckBufferInvariants()); | |
1008 size_t actually_num_read = | |
1009 buffer_->GetReadableRegions(dest_iov, num_read); | |
1010 ASSERT_LE(actually_num_read, num_read); | |
1011 size_t avail_bytes = 0; | |
1012 for (size_t i = 0; i < actually_num_read; ++i) { | |
1013 avail_bytes += dest_iov[i].iov_len; | |
1014 } | |
1015 // process random number of bytes (check the value of each byte). | |
1016 size_t bytes_to_process = rng_.RandUint64() % (avail_bytes + 1); | |
1017 size_t bytes_processed = 0; | |
1018 for (size_t i = 0; i < actually_num_read; ++i) { | |
1019 size_t bytes_in_block = min<size_t>( | |
1020 bytes_to_process - bytes_processed, dest_iov[i].iov_len); | |
1021 if (bytes_in_block == 0) { | |
1022 break; | |
1023 } | |
1024 for (size_t j = 0; j < bytes_in_block; ++j) { | |
1025 ASSERT_LE(bytes_processed, bytes_to_process); | |
1026 char char_expected = | |
1027 (buffer_->BytesConsumed() + bytes_processed) % 256; | |
1028 ASSERT_EQ(char_expected, | |
1029 reinterpret_cast<const char*>(dest_iov[i].iov_base)[j]) | |
1030 << " at iteration " << iterations; | |
1031 ++bytes_processed; | |
1032 } | |
1033 } | |
1034 | |
1035 buffer_->MarkConsumed(bytes_processed); | |
1036 | |
1037 DVLOG(1) << "iteration " << iterations << ": try to get " << num_read | |
1038 << " readable regions, actually get " << actually_num_read | |
1039 << " from offset: " << total_bytes_read_ | |
1040 << "\nprocesse bytes: " << bytes_processed; | |
1041 total_bytes_read_ += bytes_processed; | |
1042 ASSERT_EQ(total_bytes_read_, buffer_->BytesConsumed()); | |
1043 ASSERT_TRUE(helper_->CheckBufferInvariants()); | |
1044 break; | |
1045 } | |
1046 } | |
1047 ++iterations; | |
1048 ASSERT_LE(total_bytes_read_, total_bytes_written_); | |
1049 } | |
1050 EXPECT_LT(iterations, bytes_to_buffer_) << "runaway test"; | |
1051 EXPECT_LE(bytes_to_buffer_, total_bytes_read_) << "iterations: " | |
1052 << iterations; | |
1053 EXPECT_LE(bytes_to_buffer_, total_bytes_written_); | |
1054 } | |
1055 | |
1056 } // anonymous namespace | |
1057 | |
1058 } // namespace test | |
1059 | |
1060 } // namespace net | |
OLD | NEW |