Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / net / disk_cache / disk_cache_test_base.cc
blob10fbd50c4816bf7e340b6efa8e07c23d83e94014
1 // Copyright (c) 2012 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.
5 #include "net/disk_cache/disk_cache_test_base.h"
7 #include "base/file_util.h"
8 #include "base/path_service.h"
9 #include "base/run_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/test_completion_callback.h"
13 #include "net/disk_cache/backend_impl.h"
14 #include "net/disk_cache/cache_util.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/disk_cache/disk_cache_test_util.h"
17 #include "net/disk_cache/mem_backend_impl.h"
18 #include "net/disk_cache/simple/simple_backend_impl.h"
20 DiskCacheTest::DiskCacheTest() {
21 CHECK(temp_dir_.CreateUniqueTempDir());
22 cache_path_ = temp_dir_.path();
23 if (!MessageLoop::current())
24 message_loop_.reset(new MessageLoopForIO());
27 DiskCacheTest::~DiskCacheTest() {
30 bool DiskCacheTest::CopyTestCache(const std::string& name) {
31 base::FilePath path;
32 PathService::Get(base::DIR_SOURCE_ROOT, &path);
33 path = path.AppendASCII("net");
34 path = path.AppendASCII("data");
35 path = path.AppendASCII("cache_tests");
36 path = path.AppendASCII(name);
38 if (!CleanupCacheDir())
39 return false;
40 return file_util::CopyDirectory(path, cache_path_, false);
43 bool DiskCacheTest::CleanupCacheDir() {
44 return DeleteCache(cache_path_);
47 void DiskCacheTest::TearDown() {
48 base::RunLoop().RunUntilIdle();
51 DiskCacheTestWithCache::DiskCacheTestWithCache()
52 : cache_(NULL),
53 cache_impl_(NULL),
54 mem_cache_(NULL),
55 mask_(0),
56 size_(0),
57 type_(net::DISK_CACHE),
58 memory_only_(false),
59 simple_cache_mode_(false),
60 force_creation_(false),
61 new_eviction_(false),
62 first_cleanup_(true),
63 integrity_(true),
64 use_current_thread_(false),
65 cache_thread_("CacheThread") {
68 DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
70 void DiskCacheTestWithCache::InitCache() {
71 if (memory_only_)
72 InitMemoryCache();
73 else
74 InitDiskCache();
76 ASSERT_TRUE(NULL != cache_);
77 if (first_cleanup_)
78 ASSERT_EQ(0, cache_->GetEntryCount());
81 // We are expected to leak memory when simulating crashes.
82 void DiskCacheTestWithCache::SimulateCrash() {
83 ASSERT_TRUE(!memory_only_);
84 net::TestCompletionCallback cb;
85 int rv = cache_impl_->FlushQueueForTest(cb.callback());
86 ASSERT_EQ(net::OK, cb.GetResult(rv));
87 cache_impl_->ClearRefCountForTest();
89 delete cache_impl_;
90 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
92 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
95 void DiskCacheTestWithCache::SetTestMode() {
96 ASSERT_TRUE(!memory_only_);
97 cache_impl_->SetUnitTestMode();
100 void DiskCacheTestWithCache::SetMaxSize(int size) {
101 size_ = size;
102 if (cache_impl_)
103 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
105 if (mem_cache_)
106 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
109 int DiskCacheTestWithCache::OpenEntry(const std::string& key,
110 disk_cache::Entry** entry) {
111 net::TestCompletionCallback cb;
112 int rv = cache_->OpenEntry(key, entry, cb.callback());
113 return cb.GetResult(rv);
116 int DiskCacheTestWithCache::CreateEntry(const std::string& key,
117 disk_cache::Entry** entry) {
118 net::TestCompletionCallback cb;
119 int rv = cache_->CreateEntry(key, entry, cb.callback());
120 return cb.GetResult(rv);
123 int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
124 net::TestCompletionCallback cb;
125 int rv = cache_->DoomEntry(key, cb.callback());
126 return cb.GetResult(rv);
129 int DiskCacheTestWithCache::DoomAllEntries() {
130 net::TestCompletionCallback cb;
131 int rv = cache_->DoomAllEntries(cb.callback());
132 return cb.GetResult(rv);
135 int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
136 const base::Time end_time) {
137 net::TestCompletionCallback cb;
138 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
139 return cb.GetResult(rv);
142 int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
143 net::TestCompletionCallback cb;
144 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
145 return cb.GetResult(rv);
148 int DiskCacheTestWithCache::OpenNextEntry(void** iter,
149 disk_cache::Entry** next_entry) {
150 net::TestCompletionCallback cb;
151 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback());
152 return cb.GetResult(rv);
155 void DiskCacheTestWithCache::FlushQueueForTest() {
156 if (memory_only_ || !cache_impl_)
157 return;
159 net::TestCompletionCallback cb;
160 int rv = cache_impl_->FlushQueueForTest(cb.callback());
161 EXPECT_EQ(net::OK, cb.GetResult(rv));
164 void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
165 if (memory_only_ || !cache_impl_) {
166 closure.Run();
167 return;
170 net::TestCompletionCallback cb;
171 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
172 EXPECT_EQ(net::OK, cb.GetResult(rv));
175 int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index,
176 int offset, net::IOBuffer* buf, int len) {
177 net::TestCompletionCallback cb;
178 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
179 return cb.GetResult(rv);
182 int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index,
183 int offset, net::IOBuffer* buf, int len,
184 bool truncate) {
185 net::TestCompletionCallback cb;
186 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
187 return cb.GetResult(rv);
190 int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
191 int64 offset, net::IOBuffer* buf,
192 int len) {
193 net::TestCompletionCallback cb;
194 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
195 return cb.GetResult(rv);
198 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
199 int64 offset,
200 net::IOBuffer* buf, int len) {
201 net::TestCompletionCallback cb;
202 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
203 return cb.GetResult(rv);
206 void DiskCacheTestWithCache::TrimForTest(bool empty) {
207 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
208 base::Unretained(cache_impl_),
209 empty));
212 void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
213 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
214 base::Unretained(cache_impl_),
215 empty));
218 void DiskCacheTestWithCache::AddDelay() {
219 base::Time initial = base::Time::Now();
220 while (base::Time::Now() <= initial) {
221 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
225 void DiskCacheTestWithCache::TearDown() {
226 base::RunLoop().RunUntilIdle();
227 delete cache_;
228 if (cache_thread_.IsRunning())
229 cache_thread_.Stop();
231 if (!memory_only_ && integrity_) {
232 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
235 PlatformTest::TearDown();
238 void DiskCacheTestWithCache::InitMemoryCache() {
239 mem_cache_ = new disk_cache::MemBackendImpl(NULL);
240 cache_ = mem_cache_;
241 ASSERT_TRUE(NULL != cache_);
243 if (size_)
244 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
246 ASSERT_TRUE(mem_cache_->Init());
249 void DiskCacheTestWithCache::InitDiskCache() {
250 if (first_cleanup_)
251 ASSERT_TRUE(CleanupCacheDir());
253 if (!cache_thread_.IsRunning()) {
254 ASSERT_TRUE(cache_thread_.StartWithOptions(
255 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
257 ASSERT_TRUE(cache_thread_.message_loop() != NULL);
259 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
262 void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
263 base::MessageLoopProxy* runner;
264 if (use_current_thread_)
265 runner = base::MessageLoopProxy::current();
266 else
267 runner = thread->message_loop_proxy();
269 if (simple_cache_mode_) {
270 net::TestCompletionCallback cb;
271 disk_cache::SimpleBackendImpl* simple_backend =
272 new disk_cache::SimpleBackendImpl(cache_path_, size_, type_,
273 make_scoped_refptr(runner), NULL);
274 int rv = simple_backend->Init(cb.callback());
275 ASSERT_EQ(net::OK, cb.GetResult(rv));
276 cache_ = simple_backend;
277 return;
280 if (mask_)
281 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
282 else
283 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
284 cache_ = cache_impl_;
285 ASSERT_TRUE(NULL != cache_);
286 if (size_)
287 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
288 if (new_eviction_)
289 cache_impl_->SetNewEviction();
290 cache_impl_->SetType(type_);
291 cache_impl_->SetFlags(flags);
292 net::TestCompletionCallback cb;
293 int rv = cache_impl_->Init(cb.callback());
294 ASSERT_EQ(net::OK, cb.GetResult(rv));
295 cache_ = cache_impl_;