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

Unified Diff: net/disk_cache/disk_cache_test_base.cc

Issue 12794003: Initialize the simple cache backend at runtime. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: upload from git Created 7 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: net/disk_cache/disk_cache_test_base.cc
diff --git a/net/disk_cache/disk_cache_test_base.cc b/net/disk_cache/disk_cache_test_base.cc
index 296713dd0fc5fb1f25bb548abe86ca890260cd0f..2249f480dcc53842aab2b452f78f830a0ccde980 100644
--- a/net/disk_cache/disk_cache_test_base.cc
+++ b/net/disk_cache/disk_cache_test_base.cc
@@ -10,8 +10,11 @@
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/backend_impl.h"
+#include "net/disk_cache/cache_util.h"
+#include "net/disk_cache/disk_cache.h"
#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"
DiskCacheTest::DiskCacheTest() {
CHECK(temp_dir_.CreateUniqueTempDir());
@@ -52,7 +55,7 @@ DiskCacheTestWithCache::DiskCacheTestWithCache()
size_(0),
type_(net::DISK_CACHE),
memory_only_(false),
- implementation_(false),
+ simple_cache_mode_(false),
force_creation_(false),
new_eviction_(false),
first_cleanup_(true),
@@ -64,9 +67,6 @@ DiskCacheTestWithCache::DiskCacheTestWithCache()
DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
void DiskCacheTestWithCache::InitCache() {
- if (mask_ || new_eviction_)
- implementation_ = true;
-
if (memory_only_)
InitMemoryCache();
else
@@ -79,7 +79,7 @@ void DiskCacheTestWithCache::InitCache() {
// We are expected to leak memory when simulating crashes.
void DiskCacheTestWithCache::SimulateCrash() {
- ASSERT_TRUE(implementation_ && !memory_only_);
+ ASSERT_TRUE(!memory_only_);
net::TestCompletionCallback cb;
int rv = cache_impl_->FlushQueueForTest(cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
@@ -88,11 +88,11 @@ void DiskCacheTestWithCache::SimulateCrash() {
delete cache_impl_;
EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
- InitDiskCacheImpl();
+ cache_ = CreateBackendAsModeSuggests(disk_cache::kNoRandom, &cache_thread_);
}
void DiskCacheTestWithCache::SetTestMode() {
- ASSERT_TRUE(implementation_ && !memory_only_);
+ ASSERT_TRUE(!memory_only_);
cache_impl_->SetUnitTestMode();
}
@@ -235,11 +235,6 @@ void DiskCacheTestWithCache::TearDown() {
}
void DiskCacheTestWithCache::InitMemoryCache() {
- if (!implementation_) {
- cache_ = disk_cache::MemBackendImpl::CreateBackend(size_, NULL);
- return;
- }
-
mem_cache_ = new disk_cache::MemBackendImpl(NULL);
cache_ = mem_cache_;
ASSERT_TRUE(NULL != cache_);
@@ -252,49 +247,68 @@ void DiskCacheTestWithCache::InitMemoryCache() {
void DiskCacheTestWithCache::InitDiskCache() {
if (first_cleanup_)
- ASSERT_TRUE(CleanupCacheDir());
+ EXPECT_TRUE(CleanupCacheDir());
if (!cache_thread_.IsRunning()) {
EXPECT_TRUE(cache_thread_.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
}
- ASSERT_TRUE(cache_thread_.message_loop() != NULL);
+ EXPECT_TRUE(cache_thread_.message_loop() != NULL);
- if (implementation_)
- return InitDiskCacheImpl();
+ cache_ = CreateBackendAsModeSuggests(disk_cache::kNoRandom, &cache_thread_);
+}
- scoped_refptr<base::MessageLoopProxy> thread =
- use_current_thread_ ? base::MessageLoopProxy::current() :
- cache_thread_.message_loop_proxy();
+// Testing backend creation retry logic is hard because the CacheCreator and
+// cache backend(s) are tightly coupled. So we take the default backend often.
+// Tests themselves need to be adjusted for platforms where the BackendImpl is
+// not the default backend.
+void DiskCacheTestWithCache::InitDefaultCacheViaCreator() {
+ if (!cache_thread_.IsRunning()) {
+ EXPECT_TRUE(cache_thread_.StartWithOptions(
+ base::Thread::Options(MessageLoop::TYPE_IO, 0)));
+ }
+ EXPECT_TRUE(cache_thread_.message_loop() != NULL);
net::TestCompletionCallback cb;
- int rv = disk_cache::BackendImpl::CreateBackend(
- cache_path_, force_creation_, size_, type_,
- disk_cache::kNoRandom, thread, NULL, &cache_, cb.callback());
- ASSERT_EQ(net::OK, cb.GetResult(rv));
+ disk_cache::CacheCreator* creator = new disk_cache::CacheCreator(cache_path_,
+ true, 0, net::DISK_CACHE, disk_cache::kNoRandom,
+ cache_thread_.message_loop_proxy(), NULL, &cache_, cb.callback());
+ int rv = creator->Run();
+ EXPECT_EQ(net::OK, cb.GetResult(rv));
}
-void DiskCacheTestWithCache::InitDiskCacheImpl() {
- scoped_refptr<base::MessageLoopProxy> thread =
- use_current_thread_ ? base::MessageLoopProxy::current() :
- cache_thread_.message_loop_proxy();
- if (mask_)
- cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, thread, NULL);
+disk_cache::Backend* DiskCacheTestWithCache::CreateBackendAsModeSuggests(
+ uint32 flags, base::Thread* thread) {
+ base::MessageLoopProxy* runner;
+ if (use_current_thread_)
+ runner = base::MessageLoopProxy::current();
else
- cache_impl_ = new disk_cache::BackendImpl(cache_path_, thread, NULL);
-
- cache_ = cache_impl_;
- ASSERT_TRUE(NULL != cache_);
-
- if (size_)
- EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
+ runner = thread->message_loop_proxy();
+
+ if (simple_cache_mode_) {
+ net::TestCompletionCallback cb;
+ disk_cache::Backend* simple_backend;
+ // TODO(pasko): split Simple Backend construction from initialization.
+ int rv = disk_cache::SimpleBackendImpl::CreateBackend(cache_path_, size_,
+ type_, disk_cache::kNone, make_scoped_refptr(runner), NULL,
+ &simple_backend, cb.callback());
+ EXPECT_EQ(net::OK, cb.GetResult(rv));
+ return simple_backend;
+ }
+ if (mask_)
+ cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
+ else
+ cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
+ cache_impl_->SetFlags(flags);
+ cache_impl_->SetMaxSize(size_);
+ cache_impl_->SetType(type_);
if (new_eviction_)
cache_impl_->SetNewEviction();
-
- cache_impl_->SetType(type_);
- cache_impl_->SetFlags(disk_cache::kNoRandom);
net::TestCompletionCallback cb;
int rv = cache_impl_->Init(cb.callback());
- ASSERT_EQ(net::OK, cb.GetResult(rv));
+ EXPECT_EQ(net::OK, cb.GetResult(rv));
+ cache_ = cache_impl_;
+ return cache_impl_;
}
+

Powered by Google App Engine
This is Rietveld 408576698