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/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/test/test_simple_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "storage/common/data_element.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace storage { |
| 17 namespace { |
| 18 |
| 19 using MemoryStrategyResult = BlobMemoryController::MemoryStrategyResult; |
| 20 using FileCreationInfo = BlobMemoryController::FileCreationInfo; |
| 21 using base::TestSimpleTaskRunner; |
| 22 |
| 23 const std::string kBlobStorageDirectory = "blob_storage"; |
| 24 const size_t kTestBlobStorageIPCThresholdBytes = 20; |
| 25 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; |
| 26 const size_t kTestBlobStorageMaxBlobMemorySize = 400; |
| 27 const uint64_t kTestBlobStorageMaxDiskSpace = 1000; |
| 28 const size_t kTestBlobStorageInFlightMemory = 10; |
| 29 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; |
| 30 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
| 31 |
| 32 // void SetFileCreationInfo(BlobMemoryController::FileCreationInfo* output, |
| 33 // const BlobMemoryController::FileCreationInfo& in) { |
| 34 // *output = in; |
| 35 //} |
| 36 // |
| 37 // void SetPointer(bool* out, bool success) { |
| 38 // *out = success; |
| 39 //} |
| 40 |
| 41 class BlobMemoryControllerTest : public testing::Test { |
| 42 protected: |
| 43 BlobMemoryControllerTest() {} |
| 44 |
| 45 void SetUp() override { |
| 46 ASSERT_EQ(true, base::CreateNewTempDirectory("blob_storage", &temp_dir_)); |
| 47 }; |
| 48 |
| 49 void TearDown() override { |
| 50 files_created_.clear(); |
| 51 // Make sure we clean up the files. |
| 52 base::RunLoop().RunUntilIdle(); |
| 53 file_runner_->RunPendingTasks(); |
| 54 base::RunLoop().RunUntilIdle(); |
| 55 ASSERT_EQ(true, base::DeleteFile(temp_dir_, true)); |
| 56 } |
| 57 |
| 58 void SetTestMemoryLimits(BlobMemoryController* controller) { |
| 59 controller->SetMemoryConstantsForTesting( |
| 60 kTestBlobStorageIPCThresholdBytes, kTestBlobStorageMaxSharedMemoryBytes, |
| 61 kTestBlobStorageMaxBlobMemorySize, kTestBlobStorageMaxDiskSpace, |
| 62 kTestBlobStorageInFlightMemory, kTestBlobStorageMinFileSizeBytes, |
| 63 kTestBlobStorageMaxFileSizeBytes); |
| 64 } |
| 65 |
| 66 void SaveFileCreationInfo(bool success, std::vector<FileCreationInfo> info) { |
| 67 EXPECT_EQ(quota_expected_result_, success); |
| 68 if (success) |
| 69 files_created_.swap(info); |
| 70 } |
| 71 |
| 72 base::Callback<void(bool, std::vector<FileCreationInfo>)> |
| 73 GetFileCreationCallback() { |
| 74 return base::Bind(&BlobMemoryControllerTest::SaveFileCreationInfo, |
| 75 base::Unretained(this)); |
| 76 } |
| 77 |
| 78 bool quota_expected_result_ = true; |
| 79 base::FilePath temp_dir_; |
| 80 std::vector<FileCreationInfo> files_created_; |
| 81 |
| 82 scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner(); |
| 83 |
| 84 base::MessageLoop fake_io_message_loop_; |
| 85 }; |
| 86 |
| 87 TEST_F(BlobMemoryControllerTest, Strategy) { |
| 88 BlobMemoryController controller; |
| 89 SetTestMemoryLimits(&controller); |
| 90 |
| 91 // No transportation needed. |
| 92 EXPECT_EQ(MemoryStrategyResult::NONE_NEEDED, |
| 93 controller.DecideBlobTransportationMemoryStrategy(0, 0)); |
| 94 |
| 95 // IPC. |
| 96 EXPECT_EQ(MemoryStrategyResult::IPC, |
| 97 controller.DecideBlobTransportationMemoryStrategy( |
| 98 0, kTestBlobStorageIPCThresholdBytes)); |
| 99 |
| 100 // Shared Memory. |
| 101 EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, |
| 102 controller.DecideBlobTransportationMemoryStrategy( |
| 103 kTestBlobStorageIPCThresholdBytes, |
| 104 kTestBlobStorageMaxSharedMemoryBytes)); |
| 105 |
| 106 // Not too large, as disk isn't enabled. |
| 107 EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, |
| 108 controller.DecideBlobTransportationMemoryStrategy( |
| 109 0, kTestBlobStorageMaxBlobMemorySize + |
| 110 kTestBlobStorageInFlightMemory)); |
| 111 |
| 112 // Too large. |
| 113 EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, |
| 114 controller.DecideBlobTransportationMemoryStrategy( |
| 115 0, kTestBlobStorageMaxBlobMemorySize + |
| 116 kTestBlobStorageInFlightMemory + 1)); |
| 117 |
| 118 // Enable disk, and check file strategies. |
| 119 controller.EnableDisk(temp_dir_, file_runner_); |
| 120 EXPECT_EQ(MemoryStrategyResult::FILE, |
| 121 controller.DecideBlobTransportationMemoryStrategy( |
| 122 0, kTestBlobStorageMaxBlobMemorySize + 1)); |
| 123 |
| 124 // Too large for disk. |
| 125 controller.EnableDisk(temp_dir_, file_runner_); |
| 126 EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, |
| 127 controller.DecideBlobTransportationMemoryStrategy( |
| 128 0, kTestBlobStorageMaxDiskSpace + 1)); |
| 129 } |
| 130 |
| 131 // TODO(dmurph): |
| 132 // * Write test for memory request when quota is available. |
| 133 // * Write test for memory request that fails because disk isn't enabled. |
| 134 // * Write test for memory request when quota isn't available and we're waiting |
| 135 // on disk paging. |
| 136 // * Same as above but then we cancel it. |
| 137 // * Write test for memory request that's pending, and then disk is disabled. |
| 138 // * Write test for file request where we have quota. |
| 139 // * Write test for file request where we don't have quota (and just fail). |
| 140 // * Write test for file request and then we disable disk. |
| 141 |
| 142 } // namespace |
| 143 } // namespace storage |
OLD | NEW |