Index: content/browser/blob_storage/blob_memory_controller_unittest.cc |
diff --git a/content/browser/blob_storage/blob_memory_controller_unittest.cc b/content/browser/blob_storage/blob_memory_controller_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..da929d3110c0c4a5e16de48a91e4d4f5f6551997 |
--- /dev/null |
+++ b/content/browser/blob_storage/blob_memory_controller_unittest.cc |
@@ -0,0 +1,236 @@ |
+// Copyright 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 "storage/browser/blob/blob_memory_controller.h" |
+ |
+#include "base/bind.h" |
+#include "base/files/file_util.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/test/test_simple_task_runner.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "storage/browser/blob/blob_data_builder.h" |
+#include "storage/browser/blob/blob_data_item.h" |
+#include "storage/browser/blob/shareable_blob_data_item.h" |
+#include "storage/common/data_element.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace storage { |
+ |
+using Strategy = BlobMemoryController::Strategy; |
+using FileCreationInfo = BlobMemoryController::FileCreationInfo; |
+using base::TestSimpleTaskRunner; |
+using ItemState = ShareableBlobDataItem::State; |
+ |
+const std::string kBlobStorageDirectory = "blob_storage"; |
+const size_t kTestBlobStorageIPCThresholdBytes = 20; |
+const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; |
+const size_t kTestBlobStorageMaxBlobMemorySize = 400; |
+const size_t kTestBlobStorageInFlightMemory = 10; |
+const uint64_t kTestBlobStorageMaxDiskSpace = 1000; |
+const uint64_t kTestBlobStorageMinFileSizeBytes = 10; |
+const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
+ |
+class BlobMemoryControllerTest : public testing::Test { |
+ protected: |
+ BlobMemoryControllerTest() {} |
+ |
+ void SetUp() override { |
+ ASSERT_EQ(true, base::CreateNewTempDirectory("blob_storage", &temp_dir_)); |
+ }; |
+ |
+ void TearDown() override { |
+ files_created_.clear(); |
+ // Make sure we clean up the files. |
+ base::RunLoop().RunUntilIdle(); |
+ file_runner_->RunPendingTasks(); |
+ base::RunLoop().RunUntilIdle(); |
+ ASSERT_EQ(true, base::DeleteFile(temp_dir_, true)); |
+ } |
+ |
+ std::vector<scoped_refptr<ShareableBlobDataItem>> CreateSharedDataItems( |
+ const BlobDataBuilder& builder, |
+ const std::vector<ItemState>& states, |
+ std::vector<ShareableBlobDataItem*>* pointers_out) { |
+ std::vector<scoped_refptr<ShareableBlobDataItem>> result; |
+ EXPECT_EQ(builder.items_.size(), states.size()); |
+ for (size_t i = 0; i < builder.items_.size(); ++i) { |
+ result.push_back(make_scoped_refptr(new ShareableBlobDataItem( |
+ builder.uuid(), builder.items_[i], states[i]))); |
+ pointers_out->push_back(result.back().get()); |
+ } |
+ return result; |
+ } |
+ |
+ void SetTestMemoryLimits(BlobMemoryController* controller) { |
+ BlobStorageLimits limits; |
+ limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; |
+ limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; |
+ limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; |
+ limits.in_flight_space = kTestBlobStorageInFlightMemory; |
+ limits.max_blob_disk_space = kTestBlobStorageMaxDiskSpace; |
+ limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; |
+ limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; |
+ controller->SetLimitsForTesting(limits); |
+ } |
+ |
+ void SaveFileCreationInfo(bool success, std::vector<FileCreationInfo> info) { |
+ file_quota_result_ = success; |
+ if (success) |
+ files_created_.swap(info); |
+ } |
+ |
+ void SaveMemoryRequest(bool success) { memory_quota_result_ = success; } |
+ |
+ BlobMemoryController::FileQuotaRequestCallback GetFileCreationCallback() { |
+ return base::Bind(&BlobMemoryControllerTest::SaveFileCreationInfo, |
+ base::Unretained(this)); |
+ } |
+ |
+ BlobMemoryController::MemoryQuotaRequestCallback GetMemoryRequestCallback() { |
+ return base::Bind(&BlobMemoryControllerTest::SaveMemoryRequest, |
+ base::Unretained(this)); |
+ } |
+ |
+ void SetItemState(ShareableBlobDataItem* item, ItemState state) { |
+ item->state_ = state; |
+ } |
+ |
+ bool file_quota_result_ = false; |
+ base::FilePath temp_dir_; |
+ std::vector<FileCreationInfo> files_created_; |
+ bool memory_quota_result_ = false; |
+ |
+ scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner(); |
+ |
+ base::MessageLoop fake_io_message_loop_; |
+}; |
+ |
+TEST_F(BlobMemoryControllerTest, Strategy) { |
+ BlobMemoryController controller; |
+ SetTestMemoryLimits(&controller); |
+ |
+ // No transportation needed. |
+ EXPECT_EQ(Strategy::NONE_NEEDED, controller.DetermineStrategy(0, 0)); |
+ |
+ // IPC. |
+ EXPECT_EQ(Strategy::IPC, |
+ controller.DetermineStrategy(0, kTestBlobStorageIPCThresholdBytes)); |
+ |
+ // Shared Memory. |
+ EXPECT_EQ(Strategy::SHARED_MEMORY, |
+ controller.DetermineStrategy(kTestBlobStorageIPCThresholdBytes, |
+ kTestBlobStorageMaxSharedMemoryBytes)); |
+ |
+ // Not too large, as disk isn't enabled. |
+ EXPECT_EQ( |
+ Strategy::SHARED_MEMORY, |
+ controller.DetermineStrategy(0, kTestBlobStorageMaxBlobMemorySize + |
+ kTestBlobStorageInFlightMemory)); |
+ |
+ // Too large. |
+ EXPECT_EQ( |
+ Strategy::TOO_LARGE, |
+ controller.DetermineStrategy(0, kTestBlobStorageMaxBlobMemorySize + |
+ kTestBlobStorageInFlightMemory + 1)); |
+ |
+ // Enable disk, and check file strategies. |
+ controller.EnableFilePaging(temp_dir_, file_runner_); |
+ EXPECT_EQ(Strategy::FILE, controller.DetermineStrategy( |
+ 0, kTestBlobStorageMaxBlobMemorySize + 1)); |
+ |
+ // Too large for disk. |
+ controller.EnableFilePaging(temp_dir_, file_runner_); |
+ EXPECT_EQ(Strategy::TOO_LARGE, |
+ controller.DetermineStrategy(0, kTestBlobStorageMaxDiskSpace + 1)); |
+} |
+ |
+TEST_F(BlobMemoryControllerTest, GrantMemory) { |
+ const std::string kId = "id"; |
+ BlobMemoryController controller; |
+ SetTestMemoryLimits(&controller); |
+ |
+ BlobDataBuilder builder(kId); |
+ builder.AppendFutureData(10); |
+ builder.AppendFutureData(20); |
+ builder.AppendFutureData(30); |
+ |
+ std::vector<ShareableBlobDataItem*> pointers; |
+ std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
+ CreateSharedDataItems(builder, |
+ {ItemState::QUOTA_NEEDED, ItemState::QUOTA_NEEDED, |
+ ItemState::QUOTA_NEEDED}, |
+ &pointers); |
+ |
+ controller.ReserveMemoryQuota(pointers, GetMemoryRequestCallback()); |
+ EXPECT_EQ(true, memory_quota_result_); |
+ EXPECT_EQ(ItemState::QUOTA_GRANTED, items[0]->state()); |
+ EXPECT_EQ(ItemState::QUOTA_GRANTED, items[1]->state()); |
+ EXPECT_EQ(ItemState::QUOTA_GRANTED, items[2]->state()); |
+} |
+ |
+TEST_F(BlobMemoryControllerTest, GrantMemoryWithPagingFileWaiting) { |
+ const std::string kId = "id"; |
+ BlobMemoryController controller; |
+ SetTestMemoryLimits(&controller); |
+ |
+ char kData[kTestBlobStorageMaxBlobMemorySize]; |
+ std::memset(kData, kTestBlobStorageMaxBlobMemorySize, 'e'); |
+ |
+ // Add memory item that is the memory quota. |
+ BlobDataBuilder builder(kId); |
+ builder.AppendFutureData(kTestBlobStorageMaxBlobMemorySize); |
+ |
+ std::vector<ShareableBlobDataItem*> pointers; |
+ std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
+ CreateSharedDataItems(builder, {ItemState::QUOTA_NEEDED}, &pointers); |
+ |
+ controller.ReserveMemoryQuota(pointers, GetMemoryRequestCallback()); |
+ EXPECT_EQ(true, memory_quota_result_); |
+ memory_quota_result_ = false; |
+ EXPECT_EQ(ItemState::QUOTA_GRANTED, items[0]->state()); |
+ |
+ // Enable disk. |
+ controller.EnableFilePaging(temp_dir_, file_runner_); |
+ |
+ // Create an item that is just a little too big. |
+ BlobDataBuilder builder2(kId); |
+ builder2.AppendFutureData(kTestBlobStorageInFlightMemory + 1); |
+ |
+ // Reserve memory, which should request successfuly but we can't fit it yet |
+ // (no callback). |
+ pointers.clear(); |
+ std::vector<scoped_refptr<ShareableBlobDataItem>> items2 = |
+ CreateSharedDataItems(builder2, {ItemState::QUOTA_NEEDED}, &pointers); |
+ controller.ReserveMemoryQuota(pointers, GetMemoryRequestCallback()); |
+ |
+ EXPECT_EQ(false, memory_quota_result_); |
+ EXPECT_EQ(ItemState::QUOTA_REQUESTED, items2[0]->state()); |
+ EXPECT_FALSE(file_runner_->HasPendingTask()); |
+ |
+ // Add our original item as populated so it's paged to disk. |
+ items[0]->item()->data_element_ptr()->SetToBytes( |
+ kData, kTestBlobStorageMaxBlobMemorySize); |
+ SetItemState(items[0].get(), ItemState::POPULATED_WITH_QUOTA); |
+ controller.UpdateBlobItemInRecents(items[0].get()); |
+ |
+ EXPECT_TRUE(file_runner_->HasPendingTask()); |
+ file_runner_->RunPendingTasks(); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(ItemState::QUOTA_GRANTED, items2[0]->state()); |
+ EXPECT_EQ(DataElement::TYPE_FILE, items[0]->item()->type()); |
+} |
+ |
+// TODO(dmurph): |
pwnall
2016/09/21 09:03:35
Can you crbug this please?
dmurph
2016/09/21 23:45:52
This will be in this patch, no need for crbug. Jus
|
+// * Write test for memory request when quota is available. |
+// * Write test for memory request that fails because disk isn't enabled. |
+// * Write test for memory request when quota isn't available and we're waiting |
+// on disk paging. |
+// * Same as above but then we cancel it. |
+// * Write test for memory request that's pending, and then disk is disabled. |
+// * Write test for file request where we have quota. |
+// * Write test for file request where we don't have quota (and just fail). |
+// * Write test for file request and then we disable disk. |
+ |
+} // namespace storage |