OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "storage/browser/blob/blob_memory_controller.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "storage/common/data_element.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace storage { |
| 15 namespace { |
| 16 |
| 17 using MemoryStrategyResult = BlobMemoryController::MemoryStrategyResult; |
| 18 using FileCreationInfo = BlobMemoryController::FileCreationInfo; |
| 19 using base::TestSimpleTaskRunner; |
| 20 |
| 21 const std::string kBlobStorageDirectory = "blob_storage"; |
| 22 const size_t kTestBlobStorageIPCThresholdBytes = 20; |
| 23 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; |
| 24 const size_t kTestBlobStorageMaxBlobMemorySize = 400; |
| 25 const size_t kTestBlobStorageMaxMemoryUsage = 500; |
| 26 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; |
| 27 const size_t kTestBlobStorageInFlightMemory = 10; |
| 28 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; |
| 29 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
| 30 |
| 31 // void SetFileCreationInfo(BlobMemoryController::FileCreationInfo* output, |
| 32 // const BlobMemoryController::FileCreationInfo& in) { |
| 33 // *output = in; |
| 34 //} |
| 35 // |
| 36 // void SetPointer(bool* out, bool success) { |
| 37 // *out = success; |
| 38 //} |
| 39 |
| 40 class BlobMemoryControllerTest : public testing::Test { |
| 41 protected: |
| 42 BlobMemoryControllerTest() : thread_runner_handle_(io_runner_) {} |
| 43 |
| 44 void SetUp() override { |
| 45 ASSERT_EQ(true, base::CreateNewTempDirectory("blob_storage", &temp_dir_)); |
| 46 }; |
| 47 |
| 48 void TearDown() override { |
| 49 files_created_.clear(); |
| 50 // Make sure we clean up the files. |
| 51 io_runner_->RunPendingTasks(); |
| 52 file_runner_->RunPendingTasks(); |
| 53 ASSERT_EQ(true, base::DeleteFile(temp_dir_, true)); |
| 54 } |
| 55 |
| 56 void SetTestMemoryLimits(BlobMemoryController* controller) { |
| 57 controller->SetMemoryConstantsForTesting( |
| 58 kTestBlobStorageIPCThresholdBytes, kTestBlobStorageMaxSharedMemoryBytes, |
| 59 kTestBlobStorageMaxBlobMemorySize, kTestBlobStorageMaxMemoryUsage, |
| 60 kTestBlobStorageMaxDiskSpace, kTestBlobStorageInFlightMemory, |
| 61 kTestBlobStorageMinFileSizeBytes, kTestBlobStorageMaxFileSizeBytes); |
| 62 } |
| 63 |
| 64 void SaveFileCreationInfo(FileCreationInfo info) { |
| 65 files_created_.push_back(std::move(info)); |
| 66 } |
| 67 |
| 68 base::Callback<void(FileCreationInfo info)> GetFileCreationCallback() { |
| 69 return base::Bind(&BlobMemoryControllerTest::SaveFileCreationInfo, |
| 70 base::Unretained(this)); |
| 71 } |
| 72 |
| 73 base::FilePath temp_dir_; |
| 74 std::vector<FileCreationInfo> files_created_; |
| 75 |
| 76 scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner(); |
| 77 |
| 78 scoped_refptr<base::TestSimpleTaskRunner> io_runner_ = |
| 79 new TestSimpleTaskRunner(); |
| 80 |
| 81 // We set this to the IO thread runner, as this is used for the |
| 82 // OnMemoryRequest calls, which are on that thread. |
| 83 base::ThreadTaskRunnerHandle thread_runner_handle_; |
| 84 }; |
| 85 |
| 86 TEST_F(BlobMemoryControllerTest, TempFiles) { |
| 87 BlobMemoryController controller; |
| 88 SetTestMemoryLimits(&controller); |
| 89 |
| 90 // Check we can't create files. |
| 91 controller.CreateTemporaryFileForRenderer(10ull, GetFileCreationCallback()); |
| 92 ASSERT_EQ(1u, files_created_.size()); |
| 93 ASSERT_EQ(base::File::FILE_ERROR_FAILED, files_created_[0].error); |
| 94 |
| 95 // Check that we can when disk is enabled. |
| 96 controller.EnableDisk(temp_dir_, file_runner_); |
| 97 controller.CreateTemporaryFileForRenderer(10, GetFileCreationCallback()); |
| 98 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 99 file_runner_->RunPendingTasks(); |
| 100 EXPECT_TRUE(io_runner_->HasPendingTask()); |
| 101 io_runner_->RunPendingTasks(); |
| 102 |
| 103 ASSERT_EQ(2u, files_created_.size()); |
| 104 EXPECT_EQ(base::File::FILE_OK, files_created_[1].error); |
| 105 files_created_.clear(); |
| 106 // Double check that we delete them. |
| 107 io_runner_->RunPendingTasks(); |
| 108 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 109 file_runner_->RunPendingTasks(); |
| 110 } |
| 111 |
| 112 TEST_F(BlobMemoryControllerTest, Strategy) { |
| 113 BlobMemoryController controller; |
| 114 SetTestMemoryLimits(&controller); |
| 115 std::vector<DataElement> elements = {DataElement()}; |
| 116 MemoryStrategyResult strategy = MemoryStrategyResult::TOO_LARGE; |
| 117 uint64_t total_bytes = 0; |
| 118 |
| 119 // No transportation needed. |
| 120 elements[0].SetToBlob("kReferencedBlobId"); |
| 121 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 122 &strategy); |
| 123 EXPECT_EQ(0ull, total_bytes); |
| 124 EXPECT_EQ(MemoryStrategyResult::NONE_NEEDED, strategy); |
| 125 |
| 126 // IPC. |
| 127 elements[0].SetToBytesDescription(kTestBlobStorageIPCThresholdBytes); |
| 128 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 129 &strategy); |
| 130 EXPECT_EQ(kTestBlobStorageIPCThresholdBytes, total_bytes); |
| 131 EXPECT_EQ(MemoryStrategyResult::IPC, strategy); |
| 132 |
| 133 // Shared Memory. |
| 134 elements[0].SetToBytesDescription(kTestBlobStorageMaxSharedMemoryBytes); |
| 135 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 136 &strategy); |
| 137 EXPECT_EQ(kTestBlobStorageMaxSharedMemoryBytes, total_bytes); |
| 138 EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, strategy); |
| 139 |
| 140 // Not too large, as disk isn't enabled. |
| 141 elements[0].SetToBytesDescription(kTestBlobStorageMaxMemoryUsage); |
| 142 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 143 &strategy); |
| 144 EXPECT_EQ(kTestBlobStorageMaxMemoryUsage, total_bytes); |
| 145 EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, strategy); |
| 146 |
| 147 // Too large. |
| 148 elements[0].SetToBytesDescription(kTestBlobStorageMaxMemoryUsage + 1); |
| 149 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 150 &strategy); |
| 151 EXPECT_EQ(kTestBlobStorageMaxMemoryUsage + 1, total_bytes); |
| 152 EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, strategy); |
| 153 |
| 154 // Enable disk, and check file strategies. |
| 155 controller.EnableDisk(temp_dir_, file_runner_); |
| 156 elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize + 1); |
| 157 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 158 &strategy); |
| 159 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize + 1, total_bytes); |
| 160 EXPECT_EQ(MemoryStrategyResult::FILE, strategy); |
| 161 |
| 162 // File strategy for > max in memory. |
| 163 elements[0].SetToBytesDescription(kTestBlobStorageMaxMemoryUsage + 1); |
| 164 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 165 &strategy); |
| 166 EXPECT_EQ(kTestBlobStorageMaxMemoryUsage + 1, total_bytes); |
| 167 EXPECT_EQ(MemoryStrategyResult::FILE, strategy); |
| 168 |
| 169 // Too large for disk. |
| 170 elements[0].SetToBytesDescription(kTestBlobStorageMaxDiskSpace + 1); |
| 171 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes, |
| 172 &strategy); |
| 173 EXPECT_EQ(kTestBlobStorageMaxDiskSpace + 1, total_bytes); |
| 174 EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, strategy); |
| 175 } |
| 176 |
| 177 } // namespace |
| 178 } // namespace storage |
OLD | NEW |