Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: net/quic/stream_sequencer_buffer_test.cc

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

Powered by Google App Engine
This is Rietveld 408576698