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

Unified Diff: net/spdy/spdy_read_queue_unittest.cc

Issue 13990005: [SPDY] Replace SpdyIOBuffer with new SpdyBuffer class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months 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 side-by-side diff with in-line comments
Download patch
Index: net/spdy/spdy_read_queue_unittest.cc
diff --git a/net/spdy/spdy_read_queue_unittest.cc b/net/spdy/spdy_read_queue_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2e71638f1ee952ab43ef3b392f94242b49789eb7
--- /dev/null
+++ b/net/spdy/spdy_read_queue_unittest.cc
@@ -0,0 +1,84 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/spdy/spdy_read_queue.h"
+
+#include <algorithm>
+#include <cstddef>
+#include <string>
+
+#include "base/memory/scoped_ptr.h"
+#include "base/stl_util.h"
+#include "net/spdy/spdy_buffer.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace {
+
+const char kData[] = "SPDY read queue test data.\0Some more data.";
+const size_t kDataSize = arraysize(kData);
+
+// Enqueues |data| onto |queue| in chunks of at most |max_buffer_size|
+// bytes.
+void EnqueueString(const std::string& data,
+ size_t max_buffer_size,
+ SpdyReadQueue* queue) {
+ ASSERT_GT(data.size(), 0u);
+ ASSERT_GT(max_buffer_size, 0u);
+ size_t old_total_size = queue->GetTotalSize();
+ for (size_t i = 0; i < data.size();) {
+ size_t buffer_size = std::min(data.size() - i, max_buffer_size);
+ queue->Enqueue(
+ scoped_ptr<SpdyBuffer>(new SpdyBuffer(data.data() + i, buffer_size)));
+ i += buffer_size;
+ EXPECT_FALSE(queue->IsEmpty());
+ EXPECT_EQ(old_total_size + i, queue->GetTotalSize());
+ }
+}
+
+// Dequeues all bytes in |queue| in chunks of at most
+// |max_buffer_size| bytes and returns the data as a string.
+std::string DrainToString(size_t max_buffer_size, SpdyReadQueue* queue) {
+ std::string data;
+ std::string buffer(max_buffer_size, '\0');
+ while (!queue->IsEmpty()) {
+ size_t old_total_size = queue->GetTotalSize();
+ EXPECT_GT(old_total_size, 0u);
+ size_t dequeued_bytes =
+ queue->Dequeue(string_as_array(&buffer), max_buffer_size);
+ data.append(buffer.data(), dequeued_bytes);
+ EXPECT_EQ(dequeued_bytes, std::min(max_buffer_size, dequeued_bytes));
+ EXPECT_EQ(queue->GetTotalSize(), old_total_size - dequeued_bytes);
+ }
+ EXPECT_TRUE(queue->IsEmpty());
+ return data;
+}
+
+class SpdyReadQueueTest : public ::testing::Test {};
+
+// Enqueue a string in one chunk and dequeue it in one chunk. The
+// resulting string should be identical to the enqueued string.
+TEST_F(SpdyReadQueueTest, LargeBuffer) {
+ std::string data(kData, kDataSize);
+ SpdyReadQueue read_queue;
+ EnqueueString(data, 2 * kDataSize, &read_queue);
+ const std::string& drained_data = DrainToString(2 * kDataSize, &read_queue);
+ EXPECT_EQ(data, drained_data);
+}
+
+// Enqueue a string one byte at a time and dequeue it one byte at a
+// time. The resulting string should be identical to the enqueued
+// string.
+TEST_F(SpdyReadQueueTest, OneByteBuffer) {
+ std::string data(kData, kDataSize);
+ SpdyReadQueue read_queue;
+ EnqueueString(data, 1, &read_queue);
+ const std::string& drained_data = DrainToString(1, &read_queue);
+ EXPECT_EQ(data, drained_data);
+}
+
+} // namespace
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698