OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "storage/browser/blob/blob_memory_controller.h" | 5 #include "storage/browser/blob/blob_memory_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; | 27 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; |
28 | 28 |
29 const std::string kBlobStorageDirectory = "blob_storage"; | 29 const std::string kBlobStorageDirectory = "blob_storage"; |
30 const size_t kTestBlobStorageIPCThresholdBytes = 20; | 30 const size_t kTestBlobStorageIPCThresholdBytes = 20; |
31 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; | 31 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; |
32 const size_t kTestBlobStorageMaxBlobMemorySize = 500; | 32 const size_t kTestBlobStorageMaxBlobMemorySize = 500; |
33 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; | 33 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; |
34 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; | 34 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; |
35 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; | 35 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
36 | 36 |
| 37 const uint64_t kTestSmallBlobStorageMaxDiskSpace = 100; |
| 38 |
37 class BlobMemoryControllerTest : public testing::Test { | 39 class BlobMemoryControllerTest : public testing::Test { |
38 protected: | 40 protected: |
39 BlobMemoryControllerTest() {} | 41 BlobMemoryControllerTest() {} |
40 | 42 |
41 void SetUp() override { | 43 void SetUp() override { |
42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
43 base::ThreadRestrictions::SetIOAllowed(false); | 45 base::ThreadRestrictions::SetIOAllowed(false); |
44 }; | 46 }; |
45 | 47 |
46 void TearDown() override { | 48 void TearDown() override { |
(...skipping 20 matching lines...) Expand all Loading... |
67 BlobStorageLimits limits; | 69 BlobStorageLimits limits; |
68 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; | 70 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; |
69 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; | 71 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; |
70 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; | 72 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; |
71 limits.max_blob_disk_space = kTestBlobStorageMaxDiskSpace; | 73 limits.max_blob_disk_space = kTestBlobStorageMaxDiskSpace; |
72 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; | 74 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; |
73 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; | 75 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; |
74 controller->set_limits_for_testing(limits); | 76 controller->set_limits_for_testing(limits); |
75 } | 77 } |
76 | 78 |
| 79 void SetSmallDiskTestMemoryLimits(BlobMemoryController* controller) { |
| 80 BlobStorageLimits limits; |
| 81 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; |
| 82 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; |
| 83 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; |
| 84 limits.max_blob_disk_space = kTestSmallBlobStorageMaxDiskSpace; |
| 85 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; |
| 86 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; |
| 87 controller->set_limits_for_testing(limits); |
| 88 } |
| 89 |
77 void SaveFileCreationInfo(std::vector<FileCreationInfo> info, bool success) { | 90 void SaveFileCreationInfo(std::vector<FileCreationInfo> info, bool success) { |
78 file_quota_result_ = success; | 91 file_quota_result_ = success; |
79 if (success) { | 92 if (success) { |
80 files_created_.swap(info); | 93 files_created_.swap(info); |
81 } | 94 } |
82 } | 95 } |
83 | 96 |
84 void SaveMemoryRequest(bool success) { memory_quota_result_ = success; } | 97 void SaveMemoryRequest(bool success) { memory_quota_result_ = success; } |
85 | 98 |
86 BlobMemoryController::FileQuotaRequestCallback GetFileCreationCallback() { | 99 BlobMemoryController::FileQuotaRequestCallback GetFileCreationCallback() { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 0, kTestBlobStorageMaxBlobMemorySize - | 165 0, kTestBlobStorageMaxBlobMemorySize - |
153 kTestBlobStorageMinFileSizeBytes + 1)); | 166 kTestBlobStorageMinFileSizeBytes + 1)); |
154 | 167 |
155 EXPECT_EQ(Strategy::FILE, controller.DetermineStrategy( | 168 EXPECT_EQ(Strategy::FILE, controller.DetermineStrategy( |
156 0, kTestBlobStorageMaxBlobMemorySize)); | 169 0, kTestBlobStorageMaxBlobMemorySize)); |
157 | 170 |
158 // Too large for disk. | 171 // Too large for disk. |
159 EXPECT_EQ(Strategy::TOO_LARGE, controller.DetermineStrategy( | 172 EXPECT_EQ(Strategy::TOO_LARGE, controller.DetermineStrategy( |
160 0, kTestBlobStorageMaxDiskSpace + 1)); | 173 0, kTestBlobStorageMaxDiskSpace + 1)); |
161 } | 174 } |
| 175 { |
| 176 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 177 SetSmallDiskTestMemoryLimits(&controller); |
| 178 |
| 179 EXPECT_TRUE(controller.CanReserveQuota(kTestBlobStorageMaxBlobMemorySize)); |
| 180 // Since our disk is too small, this should be sent with shared memory. |
| 181 EXPECT_EQ( |
| 182 Strategy::SHARED_MEMORY, |
| 183 controller.DetermineStrategy(0, kTestBlobStorageMaxBlobMemorySize)); |
| 184 } |
162 } | 185 } |
163 | 186 |
164 TEST_F(BlobMemoryControllerTest, GrantMemory) { | 187 TEST_F(BlobMemoryControllerTest, GrantMemory) { |
165 const std::string kId = "id"; | 188 const std::string kId = "id"; |
166 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); | 189 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
167 SetTestMemoryLimits(&controller); | 190 SetTestMemoryLimits(&controller); |
168 | 191 |
169 BlobDataBuilder builder(kId); | 192 BlobDataBuilder builder(kId); |
170 builder.AppendFutureData(10); | 193 builder.AppendFutureData(10); |
171 builder.AppendFutureData(20); | 194 builder.AppendFutureData(20); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 items2.clear(); | 297 items2.clear(); |
275 EXPECT_EQ(0u, controller.memory_usage()); | 298 EXPECT_EQ(0u, controller.memory_usage()); |
276 items.clear(); | 299 items.clear(); |
277 EXPECT_TRUE(file_runner_->HasPendingTask()); | 300 EXPECT_TRUE(file_runner_->HasPendingTask()); |
278 RunFileThreadTasks(); | 301 RunFileThreadTasks(); |
279 base::RunLoop().RunUntilIdle(); | 302 base::RunLoop().RunUntilIdle(); |
280 EXPECT_EQ(0u, controller.disk_usage()); | 303 EXPECT_EQ(0u, controller.disk_usage()); |
281 } | 304 } |
282 | 305 |
283 TEST_F(BlobMemoryControllerTest, NoDiskTooLarge) { | 306 TEST_F(BlobMemoryControllerTest, NoDiskTooLarge) { |
284 const std::string kId = "id"; | |
285 BlobMemoryController controller(temp_dir_.GetPath(), nullptr); | 307 BlobMemoryController controller(temp_dir_.GetPath(), nullptr); |
286 SetTestMemoryLimits(&controller); | 308 SetTestMemoryLimits(&controller); |
287 | 309 |
288 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxBlobMemorySize + | 310 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxBlobMemorySize + |
289 kTestBlobStorageMinFileSizeBytes + | 311 kTestBlobStorageMinFileSizeBytes + |
290 1)); | 312 1)); |
291 } | 313 } |
292 | 314 |
293 TEST_F(BlobMemoryControllerTest, TooLargeForDisk) { | 315 TEST_F(BlobMemoryControllerTest, TooLargeForDisk) { |
294 const std::string kId = "id"; | |
295 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); | 316 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
296 SetTestMemoryLimits(&controller); | 317 SetTestMemoryLimits(&controller); |
297 | 318 |
298 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxDiskSpace + 1)); | 319 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxDiskSpace + 1)); |
299 } | 320 } |
300 | 321 |
301 TEST_F(BlobMemoryControllerTest, CancelMemoryRequest) { | 322 TEST_F(BlobMemoryControllerTest, CancelMemoryRequest) { |
302 const std::string kId = "id"; | 323 const std::string kId = "id"; |
303 const std::string kId2 = "id2"; | 324 const std::string kId2 = "id2"; |
304 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); | 325 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 file_items.clear(); | 692 file_items.clear(); |
672 items.clear(); | 693 items.clear(); |
673 | 694 |
674 RunFileThreadTasks(); | 695 RunFileThreadTasks(); |
675 base::RunLoop().RunUntilIdle(); | 696 base::RunLoop().RunUntilIdle(); |
676 | 697 |
677 EXPECT_EQ(0ull, controller.disk_usage()); | 698 EXPECT_EQ(0ull, controller.disk_usage()); |
678 EXPECT_EQ(0ull, controller.memory_usage()); | 699 EXPECT_EQ(0ull, controller.memory_usage()); |
679 } | 700 } |
680 } // namespace storage | 701 } // namespace storage |
OLD | NEW |