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/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/test/test_simple_task_runner.h" |
| 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "storage/browser/blob/blob_data_builder.h" |
| 16 #include "storage/browser/blob/blob_data_item.h" |
| 17 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 18 #include "storage/common/data_element.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace storage { |
| 22 |
| 23 using Strategy = BlobMemoryController::Strategy; |
| 24 using FileCreationInfo = BlobMemoryController::FileCreationInfo; |
| 25 using base::TestSimpleTaskRunner; |
| 26 using ItemState = ShareableBlobDataItem::State; |
| 27 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; |
| 28 |
| 29 const std::string kBlobStorageDirectory = "blob_storage"; |
| 30 const size_t kTestBlobStorageIPCThresholdBytes = 20; |
| 31 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; |
| 32 const size_t kTestBlobStorageMaxBlobMemorySize = 490; |
| 33 const size_t kTestBlobStorageInFlightMemory = 10; |
| 34 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; |
| 35 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; |
| 36 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
| 37 |
| 38 class BlobMemoryControllerTest : public testing::Test { |
| 39 protected: |
| 40 BlobMemoryControllerTest() {} |
| 41 |
| 42 void SetUp() override { |
| 43 ASSERT_EQ(true, temp_dir_.CreateUniqueTempDir()); |
| 44 base::ThreadRestrictions::SetIOAllowed(false); |
| 45 }; |
| 46 |
| 47 void TearDown() override { |
| 48 files_created_.clear(); |
| 49 // Make sure we clean up the files. |
| 50 base::RunLoop().RunUntilIdle(); |
| 51 RunFileThreadTasks(); |
| 52 base::RunLoop().RunUntilIdle(); |
| 53 base::ThreadRestrictions::SetIOAllowed(true); |
| 54 ASSERT_EQ(true, temp_dir_.Delete()); |
| 55 } |
| 56 |
| 57 std::vector<scoped_refptr<ShareableBlobDataItem>> CreateSharedDataItems( |
| 58 const BlobDataBuilder& builder) { |
| 59 std::vector<scoped_refptr<ShareableBlobDataItem>> result; |
| 60 for (size_t i = 0; i < builder.items_.size(); ++i) { |
| 61 result.push_back(make_scoped_refptr( |
| 62 new ShareableBlobDataItem(builder.uuid(), builder.items_[i], |
| 63 ShareableBlobDataItem::QUOTA_NEEDED))); |
| 64 } |
| 65 return result; |
| 66 } |
| 67 |
| 68 void SetTestMemoryLimits(BlobMemoryController* controller) { |
| 69 BlobStorageLimits limits; |
| 70 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; |
| 71 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; |
| 72 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; |
| 73 limits.in_flight_space = kTestBlobStorageInFlightMemory; |
| 74 limits.max_blob_disk_space = kTestBlobStorageMaxDiskSpace; |
| 75 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; |
| 76 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; |
| 77 controller->set_limits_for_testing(limits); |
| 78 } |
| 79 |
| 80 void SaveFileCreationInfo(bool success, std::vector<FileCreationInfo> info) { |
| 81 file_quota_result_ = success; |
| 82 if (success) |
| 83 files_created_.swap(info); |
| 84 } |
| 85 |
| 86 void SaveMemoryRequest(bool success) { memory_quota_result_ = success; } |
| 87 |
| 88 BlobMemoryController::FileQuotaRequestCallback GetFileCreationCallback() { |
| 89 return base::Bind(&BlobMemoryControllerTest::SaveFileCreationInfo, |
| 90 base::Unretained(this)); |
| 91 } |
| 92 |
| 93 BlobMemoryController::MemoryQuotaRequestCallback GetMemoryRequestCallback() { |
| 94 return base::Bind(&BlobMemoryControllerTest::SaveMemoryRequest, |
| 95 base::Unretained(this)); |
| 96 } |
| 97 |
| 98 void CleanUpMemoryItemsFromController( |
| 99 BlobMemoryController* controller, |
| 100 std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
| 101 const std::string& blob_id) { |
| 102 // Make items free from blob references |
| 103 for (const auto& item : items) { |
| 104 item->referencing_blobs_mutable()->erase(blob_id); |
| 105 } |
| 106 |
| 107 controller->MaybeFreeMemoryQuotaForItems(items); |
| 108 } |
| 109 |
| 110 void CleanUpFileItemsFromController( |
| 111 BlobMemoryController* controller, |
| 112 std::vector<scoped_refptr<ShareableBlobDataItem>> items, |
| 113 const std::string& blob_id) { |
| 114 // Make items free from blob references |
| 115 for (const auto& item : items) { |
| 116 item->referencing_blobs_mutable()->erase(blob_id); |
| 117 } |
| 118 |
| 119 // We need the shareable file reference to go away, which schedules tasks |
| 120 // that |
| 121 // eventually calls back to our system and decrements our disk usage. |
| 122 items.clear(); |
| 123 base::RunLoop().RunUntilIdle(); |
| 124 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 125 RunFileThreadTasks(); |
| 126 base::RunLoop().RunUntilIdle(); |
| 127 } |
| 128 |
| 129 void RunFileThreadTasks() { |
| 130 base::ThreadRestrictions::SetIOAllowed(true); |
| 131 file_runner_->RunPendingTasks(); |
| 132 base::ThreadRestrictions::SetIOAllowed(false); |
| 133 } |
| 134 |
| 135 bool file_quota_result_ = false; |
| 136 base::ScopedTempDir temp_dir_; |
| 137 std::vector<FileCreationInfo> files_created_; |
| 138 bool memory_quota_result_ = false; |
| 139 |
| 140 scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner(); |
| 141 |
| 142 base::MessageLoop fake_io_message_loop_; |
| 143 }; |
| 144 |
| 145 TEST_F(BlobMemoryControllerTest, Strategy) { |
| 146 { |
| 147 BlobMemoryController controller(temp_dir_.GetPath(), nullptr); |
| 148 SetTestMemoryLimits(&controller); |
| 149 |
| 150 // No transportation needed. |
| 151 EXPECT_EQ(Strategy::NONE_NEEDED, controller.DetermineStrategy(0, 0)); |
| 152 |
| 153 // IPC. |
| 154 EXPECT_EQ(Strategy::IPC, controller.DetermineStrategy( |
| 155 0, kTestBlobStorageIPCThresholdBytes)); |
| 156 |
| 157 // Shared Memory. |
| 158 EXPECT_EQ( |
| 159 Strategy::SHARED_MEMORY, |
| 160 controller.DetermineStrategy(kTestBlobStorageIPCThresholdBytes, |
| 161 kTestBlobStorageMaxSharedMemoryBytes)); |
| 162 |
| 163 // Not too large, as disk isn't enabled. |
| 164 EXPECT_EQ( |
| 165 Strategy::SHARED_MEMORY, |
| 166 controller.DetermineStrategy(0, kTestBlobStorageMaxBlobMemorySize + |
| 167 kTestBlobStorageInFlightMemory)); |
| 168 |
| 169 // Too large. |
| 170 EXPECT_EQ(Strategy::TOO_LARGE, |
| 171 controller.DetermineStrategy( |
| 172 0, kTestBlobStorageMaxBlobMemorySize + |
| 173 kTestBlobStorageInFlightMemory + 1)); |
| 174 } |
| 175 { |
| 176 // Enable disk, and check file strategies. |
| 177 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 178 SetTestMemoryLimits(&controller); |
| 179 EXPECT_EQ(Strategy::FILE, controller.DetermineStrategy( |
| 180 0, kTestBlobStorageMaxBlobMemorySize + 1)); |
| 181 |
| 182 // Too large for disk. |
| 183 EXPECT_EQ(Strategy::TOO_LARGE, controller.DetermineStrategy( |
| 184 0, kTestBlobStorageMaxDiskSpace + 1)); |
| 185 } |
| 186 } |
| 187 |
| 188 TEST_F(BlobMemoryControllerTest, GrantMemory) { |
| 189 const std::string kId = "id"; |
| 190 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 191 SetTestMemoryLimits(&controller); |
| 192 |
| 193 BlobDataBuilder builder(kId); |
| 194 builder.AppendFutureData(10); |
| 195 builder.AppendFutureData(20); |
| 196 builder.AppendFutureData(30); |
| 197 |
| 198 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 199 CreateSharedDataItems(builder); |
| 200 |
| 201 controller.ReserveMemoryQuota(items, GetMemoryRequestCallback()); |
| 202 EXPECT_TRUE(memory_quota_result_); |
| 203 EXPECT_EQ(ItemState::QUOTA_GRANTED, items[0]->state()); |
| 204 EXPECT_EQ(ItemState::QUOTA_GRANTED, items[1]->state()); |
| 205 EXPECT_EQ(ItemState::QUOTA_GRANTED, items[2]->state()); |
| 206 } |
| 207 |
| 208 TEST_F(BlobMemoryControllerTest, SimpleMemoryRequest) { |
| 209 const std::string kId = "id"; |
| 210 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 211 SetTestMemoryLimits(&controller); |
| 212 |
| 213 // Add memory item that is the memory quota. |
| 214 BlobDataBuilder builder(kId); |
| 215 builder.AppendFutureData(kTestBlobStorageMaxBlobMemorySize); |
| 216 |
| 217 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 218 CreateSharedDataItems(builder); |
| 219 |
| 220 controller.ReserveMemoryQuota(items, GetMemoryRequestCallback()); |
| 221 EXPECT_TRUE(memory_quota_result_); |
| 222 memory_quota_result_ = false; |
| 223 EXPECT_EQ(ItemState::QUOTA_GRANTED, items[0]->state()); |
| 224 EXPECT_FALSE(file_runner_->HasPendingTask()); |
| 225 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.memory_usage()); |
| 226 EXPECT_EQ(0u, controller.disk_usage()); |
| 227 |
| 228 CleanUpMemoryItemsFromController(&controller, std::move(items), kId); |
| 229 EXPECT_EQ(0u, controller.memory_usage()); |
| 230 } |
| 231 |
| 232 TEST_F(BlobMemoryControllerTest, PageToDisk) { |
| 233 const std::string kId = "id"; |
| 234 const std::string kId2 = "id2"; |
| 235 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 236 SetTestMemoryLimits(&controller); |
| 237 |
| 238 char kData[kTestBlobStorageMaxBlobMemorySize]; |
| 239 std::memset(kData, kTestBlobStorageMaxBlobMemorySize, 'e'); |
| 240 |
| 241 // Add memory item that is the memory quota. |
| 242 BlobDataBuilder builder(kId); |
| 243 builder.AppendFutureData(kTestBlobStorageMaxBlobMemorySize); |
| 244 |
| 245 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 246 CreateSharedDataItems(builder); |
| 247 |
| 248 controller.ReserveMemoryQuota(items, GetMemoryRequestCallback()); |
| 249 EXPECT_TRUE(memory_quota_result_); |
| 250 memory_quota_result_ = false; |
| 251 EXPECT_EQ(ItemState::QUOTA_GRANTED, items[0]->state()); |
| 252 EXPECT_FALSE(file_runner_->HasPendingTask()); |
| 253 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.memory_usage()); |
| 254 EXPECT_EQ(0u, controller.disk_usage()); |
| 255 |
| 256 // Create an item that is just a little too big. |
| 257 BlobDataBuilder builder2(kId2); |
| 258 builder2.AppendFutureData(kTestBlobStorageInFlightMemory + 1); |
| 259 |
| 260 // Reserve memory, which should request successfuly but we can't fit it yet |
| 261 // (no callback). |
| 262 std::vector<scoped_refptr<ShareableBlobDataItem>> items2 = |
| 263 CreateSharedDataItems(builder2); |
| 264 controller.ReserveMemoryQuota(items2, GetMemoryRequestCallback()); |
| 265 // We don't count the usage yet. |
| 266 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.memory_usage()); |
| 267 EXPECT_EQ(0u, controller.disk_usage()); |
| 268 |
| 269 EXPECT_FALSE(memory_quota_result_); |
| 270 EXPECT_EQ(ItemState::QUOTA_REQUESTED, items2[0]->state()); |
| 271 EXPECT_FALSE(file_runner_->HasPendingTask()); |
| 272 |
| 273 // Add our original item as populated so it's paged to disk. |
| 274 items[0]->item()->data_element_ptr()->SetToBytes( |
| 275 kData, kTestBlobStorageMaxBlobMemorySize); |
| 276 items[0]->set_state(ItemState::POPULATED_WITH_QUOTA); |
| 277 controller.NotifyMemoryItemUsed(items[0].get()); |
| 278 |
| 279 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 280 RunFileThreadTasks(); |
| 281 base::RunLoop().RunUntilIdle(); |
| 282 EXPECT_EQ(ItemState::QUOTA_GRANTED, items2[0]->state()); |
| 283 EXPECT_EQ(DataElement::TYPE_FILE, items[0]->item()->type()); |
| 284 EXPECT_EQ(kTestBlobStorageInFlightMemory + 1, controller.memory_usage()); |
| 285 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.disk_usage()); |
| 286 |
| 287 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxDiskSpace)); |
| 288 |
| 289 CleanUpMemoryItemsFromController(&controller, std::move(items2), kId2); |
| 290 EXPECT_EQ(0u, controller.memory_usage()); |
| 291 CleanUpFileItemsFromController(&controller, std::move(items), kId); |
| 292 EXPECT_EQ(0u, controller.disk_usage()); |
| 293 } |
| 294 |
| 295 TEST_F(BlobMemoryControllerTest, NoDiskTooLarge) { |
| 296 const std::string kId = "id"; |
| 297 BlobMemoryController controller(temp_dir_.GetPath(), nullptr); |
| 298 SetTestMemoryLimits(&controller); |
| 299 |
| 300 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxBlobMemorySize + |
| 301 kTestBlobStorageInFlightMemory + 1)); |
| 302 } |
| 303 |
| 304 TEST_F(BlobMemoryControllerTest, TooLargeForDisk) { |
| 305 const std::string kId = "id"; |
| 306 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 307 SetTestMemoryLimits(&controller); |
| 308 |
| 309 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxDiskSpace + 1)); |
| 310 } |
| 311 |
| 312 TEST_F(BlobMemoryControllerTest, CancelMemoryRequest) { |
| 313 const std::string kId = "id"; |
| 314 const std::string kId2 = "id2"; |
| 315 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 316 SetTestMemoryLimits(&controller); |
| 317 |
| 318 char kData[kTestBlobStorageMaxBlobMemorySize]; |
| 319 std::memset(kData, kTestBlobStorageMaxBlobMemorySize, 'e'); |
| 320 |
| 321 // Add memory item that is the memory quota. |
| 322 BlobDataBuilder builder(kId); |
| 323 builder.AppendFutureData(kTestBlobStorageMaxBlobMemorySize); |
| 324 |
| 325 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 326 CreateSharedDataItems(builder); |
| 327 |
| 328 controller.ReserveMemoryQuota(items, GetMemoryRequestCallback()); |
| 329 |
| 330 // Create an item that is just a little too big. |
| 331 BlobDataBuilder builder2(kId2); |
| 332 builder2.AppendFutureData(kTestBlobStorageInFlightMemory + 1); |
| 333 |
| 334 // Reserve memory, which should request successfuly but we can't fit it yet |
| 335 // (no callback). |
| 336 std::vector<scoped_refptr<ShareableBlobDataItem>> items2 = |
| 337 CreateSharedDataItems(builder2); |
| 338 base::WeakPtr<QuotaAllocationTask> task = |
| 339 controller.ReserveMemoryQuota(items2, GetMemoryRequestCallback()); |
| 340 // We don't count the usage yet. |
| 341 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.memory_usage()); |
| 342 EXPECT_EQ(0u, controller.disk_usage()); |
| 343 |
| 344 // Add our original item as populated so we start paging to disk. |
| 345 items[0]->item()->data_element_ptr()->SetToBytes( |
| 346 kData, kTestBlobStorageMaxBlobMemorySize); |
| 347 items[0]->set_state(ItemState::POPULATED_WITH_QUOTA); |
| 348 controller.NotifyMemoryItemUsed(items[0].get()); |
| 349 |
| 350 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 351 EXPECT_TRUE(task); |
| 352 |
| 353 task->Cancel(); |
| 354 EXPECT_FALSE(task); |
| 355 RunFileThreadTasks(); |
| 356 base::RunLoop().RunUntilIdle(); |
| 357 EXPECT_EQ(ItemState::QUOTA_REQUESTED, items2[0]->state()); |
| 358 EXPECT_EQ(DataElement::TYPE_FILE, items[0]->item()->type()); |
| 359 EXPECT_EQ(0u, controller.memory_usage()); |
| 360 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.disk_usage()); |
| 361 |
| 362 CleanUpFileItemsFromController(&controller, std::move(items), kId); |
| 363 EXPECT_EQ(0u, controller.disk_usage()); |
| 364 } |
| 365 |
| 366 TEST_F(BlobMemoryControllerTest, FileRequest) { |
| 367 const std::string kId = "id"; |
| 368 const size_t kBlobSize = kTestBlobStorageMaxBlobMemorySize + 1; |
| 369 |
| 370 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 371 SetTestMemoryLimits(&controller); |
| 372 |
| 373 char kData[kBlobSize]; |
| 374 std::memset(kData, kBlobSize, 'e'); |
| 375 |
| 376 // Add item that is the file quota. |
| 377 BlobDataBuilder builder(kId); |
| 378 builder.AppendFutureFile(0, kBlobSize, 0); |
| 379 |
| 380 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 381 CreateSharedDataItems(builder); |
| 382 |
| 383 file_quota_result_ = false; |
| 384 base::WeakPtr<QuotaAllocationTask> task = |
| 385 controller.ReserveFileQuota(items, GetFileCreationCallback()); |
| 386 EXPECT_TRUE(task); |
| 387 EXPECT_FALSE(file_quota_result_); |
| 388 EXPECT_EQ(ItemState::QUOTA_REQUESTED, items[0]->state()); |
| 389 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 390 EXPECT_EQ(0u, controller.memory_usage()); |
| 391 EXPECT_EQ(kBlobSize, controller.disk_usage()); |
| 392 |
| 393 EXPECT_FALSE(controller.CanReserveQuota(kTestBlobStorageMaxDiskSpace)); |
| 394 |
| 395 RunFileThreadTasks(); |
| 396 base::RunLoop().RunUntilIdle(); |
| 397 EXPECT_TRUE(file_quota_result_); |
| 398 EXPECT_FALSE(file_runner_->HasPendingTask()); |
| 399 EXPECT_FALSE(task); |
| 400 |
| 401 // Do the work to populate the file. |
| 402 EXPECT_EQ(1u, files_created_.size()); |
| 403 EXPECT_TRUE( |
| 404 builder.PopulateFutureFile(0, std::move(files_created_[0].file_reference), |
| 405 files_created_[0].last_modified)); |
| 406 base::ThreadRestrictions::SetIOAllowed(true); |
| 407 files_created_.clear(); |
| 408 base::ThreadRestrictions::SetIOAllowed(false); |
| 409 EXPECT_EQ(DataElement::TYPE_FILE, items[0]->item()->type()); |
| 410 EXPECT_FALSE( |
| 411 BlobDataBuilder::IsFutureFileItem(items[0]->item()->data_element())); |
| 412 |
| 413 builder.Clear(); |
| 414 CleanUpFileItemsFromController(&controller, std::move(items), kId); |
| 415 EXPECT_EQ(0u, controller.disk_usage()); |
| 416 } |
| 417 |
| 418 TEST_F(BlobMemoryControllerTest, CancelFileRequest) { |
| 419 const std::string kId = "id"; |
| 420 const size_t kBlobSize = kTestBlobStorageMaxBlobMemorySize + 1; |
| 421 |
| 422 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 423 SetTestMemoryLimits(&controller); |
| 424 |
| 425 char kData[kBlobSize]; |
| 426 std::memset(kData, kBlobSize, 'e'); |
| 427 |
| 428 // Add memory item that is the memory quota. |
| 429 BlobDataBuilder builder(kId); |
| 430 builder.AppendFutureFile(0, kBlobSize, 0); |
| 431 |
| 432 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 433 CreateSharedDataItems(builder); |
| 434 |
| 435 base::WeakPtr<QuotaAllocationTask> task = |
| 436 controller.ReserveFileQuota(items, GetFileCreationCallback()); |
| 437 EXPECT_TRUE(task); |
| 438 EXPECT_EQ(ItemState::QUOTA_REQUESTED, items[0]->state()); |
| 439 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 440 EXPECT_EQ(0u, controller.memory_usage()); |
| 441 EXPECT_EQ(kBlobSize, controller.disk_usage()); |
| 442 |
| 443 task->Cancel(); |
| 444 EXPECT_FALSE(task); |
| 445 EXPECT_EQ(kBlobSize, controller.disk_usage()); |
| 446 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 447 RunFileThreadTasks(); |
| 448 base::RunLoop().RunUntilIdle(); |
| 449 EXPECT_EQ(0u, controller.disk_usage()); |
| 450 } |
| 451 |
| 452 TEST_F(BlobMemoryControllerTest, DisableDiskWithFileAndMemoryPending) { |
| 453 const std::string kFirstMemoryId = "id"; |
| 454 const uint64_t kFirstMemorySize = kTestBlobStorageMaxBlobMemorySize; |
| 455 const std::string kSecondMemoryId = "id2"; |
| 456 const uint64_t kSecondMemorySize = kTestBlobStorageInFlightMemory + 1; |
| 457 const std::string kFileId = "id2"; |
| 458 const uint64_t kFileBlobSize = kTestBlobStorageMaxBlobMemorySize + 1; |
| 459 |
| 460 BlobMemoryController controller(temp_dir_.GetPath(), file_runner_); |
| 461 SetTestMemoryLimits(&controller); |
| 462 |
| 463 char kDataMemoryData[kFirstMemorySize]; |
| 464 std::memset(kDataMemoryData, kFirstMemorySize, 'e'); |
| 465 |
| 466 // Add first memory item to fill up some memory quota. |
| 467 BlobDataBuilder builder(kFirstMemoryId); |
| 468 builder.AppendFutureData(kTestBlobStorageMaxBlobMemorySize); |
| 469 |
| 470 std::vector<scoped_refptr<ShareableBlobDataItem>> items = |
| 471 CreateSharedDataItems(builder); |
| 472 |
| 473 controller.ReserveMemoryQuota(items, GetMemoryRequestCallback()); |
| 474 |
| 475 // Create a second memory item that is just a little too big. |
| 476 BlobDataBuilder builder2(kSecondMemoryId); |
| 477 builder2.AppendFutureData(kSecondMemorySize); |
| 478 |
| 479 // Reserve memory, which should request successfuly but we can't fit it yet. |
| 480 std::vector<scoped_refptr<ShareableBlobDataItem>> items2 = |
| 481 CreateSharedDataItems(builder2); |
| 482 base::WeakPtr<QuotaAllocationTask> memory_task = |
| 483 controller.ReserveMemoryQuota(items2, GetMemoryRequestCallback()); |
| 484 // We don't count the usage yet. |
| 485 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.memory_usage()); |
| 486 EXPECT_EQ(0u, controller.disk_usage()); |
| 487 |
| 488 // Add our original item as populated so we start paging it to disk. |
| 489 items[0]->item()->data_element_ptr()->SetToBytes(kDataMemoryData, |
| 490 kFirstMemorySize); |
| 491 items[0]->set_state(ItemState::POPULATED_WITH_QUOTA); |
| 492 controller.NotifyMemoryItemUsed(items[0].get()); |
| 493 |
| 494 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 495 EXPECT_TRUE(memory_task); |
| 496 EXPECT_EQ(kFirstMemorySize, controller.disk_usage()); |
| 497 |
| 498 // Add our file item now. |
| 499 BlobDataBuilder file_builder(kFileId); |
| 500 file_builder.AppendFutureFile(0, kFileBlobSize, 0); |
| 501 |
| 502 std::vector<scoped_refptr<ShareableBlobDataItem>> file_items = |
| 503 CreateSharedDataItems(file_builder); |
| 504 |
| 505 base::WeakPtr<QuotaAllocationTask> file_task = |
| 506 controller.ReserveFileQuota(file_items, GetFileCreationCallback()); |
| 507 EXPECT_TRUE(file_task); |
| 508 EXPECT_TRUE(file_runner_->HasPendingTask()); |
| 509 |
| 510 // We should have both memory paging tasks and file paging tasks. |
| 511 EXPECT_EQ(kFirstMemorySize, controller.memory_usage()); |
| 512 EXPECT_EQ(kFirstMemorySize + kFileBlobSize, controller.disk_usage()); |
| 513 file_quota_result_ = true; |
| 514 memory_quota_result_ = true; |
| 515 |
| 516 files_created_.clear(); |
| 517 |
| 518 // Disable paging! This should cancel all file-related tasks and leave us with |
| 519 // only the first memory item. |
| 520 controller.DisableFilePaging(); |
| 521 EXPECT_FALSE(file_quota_result_); |
| 522 EXPECT_FALSE(memory_quota_result_); |
| 523 EXPECT_FALSE(file_task); |
| 524 EXPECT_FALSE(memory_task); |
| 525 |
| 526 file_items.clear(); |
| 527 items.clear(); |
| 528 |
| 529 RunFileThreadTasks(); |
| 530 base::RunLoop().RunUntilIdle(); |
| 531 |
| 532 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize, controller.memory_usage()); |
| 533 EXPECT_EQ(0ull, controller.disk_usage()); |
| 534 } |
| 535 } // namespace storage |
OLD | NEW |