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