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 |
| 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 char GetCharFromIOVecs(size_t offset, iovec iov[], size_t count) { |
| 24 size_t start_offset = 0; |
| 25 for (size_t i = 0; i < count; i++) { |
| 26 if (iov[i].iov_len == 0) { |
| 27 continue; |
| 28 } |
| 29 size_t end_offset = start_offset + iov[i].iov_len - 1; |
| 30 if (offset >= start_offset && offset <= end_offset) { |
| 31 const char* buf = reinterpret_cast<const char*>(iov[i].iov_base); |
| 32 return buf[offset - start_offset]; |
| 33 } |
| 34 start_offset += iov[i].iov_len; |
| 35 } |
| 36 LOG(ERROR) << "Could not locate char at offset " << offset << " in " << count |
| 37 << " iovecs"; |
| 38 for (size_t i = 0; i < count; ++i) { |
| 39 LOG(ERROR) << " iov[" << i << "].iov_len = " << iov[i].iov_len; |
| 40 } |
| 41 return '\0'; |
| 42 } |
| 43 |
| 44 static const size_t kBlockSizeBytes = StreamSequencerBuffer::kBlockSizeBytes; |
| 45 typedef StreamSequencerBuffer::BufferBlock BufferBlock; |
| 46 typedef StreamSequencerBuffer::Gap Gap; |
| 47 typedef StreamSequencerBuffer::FrameInfo FrameInfo; |
| 48 |
| 49 class StreamSequencerBufferPeer { |
| 50 public: |
| 51 explicit StreamSequencerBufferPeer(StreamSequencerBuffer* buffer) |
| 52 : buffer_(buffer) {} |
| 53 |
| 54 // Read from this buffer_->into the given destination buffer_-> up to the |
| 55 // size of the destination. Returns the number of bytes read. Reading from |
| 56 // an empty buffer_->returns 0. |
| 57 size_t Read(char* dest_buffer, size_t size) { |
| 58 iovec dest; |
| 59 dest.iov_base = dest_buffer, dest.iov_len = size; |
| 60 return buffer_->Readv(&dest, 1); |
| 61 } |
| 62 |
| 63 // If buffer is empty, the blocks_ array must be empty, which means all |
| 64 // blocks are deallocated. |
| 65 bool CheckEmptyInvariants() { |
| 66 return !buffer_->Empty() || IsBlockArrayEmpty(); |
| 67 } |
| 68 |
| 69 bool IsBlockArrayEmpty() { |
| 70 size_t count = buffer_->blocks_count_; |
| 71 for (size_t i = 0; i < count; i++) { |
| 72 if (buffer_->blocks_[i] != nullptr) { |
| 73 return false; |
| 74 } |
| 75 } |
| 76 return true; |
| 77 } |
| 78 |
| 79 bool CheckInitialState() { |
| 80 EXPECT_TRUE(buffer_->Empty() && buffer_->total_bytes_read_ == 0 && |
| 81 buffer_->num_bytes_buffered_ == 0); |
| 82 return CheckBufferInvariants(); |
| 83 } |
| 84 |
| 85 bool CheckBufferInvariants() { |
| 86 QuicStreamOffset data_span = |
| 87 buffer_->gaps_.back().begin_offset - buffer_->total_bytes_read_; |
| 88 bool capacity_sane = data_span <= buffer_->max_buffer_capacity_bytes_ && |
| 89 data_span >= buffer_->num_bytes_buffered_; |
| 90 if (!capacity_sane) { |
| 91 LOG(ERROR) << "data span is larger than capacity."; |
| 92 LOG(ERROR) << "total read: " << buffer_->total_bytes_read_ |
| 93 << " last byte: " << buffer_->gaps_.back().begin_offset; |
| 94 } |
| 95 bool total_read_sane = |
| 96 buffer_->gaps_.front().begin_offset >= buffer_->total_bytes_read_; |
| 97 if (!total_read_sane) { |
| 98 LOG(ERROR) << "read across 1st gap."; |
| 99 } |
| 100 bool read_offset_sane = buffer_->ReadOffset() < kBlockSizeBytes; |
| 101 if (!capacity_sane) { |
| 102 LOG(ERROR) << "read offset go beyond 1st block"; |
| 103 } |
| 104 bool block_match_capacity = |
| 105 (buffer_->max_buffer_capacity_bytes_ <= |
| 106 buffer_->blocks_count_ * kBlockSizeBytes) && |
| 107 (buffer_->max_buffer_capacity_bytes_ > |
| 108 (buffer_->blocks_count_ - 1) * kBlockSizeBytes); |
| 109 if (!capacity_sane) { |
| 110 LOG(ERROR) << "block number not match capcaity."; |
| 111 } |
| 112 bool block_retired_when_empty = CheckEmptyInvariants(); |
| 113 if (!block_retired_when_empty) { |
| 114 LOG(ERROR) << "block is not retired after use."; |
| 115 } |
| 116 return capacity_sane && total_read_sane && read_offset_sane && |
| 117 block_match_capacity && block_retired_when_empty; |
| 118 } |
| 119 |
| 120 size_t GetInBlockOffset(QuicStreamOffset offset) { |
| 121 return buffer_->GetInBlockOffset(offset); |
| 122 } |
| 123 |
| 124 BufferBlock* GetBlock(size_t index) { return buffer_->blocks_[index]; } |
| 125 |
| 126 int GapSize() { return buffer_->gaps_.size(); } |
| 127 |
| 128 std::list<Gap> GetGaps() { return buffer_->gaps_; } |
| 129 |
| 130 size_t max_buffer_capacity() { return buffer_->max_buffer_capacity_bytes_; } |
| 131 |
| 132 size_t ReadableBytes() { return buffer_->ReadableBytes(); } |
| 133 |
| 134 std::map<QuicStreamOffset, FrameInfo>* frame_arrival_time_map() { |
| 135 return &(buffer_->frame_arrival_time_map_); |
| 136 } |
| 137 |
| 138 private: |
| 139 StreamSequencerBuffer* buffer_; |
| 140 }; |
| 141 |
| 142 namespace { |
| 143 |
| 144 class StreamSequencerBufferTest : public testing::Test { |
| 145 public: |
| 146 void SetUp() override { Initialize(); } |
| 147 |
| 148 void ResetMaxCapacityBytes(size_t max_capacity_bytes) { |
| 149 max_capacity_bytes_ = max_capacity_bytes; |
| 150 Initialize(); |
| 151 } |
| 152 |
| 153 protected: |
| 154 void Initialize() { |
| 155 buffer_.reset(new StreamSequencerBuffer(max_capacity_bytes_)); |
| 156 helper_.reset(new StreamSequencerBufferPeer(buffer_.get())); |
| 157 } |
| 158 |
| 159 // Use 2.5 here to make sure the buffer has more than one block and its end |
| 160 // doesn't align with the end of a block in order to test all the offset |
| 161 // calculation. |
| 162 size_t max_capacity_bytes_ = 2.5 * kBlockSizeBytes; |
| 163 |
| 164 MockClock clock_; |
| 165 std::unique_ptr<StreamSequencerBuffer> buffer_; |
| 166 std::unique_ptr<StreamSequencerBufferPeer> helper_; |
| 167 }; |
| 168 |
| 169 TEST_F(StreamSequencerBufferTest, InitializationWithDifferentSizes) { |
| 170 const size_t kCapacity = 2 * StreamSequencerBuffer::kBlockSizeBytes; |
| 171 ResetMaxCapacityBytes(kCapacity); |
| 172 EXPECT_EQ(max_capacity_bytes_, helper_->max_buffer_capacity()); |
| 173 EXPECT_TRUE(helper_->CheckInitialState()); |
| 174 |
| 175 const size_t kCapacity1 = 8 * StreamSequencerBuffer::kBlockSizeBytes; |
| 176 ResetMaxCapacityBytes(kCapacity1); |
| 177 EXPECT_EQ(kCapacity1, helper_->max_buffer_capacity()); |
| 178 EXPECT_TRUE(helper_->CheckInitialState()); |
| 179 } |
| 180 |
| 181 TEST_F(StreamSequencerBufferTest, ClearOnEmpty) { |
| 182 buffer_->Clear(); |
| 183 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 184 } |
| 185 |
| 186 TEST_F(StreamSequencerBufferTest, OnStreamData0length) { |
| 187 std::string source; |
| 188 size_t written; |
| 189 EXPECT_DFATAL( |
| 190 buffer_->OnStreamData(800, source, clock_.ApproximateNow(), &written), |
| 191 "Attempted to write 0 bytes of data."); |
| 192 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 193 } |
| 194 |
| 195 TEST_F(StreamSequencerBufferTest, OnStreamDataWithinBlock) { |
| 196 std::string source(1024, 'a'); |
| 197 size_t written; |
| 198 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 199 QuicTime t = clock_.ApproximateNow(); |
| 200 EXPECT_EQ(QUIC_NO_ERROR, buffer_->OnStreamData(800, source, t, &written)); |
| 201 BufferBlock* block_ptr = helper_->GetBlock(0); |
| 202 for (size_t i = 0; i < source.size(); ++i) { |
| 203 ASSERT_EQ('a', block_ptr->buffer[helper_->GetInBlockOffset(800) + i]); |
| 204 } |
| 205 EXPECT_EQ(2, helper_->GapSize()); |
| 206 std::list<Gap> gaps = helper_->GetGaps(); |
| 207 EXPECT_EQ(800u, gaps.front().end_offset); |
| 208 EXPECT_EQ(1824u, gaps.back().begin_offset); |
| 209 auto frame_map = helper_->frame_arrival_time_map(); |
| 210 EXPECT_EQ(1u, frame_map->size()); |
| 211 EXPECT_EQ(800u, frame_map->begin()->first); |
| 212 EXPECT_EQ(t, (*frame_map)[800].timestamp); |
| 213 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 214 } |
| 215 |
| 216 TEST_F(StreamSequencerBufferTest, OnStreamDataWithOverlap) { |
| 217 std::string source(1024, 'a'); |
| 218 // Write something into [800, 1824) |
| 219 size_t written; |
| 220 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 221 QuicTime t1 = clock_.ApproximateNow(); |
| 222 EXPECT_EQ(QUIC_NO_ERROR, buffer_->OnStreamData(800, source, t1, &written)); |
| 223 // Try to write to [0, 1024) and [1024, 2048). |
| 224 // But no byte will be written since overlap. |
| 225 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 226 QuicTime t2 = clock_.ApproximateNow(); |
| 227 EXPECT_EQ(QUIC_INVALID_STREAM_DATA, |
| 228 buffer_->OnStreamData(0, source, t2, &written)); |
| 229 EXPECT_EQ(QUIC_INVALID_STREAM_DATA, |
| 230 buffer_->OnStreamData(1024, source, t2, &written)); |
| 231 auto frame_map = helper_->frame_arrival_time_map(); |
| 232 EXPECT_EQ(1u, frame_map->size()); |
| 233 EXPECT_EQ(t1, (*frame_map)[800].timestamp); |
| 234 } |
| 235 |
| 236 TEST_F(StreamSequencerBufferTest, OnStreamDataOverlapAndDuplicateCornerCases) { |
| 237 std::string source(1024, 'a'); |
| 238 // Write something into [800, 1824) |
| 239 size_t written; |
| 240 buffer_->OnStreamData(800, source, clock_.ApproximateNow(), &written); |
| 241 source = std::string(800, 'b'); |
| 242 // Try to write to [1, 801), but should fail due to overlapping |
| 243 EXPECT_EQ( |
| 244 QUIC_INVALID_STREAM_DATA, |
| 245 buffer_->OnStreamData(1, source, clock_.ApproximateNow(), &written)); |
| 246 // write to [0, 800) |
| 247 EXPECT_EQ(QUIC_NO_ERROR, buffer_->OnStreamData( |
| 248 0, source, clock_.ApproximateNow(), &written)); |
| 249 // Try to write one byte to [1823, 1824), but should count as duplicate |
| 250 std::string one_byte = "c"; |
| 251 EXPECT_EQ( |
| 252 QUIC_NO_ERROR, |
| 253 buffer_->OnStreamData(1823, one_byte, clock_.ApproximateNow(), &written)); |
| 254 EXPECT_EQ(0u, written); |
| 255 // write one byte to [1824, 1825) |
| 256 EXPECT_EQ( |
| 257 QUIC_NO_ERROR, |
| 258 buffer_->OnStreamData(1824, one_byte, clock_.ApproximateNow(), &written)); |
| 259 auto frame_map = helper_->frame_arrival_time_map(); |
| 260 EXPECT_EQ(3u, frame_map->size()); |
| 261 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 262 } |
| 263 |
| 264 TEST_F(StreamSequencerBufferTest, OnStreamDataWithoutOverlap) { |
| 265 std::string source(1024, 'a'); |
| 266 // Write something into [800, 1824). |
| 267 size_t written; |
| 268 EXPECT_EQ(QUIC_NO_ERROR, buffer_->OnStreamData( |
| 269 800, source, clock_.ApproximateNow(), &written)); |
| 270 source = std::string(100, 'b'); |
| 271 // Write something into [kBlockSizeBytes * 2 - 20, kBlockSizeBytes * 2 + 80). |
| 272 EXPECT_EQ(QUIC_NO_ERROR, |
| 273 buffer_->OnStreamData(kBlockSizeBytes * 2 - 20, source, |
| 274 clock_.ApproximateNow(), &written)); |
| 275 EXPECT_EQ(3, helper_->GapSize()); |
| 276 EXPECT_EQ(1024u + 100u, buffer_->BytesBuffered()); |
| 277 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 278 } |
| 279 |
| 280 TEST_F(StreamSequencerBufferTest, OnStreamDataTillEnd) { |
| 281 // Write 50 bytes to the end. |
| 282 const size_t kBytesToWrite = 50; |
| 283 std::string source(kBytesToWrite, 'a'); |
| 284 size_t written; |
| 285 EXPECT_EQ(QUIC_NO_ERROR, |
| 286 buffer_->OnStreamData(max_capacity_bytes_ - kBytesToWrite, source, |
| 287 clock_.ApproximateNow(), &written)); |
| 288 EXPECT_EQ(50u, buffer_->BytesBuffered()); |
| 289 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 290 } |
| 291 |
| 292 TEST_F(StreamSequencerBufferTest, OnStreamDataTillEndCorner) { |
| 293 // Write 1 byte to the end. |
| 294 const size_t kBytesToWrite = 1; |
| 295 std::string source(kBytesToWrite, 'a'); |
| 296 size_t written; |
| 297 EXPECT_EQ(QUIC_NO_ERROR, |
| 298 buffer_->OnStreamData(max_capacity_bytes_ - kBytesToWrite, source, |
| 299 clock_.ApproximateNow(), &written)); |
| 300 EXPECT_EQ(1u, buffer_->BytesBuffered()); |
| 301 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 302 } |
| 303 |
| 304 TEST_F(StreamSequencerBufferTest, OnStreamDataBeyondCapacity) { |
| 305 std::string source(60, 'a'); |
| 306 size_t written; |
| 307 EXPECT_EQ(QUIC_INTERNAL_ERROR, |
| 308 buffer_->OnStreamData(max_capacity_bytes_ - 50, source, |
| 309 clock_.ApproximateNow(), &written)); |
| 310 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 311 |
| 312 source = "b"; |
| 313 EXPECT_EQ(QUIC_INTERNAL_ERROR, |
| 314 buffer_->OnStreamData(max_capacity_bytes_, source, |
| 315 clock_.ApproximateNow(), &written)); |
| 316 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 317 |
| 318 EXPECT_EQ(QUIC_INTERNAL_ERROR, |
| 319 buffer_->OnStreamData(max_capacity_bytes_ * 1000, source, |
| 320 clock_.ApproximateNow(), &written)); |
| 321 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 322 EXPECT_EQ(0u, buffer_->BytesBuffered()); |
| 323 } |
| 324 |
| 325 TEST_F(StreamSequencerBufferTest, Readv100Bytes) { |
| 326 std::string source(1024, 'a'); |
| 327 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 328 QuicTime t1 = clock_.ApproximateNow(); |
| 329 // Write something into [kBlockSizeBytes, kBlockSizeBytes + 1024). |
| 330 size_t written; |
| 331 buffer_->OnStreamData(kBlockSizeBytes, source, t1, &written); |
| 332 EXPECT_FALSE(buffer_->HasBytesToRead()); |
| 333 source = std::string(100, 'b'); |
| 334 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 335 QuicTime t2 = clock_.ApproximateNow(); |
| 336 // Write something into [0, 100). |
| 337 buffer_->OnStreamData(0, source, t2, &written); |
| 338 EXPECT_TRUE(buffer_->HasBytesToRead()); |
| 339 EXPECT_EQ(2u, helper_->frame_arrival_time_map()->size()); |
| 340 // Read into a iovec array with total capacity of 120 bytes. |
| 341 char dest[120]; |
| 342 iovec iovecs[3]{iovec{dest, 40}, iovec{dest + 40, 40}, iovec{dest + 80, 40}}; |
| 343 size_t read = buffer_->Readv(iovecs, 3); |
| 344 EXPECT_EQ(100u, read); |
| 345 EXPECT_EQ(100u, buffer_->BytesConsumed()); |
| 346 EXPECT_EQ(source, std::string(dest, read)); |
| 347 EXPECT_EQ(1u, helper_->frame_arrival_time_map()->size()); |
| 348 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 349 } |
| 350 |
| 351 TEST_F(StreamSequencerBufferTest, ReadvAcrossBlocks) { |
| 352 std::string source(kBlockSizeBytes + 50, 'a'); |
| 353 // Write 1st block to full and extand 50 bytes to next block. |
| 354 size_t written; |
| 355 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 356 EXPECT_EQ(source.size(), helper_->ReadableBytes()); |
| 357 // Iteratively read 512 bytes from buffer_-> Overwrite dest[] each time. |
| 358 char dest[512]; |
| 359 while (helper_->ReadableBytes()) { |
| 360 std::fill(dest, dest + 512, 0); |
| 361 iovec iovecs[2]{iovec{dest, 256}, iovec{dest + 256, 256}}; |
| 362 buffer_->Readv(iovecs, 2); |
| 363 } |
| 364 // The last read only reads the rest 50 bytes in 2nd block. |
| 365 EXPECT_EQ(std::string(50, 'a'), std::string(dest, 50)); |
| 366 EXPECT_EQ(0, dest[50]) << "Dest[50] shouln't be filled."; |
| 367 EXPECT_EQ(source.size(), buffer_->BytesConsumed()); |
| 368 EXPECT_TRUE(buffer_->Empty()); |
| 369 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 370 } |
| 371 |
| 372 TEST_F(StreamSequencerBufferTest, ClearAfterRead) { |
| 373 std::string source(kBlockSizeBytes + 50, 'a'); |
| 374 // Write 1st block to full with 'a'. |
| 375 size_t written; |
| 376 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 377 // Read first 512 bytes from buffer to make space at the beginning. |
| 378 char dest[512]{0}; |
| 379 const iovec iov{dest, 512}; |
| 380 buffer_->Readv(&iov, 1); |
| 381 // Clear() should make buffer empty while preserving BytesConsumed() |
| 382 buffer_->Clear(); |
| 383 EXPECT_TRUE(buffer_->Empty()); |
| 384 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 385 } |
| 386 |
| 387 TEST_F(StreamSequencerBufferTest, OnStreamDataAcrossLastBlockAndFillCapacity) { |
| 388 std::string source(kBlockSizeBytes + 50, 'a'); |
| 389 // Write 1st block to full with 'a'. |
| 390 size_t written; |
| 391 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 392 // Read first 512 bytes from buffer to make space at the beginning. |
| 393 char dest[512]{0}; |
| 394 const iovec iov{dest, 512}; |
| 395 buffer_->Readv(&iov, 1); |
| 396 EXPECT_EQ(source.size(), written); |
| 397 |
| 398 // Write more than half block size of bytes in the last block with 'b', which |
| 399 // will wrap to the beginning and reaches the full capacity. |
| 400 source = std::string(0.5 * kBlockSizeBytes + 512, 'b'); |
| 401 EXPECT_EQ(QUIC_NO_ERROR, |
| 402 buffer_->OnStreamData(2 * kBlockSizeBytes, source, |
| 403 clock_.ApproximateNow(), &written)); |
| 404 EXPECT_EQ(source.size(), written); |
| 405 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 406 } |
| 407 |
| 408 TEST_F(StreamSequencerBufferTest, |
| 409 OnStreamDataAcrossLastBlockAndExceedCapacity) { |
| 410 std::string source(kBlockSizeBytes + 50, 'a'); |
| 411 // Write 1st block to full. |
| 412 size_t written; |
| 413 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 414 // Read first 512 bytes from buffer to make space at the beginning. |
| 415 char dest[512]{0}; |
| 416 const iovec iov{dest, 512}; |
| 417 buffer_->Readv(&iov, 1); |
| 418 |
| 419 // Try to write from [max_capacity_bytes_ - 0.5 * kBlockSizeBytes, |
| 420 // max_capacity_bytes_ + 512 + 1). But last bytes exceeds current capacity. |
| 421 source = std::string(0.5 * kBlockSizeBytes + 512 + 1, 'b'); |
| 422 EXPECT_EQ(QUIC_INTERNAL_ERROR, |
| 423 buffer_->OnStreamData(2 * kBlockSizeBytes, source, |
| 424 clock_.ApproximateNow(), &written)); |
| 425 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 426 } |
| 427 |
| 428 TEST_F(StreamSequencerBufferTest, ReadvAcrossLastBlock) { |
| 429 // Write to full capacity and read out 512 bytes at beginning and continue |
| 430 // appending 256 bytes. |
| 431 std::string source(max_capacity_bytes_, 'a'); |
| 432 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 433 QuicTime t = clock_.ApproximateNow(); |
| 434 size_t written; |
| 435 buffer_->OnStreamData(0, source, t, &written); |
| 436 char dest[512]{0}; |
| 437 const iovec iov{dest, 512}; |
| 438 buffer_->Readv(&iov, 1); |
| 439 source = std::string(256, 'b'); |
| 440 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 441 QuicTime t2 = clock_.ApproximateNow(); |
| 442 buffer_->OnStreamData(max_capacity_bytes_, source, t2, &written); |
| 443 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 444 EXPECT_EQ(2u, helper_->frame_arrival_time_map()->size()); |
| 445 |
| 446 // Read all data out. |
| 447 std::unique_ptr<char[]> dest1{new char[max_capacity_bytes_]{0}}; |
| 448 const iovec iov1{dest1.get(), max_capacity_bytes_}; |
| 449 EXPECT_EQ(max_capacity_bytes_ - 512 + 256, buffer_->Readv(&iov1, 1)); |
| 450 EXPECT_EQ(max_capacity_bytes_ + 256, buffer_->BytesConsumed()); |
| 451 EXPECT_TRUE(buffer_->Empty()); |
| 452 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 453 EXPECT_EQ(0u, helper_->frame_arrival_time_map()->size()); |
| 454 } |
| 455 |
| 456 TEST_F(StreamSequencerBufferTest, ReadvEmpty) { |
| 457 char dest[512]{0}; |
| 458 iovec iov{dest, 512}; |
| 459 size_t read = buffer_->Readv(&iov, 1); |
| 460 EXPECT_EQ(0u, read); |
| 461 EXPECT_TRUE(helper_->CheckBufferInvariants()); |
| 462 } |
| 463 |
| 464 TEST_F(StreamSequencerBufferTest, GetReadableRegionsEmpty) { |
| 465 iovec iovs[2]; |
| 466 int iov_count = buffer_->GetReadableRegions(iovs, 2); |
| 467 EXPECT_EQ(0, iov_count); |
| 468 EXPECT_EQ(nullptr, iovs[iov_count].iov_base); |
| 469 EXPECT_EQ(0u, iovs[iov_count].iov_len); |
| 470 } |
| 471 |
| 472 TEST_F(StreamSequencerBufferTest, GetReadableRegionsBlockedByGap) { |
| 473 // Write into [1, 1024). |
| 474 std::string source(1023, 'a'); |
| 475 size_t written; |
| 476 buffer_->OnStreamData(1, source, clock_.ApproximateNow(), &written); |
| 477 // Try to get readable regions, but none is there. |
| 478 iovec iovs[2]; |
| 479 int iov_count = buffer_->GetReadableRegions(iovs, 2); |
| 480 EXPECT_EQ(0, iov_count); |
| 481 } |
| 482 |
| 483 TEST_F(StreamSequencerBufferTest, GetReadableRegionsTillEndOfBlock) { |
| 484 // Write first block to full with [0, 256) 'a' and the rest 'b' then read out |
| 485 // [0, 256) |
| 486 std::string source(kBlockSizeBytes, 'a'); |
| 487 size_t written; |
| 488 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 489 char dest[256]; |
| 490 helper_->Read(dest, 256); |
| 491 // Get readable region from [256, 1024) |
| 492 iovec iovs[2]; |
| 493 int iov_count = buffer_->GetReadableRegions(iovs, 2); |
| 494 EXPECT_EQ(1, iov_count); |
| 495 EXPECT_EQ(std::string(kBlockSizeBytes - 256, 'a'), |
| 496 std::string(reinterpret_cast<const char*>(iovs[0].iov_base), |
| 497 iovs[0].iov_len)); |
| 498 } |
| 499 |
| 500 TEST_F(StreamSequencerBufferTest, GetReadableRegionsWithinOneBlock) { |
| 501 // Write into [0, 1024) and then read out [0, 256) |
| 502 std::string source(1024, 'a'); |
| 503 size_t written; |
| 504 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 505 char dest[256]; |
| 506 helper_->Read(dest, 256); |
| 507 // Get readable region from [256, 1024) |
| 508 iovec iovs[2]; |
| 509 int iov_count = buffer_->GetReadableRegions(iovs, 2); |
| 510 EXPECT_EQ(1, iov_count); |
| 511 EXPECT_EQ(std::string(1024 - 256, 'a'), |
| 512 std::string(reinterpret_cast<const char*>(iovs[0].iov_base), |
| 513 iovs[0].iov_len)); |
| 514 } |
| 515 |
| 516 TEST_F(StreamSequencerBufferTest, GetReadableRegionsAcrossBlockWithLongIOV) { |
| 517 // Write into [0, 2 * kBlockSizeBytes + 1024) and then read out [0, 1024) |
| 518 std::string source(2 * kBlockSizeBytes + 1024, 'a'); |
| 519 size_t written; |
| 520 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 521 char dest[1024]; |
| 522 helper_->Read(dest, 1024); |
| 523 |
| 524 iovec iovs[4]; |
| 525 int iov_count = buffer_->GetReadableRegions(iovs, 4); |
| 526 EXPECT_EQ(3, iov_count); |
| 527 EXPECT_EQ(kBlockSizeBytes - 1024, iovs[0].iov_len); |
| 528 EXPECT_EQ(kBlockSizeBytes, iovs[1].iov_len); |
| 529 EXPECT_EQ(1024u, iovs[2].iov_len); |
| 530 } |
| 531 |
| 532 TEST_F(StreamSequencerBufferTest, GetReadableRegionsWithMultipleIOVsAcrossEnd) { |
| 533 // Write into [0, 2 * kBlockSizeBytes + 1024) and then read out [0, 1024) |
| 534 // and then append 1024 + 512 bytes. |
| 535 std::string source(2.5 * kBlockSizeBytes - 1024, 'a'); |
| 536 size_t written; |
| 537 buffer_->OnStreamData(0, source, clock_.ApproximateNow(), &written); |
| 538 char dest[1024]; |
| 539 helper_->Read(dest, 1024); |
| 540 // Write across the end. |
| 541 source = std::string(1024 + 512, 'b'); |
| 542 buffer_->OnStreamData(2.5 * kBlockSizeBytes - 1024, source, |
| 543 clock_.ApproximateNow(), &written); |
| 544 // Use short iovec's. |
| 545 iovec iovs[2]; |
| 546 int iov_count = buffer_->GetReadableRegions(iovs, 2); |
| 547 EXPECT_EQ(2, iov_count); |
| 548 EXPECT_EQ(kBlockSizeBytes - 1024, iovs[0].iov_len); |
| 549 EXPECT_EQ(kBlockSizeBytes, iovs[1].iov_len); |
| 550 // Use long iovec's and wrap the end of buffer. |
| 551 iovec iovs1[5]; |
| 552 EXPECT_EQ(4, buffer_->GetReadableRegions(iovs1, 5)); |
| 553 EXPECT_EQ(0.5 * kBlockSizeBytes, iovs1[2].iov_len); |
| 554 EXPECT_EQ(512u, iovs1[3].iov_len); |
| 555 EXPECT_EQ(std::string(512, 'b'), |
| 556 std::string(reinterpret_cast<const char*>(iovs1[3].iov_base), |
| 557 iovs1[3].iov_len)); |
| 558 } |
| 559 |
| 560 TEST_F(StreamSequencerBufferTest, GetReadableRegionEmpty) { |
| 561 iovec iov; |
| 562 QuicTime t = QuicTime::Zero(); |
| 563 EXPECT_FALSE(buffer_->GetReadableRegion(&iov, &t)); |
| 564 EXPECT_EQ(nullptr, iov.iov_base); |
| 565 EXPECT_EQ(0u, iov.iov_len); |
| 566 } |
| 567 |
| 568 TEST_F(StreamSequencerBufferTest, GetReadableRegionBeforeGap) { |
| 569 // Write into [1, 1024). |
| 570 std::string source(1023, 'a'); |
| 571 size_t written; |
| 572 buffer_->OnStreamData(1, source, clock_.ApproximateNow(), &written); |
| 573 // GetReadableRegion should return false because range [0,1) hasn't been |
| 574 // filled yet. |
| 575 iovec iov; |
| 576 QuicTime t = QuicTime::Zero(); |
| 577 EXPECT_FALSE(buffer_->GetReadableRegion(&iov, &t)); |
| 578 } |
| 579 |
| 580 TEST_F(StreamSequencerBufferTest, GetReadableRegionTillEndOfBlock) { |
| 581 // Write into [0, kBlockSizeBytes + 1) and then read out [0, 256) |
| 582 std::string source(kBlockSizeBytes + 1, 'a'); |
| 583 size_t written; |
| 584 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 585 QuicTime t = clock_.ApproximateNow(); |
| 586 buffer_->OnStreamData(0, source, t, &written); |
| 587 char dest[256]; |
| 588 helper_->Read(dest, 256); |
| 589 // Get readable region from [256, 1024) |
| 590 iovec iov; |
| 591 QuicTime t2 = QuicTime::Zero(); |
| 592 EXPECT_TRUE(buffer_->GetReadableRegion(&iov, &t2)); |
| 593 EXPECT_EQ(t, t2); |
| 594 EXPECT_EQ( |
| 595 std::string(kBlockSizeBytes - 256, 'a'), |
| 596 std::string(reinterpret_cast<const char*>(iov.iov_base), iov.iov_len)); |
| 597 } |
| 598 |
| 599 TEST_F(StreamSequencerBufferTest, GetReadableRegionTillGap) { |
| 600 // Write into [0, kBlockSizeBytes - 1) and then read out [0, 256) |
| 601 std::string source(kBlockSizeBytes - 1, 'a'); |
| 602 size_t written; |
| 603 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 604 QuicTime t = clock_.ApproximateNow(); |
| 605 buffer_->OnStreamData(0, source, t, &written); |
| 606 char dest[256]; |
| 607 helper_->Read(dest, 256); |
| 608 // Get readable region from [256, 1023) |
| 609 iovec iov; |
| 610 QuicTime t2 = QuicTime::Zero(); |
| 611 EXPECT_TRUE(buffer_->GetReadableRegion(&iov, &t2)); |
| 612 EXPECT_EQ(t, t2); |
| 613 EXPECT_EQ( |
| 614 std::string(kBlockSizeBytes - 1 - 256, 'a'), |
| 615 std::string(reinterpret_cast<const char*>(iov.iov_base), iov.iov_len)); |
| 616 } |
| 617 |
| 618 TEST_F(StreamSequencerBufferTest, GetReadableRegionByArrivalTime) { |
| 619 // Write into [0, kBlockSizeBytes - 100) and then read out [0, 256) |
| 620 std::string source(kBlockSizeBytes - 100, 'a'); |
| 621 size_t written; |
| 622 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 623 QuicTime t = clock_.ApproximateNow(); |
| 624 buffer_->OnStreamData(0, source, t, &written); |
| 625 char dest[256]; |
| 626 helper_->Read(dest, 256); |
| 627 // Write into [kBlockSizeBytes - 100, kBlockSizeBytes - 50)] in same time |
| 628 std::string source2(50, 'b'); |
| 629 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 630 buffer_->OnStreamData(kBlockSizeBytes - 100, source2, t, &written); |
| 631 |
| 632 // Write into [kBlockSizeBytes - 50, kBlockSizeBytes)] in another time |
| 633 std::string source3(50, 'c'); |
| 634 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 635 QuicTime t3 = clock_.ApproximateNow(); |
| 636 buffer_->OnStreamData(kBlockSizeBytes - 50, source3, t3, &written); |
| 637 |
| 638 // Get readable region from [256, 1024 - 50) |
| 639 iovec iov; |
| 640 QuicTime t4 = QuicTime::Zero(); |
| 641 EXPECT_TRUE(buffer_->GetReadableRegion(&iov, &t4)); |
| 642 EXPECT_EQ(t, t4); |
| 643 EXPECT_EQ( |
| 644 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, clock_.ApproximateNow(), |
| 741 &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] = |
| 787 OffsetSizePair(start_chopping_offset, 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 << " write result: " << result |
| 831 << " left over: " << shuffled_buf_.size(); |
| 832 } |
| 833 |
| 834 protected: |
| 835 std::list<OffsetSizePair> shuffled_buf_; |
| 836 size_t max_chunk_size_bytes_; |
| 837 QuicStreamOffset bytes_to_buffer_; |
| 838 size_t total_bytes_written_ = 0; |
| 839 size_t total_bytes_read_ = 0; |
| 840 SimpleRandom rng_; |
| 841 }; |
| 842 |
| 843 TEST_F(StreamSequencerBufferRandomIOTest, RandomWriteAndReadv) { |
| 844 // Set kMaxReadSize larger than kBlockSizeBytes to test both small and large |
| 845 // read. |
| 846 const size_t kMaxReadSize = kBlockSizeBytes * 2; |
| 847 // kNumReads is larger than 1 to test how multiple read destinations work. |
| 848 const size_t kNumReads = 2; |
| 849 // Since write and read operation have equal possibility to be called. Bytes |
| 850 // to be written into and read out of should roughly the same. |
| 851 const size_t kMaxWriteSize = kNumReads * kMaxReadSize; |
| 852 size_t iterations = 0; |
| 853 |
| 854 CreateSourceAndShuffle(kMaxWriteSize); |
| 855 |
| 856 while ((!shuffled_buf_.empty() || total_bytes_read_ < bytes_to_buffer_) && |
| 857 iterations <= 2 * bytes_to_buffer_) { |
| 858 uint8 next_action = |
| 859 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, dest_iov, kNumReads)) |
| 886 << " at iteration " << iterations; |
| 887 } |
| 888 total_bytes_read_ += actually_read; |
| 889 ASSERT_EQ(total_bytes_read_, buffer_->BytesConsumed()); |
| 890 ASSERT_TRUE(helper_->CheckBufferInvariants()); |
| 891 break; |
| 892 } |
| 893 } |
| 894 ++iterations; |
| 895 ASSERT_LE(total_bytes_read_, total_bytes_written_); |
| 896 } |
| 897 EXPECT_LT(iterations, bytes_to_buffer_) << "runaway test"; |
| 898 EXPECT_LE(bytes_to_buffer_, total_bytes_read_) << "iterations: " |
| 899 << iterations; |
| 900 EXPECT_LE(bytes_to_buffer_, total_bytes_written_); |
| 901 } |
| 902 |
| 903 TEST_F(StreamSequencerBufferRandomIOTest, RandomWriteAndConsumeInPlace) { |
| 904 // The value 4 is chosen such that the max write size is no larger than the |
| 905 // maximum buffer capacity. |
| 906 const size_t kMaxNumReads = 4; |
| 907 // Adjust write amount be roughly equal to that GetReadableRegions() can get. |
| 908 const size_t kMaxWriteSize = kMaxNumReads * kBlockSizeBytes; |
| 909 ASSERT_LE(kMaxWriteSize, max_capacity_bytes_); |
| 910 size_t iterations = 0; |
| 911 |
| 912 CreateSourceAndShuffle(kMaxWriteSize); |
| 913 |
| 914 while ((!shuffled_buf_.empty() || total_bytes_read_ < bytes_to_buffer_) && |
| 915 iterations <= 2 * bytes_to_buffer_) { |
| 916 uint8 next_action = |
| 917 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 = |
| 930 buffer_->GetReadableRegions(dest_iov, num_read); |
| 931 ASSERT_LE(actually_num_read, num_read); |
| 932 size_t avail_bytes = 0; |
| 933 for (size_t i = 0; i < actually_num_read; ++i) { |
| 934 avail_bytes += dest_iov[i].iov_len; |
| 935 } |
| 936 // process random number of bytes (check the value of each byte). |
| 937 size_t bytes_to_process = rng_.RandUint64() % (avail_bytes + 1); |
| 938 size_t bytes_processed = 0; |
| 939 for (size_t i = 0; i < actually_num_read; ++i) { |
| 940 size_t bytes_in_block = min<size_t>( |
| 941 bytes_to_process - bytes_processed, dest_iov[i].iov_len); |
| 942 if (bytes_in_block == 0) { |
| 943 break; |
| 944 } |
| 945 for (size_t j = 0; j < bytes_in_block; ++j) { |
| 946 ASSERT_LE(bytes_processed, bytes_to_process); |
| 947 char char_expected = |
| 948 (buffer_->BytesConsumed() + bytes_processed) % 256; |
| 949 ASSERT_EQ(char_expected, |
| 950 reinterpret_cast<const char*>(dest_iov[i].iov_base)[j]) |
| 951 << " at iteration " << iterations; |
| 952 ++bytes_processed; |
| 953 } |
| 954 } |
| 955 |
| 956 buffer_->MarkConsumed(bytes_processed); |
| 957 |
| 958 DVLOG(1) << "iteration " << iterations << ": try to get " << num_read |
| 959 << " readable regions, actually get " << actually_num_read |
| 960 << " from offset: " << total_bytes_read_ |
| 961 << "\nprocesse bytes: " << bytes_processed; |
| 962 total_bytes_read_ += bytes_processed; |
| 963 ASSERT_EQ(total_bytes_read_, buffer_->BytesConsumed()); |
| 964 ASSERT_TRUE(helper_->CheckBufferInvariants()); |
| 965 break; |
| 966 } |
| 967 } |
| 968 ++iterations; |
| 969 ASSERT_LE(total_bytes_read_, total_bytes_written_); |
| 970 } |
| 971 EXPECT_LT(iterations, bytes_to_buffer_) << "runaway test"; |
| 972 EXPECT_LE(bytes_to_buffer_, total_bytes_read_) << "iterations: " |
| 973 << iterations; |
| 974 EXPECT_LE(bytes_to_buffer_, total_bytes_written_); |
| 975 } |
| 976 |
| 977 } // anonymous namespace |
| 978 |
| 979 } // namespace test |
| 980 |
| 981 } // namespace net |
OLD | NEW |