Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: content/browser/blob_storage/blob_memory_controller_unittest.cc

Issue 2055053003: [BlobAsync] Disk support for blob storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Finished comments, added new pending enum state Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 uint64_t kTestBlobStorageMaxDiskSpace = 1000;
26 const size_t kTestBlobStorageInFlightMemory = 10;
27 const uint64_t kTestBlobStorageMinFileSizeBytes = 10;
28 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100;
29
30 // void SetFileCreationInfo(BlobMemoryController::FileCreationInfo* output,
31 // const BlobMemoryController::FileCreationInfo& in) {
32 // *output = in;
33 //}
34 //
35 // void SetPointer(bool* out, bool success) {
36 // *out = success;
37 //}
38
39 class BlobMemoryControllerTest : public testing::Test {
40 protected:
41 BlobMemoryControllerTest() : thread_runner_handle_(io_runner_) {}
42
43 void SetUp() override {
44 ASSERT_EQ(true, base::CreateNewTempDirectory("blob_storage", &temp_dir_));
45 };
46
47 void TearDown() override {
48 files_created_.clear();
49 // Make sure we clean up the files.
50 io_runner_->RunPendingTasks();
51 file_runner_->RunPendingTasks();
52 ASSERT_EQ(true, base::DeleteFile(temp_dir_, true));
53 }
54
55 void SetTestMemoryLimits(BlobMemoryController* controller) {
56 controller->SetMemoryConstantsForTesting(
57 kTestBlobStorageIPCThresholdBytes, kTestBlobStorageMaxSharedMemoryBytes,
58 kTestBlobStorageMaxBlobMemorySize, kTestBlobStorageMaxDiskSpace,
59 kTestBlobStorageInFlightMemory, kTestBlobStorageMinFileSizeBytes,
60 kTestBlobStorageMaxFileSizeBytes);
61 }
62
63 void SaveFileCreationInfo(FileCreationInfo info) {
64 files_created_.push_back(std::move(info));
65 }
66
67 base::Callback<void(FileCreationInfo info)> GetFileCreationCallback() {
68 return base::Bind(&BlobMemoryControllerTest::SaveFileCreationInfo,
69 base::Unretained(this));
70 }
71
72 base::FilePath temp_dir_;
73 std::vector<FileCreationInfo> files_created_;
74
75 scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner();
76
77 scoped_refptr<base::TestSimpleTaskRunner> io_runner_ =
78 new TestSimpleTaskRunner();
79
80 // We set this to the IO thread runner, as this is used for the
81 // OnMemoryRequest calls, which are on that thread.
82 base::ThreadTaskRunnerHandle thread_runner_handle_;
83 };
84
85 TEST_F(BlobMemoryControllerTest, TempFiles) {
86 BlobMemoryController controller;
87 SetTestMemoryLimits(&controller);
88
89 // Check we can't create files.
90 controller.CreateTemporaryFile(10ull, GetFileCreationCallback());
91 ASSERT_EQ(1u, files_created_.size());
92 ASSERT_EQ(base::File::FILE_ERROR_FAILED, files_created_[0].error);
93
94 // Check that we can when disk is enabled.
95 controller.EnableDisk(temp_dir_, file_runner_);
96 controller.CreateTemporaryFile(10, GetFileCreationCallback());
97 EXPECT_TRUE(file_runner_->HasPendingTask());
98 file_runner_->RunPendingTasks();
99 EXPECT_TRUE(io_runner_->HasPendingTask());
100 io_runner_->RunPendingTasks();
101
102 ASSERT_EQ(2u, files_created_.size());
103 EXPECT_EQ(base::File::FILE_OK, files_created_[1].error);
104 files_created_.clear();
105 // Double check that we delete them.
106 io_runner_->RunPendingTasks();
107 EXPECT_TRUE(file_runner_->HasPendingTask());
108 file_runner_->RunPendingTasks();
109 }
110
111 TEST_F(BlobMemoryControllerTest, Strategy) {
112 BlobMemoryController controller;
113 SetTestMemoryLimits(&controller);
114 std::vector<DataElement> elements = {DataElement()};
115 MemoryStrategyResult strategy = MemoryStrategyResult::TOO_LARGE;
116 uint64_t total_bytes = 0;
117
118 // No transportation needed.
119 elements[0].SetToBlob("kReferencedBlobId");
120 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
121 &strategy);
122 EXPECT_EQ(0ull, total_bytes);
123 EXPECT_EQ(MemoryStrategyResult::NONE_NEEDED, strategy);
124
125 // IPC.
126 elements[0].SetToBytesDescription(kTestBlobStorageIPCThresholdBytes);
127 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
128 &strategy);
129 EXPECT_EQ(kTestBlobStorageIPCThresholdBytes, total_bytes);
130 EXPECT_EQ(MemoryStrategyResult::IPC, strategy);
131
132 // Shared Memory.
133 elements[0].SetToBytesDescription(kTestBlobStorageMaxSharedMemoryBytes);
134 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
135 &strategy);
136 EXPECT_EQ(kTestBlobStorageMaxSharedMemoryBytes, total_bytes);
137 EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, strategy);
138
139 // Not too large, as disk isn't enabled.
140 elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize +
141 kTestBlobStorageInFlightMemory);
142 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
143 &strategy);
144 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize + kTestBlobStorageInFlightMemory,
145 total_bytes);
146 EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, strategy);
147
148 // Too large.
149 elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize +
150 kTestBlobStorageInFlightMemory + 1);
151 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
152 &strategy);
153 EXPECT_EQ(
154 kTestBlobStorageMaxBlobMemorySize + kTestBlobStorageInFlightMemory + 1,
155 total_bytes);
156 EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, strategy);
157
158 // Enable disk, and check file strategies.
159 controller.EnableDisk(temp_dir_, file_runner_);
160 elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize + 1);
161 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
162 &strategy);
163 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize + 1, total_bytes);
164 EXPECT_EQ(MemoryStrategyResult::FILE, strategy);
165
166 // File strategy for > max in memory.
167 elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize + 1);
168 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
169 &strategy);
170 EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize + 1, total_bytes);
171 EXPECT_EQ(MemoryStrategyResult::FILE, strategy);
172
173 // Too large for disk.
174 elements[0].SetToBytesDescription(kTestBlobStorageMaxDiskSpace + 1);
175 controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
176 &strategy);
177 EXPECT_EQ(kTestBlobStorageMaxDiskSpace + 1, total_bytes);
178 EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, strategy);
179 }
180
181 } // namespace
182 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698