| Index: content/browser/blob_storage/blob_memory_controller_unittest.cc
|
| diff --git a/content/browser/blob_storage/blob_memory_controller_unittest.cc b/content/browser/blob_storage/blob_memory_controller_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cbb866386b41d4f46d1ca98da01d0e13624b1806
|
| --- /dev/null
|
| +++ b/content/browser/blob_storage/blob_memory_controller_unittest.cc
|
| @@ -0,0 +1,182 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "storage/browser/blob/blob_memory_controller.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/test/test_simple_task_runner.h"
|
| +#include "base/threading/thread_task_runner_handle.h"
|
| +#include "storage/common/data_element.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace storage {
|
| +namespace {
|
| +
|
| +using MemoryStrategyResult = BlobMemoryController::MemoryStrategyResult;
|
| +using FileCreationInfo = BlobMemoryController::FileCreationInfo;
|
| +using base::TestSimpleTaskRunner;
|
| +
|
| +const std::string kBlobStorageDirectory = "blob_storage";
|
| +const size_t kTestBlobStorageIPCThresholdBytes = 20;
|
| +const size_t kTestBlobStorageMaxSharedMemoryBytes = 50;
|
| +const size_t kTestBlobStorageMaxBlobMemorySize = 400;
|
| +const uint64_t kTestBlobStorageMaxDiskSpace = 1000;
|
| +const size_t kTestBlobStorageInFlightMemory = 10;
|
| +const uint64_t kTestBlobStorageMinFileSizeBytes = 10;
|
| +const uint64_t kTestBlobStorageMaxFileSizeBytes = 100;
|
| +
|
| +// void SetFileCreationInfo(BlobMemoryController::FileCreationInfo* output,
|
| +// const BlobMemoryController::FileCreationInfo& in) {
|
| +// *output = in;
|
| +//}
|
| +//
|
| +// void SetPointer(bool* out, bool success) {
|
| +// *out = success;
|
| +//}
|
| +
|
| +class BlobMemoryControllerTest : public testing::Test {
|
| + protected:
|
| + BlobMemoryControllerTest() : thread_runner_handle_(io_runner_) {}
|
| +
|
| + void SetUp() override {
|
| + ASSERT_EQ(true, base::CreateNewTempDirectory("blob_storage", &temp_dir_));
|
| + };
|
| +
|
| + void TearDown() override {
|
| + files_created_.clear();
|
| + // Make sure we clean up the files.
|
| + io_runner_->RunPendingTasks();
|
| + file_runner_->RunPendingTasks();
|
| + ASSERT_EQ(true, base::DeleteFile(temp_dir_, true));
|
| + }
|
| +
|
| + void SetTestMemoryLimits(BlobMemoryController* controller) {
|
| + controller->SetMemoryConstantsForTesting(
|
| + kTestBlobStorageIPCThresholdBytes, kTestBlobStorageMaxSharedMemoryBytes,
|
| + kTestBlobStorageMaxBlobMemorySize, kTestBlobStorageMaxDiskSpace,
|
| + kTestBlobStorageInFlightMemory, kTestBlobStorageMinFileSizeBytes,
|
| + kTestBlobStorageMaxFileSizeBytes);
|
| + }
|
| +
|
| + void SaveFileCreationInfo(FileCreationInfo info) {
|
| + files_created_.push_back(std::move(info));
|
| + }
|
| +
|
| + base::Callback<void(FileCreationInfo info)> GetFileCreationCallback() {
|
| + return base::Bind(&BlobMemoryControllerTest::SaveFileCreationInfo,
|
| + base::Unretained(this));
|
| + }
|
| +
|
| + base::FilePath temp_dir_;
|
| + std::vector<FileCreationInfo> files_created_;
|
| +
|
| + scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner();
|
| +
|
| + scoped_refptr<base::TestSimpleTaskRunner> io_runner_ =
|
| + new TestSimpleTaskRunner();
|
| +
|
| + // We set this to the IO thread runner, as this is used for the
|
| + // OnMemoryRequest calls, which are on that thread.
|
| + base::ThreadTaskRunnerHandle thread_runner_handle_;
|
| +};
|
| +
|
| +TEST_F(BlobMemoryControllerTest, TempFiles) {
|
| + BlobMemoryController controller;
|
| + SetTestMemoryLimits(&controller);
|
| +
|
| + // Check we can't create files.
|
| + controller.CreateTemporaryFile(10ull, GetFileCreationCallback());
|
| + ASSERT_EQ(1u, files_created_.size());
|
| + ASSERT_EQ(base::File::FILE_ERROR_FAILED, files_created_[0].error);
|
| +
|
| + // Check that we can when disk is enabled.
|
| + controller.EnableDisk(temp_dir_, file_runner_);
|
| + controller.CreateTemporaryFile(10, GetFileCreationCallback());
|
| + EXPECT_TRUE(file_runner_->HasPendingTask());
|
| + file_runner_->RunPendingTasks();
|
| + EXPECT_TRUE(io_runner_->HasPendingTask());
|
| + io_runner_->RunPendingTasks();
|
| +
|
| + ASSERT_EQ(2u, files_created_.size());
|
| + EXPECT_EQ(base::File::FILE_OK, files_created_[1].error);
|
| + files_created_.clear();
|
| + // Double check that we delete them.
|
| + io_runner_->RunPendingTasks();
|
| + EXPECT_TRUE(file_runner_->HasPendingTask());
|
| + file_runner_->RunPendingTasks();
|
| +}
|
| +
|
| +TEST_F(BlobMemoryControllerTest, Strategy) {
|
| + BlobMemoryController controller;
|
| + SetTestMemoryLimits(&controller);
|
| + std::vector<DataElement> elements = {DataElement()};
|
| + MemoryStrategyResult strategy = MemoryStrategyResult::TOO_LARGE;
|
| + uint64_t total_bytes = 0;
|
| +
|
| + // No transportation needed.
|
| + elements[0].SetToBlob("kReferencedBlobId");
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(0ull, total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::NONE_NEEDED, strategy);
|
| +
|
| + // IPC.
|
| + elements[0].SetToBytesDescription(kTestBlobStorageIPCThresholdBytes);
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(kTestBlobStorageIPCThresholdBytes, total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::IPC, strategy);
|
| +
|
| + // Shared Memory.
|
| + elements[0].SetToBytesDescription(kTestBlobStorageMaxSharedMemoryBytes);
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(kTestBlobStorageMaxSharedMemoryBytes, total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, strategy);
|
| +
|
| + // Not too large, as disk isn't enabled.
|
| + elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize +
|
| + kTestBlobStorageInFlightMemory);
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize + kTestBlobStorageInFlightMemory,
|
| + total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::SHARED_MEMORY, strategy);
|
| +
|
| + // Too large.
|
| + elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize +
|
| + kTestBlobStorageInFlightMemory + 1);
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(
|
| + kTestBlobStorageMaxBlobMemorySize + kTestBlobStorageInFlightMemory + 1,
|
| + total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, strategy);
|
| +
|
| + // Enable disk, and check file strategies.
|
| + controller.EnableDisk(temp_dir_, file_runner_);
|
| + elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize + 1);
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize + 1, total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::FILE, strategy);
|
| +
|
| + // File strategy for > max in memory.
|
| + elements[0].SetToBytesDescription(kTestBlobStorageMaxBlobMemorySize + 1);
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(kTestBlobStorageMaxBlobMemorySize + 1, total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::FILE, strategy);
|
| +
|
| + // Too large for disk.
|
| + elements[0].SetToBytesDescription(kTestBlobStorageMaxDiskSpace + 1);
|
| + controller.DecideBlobTransportationMemoryStrategy(elements, &total_bytes,
|
| + &strategy);
|
| + EXPECT_EQ(kTestBlobStorageMaxDiskSpace + 1, total_bytes);
|
| + EXPECT_EQ(MemoryStrategyResult::TOO_LARGE, strategy);
|
| +}
|
| +
|
| +} // namespace
|
| +} // namespace storage
|
|
|