OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/spdy/spdy_read_queue.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <cstddef> |
| 9 #include <string> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/stl_util.h" |
| 13 #include "net/spdy/spdy_buffer.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const char kData[] = "SPDY read queue test data.\0Some more data."; |
| 21 const size_t kDataSize = arraysize(kData); |
| 22 |
| 23 // Enqueues |data| onto |queue| in chunks of at most |max_buffer_size| |
| 24 // bytes. |
| 25 void EnqueueString(const std::string& data, |
| 26 size_t max_buffer_size, |
| 27 SpdyReadQueue* queue) { |
| 28 ASSERT_GT(data.size(), 0u); |
| 29 ASSERT_GT(max_buffer_size, 0u); |
| 30 size_t old_total_size = queue->GetTotalSize(); |
| 31 for (size_t i = 0; i < data.size();) { |
| 32 size_t buffer_size = std::min(data.size() - i, max_buffer_size); |
| 33 queue->Enqueue( |
| 34 scoped_ptr<SpdyBuffer>(new SpdyBuffer(data.data() + i, buffer_size))); |
| 35 i += buffer_size; |
| 36 EXPECT_FALSE(queue->IsEmpty()); |
| 37 EXPECT_EQ(old_total_size + i, queue->GetTotalSize()); |
| 38 } |
| 39 } |
| 40 |
| 41 // Dequeues all bytes in |queue| in chunks of at most |
| 42 // |max_buffer_size| bytes and returns the data as a string. |
| 43 std::string DrainToString(size_t max_buffer_size, SpdyReadQueue* queue) { |
| 44 std::string data; |
| 45 std::string buffer(max_buffer_size, '\0'); |
| 46 while (!queue->IsEmpty()) { |
| 47 size_t old_total_size = queue->GetTotalSize(); |
| 48 EXPECT_GT(old_total_size, 0u); |
| 49 size_t dequeued_bytes = |
| 50 queue->Dequeue(string_as_array(&buffer), max_buffer_size); |
| 51 data.append(buffer.data(), dequeued_bytes); |
| 52 EXPECT_EQ(dequeued_bytes, std::min(max_buffer_size, dequeued_bytes)); |
| 53 EXPECT_EQ(queue->GetTotalSize(), old_total_size - dequeued_bytes); |
| 54 } |
| 55 EXPECT_TRUE(queue->IsEmpty()); |
| 56 return data; |
| 57 } |
| 58 |
| 59 class SpdyReadQueueTest : public ::testing::Test {}; |
| 60 |
| 61 // Enqueue a string in one chunk and dequeue it in one chunk. The |
| 62 // resulting string should be identical to the enqueued string. |
| 63 TEST_F(SpdyReadQueueTest, LargeBuffer) { |
| 64 std::string data(kData, kDataSize); |
| 65 SpdyReadQueue read_queue; |
| 66 EnqueueString(data, 2 * kDataSize, &read_queue); |
| 67 const std::string& drained_data = DrainToString(2 * kDataSize, &read_queue); |
| 68 EXPECT_EQ(data, drained_data); |
| 69 } |
| 70 |
| 71 // Enqueue a string one byte at a time and dequeue it one byte at a |
| 72 // time. The resulting string should be identical to the enqueued |
| 73 // string. |
| 74 TEST_F(SpdyReadQueueTest, OneByteBuffer) { |
| 75 std::string data(kData, kDataSize); |
| 76 SpdyReadQueue read_queue; |
| 77 EnqueueString(data, 1, &read_queue); |
| 78 const std::string& drained_data = DrainToString(1, &read_queue); |
| 79 EXPECT_EQ(data, drained_data); |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 |
| 84 } // namespace net |
OLD | NEW |