Index: net/quic/quic_buffer_pool_test.cc |
diff --git a/net/quic/quic_buffer_pool_test.cc b/net/quic/quic_buffer_pool_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..87425d0265d264097a915d13da92098231980751 |
--- /dev/null |
+++ b/net/quic/quic_buffer_pool_test.cc |
@@ -0,0 +1,158 @@ |
+// Copyright (c) 2016 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/quic/quic_buffer_pool.h" |
+ |
+#include "net/test/gtest_util.h" |
+ |
+namespace net { |
+ |
+class QuicBufferPoolTest : public ::testing::Test { |
+ public: |
+ template <typename BufferPool> |
+ static int GetMaxRegion(const BufferPool& pool) { |
+ return pool.max_region_; |
+ } |
+}; |
+ |
+TEST_F(QuicBufferPoolTest, NewDelete) { |
+ QuicBufferPool<1024, 1> l; |
+ |
+ char* b1 = l.New(1024); |
+ char* b2 = l.New(1024); |
+ char* b3 = l.New(1024); |
+ |
+ ASSERT_NE(nullptr, b1); |
+ ASSERT_NE(nullptr, b2); |
+ ASSERT_NE(nullptr, b3); |
+ |
+ l.Delete(b3); |
+ |
+ char* b4 = l.New(1024); |
+ // Should reuse b3. |
+ EXPECT_EQ(b4, b3); |
+ |
+ l.Delete(b1); |
+ l.Delete(b2); |
+ |
+ char* b5 = l.New(1024); |
+ // Should reuse b2. |
+ EXPECT_EQ(b5, b2); |
+ |
+ char* b6 = l.New(1024); |
+ // Should reuse b1. |
+ EXPECT_EQ(b6, b1); |
+ |
+ l.Delete(b4); |
+ l.Delete(b5); |
+ l.Delete(b6); |
+ |
+ EXPECT_EQ(1, GetMaxRegion(l)); |
+} |
+ |
+TEST_F(QuicBufferPoolTest, OutstandingAllocations) { |
+ scoped_ptr<QuicBufferPool<1024, 1>> l(new QuicBufferPool<1024, 1>()); |
+ l->New(1024); |
+ EXPECT_DFATAL(l.reset(), "unfreed allocations"); |
+} |
+ |
+TEST_F(QuicBufferPoolTest, HugeBuffer) { |
+ QuicBufferPool<1024 * 1024, 1> l; |
+ char* b1 = l.New(1024 * 1024); |
+ char* b2 = l.New(1024 * 1024); |
+ char* b3 = l.New(1024 * 1024); |
+ |
+ ASSERT_NE(nullptr, b1); |
+ ASSERT_NE(nullptr, b2); |
+ ASSERT_NE(nullptr, b3); |
+ |
+ l.Delete(b3); |
+ l.Delete(b2); |
+ l.Delete(b1); |
+ |
+ char* b4 = l.New(1024 * 1024); |
+ // Should reuse b1. |
+ EXPECT_EQ(b4, b1); |
+ |
+ l.Delete(b4); |
+ EXPECT_EQ(1, GetMaxRegion(l)); |
+} |
+ |
+TEST_F(QuicBufferPoolTest, DeleteNull) { |
+ QuicBufferPool<1024, 1> l; |
+ l.Delete(nullptr); |
+ EXPECT_EQ(0, GetMaxRegion(l)); |
+} |
+ |
+TEST_F(QuicBufferPoolTest, Saturate) { |
+ QuicBufferPool<1024 * 1024, 3, 2 * 1024 * 1024> l; |
+ std::vector<char*> buffers; |
+ for (int i = 0; i < 3; ++i) { |
+ buffers.push_back(l.New(1024 * 1024)); |
+ ASSERT_NE(nullptr, buffers.back()); |
+ } |
+ for (int i = 0; i < 2; ++i) { |
+ EXPECT_DFATAL(buffers.push_back(l.New(1024 * 1024)), |
+ "Ran out of room in QuicBufferPool"); |
+ } |
+ for (char* buffer : buffers) { |
+ l.Delete(buffer); |
+ } |
+ EXPECT_EQ(1, GetMaxRegion(l)); |
+} |
+ |
+TEST_F(QuicBufferPoolTest, Flaggable) { |
+ QuicBufferPool<1024, 1> l; |
+ char* b1 = l.New(1024); |
+ ASSERT_NE(nullptr, b1); |
+ l.Delete(b1); |
+ |
+ // Flagged on should return b1. |
+ ASSERT_EQ(b1, l.New(1024, true)); |
+ l.Delete(b1); |
+ |
+ // Flagged off should not return b1. |
+ char* b2 = l.New(1024, false); |
+ ASSERT_NE(b2, b1); |
+ l.Delete(b2); |
+ EXPECT_EQ(1, GetMaxRegion(l)); |
+} |
+ |
+TEST_F(QuicBufferPoolTest, OverSized) { |
+ QuicBufferPool<1024, 1> l; |
+ char* b1 = nullptr; |
+ EXPECT_DFATAL(b1 = l.New(2 * 1024 * 1024), "greater than pool buffer size"); |
+ ASSERT_NE(nullptr, b1); |
+ l.Delete(b1); |
+ EXPECT_EQ(0, GetMaxRegion(l)); |
+} |
+ |
+TEST_F(QuicBufferPoolTest, ReleaseReuse) { |
+ QuicBufferPool<1024, 1> l; |
+ char* b1 = l.New(1024); |
+ ASSERT_NE(nullptr, b1); |
+ |
+ // This should have no effect. |
+ l.MarkAllocatorIdle(); |
+ EXPECT_EQ(1, GetMaxRegion(l)); |
+ |
+ l.Delete(b1); |
+ |
+ // There should still be one region left. |
+ EXPECT_EQ(1, GetMaxRegion(l)); |
+ |
+ l.MarkAllocatorIdle(); |
+ // After the allocator is marked as idle, with no allocations pending, the |
+ // allocator should not take up any space. |
+ EXPECT_EQ(0, GetMaxRegion(l)); |
+ |
+ // The allocator should still work after though. |
+ char* b2 = l.New(1024); |
+ ASSERT_NE(nullptr, b2); |
+ l.Delete(b2); |
+ |
+ EXPECT_EQ(1, GetMaxRegion(l)); |
+} |
+ |
+} // namespace net |