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