[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/disk_cache/disk_cache_test_base.h" |
| 6 | |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 8 | #include "base/path_service.h" |
[email protected] | 94a9d1a | 2013-03-24 09:50:31 | [diff] [blame] | 9 | #include "base/run_loop.h" |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 10 | #include "base/single_thread_task_runner.h" |
| 11 | #include "base/thread_task_runner_handle.h" |
[email protected] | 8b2f8a5 | 2013-08-28 08:45:35 | [diff] [blame] | 12 | #include "base/threading/platform_thread.h" |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 13 | #include "net/base/io_buffer.h" |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 14 | #include "net/base/net_errors.h" |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 15 | #include "net/base/test_completion_callback.h" |
[email protected] | c2c5cfc | 2014-03-03 16:35:28 | [diff] [blame] | 16 | #include "net/disk_cache/blockfile/backend_impl.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 17 | #include "net/disk_cache/cache_util.h" |
| 18 | #include "net/disk_cache/disk_cache.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 19 | #include "net/disk_cache/disk_cache_test_util.h" |
[email protected] | c2c5cfc | 2014-03-03 16:35:28 | [diff] [blame] | 20 | #include "net/disk_cache/memory/mem_backend_impl.h" |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 21 | #include "net/disk_cache/simple/simple_backend_impl.h" |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 22 | #include "net/disk_cache/simple/simple_index.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 24 | DiskCacheTest::DiskCacheTest() { |
[email protected] | 784f930 | 2012-08-17 00:13:43 | [diff] [blame] | 25 | CHECK(temp_dir_.CreateUniqueTempDir()); |
| 26 | cache_path_ = temp_dir_.path(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 27 | if (!base::MessageLoop::current()) |
| 28 | message_loop_.reset(new base::MessageLoopForIO()); |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | DiskCacheTest::~DiskCacheTest() { |
| 32 | } |
| 33 | |
| 34 | bool DiskCacheTest::CopyTestCache(const std::string& name) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 35 | base::FilePath path; |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 36 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 37 | path = path.AppendASCII("net"); |
| 38 | path = path.AppendASCII("data"); |
| 39 | path = path.AppendASCII("cache_tests"); |
| 40 | path = path.AppendASCII(name); |
| 41 | |
| 42 | if (!CleanupCacheDir()) |
| 43 | return false; |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 44 | return base::CopyDirectory(path, cache_path_, false); |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool DiskCacheTest::CleanupCacheDir() { |
| 48 | return DeleteCache(cache_path_); |
| 49 | } |
| 50 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 51 | void DiskCacheTest::TearDown() { |
[email protected] | 94a9d1a | 2013-03-24 09:50:31 | [diff] [blame] | 52 | base::RunLoop().RunUntilIdle(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 53 | } |
| 54 | |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 55 | DiskCacheTestWithCache::TestIterator::TestIterator( |
| 56 | scoped_ptr<disk_cache::Backend::Iterator> iterator) |
| 57 | : iterator_(iterator.Pass()) { |
| 58 | } |
| 59 | |
| 60 | DiskCacheTestWithCache::TestIterator::~TestIterator() {} |
| 61 | |
| 62 | int DiskCacheTestWithCache::TestIterator::OpenNextEntry( |
| 63 | disk_cache::Entry** next_entry) { |
| 64 | net::TestCompletionCallback cb; |
| 65 | int rv = iterator_->OpenNextEntry(next_entry, cb.callback()); |
| 66 | return cb.GetResult(rv); |
| 67 | } |
| 68 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 69 | DiskCacheTestWithCache::DiskCacheTestWithCache() |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 70 | : cache_impl_(NULL), |
[email protected] | 1ed9575 | 2013-04-23 00:12:36 | [diff] [blame] | 71 | simple_cache_impl_(NULL), |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 72 | mem_cache_(NULL), |
| 73 | mask_(0), |
| 74 | size_(0), |
| 75 | type_(net::DISK_CACHE), |
| 76 | memory_only_(false), |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 77 | simple_cache_mode_(false), |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 78 | simple_cache_wait_for_index_(true), |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 79 | force_creation_(false), |
| 80 | new_eviction_(false), |
| 81 | first_cleanup_(true), |
| 82 | integrity_(true), |
| 83 | use_current_thread_(false), |
| 84 | cache_thread_("CacheThread") { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 87 | DiskCacheTestWithCache::~DiskCacheTestWithCache() {} |
| 88 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 89 | void DiskCacheTestWithCache::InitCache() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 90 | if (memory_only_) |
| 91 | InitMemoryCache(); |
| 92 | else |
| 93 | InitDiskCache(); |
| 94 | |
| 95 | ASSERT_TRUE(NULL != cache_); |
[email protected] | a255384 | 2013-04-19 14:25:18 | [diff] [blame] | 96 | if (first_cleanup_) |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 97 | ASSERT_EQ(0, cache_->GetEntryCount()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 98 | } |
| 99 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 100 | // We are expected to leak memory when simulating crashes. |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 101 | void DiskCacheTestWithCache::SimulateCrash() { |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 102 | ASSERT_TRUE(!memory_only_); |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 103 | net::TestCompletionCallback cb; |
| 104 | int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 105 | ASSERT_EQ(net::OK, cb.GetResult(rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 106 | cache_impl_->ClearRefCountForTest(); |
| 107 | |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 108 | cache_.reset(); |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 109 | EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 110 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 111 | CreateBackend(disk_cache::kNoRandom, &cache_thread_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | } |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 113 | |
[email protected] | 4d5e036 | 2008-08-28 00:59:06 | [diff] [blame] | 114 | void DiskCacheTestWithCache::SetTestMode() { |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 115 | ASSERT_TRUE(!memory_only_); |
[email protected] | a9da16d | 2008-07-30 21:41:54 | [diff] [blame] | 116 | cache_impl_->SetUnitTestMode(); |
| 117 | } |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 118 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 119 | void DiskCacheTestWithCache::SetMaxSize(int size) { |
| 120 | size_ = size; |
[email protected] | 1ed9575 | 2013-04-23 00:12:36 | [diff] [blame] | 121 | if (simple_cache_impl_) |
| 122 | EXPECT_TRUE(simple_cache_impl_->SetMaxSize(size)); |
| 123 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 124 | if (cache_impl_) |
| 125 | EXPECT_TRUE(cache_impl_->SetMaxSize(size)); |
| 126 | |
| 127 | if (mem_cache_) |
| 128 | EXPECT_TRUE(mem_cache_->SetMaxSize(size)); |
| 129 | } |
| 130 | |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 131 | int DiskCacheTestWithCache::OpenEntry(const std::string& key, |
| 132 | disk_cache::Entry** entry) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 133 | net::TestCompletionCallback cb; |
| 134 | int rv = cache_->OpenEntry(key, entry, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 135 | return cb.GetResult(rv); |
| 136 | } |
| 137 | |
| 138 | int DiskCacheTestWithCache::CreateEntry(const std::string& key, |
| 139 | disk_cache::Entry** entry) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 140 | net::TestCompletionCallback cb; |
| 141 | int rv = cache_->CreateEntry(key, entry, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 142 | return cb.GetResult(rv); |
| 143 | } |
| 144 | |
| 145 | int DiskCacheTestWithCache::DoomEntry(const std::string& key) { |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 146 | net::TestCompletionCallback cb; |
| 147 | int rv = cache_->DoomEntry(key, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 148 | return cb.GetResult(rv); |
| 149 | } |
| 150 | |
| 151 | int DiskCacheTestWithCache::DoomAllEntries() { |
[email protected] | 6ad7c091 | 2011-12-15 19:10:19 | [diff] [blame] | 152 | net::TestCompletionCallback cb; |
| 153 | int rv = cache_->DoomAllEntries(cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 154 | return cb.GetResult(rv); |
| 155 | } |
| 156 | |
| 157 | int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time, |
| 158 | const base::Time end_time) { |
[email protected] | 6ad7c091 | 2011-12-15 19:10:19 | [diff] [blame] | 159 | net::TestCompletionCallback cb; |
| 160 | int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 161 | return cb.GetResult(rv); |
| 162 | } |
| 163 | |
| 164 | int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 165 | net::TestCompletionCallback cb; |
| 166 | int rv = cache_->DoomEntriesSince(initial_time, cb.callback()); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 167 | return cb.GetResult(rv); |
| 168 | } |
| 169 | |
msramek | aee01ceb | 2015-10-07 14:23:33 | [diff] [blame^] | 170 | int DiskCacheTestWithCache::CalculateSizeOfAllEntries() { |
| 171 | net::TestCompletionCallback cb; |
| 172 | int rv = cache_->CalculateSizeOfAllEntries(cb.callback()); |
| 173 | return cb.GetResult(rv); |
| 174 | } |
| 175 | |
gavinp | b1aaa05 | 2014-09-24 14:54:35 | [diff] [blame] | 176 | scoped_ptr<DiskCacheTestWithCache::TestIterator> |
| 177 | DiskCacheTestWithCache::CreateIterator() { |
| 178 | return scoped_ptr<TestIterator>(new TestIterator(cache_->CreateIterator())); |
[email protected] | bc4fb8e | 2010-03-18 23:58:17 | [diff] [blame] | 179 | } |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 180 | |
| 181 | void DiskCacheTestWithCache::FlushQueueForTest() { |
| 182 | if (memory_only_ || !cache_impl_) |
| 183 | return; |
| 184 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 185 | net::TestCompletionCallback cb; |
| 186 | int rv = cache_impl_->FlushQueueForTest(cb.callback()); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 187 | EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 188 | } |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 189 | |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 190 | void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) { |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 191 | if (memory_only_ || !cache_impl_) { |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 192 | closure.Run(); |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 196 | net::TestCompletionCallback cb; |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 197 | int rv = cache_impl_->RunTaskForTest(closure, cb.callback()); |
[email protected] | 65188eb | 2010-09-16 20:59:29 | [diff] [blame] | 198 | EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 199 | } |
| 200 | |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 201 | int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index, |
| 202 | int offset, net::IOBuffer* buf, int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 203 | net::TestCompletionCallback cb; |
| 204 | int rv = entry->ReadData(index, offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 205 | return cb.GetResult(rv); |
| 206 | } |
| 207 | |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 208 | int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index, |
| 209 | int offset, net::IOBuffer* buf, int len, |
| 210 | bool truncate) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 211 | net::TestCompletionCallback cb; |
| 212 | int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 213 | return cb.GetResult(rv); |
| 214 | } |
| 215 | |
| 216 | int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry, |
| 217 | int64 offset, net::IOBuffer* buf, |
| 218 | int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 219 | net::TestCompletionCallback cb; |
| 220 | int rv = entry->ReadSparseData(offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 221 | return cb.GetResult(rv); |
| 222 | } |
| 223 | |
| 224 | int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry, |
| 225 | int64 offset, |
| 226 | net::IOBuffer* buf, int len) { |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 227 | net::TestCompletionCallback cb; |
| 228 | int rv = entry->WriteSparseData(offset, buf, len, cb.callback()); |
[email protected] | e1fcf14 | 2010-08-23 18:47:25 | [diff] [blame] | 229 | return cb.GetResult(rv); |
| 230 | } |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 231 | |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 232 | void DiskCacheTestWithCache::TrimForTest(bool empty) { |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 233 | RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest, |
| 234 | base::Unretained(cache_impl_), |
| 235 | empty)); |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) { |
[email protected] | f27bbe00 | 2011-12-22 11:29:34 | [diff] [blame] | 239 | RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest, |
| 240 | base::Unretained(cache_impl_), |
| 241 | empty)); |
[email protected] | ceb61da3 | 2011-01-25 23:52:02 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 244 | void DiskCacheTestWithCache::AddDelay() { |
[email protected] | 8b2f8a5 | 2013-08-28 08:45:35 | [diff] [blame] | 245 | if (simple_cache_mode_) { |
| 246 | // The simple cache uses second resolution for many timeouts, so it's safest |
| 247 | // to advance by at least whole seconds before falling back into the normal |
| 248 | // disk cache epsilon advance. |
| 249 | const base::Time initial_time = base::Time::Now(); |
| 250 | do { |
| 251 | base::PlatformThread::YieldCurrentThread(); |
| 252 | } while (base::Time::Now() - |
| 253 | initial_time < base::TimeDelta::FromSeconds(1)); |
| 254 | } |
| 255 | |
[email protected] | 05f8087d | 2012-06-29 18:58:37 | [diff] [blame] | 256 | base::Time initial = base::Time::Now(); |
| 257 | while (base::Time::Now() <= initial) { |
| 258 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
| 259 | }; |
| 260 | } |
| 261 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 262 | void DiskCacheTestWithCache::TearDown() { |
[email protected] | 94a9d1a | 2013-03-24 09:50:31 | [diff] [blame] | 263 | base::RunLoop().RunUntilIdle(); |
[email protected] | 71cfb089 | 2013-08-30 18:29:14 | [diff] [blame] | 264 | disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 265 | base::RunLoop().RunUntilIdle(); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 266 | cache_.reset(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 267 | if (cache_thread_.IsRunning()) |
| 268 | cache_thread_.Stop(); |
| 269 | |
[email protected] | fccce90 | 2013-04-15 19:00:51 | [diff] [blame] | 270 | if (!memory_only_ && !simple_cache_mode_ && integrity_) { |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 271 | EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 272 | } |
[email protected] | de677dc | 2013-09-13 02:12:25 | [diff] [blame] | 273 | base::RunLoop().RunUntilIdle(); |
| 274 | disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 275 | DiskCacheTest::TearDown(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void DiskCacheTestWithCache::InitMemoryCache() { |
[email protected] | 9eb8cdf | 2011-03-17 18:53:02 | [diff] [blame] | 279 | mem_cache_ = new disk_cache::MemBackendImpl(NULL); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 280 | cache_.reset(mem_cache_); |
| 281 | ASSERT_TRUE(cache_); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 282 | |
| 283 | if (size_) |
| 284 | EXPECT_TRUE(mem_cache_->SetMaxSize(size_)); |
| 285 | |
| 286 | ASSERT_TRUE(mem_cache_->Init()); |
| 287 | } |
| 288 | |
| 289 | void DiskCacheTestWithCache::InitDiskCache() { |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 290 | if (first_cleanup_) |
[email protected] | 79b3fec | 2011-11-15 22:33:47 | [diff] [blame] | 291 | ASSERT_TRUE(CleanupCacheDir()); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 292 | |
| 293 | if (!cache_thread_.IsRunning()) { |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 294 | ASSERT_TRUE(cache_thread_.StartWithOptions( |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 295 | base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 296 | } |
| 297 | ASSERT_TRUE(cache_thread_.message_loop() != NULL); |
| 298 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 299 | CreateBackend(disk_cache::kNoRandom, &cache_thread_); |
| 300 | } |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 301 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 302 | void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) { |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 303 | scoped_refptr<base::SingleThreadTaskRunner> runner; |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 304 | if (use_current_thread_) |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 305 | runner = base::ThreadTaskRunnerHandle::Get(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 306 | else |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 307 | runner = thread->task_runner(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 308 | |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 309 | if (simple_cache_mode_) { |
| 310 | net::TestCompletionCallback cb; |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 311 | scoped_ptr<disk_cache::SimpleBackendImpl> simple_backend( |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 312 | new disk_cache::SimpleBackendImpl( |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 313 | cache_path_, size_, type_, runner, NULL)); |
[email protected] | 00831c82 | 2013-04-10 15:37:12 | [diff] [blame] | 314 | int rv = simple_backend->Init(cb.callback()); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 315 | ASSERT_EQ(net::OK, cb.GetResult(rv)); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 316 | simple_cache_impl_ = simple_backend.get(); |
dcheng | e3d1ddc | 2014-10-15 19:30:51 | [diff] [blame] | 317 | cache_ = simple_backend.Pass(); |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 318 | if (simple_cache_wait_for_index_) { |
| 319 | net::TestCompletionCallback wait_for_index_cb; |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 320 | rv = simple_cache_impl_->index()->ExecuteWhenReady( |
[email protected] | 7e48c1b | 2013-07-12 12:15:43 | [diff] [blame] | 321 | wait_for_index_cb.callback()); |
| 322 | ASSERT_EQ(net::OK, wait_for_index_cb.GetResult(rv)); |
| 323 | } |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | |
| 327 | if (mask_) |
| 328 | cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL); |
| 329 | else |
| 330 | cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL); |
[email protected] | 8c3f5a3 | 2013-08-01 11:57:53 | [diff] [blame] | 331 | cache_.reset(cache_impl_); |
| 332 | ASSERT_TRUE(cache_); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 333 | if (size_) |
| 334 | EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 335 | if (new_eviction_) |
| 336 | cache_impl_->SetNewEviction(); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 337 | cache_impl_->SetType(type_); |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 338 | cache_impl_->SetFlags(flags); |
[email protected] | 42c45963 | 2011-12-17 02:20:23 | [diff] [blame] | 339 | net::TestCompletionCallback cb; |
| 340 | int rv = cache_impl_->Init(cb.callback()); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 341 | ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 342 | } |