blob: 35c36db62a417e1f1f0c3b82b7115993f8cf9db1 [file] [log] [blame]
[email protected]05f8087d2012-06-29 18:58:371// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/disk_cache/disk_cache_test_base.h"
6
dchengc7eeda422015-12-26 03:56:487#include <utility>
8
Sebastien Marchand6d0558fd2019-01-25 16:49:379#include "base/bind.h"
thestigd8df0332014-09-04 06:33:2910#include "base/files/file_util.h"
[email protected]79b3fec2011-11-15 22:33:4711#include "base/path_service.h"
[email protected]94a9d1a2013-03-24 09:50:3112#include "base/run_loop.h"
[email protected]81e549a2014-08-21 04:19:5513#include "base/single_thread_task_runner.h"
[email protected]8b2f8a52013-08-28 08:45:3514#include "base/threading/platform_thread.h"
gabf767595f2016-05-11 18:50:3515#include "base/threading/thread_task_runner_handle.h"
[email protected]e1fcf142010-08-23 18:47:2516#include "net/base/io_buffer.h"
[email protected]fb2622f2010-07-13 18:00:5617#include "net/base/net_errors.h"
Josh Karlindd9a5d142018-06-06 00:35:4818#include "net/base/request_priority.h"
[email protected]bc4fb8e2010-03-18 23:58:1719#include "net/base/test_completion_callback.h"
Maks Orlovich036fd1f2017-08-07 17:51:1120#include "net/disk_cache/backend_cleanup_tracker.h"
[email protected]c2c5cfc2014-03-03 16:35:2821#include "net/disk_cache/blockfile/backend_impl.h"
[email protected]398ad132013-04-02 15:11:0122#include "net/disk_cache/cache_util.h"
23#include "net/disk_cache/disk_cache.h"
initial.commit586acc5fe2008-07-26 22:42:5224#include "net/disk_cache/disk_cache_test_util.h"
[email protected]c2c5cfc2014-03-03 16:35:2825#include "net/disk_cache/memory/mem_backend_impl.h"
[email protected]398ad132013-04-02 15:11:0126#include "net/disk_cache/simple/simple_backend_impl.h"
Maks Orlovichf378b3a2017-10-31 16:27:3027#include "net/disk_cache/simple/simple_file_tracker.h"
[email protected]7e48c1b2013-07-12 12:15:4328#include "net/disk_cache/simple/simple_index.h"
robpercival214763f2016-07-01 23:27:0129#include "net/test/gtest_util.h"
30#include "testing/gmock/include/gmock/gmock.h"
31#include "testing/gtest/include/gtest/gtest.h"
32
33using net::test::IsOk;
initial.commit586acc5fe2008-07-26 22:42:5234
[email protected]79b3fec2011-11-15 22:33:4735DiskCacheTest::DiskCacheTest() {
[email protected]784f9302012-08-17 00:13:4336 CHECK(temp_dir_.CreateUniqueTempDir());
Maks Orlovich8acfd6d2019-09-04 00:31:1337 // Put the cache into a subdir of |temp_dir_|, to permit tests to safely
38 // remove the cache directory without risking collisions with other tests.
39 cache_path_ = temp_dir_.GetPath().AppendASCII("cache");
40 CHECK(base::CreateDirectory(cache_path_));
[email protected]79b3fec2011-11-15 22:33:4741}
42
Chris Watkins68b15032017-12-01 03:07:1343DiskCacheTest::~DiskCacheTest() = default;
[email protected]79b3fec2011-11-15 22:33:4744
45bool DiskCacheTest::CopyTestCache(const std::string& name) {
[email protected]6cdfd7f2013-02-08 20:40:1546 base::FilePath path;
Avi Drissman5c80d832018-05-01 17:01:1947 base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]79b3fec2011-11-15 22:33:4748 path = path.AppendASCII("net");
49 path = path.AppendASCII("data");
50 path = path.AppendASCII("cache_tests");
51 path = path.AppendASCII(name);
52
53 if (!CleanupCacheDir())
54 return false;
[email protected]f0ff2ad2013-07-09 17:42:2655 return base::CopyDirectory(path, cache_path_, false);
[email protected]79b3fec2011-11-15 22:33:4756}
57
58bool DiskCacheTest::CleanupCacheDir() {
59 return DeleteCache(cache_path_);
60}
61
[email protected]17b89142008-11-07 21:52:1562void DiskCacheTest::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:3163 base::RunLoop().RunUntilIdle();
[email protected]17b89142008-11-07 21:52:1564}
65
gavinpb1aaa052014-09-24 14:54:3566DiskCacheTestWithCache::TestIterator::TestIterator(
danakjd04b92d2016-04-16 01:16:4967 std::unique_ptr<disk_cache::Backend::Iterator> iterator)
dchengc7eeda422015-12-26 03:56:4868 : iterator_(std::move(iterator)) {}
gavinpb1aaa052014-09-24 14:54:3569
Chris Watkins68b15032017-12-01 03:07:1370DiskCacheTestWithCache::TestIterator::~TestIterator() = default;
gavinpb1aaa052014-09-24 14:54:3571
72int DiskCacheTestWithCache::TestIterator::OpenNextEntry(
73 disk_cache::Entry** next_entry) {
Maks Orlovich8efea482019-08-20 17:14:5374 TestEntryResultCompletionCallback cb;
75 disk_cache::EntryResult result =
76 cb.GetResult(iterator_->OpenNextEntry(cb.callback()));
77 int rv = result.net_error();
78 *next_entry = result.ReleaseEntry();
79 return rv;
gavinpb1aaa052014-09-24 14:54:3580}
81
[email protected]7aefb152011-01-21 23:46:4982DiskCacheTestWithCache::DiskCacheTestWithCache()
Raul Tambre94493c652019-03-11 17:18:3583 : cache_impl_(nullptr),
84 simple_cache_impl_(nullptr),
85 mem_cache_(nullptr),
[email protected]7aefb152011-01-21 23:46:4986 mask_(0),
87 size_(0),
88 type_(net::DISK_CACHE),
89 memory_only_(false),
[email protected]398ad132013-04-02 15:11:0190 simple_cache_mode_(false),
[email protected]7e48c1b2013-07-12 12:15:4391 simple_cache_wait_for_index_(true),
[email protected]7aefb152011-01-21 23:46:4992 force_creation_(false),
93 new_eviction_(false),
94 first_cleanup_(true),
95 integrity_(true),
Bence Béky98447b12018-05-08 03:14:0196 use_current_thread_(false) {}
initial.commit586acc5fe2008-07-26 22:42:5297
Chris Watkins68b15032017-12-01 03:07:1398DiskCacheTestWithCache::~DiskCacheTestWithCache() = default;
[email protected]7aefb152011-01-21 23:46:4999
[email protected]4d5e0362008-08-28 00:59:06100void DiskCacheTestWithCache::InitCache() {
initial.commit586acc5fe2008-07-26 22:42:52101 if (memory_only_)
102 InitMemoryCache();
103 else
104 InitDiskCache();
105
Raul Tambre94493c652019-03-11 17:18:35106 ASSERT_TRUE(nullptr != cache_);
[email protected]a2553842013-04-19 14:25:18107 if (first_cleanup_)
[email protected]a9da16d2008-07-30 21:41:54108 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:52109}
110
initial.commit586acc5fe2008-07-26 22:42:52111// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:06112void DiskCacheTestWithCache::SimulateCrash() {
[email protected]398ad132013-04-02 15:11:01113 ASSERT_TRUE(!memory_only_);
[email protected]2a65aceb82011-12-19 20:59:27114 net::TestCompletionCallback cb;
115 int rv = cache_impl_->FlushQueueForTest(cb.callback());
robpercival214763f2016-07-01 23:27:01116 ASSERT_THAT(cb.GetResult(rv), IsOk());
initial.commit586acc5fe2008-07-26 22:42:52117 cache_impl_->ClearRefCountForTest();
118
[email protected]8c3f5a32013-08-01 11:57:53119 cache_.reset();
Maks Orlovichf3860652017-12-13 18:03:16120 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
initial.commit586acc5fe2008-07-26 22:42:52121
Maks Orlovich463dc502017-07-25 14:52:49122 CreateBackend(disk_cache::kNoRandom);
initial.commit586acc5fe2008-07-26 22:42:52123}
[email protected]a9da16d2008-07-30 21:41:54124
[email protected]4d5e0362008-08-28 00:59:06125void DiskCacheTestWithCache::SetTestMode() {
[email protected]398ad132013-04-02 15:11:01126 ASSERT_TRUE(!memory_only_);
[email protected]a9da16d2008-07-30 21:41:54127 cache_impl_->SetUnitTestMode();
128}
[email protected]bc4fb8e2010-03-18 23:58:17129
Ben Kelly5a16ed02018-09-07 21:28:09130void DiskCacheTestWithCache::SetMaxSize(int64_t size, bool should_succeed) {
[email protected]7aefb152011-01-21 23:46:49131 size_ = size;
[email protected]1ed95752013-04-23 00:12:36132 if (simple_cache_impl_)
Ben Kelly5a16ed02018-09-07 21:28:09133 EXPECT_EQ(should_succeed, simple_cache_impl_->SetMaxSize(size));
[email protected]1ed95752013-04-23 00:12:36134
[email protected]7aefb152011-01-21 23:46:49135 if (cache_impl_)
Ben Kelly5a16ed02018-09-07 21:28:09136 EXPECT_EQ(should_succeed, cache_impl_->SetMaxSize(size));
[email protected]7aefb152011-01-21 23:46:49137
138 if (mem_cache_)
Ben Kelly5a16ed02018-09-07 21:28:09139 EXPECT_EQ(should_succeed, mem_cache_->SetMaxSize(size));
[email protected]7aefb152011-01-21 23:46:49140}
141
Maks Orlovich8efea482019-08-20 17:14:53142disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntry(
143 const std::string& key) {
144 return OpenOrCreateEntryWithPriority(key, net::HIGHEST);
Steven Bingler6afedf82019-01-08 19:34:49145}
146
Maks Orlovich8efea482019-08-20 17:14:53147disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntryWithPriority(
Steven Bingler6afedf82019-01-08 19:34:49148 const std::string& key,
Maks Orlovich8efea482019-08-20 17:14:53149 net::RequestPriority request_priority) {
150 TestEntryResultCompletionCallback cb;
151 disk_cache::EntryResult result =
152 cache_->OpenOrCreateEntry(key, request_priority, cb.callback());
153 return cb.GetResult(std::move(result));
Steven Bingler6afedf82019-01-08 19:34:49154}
155
[email protected]bc4fb8e2010-03-18 23:58:17156int DiskCacheTestWithCache::OpenEntry(const std::string& key,
157 disk_cache::Entry** entry) {
Josh Karlindd9a5d142018-06-06 00:35:48158 return OpenEntryWithPriority(key, net::HIGHEST, entry);
159}
160
161int DiskCacheTestWithCache::OpenEntryWithPriority(
162 const std::string& key,
163 net::RequestPriority request_priority,
164 disk_cache::Entry** entry) {
Maks Orlovich8efea482019-08-20 17:14:53165 TestEntryResultCompletionCallback cb;
166 disk_cache::EntryResult result =
167 cb.GetResult(cache_->OpenEntry(key, request_priority, cb.callback()));
168 int rv = result.net_error();
169 *entry = result.ReleaseEntry();
170 return rv;
[email protected]bc4fb8e2010-03-18 23:58:17171}
172
173int DiskCacheTestWithCache::CreateEntry(const std::string& key,
174 disk_cache::Entry** entry) {
Josh Karlindd9a5d142018-06-06 00:35:48175 return CreateEntryWithPriority(key, net::HIGHEST, entry);
176}
177
178int DiskCacheTestWithCache::CreateEntryWithPriority(
179 const std::string& key,
180 net::RequestPriority request_priority,
181 disk_cache::Entry** entry) {
Maks Orlovich8efea482019-08-20 17:14:53182 TestEntryResultCompletionCallback cb;
183 disk_cache::EntryResult result =
184 cb.GetResult(cache_->CreateEntry(key, request_priority, cb.callback()));
185 int rv = result.net_error();
186 *entry = result.ReleaseEntry();
187 return rv;
[email protected]bc4fb8e2010-03-18 23:58:17188}
189
190int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23191 net::TestCompletionCallback cb;
Josh Karlindd9a5d142018-06-06 00:35:48192 int rv = cache_->DoomEntry(key, net::HIGHEST, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17193 return cb.GetResult(rv);
194}
195
196int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19197 net::TestCompletionCallback cb;
198 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17199 return cb.GetResult(rv);
200}
201
202int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
203 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19204 net::TestCompletionCallback cb;
205 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17206 return cb.GetResult(rv);
207}
208
209int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27210 net::TestCompletionCallback cb;
211 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17212 return cb.GetResult(rv);
213}
214
Ben Kelly5a16ed02018-09-07 21:28:09215int64_t DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
216 net::TestInt64CompletionCallback cb;
217 int64_t rv = cache_->CalculateSizeOfAllEntries(cb.callback());
msramekaee01ceb2015-10-07 14:23:33218 return cb.GetResult(rv);
219}
220
Ben Kelly5a16ed02018-09-07 21:28:09221int64_t DiskCacheTestWithCache::CalculateSizeOfEntriesBetween(
dullwebera0030052017-01-16 11:56:04222 const base::Time initial_time,
223 const base::Time end_time) {
Ben Kelly5a16ed02018-09-07 21:28:09224 net::TestInt64CompletionCallback cb;
225 int64_t rv = cache_->CalculateSizeOfEntriesBetween(initial_time, end_time,
226 cb.callback());
dullwebera0030052017-01-16 11:56:04227 return cb.GetResult(rv);
228}
229
danakjd04b92d2016-04-16 01:16:49230std::unique_ptr<DiskCacheTestWithCache::TestIterator>
231DiskCacheTestWithCache::CreateIterator() {
232 return std::unique_ptr<TestIterator>(
233 new TestIterator(cache_->CreateIterator()));
[email protected]bc4fb8e2010-03-18 23:58:17234}
[email protected]fb2622f2010-07-13 18:00:56235
236void DiskCacheTestWithCache::FlushQueueForTest() {
237 if (memory_only_ || !cache_impl_)
238 return;
239
[email protected]2a65aceb82011-12-19 20:59:27240 net::TestCompletionCallback cb;
241 int rv = cache_impl_->FlushQueueForTest(cb.callback());
robpercival214763f2016-07-01 23:27:01242 EXPECT_THAT(cb.GetResult(rv), IsOk());
[email protected]fb2622f2010-07-13 18:00:56243}
[email protected]e1fcf142010-08-23 18:47:25244
[email protected]f27bbe002011-12-22 11:29:34245void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
[email protected]65188eb2010-09-16 20:59:29246 if (memory_only_ || !cache_impl_) {
[email protected]f27bbe002011-12-22 11:29:34247 closure.Run();
[email protected]65188eb2010-09-16 20:59:29248 return;
249 }
250
[email protected]2a65aceb82011-12-19 20:59:27251 net::TestCompletionCallback cb;
[email protected]f27bbe002011-12-22 11:29:34252 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
robpercival214763f2016-07-01 23:27:01253 EXPECT_THAT(cb.GetResult(rv), IsOk());
[email protected]65188eb2010-09-16 20:59:29254}
255
Matthew Denton29cbe0ee2019-03-27 00:10:41256int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry,
257 int index,
258 int offset,
259 net::IOBuffer* buf,
260 int len) {
[email protected]2a65aceb82011-12-19 20:59:27261 net::TestCompletionCallback cb;
262 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25263 return cb.GetResult(rv);
264}
265
Matthew Denton29cbe0ee2019-03-27 00:10:41266int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry,
267 int index,
268 int offset,
269 net::IOBuffer* buf,
270 int len,
[email protected]e1fcf142010-08-23 18:47:25271 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27272 net::TestCompletionCallback cb;
273 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25274 return cb.GetResult(rv);
275}
276
277int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46278 int64_t offset,
279 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25280 int len) {
[email protected]2a65aceb82011-12-19 20:59:27281 net::TestCompletionCallback cb;
282 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25283 return cb.GetResult(rv);
284}
285
286int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46287 int64_t offset,
288 net::IOBuffer* buf,
289 int len) {
[email protected]2a65aceb82011-12-19 20:59:27290 net::TestCompletionCallback cb;
291 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25292 return cb.GetResult(rv);
293}
[email protected]7aefb152011-01-21 23:46:49294
Matthew Denton29cbe0ee2019-03-27 00:10:41295int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry,
296 int64_t offset,
297 int len,
298 int64_t* start) {
299 net::TestCompletionCallback cb;
300 int rv = entry->GetAvailableRange(offset, len, start, cb.callback());
301 return cb.GetResult(rv);
302}
303
[email protected]ceb61da32011-01-25 23:52:02304void DiskCacheTestWithCache::TrimForTest(bool empty) {
Matthew Denton29cbe0ee2019-03-27 00:10:41305 if (memory_only_ || !cache_impl_)
306 return;
307
[email protected]f27bbe002011-12-22 11:29:34308 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
Matthew Denton29cbe0ee2019-03-27 00:10:41309 base::Unretained(cache_impl_), empty));
[email protected]ceb61da32011-01-25 23:52:02310}
311
312void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
Matthew Denton29cbe0ee2019-03-27 00:10:41313 if (memory_only_ || !cache_impl_)
314 return;
315
[email protected]f27bbe002011-12-22 11:29:34316 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
Matthew Denton29cbe0ee2019-03-27 00:10:41317 base::Unretained(cache_impl_), empty));
[email protected]ceb61da32011-01-25 23:52:02318}
319
[email protected]05f8087d2012-06-29 18:58:37320void DiskCacheTestWithCache::AddDelay() {
[email protected]8b2f8a52013-08-28 08:45:35321 if (simple_cache_mode_) {
322 // The simple cache uses second resolution for many timeouts, so it's safest
323 // to advance by at least whole seconds before falling back into the normal
324 // disk cache epsilon advance.
325 const base::Time initial_time = base::Time::Now();
326 do {
327 base::PlatformThread::YieldCurrentThread();
Matthew Denton29cbe0ee2019-03-27 00:10:41328 } while (base::Time::Now() - initial_time <
329 base::TimeDelta::FromSeconds(1));
[email protected]8b2f8a52013-08-28 08:45:35330 }
331
[email protected]05f8087d2012-06-29 18:58:37332 base::Time initial = base::Time::Now();
333 while (base::Time::Now() <= initial) {
334 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
335 };
336}
337
Matthew Denton29cbe0ee2019-03-27 00:10:41338void DiskCacheTestWithCache::OnExternalCacheHit(const std::string& key) {
339 cache_->OnExternalCacheHit(key);
340}
341
[email protected]7aefb152011-01-21 23:46:49342void DiskCacheTestWithCache::TearDown() {
Bence Béky98447b12018-05-08 03:14:01343 RunUntilIdle();
[email protected]8c3f5a32013-08-01 11:57:53344 cache_.reset();
[email protected]7aefb152011-01-21 23:46:49345
[email protected]fccce902013-04-15 19:00:51346 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
Maks Orlovichf3860652017-12-13 18:03:16347 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
[email protected]7aefb152011-01-21 23:46:49348 }
Bence Béky98447b12018-05-08 03:14:01349 RunUntilIdle();
Maks Orlovichf378b3a2017-10-31 16:27:30350 if (simple_cache_mode_ && simple_file_tracker_)
351 EXPECT_TRUE(simple_file_tracker_->IsEmptyForTesting());
352
[email protected]de677dc2013-09-13 02:12:25353 DiskCacheTest::TearDown();
[email protected]7aefb152011-01-21 23:46:49354}
355
356void DiskCacheTestWithCache::InitMemoryCache() {
Raul Tambre94493c652019-03-11 17:18:35357 mem_cache_ = new disk_cache::MemBackendImpl(nullptr);
[email protected]8c3f5a32013-08-01 11:57:53358 cache_.reset(mem_cache_);
359 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49360
361 if (size_)
362 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
363
364 ASSERT_TRUE(mem_cache_->Init());
365}
366
367void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49368 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47369 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49370
Maks Orlovich463dc502017-07-25 14:52:49371 CreateBackend(disk_cache::kNoRandom);
[email protected]398ad132013-04-02 15:11:01372}
[email protected]7aefb152011-01-21 23:46:49373
Maks Orlovich463dc502017-07-25 14:52:49374void DiskCacheTestWithCache::CreateBackend(uint32_t flags) {
[email protected]81e549a2014-08-21 04:19:55375 scoped_refptr<base::SingleThreadTaskRunner> runner;
[email protected]398ad132013-04-02 15:11:01376 if (use_current_thread_)
[email protected]81e549a2014-08-21 04:19:55377 runner = base::ThreadTaskRunnerHandle::Get();
[email protected]7aefb152011-01-21 23:46:49378 else
Maks Orlovich463dc502017-07-25 14:52:49379 runner = nullptr; // let the backend sort it out.
[email protected]7aefb152011-01-21 23:46:49380
[email protected]398ad132013-04-02 15:11:01381 if (simple_cache_mode_) {
Maks Orlovich922db0c2018-02-21 15:28:46382 DCHECK(!use_current_thread_)
383 << "Using current thread unsupported by SimpleCache";
[email protected]398ad132013-04-02 15:11:01384 net::TestCompletionCallback cb;
Maks Orlovichdb1f0922018-01-31 03:41:17385 // We limit ourselves to 64 fds since OS X by default gives us 256.
386 // (Chrome raises the number on startup, but the test fixture doesn't).
Maks Orlovichf378b3a2017-10-31 16:27:30387 if (!simple_file_tracker_)
Maks Orlovichdb1f0922018-01-31 03:41:17388 simple_file_tracker_ =
389 std::make_unique<disk_cache::SimpleFileTracker>(64);
Maks Orlovichf378b3a2017-10-31 16:27:30390 std::unique_ptr<disk_cache::SimpleBackendImpl> simple_backend =
391 std::make_unique<disk_cache::SimpleBackendImpl>(
392 cache_path_, /* cleanup_tracker = */ nullptr,
Maks Orlovich922db0c2018-02-21 15:28:46393 simple_file_tracker_.get(), size_, type_, /*net_log = */ nullptr);
[email protected]00831c822013-04-10 15:37:12394 int rv = simple_backend->Init(cb.callback());
robpercival214763f2016-07-01 23:27:01395 ASSERT_THAT(cb.GetResult(rv), IsOk());
[email protected]8c3f5a32013-08-01 11:57:53396 simple_cache_impl_ = simple_backend.get();
dchengc7eeda422015-12-26 03:56:48397 cache_ = std::move(simple_backend);
[email protected]7e48c1b2013-07-12 12:15:43398 if (simple_cache_wait_for_index_) {
399 net::TestCompletionCallback wait_for_index_cb;
Maks Orlovich928035b2019-07-29 20:19:45400 simple_cache_impl_->index()->ExecuteWhenReady(
[email protected]7e48c1b2013-07-12 12:15:43401 wait_for_index_cb.callback());
Maks Orlovich928035b2019-07-29 20:19:45402 rv = wait_for_index_cb.WaitForResult();
403 ASSERT_THAT(rv, IsOk());
[email protected]7e48c1b2013-07-12 12:15:43404 }
[email protected]398ad132013-04-02 15:11:01405 return;
406 }
407
408 if (mask_)
Steven Bingler252ffae2019-03-19 17:43:16409 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, type_,
Maks Orlovich036fd1f2017-08-07 17:51:11410 /* net_log = */ nullptr);
[email protected]398ad132013-04-02 15:11:01411 else
Steven Bingler252ffae2019-03-19 17:43:16412 cache_impl_ = new disk_cache::BackendImpl(
413 cache_path_, /* cleanup_tracker = */ nullptr, runner, type_,
414 /* net_log = */ nullptr);
[email protected]8c3f5a32013-08-01 11:57:53415 cache_.reset(cache_impl_);
416 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49417 if (size_)
418 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
[email protected]7aefb152011-01-21 23:46:49419 if (new_eviction_)
420 cache_impl_->SetNewEviction();
[email protected]398ad132013-04-02 15:11:01421 cache_impl_->SetFlags(flags);
[email protected]42c459632011-12-17 02:20:23422 net::TestCompletionCallback cb;
423 int rv = cache_impl_->Init(cb.callback());
robpercival214763f2016-07-01 23:27:01424 ASSERT_THAT(cb.GetResult(rv), IsOk());
[email protected]7aefb152011-01-21 23:46:49425}