| Index: net/disk_cache/disk_cache_test_base.cc
|
| ===================================================================
|
| --- net/disk_cache/disk_cache_test_base.cc (revision 199883)
|
| +++ net/disk_cache/disk_cache_test_base.cc (working copy)
|
| @@ -4,6 +4,7 @@
|
|
|
| #include "net/disk_cache/disk_cache_test_base.h"
|
|
|
| +#include "base/command_line.h"
|
| #include "base/file_util.h"
|
| #include "base/path_service.h"
|
| #include "base/run_loop.h"
|
| @@ -16,10 +17,32 @@
|
| #include "net/disk_cache/disk_cache_test_util.h"
|
| #include "net/disk_cache/mem_backend_impl.h"
|
| #include "net/disk_cache/simple/simple_backend_impl.h"
|
| +#include "net/disk_cache/v3/backend_impl_v3.h"
|
|
|
| +namespace {
|
| +
|
| +base::FilePath GetCachePath() {
|
| + base::FilePath path;
|
| + PathService::Get(base::DIR_TEMP, &path); // Ignore return value;
|
| + path = path.AppendASCII("cache_test");
|
| + if (!file_util::PathExists(path))
|
| + file_util::CreateDirectory(path);
|
| +
|
| + return path;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| DiskCacheTest::DiskCacheTest() {
|
| - CHECK(temp_dir_.CreateUniqueTempDir());
|
| - cache_path_ = temp_dir_.path();
|
| + CommandLine* command_line = CommandLine::ForCurrentProcess();
|
| + //if (command_line->HasSwitch("fixed-cache-dir")) {
|
| + if (true) {
|
| + cache_path_ = GetCachePath();
|
| + } else {
|
| + CHECK(temp_dir_.CreateUniqueTempDir());
|
| + cache_path_ = temp_dir_.path();
|
| + }
|
| +
|
| if (!MessageLoop::current())
|
| message_loop_.reset(new MessageLoopForIO());
|
| }
|
| @@ -44,6 +67,15 @@
|
| return DeleteCache(cache_path_);
|
| }
|
|
|
| +bool DiskCacheTest::ForceRandomCacheDir() {
|
| + if (temp_dir_.path().empty()) {
|
| + if (!temp_dir_.CreateUniqueTempDir())
|
| + return false;
|
| + cache_path_ = temp_dir_.path();
|
| + }
|
| + return true;
|
| +}
|
| +
|
| void DiskCacheTest::TearDown() {
|
| base::RunLoop().RunUntilIdle();
|
| }
|
| @@ -53,6 +85,7 @@
|
| cache_impl_(NULL),
|
| simple_cache_impl_(NULL),
|
| mem_cache_(NULL),
|
| + cache_impl_v3_(NULL),
|
| mask_(0),
|
| size_(0),
|
| type_(net::DISK_CACHE),
|
| @@ -60,9 +93,12 @@
|
| simple_cache_mode_(false),
|
| force_creation_(false),
|
| new_eviction_(false),
|
| + v3_(false),
|
| first_cleanup_(true),
|
| integrity_(true),
|
| use_current_thread_(false),
|
| + avoid_test_flag_(false),
|
| + unit_test_mode_(false),
|
| cache_thread_("CacheThread") {
|
| }
|
|
|
| @@ -82,20 +118,34 @@
|
| // We are expected to leak memory when simulating crashes.
|
| void DiskCacheTestWithCache::SimulateCrash() {
|
| ASSERT_TRUE(!memory_only_);
|
| - net::TestCompletionCallback cb;
|
| - int rv = cache_impl_->FlushQueueForTest(cb.callback());
|
| - ASSERT_EQ(net::OK, cb.GetResult(rv));
|
| - cache_impl_->ClearRefCountForTest();
|
| + if (cache_impl_) {
|
| + net::TestCompletionCallback cb;
|
| + int rv = cache_impl_->FlushQueueForTest(cb.callback());
|
| + ASSERT_EQ(net::OK, cb.GetResult(rv));
|
| + cache_impl_->ClearRefCountForTest();
|
|
|
| - delete cache_impl_;
|
| - EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
|
| + delete cache_impl_;
|
| + cache_impl_ = NULL;
|
| + EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
|
| + } else if (cache_impl_v3_) {
|
| + cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::NO_CLEAN_ON_EXIT);
|
| + delete cache_impl_v3_;
|
| + cache_impl_v3_ = NULL;
|
| + } else {
|
| + ASSERT_TRUE(0);
|
| + }
|
| + cache_ = NULL;
|
|
|
| - CreateBackend(disk_cache::kNoRandom, &cache_thread_);
|
| + CreateBackend(&cache_thread_);
|
| }
|
|
|
| void DiskCacheTestWithCache::SetTestMode() {
|
| ASSERT_TRUE(!memory_only_);
|
| - cache_impl_->SetUnitTestMode();
|
| + EXPECT_TRUE(cache_);
|
| + if (cache_impl_)
|
| + cache_impl_->SetUnitTestMode();
|
| + else if (cache_impl_v3_)
|
| + cache_impl_v3_->SetUnitTestMode();
|
| }
|
|
|
| void DiskCacheTestWithCache::SetMaxSize(int size) {
|
| @@ -108,6 +158,9 @@
|
|
|
| if (mem_cache_)
|
| EXPECT_TRUE(mem_cache_->SetMaxSize(size));
|
| +
|
| + if (cache_impl_v3_)
|
| + EXPECT_TRUE(cache_impl_v3_->SetMaxSize(size));
|
| }
|
|
|
| int DiskCacheTestWithCache::OpenEntry(const std::string& key,
|
| @@ -157,14 +210,27 @@
|
| }
|
|
|
| void DiskCacheTestWithCache::FlushQueueForTest() {
|
| - if (memory_only_ || !cache_impl_)
|
| + if (memory_only_ || !(cache_impl_ || cache_impl_v3_))
|
| return;
|
|
|
| net::TestCompletionCallback cb;
|
| - int rv = cache_impl_->FlushQueueForTest(cb.callback());
|
| + int rv;
|
| + if (cache_impl_)
|
| + rv = cache_impl_->FlushQueueForTest(cb.callback());
|
| + else
|
| + rv = cache_impl_v3_->FlushQueueForTest(cb.callback());
|
| EXPECT_EQ(net::OK, cb.GetResult(rv));
|
| }
|
|
|
| +void DiskCacheTestWithCache::CleanupForTest() {
|
| + if (!cache_impl_v3_)
|
| + return base::RunLoop().RunUntilIdle();
|
| +
|
| + net::TestCompletionCallback cb;
|
| + int rv = cache_impl_v3_->CleanupForTest(cb.callback());
|
| + EXPECT_EQ(net::OK, cb.GetResult(rv));
|
| +}
|
| +
|
| void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
|
| if (memory_only_ || !cache_impl_) {
|
| closure.Run();
|
| @@ -207,18 +273,88 @@
|
| return cb.GetResult(rv);
|
| }
|
|
|
| +int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry,
|
| + int64 offset, int len,
|
| + int64* start) {
|
| + net::TestCompletionCallback cb;
|
| + int rv = entry->GetAvailableRange(offset, len, start, cb.callback());
|
| + return cb.GetResult(rv);
|
| +}
|
| +
|
| +bool DiskCacheTestWithCache::IsAllocAllowed(int current_size, int new_size) {
|
| + if (cache_impl_)
|
| + return cache_impl_->IsAllocAllowed(current_size, new_size);
|
| + if (cache_impl_v3_)
|
| + return cache_impl_v3_->IsAllocAllowed(current_size, new_size, false);
|
| + return false;
|
| +}
|
| +
|
| +void DiskCacheTestWithCache::BufferDeleted(int size) {
|
| + if (cache_impl_)
|
| + cache_impl_->BufferDeleted(size);
|
| + else if (cache_impl_v3_)
|
| + cache_impl_v3_->BufferDeleted(size);
|
| +}
|
| +
|
| +int DiskCacheTestWithCache::GetTotalBuffersSize() {
|
| + if (cache_impl_)
|
| + return cache_impl_->GetTotalBuffersSize();
|
| + if (cache_impl_v3_)
|
| + return cache_impl_v3_->GetTotalBuffersSize();
|
| + return 0;
|
| +}
|
| +
|
| +void DiskCacheTestWithCache::SetNoBuffering() {
|
| + EXPECT_TRUE(cache_);
|
| + if (cache_impl_)
|
| + cache_impl_->SetFlags(disk_cache::kNoBuffering);
|
| + else if (cache_impl_v3_)
|
| + cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::NO_BUFFERING);
|
| +}
|
| +
|
| +void DiskCacheTestWithCache::WaitForEntryToClose(const std::string& key) {
|
| + if (!cache_impl_v3_)
|
| + return FlushQueueForTest();
|
| +
|
| + net::TestCompletionCallback cb;
|
| + int rv = cache_impl_v3_->WaitForEntryToCloseForTest(key, cb.callback());
|
| + EXPECT_EQ(net::OK, cb.GetResult(rv));
|
| +}
|
| +
|
| void DiskCacheTestWithCache::TrimForTest(bool empty) {
|
| - RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
|
| - base::Unretained(cache_impl_),
|
| - empty));
|
| + if (cache_impl_) {
|
| + RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
|
| + base::Unretained(cache_impl_), empty));
|
| + } else {
|
| + cache_impl_v3_->TrimForTest(empty);
|
| + }
|
| }
|
|
|
| void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
|
| - RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
|
| - base::Unretained(cache_impl_),
|
| - empty));
|
| + if (cache_impl_) {
|
| + RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
|
| + base::Unretained(cache_impl_), empty));
|
| + } else {
|
| + cache_impl_v3_->TrimDeletedListForTest(empty);
|
| + }
|
| }
|
|
|
| +base::Time DiskCacheTestWithCache::GetCurrentTime() {
|
| + if (cache_impl_v3_) {
|
| + return cache_impl_v3_->GetCurrentTime();
|
| + } else {
|
| + return base::Time::Now();
|
| + }
|
| +}
|
| +
|
| +void DiskCacheTestWithCache::AddDelayForTest(int seconds) {
|
| + if (cache_impl_v3_) {
|
| + cache_impl_v3_->AddDelayForTest(seconds);
|
| + } else {
|
| + AddDelay();
|
| + }
|
| +}
|
| +
|
| void DiskCacheTestWithCache::AddDelay() {
|
| base::Time initial = base::Time::Now();
|
| while (base::Time::Now() <= initial) {
|
| @@ -227,13 +363,16 @@
|
| }
|
|
|
| void DiskCacheTestWithCache::TearDown() {
|
| - base::RunLoop().RunUntilIdle();
|
| + CleanupForTest();
|
| delete cache_;
|
| if (cache_thread_.IsRunning())
|
| cache_thread_.Stop();
|
|
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| if (!memory_only_ && !simple_cache_mode_ && integrity_) {
|
| - EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
|
| + if (!v3_)
|
| + EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
|
| }
|
|
|
| PlatformTest::TearDown();
|
| @@ -260,10 +399,10 @@
|
| }
|
| ASSERT_TRUE(cache_thread_.message_loop() != NULL);
|
|
|
| - CreateBackend(disk_cache::kNoRandom, &cache_thread_);
|
| + CreateBackend(&cache_thread_);
|
| }
|
|
|
| -void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
|
| +void DiskCacheTestWithCache::CreateBackend(base::Thread* thread) {
|
| base::MessageLoopProxy* runner;
|
| if (use_current_thread_)
|
| runner = base::MessageLoopProxy::current();
|
| @@ -283,18 +422,39 @@
|
|
|
| if (mask_)
|
| cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
|
| + else if (v3_)
|
| + cache_impl_v3_ = new disk_cache::BackendImplV3(cache_path_, runner, NULL);
|
| else
|
| cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
|
| cache_ = cache_impl_;
|
| + if (!cache_)
|
| + cache_ = cache_impl_v3_;
|
| ASSERT_TRUE(NULL != cache_);
|
| - if (size_)
|
| - EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
|
| - if (new_eviction_)
|
| - cache_impl_->SetNewEviction();
|
| - cache_impl_->SetType(type_);
|
| - cache_impl_->SetFlags(flags);
|
| net::TestCompletionCallback cb;
|
| - int rv = cache_impl_->Init(cb.callback());
|
| + int rv = 0;
|
| + if (cache_impl_) {
|
| + if (size_)
|
| + EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
|
| + if (new_eviction_)
|
| + cache_impl_->SetNewEviction();
|
| +
|
| + cache_impl_->SetType(type_);
|
| + if (unit_test_mode_)
|
| + cache_impl_->SetFlags(disk_cache::kUnitTestMode);
|
| + if (!avoid_test_flag_)
|
| + cache_impl_->SetFlags(disk_cache::kNoRandom);
|
| + rv = cache_impl_->Init(cb.callback());
|
| + } else {
|
| + if (size_)
|
| + EXPECT_TRUE(cache_impl_v3_->SetMaxSize(size_));
|
| + if (new_eviction_)
|
| + cache_impl_v3_->SetNewEviction();
|
| +
|
| + cache_impl_v3_->SetType(type_);
|
| + cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::BASIC_UNIT_TEST);
|
| + if (unit_test_mode_)
|
| + cache_impl_v3_->SetFlags(disk_cache::BackendImplV3::UNIT_TEST_MODE);
|
| + rv = cache_impl_v3_->Init(cb.callback());
|
| + }
|
| ASSERT_EQ(net::OK, cb.GetResult(rv));
|
| - cache_ = cache_impl_;
|
| }
|
|
|