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

Unified Diff: net/disk_cache/disk_cache_test_base.cc

Issue 17507006: Disk cache v3 ref2 Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Incl IndexTable cl Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/disk_cache/disk_cache_test_base.h ('k') | net/disk_cache/disk_cache_test_util.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/disk_cache_test_base.cc
===================================================================
--- net/disk_cache/disk_cache_test_base.cc (revision 232523)
+++ 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"
@@ -18,10 +19,32 @@
#include "net/disk_cache/mem_backend_impl.h"
#include "net/disk_cache/simple/simple_backend_impl.h"
#include "net/disk_cache/simple/simple_index.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 (!base::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 (!base::MessageLoop::current())
message_loop_.reset(new base::MessageLoopForIO());
}
@@ -46,6 +69,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();
}
@@ -54,6 +86,7 @@
: cache_impl_(NULL),
simple_cache_impl_(NULL),
mem_cache_(NULL),
+ cache_impl_v3_(NULL),
mask_(0),
size_(0),
type_(net::DISK_CACHE),
@@ -62,9 +95,12 @@
simple_cache_wait_for_index_(true),
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") {
}
@@ -84,20 +120,32 @@
// 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();
+ cache_.reset();
+ 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);
+ cache_.reset();
+ cache_impl_v3_ = NULL;
+ } else {
+ ASSERT_TRUE(0);
+ }
- cache_.reset();
- EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
-
- 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) {
@@ -110,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,
@@ -159,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();
@@ -209,18 +273,90 @@
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) {
+ FlushQueueForTest();
+ if (!cache_impl_v3_)
+ return;
+
+ net::TestCompletionCallback cb;
+ cache_impl_v3_->AddDelayForTest(30);
+ 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() {
if (simple_cache_mode_) {
// The simple cache uses second resolution for many timeouts, so it's safest
@@ -240,15 +376,18 @@
}
void DiskCacheTestWithCache::TearDown() {
- base::RunLoop().RunUntilIdle();
+ CleanupForTest();
disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
base::RunLoop().RunUntilIdle();
cache_.reset();
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_));
}
base::RunLoop().RunUntilIdle();
disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
@@ -272,25 +411,26 @@
if (!cache_thread_.IsRunning()) {
ASSERT_TRUE(cache_thread_.StartWithOptions(
- base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
}
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().get();
+ runner = base::MessageLoopProxy::current();
else
- runner = thread->message_loop_proxy().get();
+ runner = thread->message_loop_proxy();
if (simple_cache_mode_) {
net::TestCompletionCallback cb;
scoped_ptr<disk_cache::SimpleBackendImpl> simple_backend(
- new disk_cache::SimpleBackendImpl(
- cache_path_, size_, type_, make_scoped_refptr(runner).get(), NULL));
+ new disk_cache::SimpleBackendImpl(cache_path_, size_, type_,
+ make_scoped_refptr(runner).get(),
+ NULL));
int rv = simple_backend->Init(cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
simple_cache_impl_ = simple_backend.get();
@@ -306,17 +446,40 @@
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_.reset(cache_impl_);
+ if (!cache_)
+ cache_.reset(cache_impl_v3_);
ASSERT_TRUE(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));
}
« no previous file with comments | « net/disk_cache/disk_cache_test_base.h ('k') | net/disk_cache/disk_cache_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698