blob: 7e2a885cd035776e278d606595c35e3fdcc92256 [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
Peter Boström8a7540692021-04-05 20:48:207#include <memory>
dchengc7eeda422015-12-26 03:56:488#include <utility>
9
Sebastien Marchand6d0558fd2019-01-25 16:49:3710#include "base/bind.h"
thestigd8df0332014-09-04 06:33:2911#include "base/files/file_util.h"
[email protected]79b3fec2011-11-15 22:33:4712#include "base/path_service.h"
[email protected]94a9d1a2013-03-24 09:50:3113#include "base/run_loop.h"
[email protected]81e549a2014-08-21 04:19:5514#include "base/single_thread_task_runner.h"
[email protected]8b2f8a52013-08-28 08:45:3515#include "base/threading/platform_thread.h"
gabf767595f2016-05-11 18:50:3516#include "base/threading/thread_task_runner_handle.h"
[email protected]e1fcf142010-08-23 18:47:2517#include "net/base/io_buffer.h"
[email protected]fb2622f2010-07-13 18:00:5618#include "net/base/net_errors.h"
Josh Karlindd9a5d142018-06-06 00:35:4819#include "net/base/request_priority.h"
[email protected]bc4fb8e2010-03-18 23:58:1720#include "net/base/test_completion_callback.h"
Maks Orlovich036fd1f2017-08-07 17:51:1121#include "net/disk_cache/backend_cleanup_tracker.h"
[email protected]c2c5cfc2014-03-03 16:35:2822#include "net/disk_cache/blockfile/backend_impl.h"
[email protected]398ad132013-04-02 15:11:0123#include "net/disk_cache/cache_util.h"
24#include "net/disk_cache/disk_cache.h"
initial.commit586acc5fe2008-07-26 22:42:5225#include "net/disk_cache/disk_cache_test_util.h"
[email protected]c2c5cfc2014-03-03 16:35:2826#include "net/disk_cache/memory/mem_backend_impl.h"
[email protected]398ad132013-04-02 15:11:0127#include "net/disk_cache/simple/simple_backend_impl.h"
Maks Orlovichf378b3a2017-10-31 16:27:3028#include "net/disk_cache/simple/simple_file_tracker.h"
[email protected]7e48c1b2013-07-12 12:15:4329#include "net/disk_cache/simple/simple_index.h"
robpercival214763f2016-07-01 23:27:0130#include "net/test/gtest_util.h"
31#include "testing/gmock/include/gmock/gmock.h"
32#include "testing/gtest/include/gtest/gtest.h"
33
34using net::test::IsOk;
initial.commit586acc5fe2008-07-26 22:42:5235
[email protected]79b3fec2011-11-15 22:33:4736DiskCacheTest::DiskCacheTest() {
[email protected]784f9302012-08-17 00:13:4337 CHECK(temp_dir_.CreateUniqueTempDir());
Maks Orlovich8acfd6d2019-09-04 00:31:1338 // Put the cache into a subdir of |temp_dir_|, to permit tests to safely
39 // remove the cache directory without risking collisions with other tests.
40 cache_path_ = temp_dir_.GetPath().AppendASCII("cache");
41 CHECK(base::CreateDirectory(cache_path_));
[email protected]79b3fec2011-11-15 22:33:4742}
43
Chris Watkins68b15032017-12-01 03:07:1344DiskCacheTest::~DiskCacheTest() = default;
[email protected]79b3fec2011-11-15 22:33:4745
46bool DiskCacheTest::CopyTestCache(const std::string& name) {
[email protected]6cdfd7f2013-02-08 20:40:1547 base::FilePath path;
Avi Drissman5c80d832018-05-01 17:01:1948 base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]79b3fec2011-11-15 22:33:4749 path = path.AppendASCII("net");
50 path = path.AppendASCII("data");
51 path = path.AppendASCII("cache_tests");
52 path = path.AppendASCII(name);
53
54 if (!CleanupCacheDir())
55 return false;
[email protected]f0ff2ad2013-07-09 17:42:2656 return base::CopyDirectory(path, cache_path_, false);
[email protected]79b3fec2011-11-15 22:33:4757}
58
59bool DiskCacheTest::CleanupCacheDir() {
60 return DeleteCache(cache_path_);
61}
62
[email protected]17b89142008-11-07 21:52:1563void DiskCacheTest::TearDown() {
[email protected]94a9d1a2013-03-24 09:50:3164 base::RunLoop().RunUntilIdle();
[email protected]17b89142008-11-07 21:52:1565}
66
gavinpb1aaa052014-09-24 14:54:3567DiskCacheTestWithCache::TestIterator::TestIterator(
danakjd04b92d2016-04-16 01:16:4968 std::unique_ptr<disk_cache::Backend::Iterator> iterator)
dchengc7eeda422015-12-26 03:56:4869 : iterator_(std::move(iterator)) {}
gavinpb1aaa052014-09-24 14:54:3570
Chris Watkins68b15032017-12-01 03:07:1371DiskCacheTestWithCache::TestIterator::~TestIterator() = default;
gavinpb1aaa052014-09-24 14:54:3572
73int DiskCacheTestWithCache::TestIterator::OpenNextEntry(
74 disk_cache::Entry** next_entry) {
Maks Orlovich8efea482019-08-20 17:14:5375 TestEntryResultCompletionCallback cb;
76 disk_cache::EntryResult result =
77 cb.GetResult(iterator_->OpenNextEntry(cb.callback()));
78 int rv = result.net_error();
79 *next_entry = result.ReleaseEntry();
80 return rv;
gavinpb1aaa052014-09-24 14:54:3581}
82
[email protected]7aefb152011-01-21 23:46:4983DiskCacheTestWithCache::DiskCacheTestWithCache()
Raul Tambre94493c652019-03-11 17:18:3584 : cache_impl_(nullptr),
85 simple_cache_impl_(nullptr),
86 mem_cache_(nullptr),
[email protected]7aefb152011-01-21 23:46:4987 mask_(0),
88 size_(0),
89 type_(net::DISK_CACHE),
90 memory_only_(false),
[email protected]398ad132013-04-02 15:11:0191 simple_cache_mode_(false),
[email protected]7e48c1b2013-07-12 12:15:4392 simple_cache_wait_for_index_(true),
[email protected]7aefb152011-01-21 23:46:4993 force_creation_(false),
94 new_eviction_(false),
95 first_cleanup_(true),
96 integrity_(true),
Bence Béky98447b12018-05-08 03:14:0197 use_current_thread_(false) {}
initial.commit586acc5fe2008-07-26 22:42:5298
Chris Watkins68b15032017-12-01 03:07:1399DiskCacheTestWithCache::~DiskCacheTestWithCache() = default;
[email protected]7aefb152011-01-21 23:46:49100
[email protected]4d5e0362008-08-28 00:59:06101void DiskCacheTestWithCache::InitCache() {
initial.commit586acc5fe2008-07-26 22:42:52102 if (memory_only_)
103 InitMemoryCache();
104 else
105 InitDiskCache();
106
Raul Tambre94493c652019-03-11 17:18:35107 ASSERT_TRUE(nullptr != cache_);
[email protected]a2553842013-04-19 14:25:18108 if (first_cleanup_)
[email protected]a9da16d2008-07-30 21:41:54109 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:52110}
111
initial.commit586acc5fe2008-07-26 22:42:52112// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:06113void DiskCacheTestWithCache::SimulateCrash() {
[email protected]398ad132013-04-02 15:11:01114 ASSERT_TRUE(!memory_only_);
[email protected]2a65aceb82011-12-19 20:59:27115 net::TestCompletionCallback cb;
116 int rv = cache_impl_->FlushQueueForTest(cb.callback());
robpercival214763f2016-07-01 23:27:01117 ASSERT_THAT(cb.GetResult(rv), IsOk());
initial.commit586acc5fe2008-07-26 22:42:52118 cache_impl_->ClearRefCountForTest();
119
[email protected]8c3f5a32013-08-01 11:57:53120 cache_.reset();
Maks Orlovichf3860652017-12-13 18:03:16121 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
initial.commit586acc5fe2008-07-26 22:42:52122
Maks Orlovich463dc502017-07-25 14:52:49123 CreateBackend(disk_cache::kNoRandom);
initial.commit586acc5fe2008-07-26 22:42:52124}
[email protected]a9da16d2008-07-30 21:41:54125
[email protected]4d5e0362008-08-28 00:59:06126void DiskCacheTestWithCache::SetTestMode() {
[email protected]398ad132013-04-02 15:11:01127 ASSERT_TRUE(!memory_only_);
[email protected]a9da16d2008-07-30 21:41:54128 cache_impl_->SetUnitTestMode();
129}
[email protected]bc4fb8e2010-03-18 23:58:17130
Ben Kelly5a16ed02018-09-07 21:28:09131void DiskCacheTestWithCache::SetMaxSize(int64_t size, bool should_succeed) {
[email protected]7aefb152011-01-21 23:46:49132 size_ = size;
[email protected]1ed95752013-04-23 00:12:36133 if (simple_cache_impl_)
Ben Kelly5a16ed02018-09-07 21:28:09134 EXPECT_EQ(should_succeed, simple_cache_impl_->SetMaxSize(size));
[email protected]1ed95752013-04-23 00:12:36135
[email protected]7aefb152011-01-21 23:46:49136 if (cache_impl_)
Ben Kelly5a16ed02018-09-07 21:28:09137 EXPECT_EQ(should_succeed, cache_impl_->SetMaxSize(size));
[email protected]7aefb152011-01-21 23:46:49138
139 if (mem_cache_)
Ben Kelly5a16ed02018-09-07 21:28:09140 EXPECT_EQ(should_succeed, mem_cache_->SetMaxSize(size));
[email protected]7aefb152011-01-21 23:46:49141}
142
Maks Orlovich8efea482019-08-20 17:14:53143disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntry(
144 const std::string& key) {
145 return OpenOrCreateEntryWithPriority(key, net::HIGHEST);
Steven Bingler6afedf82019-01-08 19:34:49146}
147
Maks Orlovich8efea482019-08-20 17:14:53148disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntryWithPriority(
Steven Bingler6afedf82019-01-08 19:34:49149 const std::string& key,
Maks Orlovich8efea482019-08-20 17:14:53150 net::RequestPriority request_priority) {
151 TestEntryResultCompletionCallback cb;
152 disk_cache::EntryResult result =
153 cache_->OpenOrCreateEntry(key, request_priority, cb.callback());
154 return cb.GetResult(std::move(result));
Steven Bingler6afedf82019-01-08 19:34:49155}
156
[email protected]bc4fb8e2010-03-18 23:58:17157int DiskCacheTestWithCache::OpenEntry(const std::string& key,
158 disk_cache::Entry** entry) {
Josh Karlindd9a5d142018-06-06 00:35:48159 return OpenEntryWithPriority(key, net::HIGHEST, entry);
160}
161
162int DiskCacheTestWithCache::OpenEntryWithPriority(
163 const std::string& key,
164 net::RequestPriority request_priority,
165 disk_cache::Entry** entry) {
Maks Orlovich8efea482019-08-20 17:14:53166 TestEntryResultCompletionCallback cb;
167 disk_cache::EntryResult result =
168 cb.GetResult(cache_->OpenEntry(key, request_priority, cb.callback()));
169 int rv = result.net_error();
170 *entry = result.ReleaseEntry();
171 return rv;
[email protected]bc4fb8e2010-03-18 23:58:17172}
173
174int DiskCacheTestWithCache::CreateEntry(const std::string& key,
175 disk_cache::Entry** entry) {
Josh Karlindd9a5d142018-06-06 00:35:48176 return CreateEntryWithPriority(key, net::HIGHEST, entry);
177}
178
179int DiskCacheTestWithCache::CreateEntryWithPriority(
180 const std::string& key,
181 net::RequestPriority request_priority,
182 disk_cache::Entry** entry) {
Maks Orlovich8efea482019-08-20 17:14:53183 TestEntryResultCompletionCallback cb;
184 disk_cache::EntryResult result =
185 cb.GetResult(cache_->CreateEntry(key, request_priority, cb.callback()));
186 int rv = result.net_error();
187 *entry = result.ReleaseEntry();
188 return rv;
[email protected]bc4fb8e2010-03-18 23:58:17189}
190
191int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23192 net::TestCompletionCallback cb;
Josh Karlindd9a5d142018-06-06 00:35:48193 int rv = cache_->DoomEntry(key, net::HIGHEST, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17194 return cb.GetResult(rv);
195}
196
197int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19198 net::TestCompletionCallback cb;
199 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17200 return cb.GetResult(rv);
201}
202
203int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
204 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19205 net::TestCompletionCallback cb;
206 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17207 return cb.GetResult(rv);
208}
209
210int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27211 net::TestCompletionCallback cb;
212 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17213 return cb.GetResult(rv);
214}
215
Ben Kelly5a16ed02018-09-07 21:28:09216int64_t DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
217 net::TestInt64CompletionCallback cb;
218 int64_t rv = cache_->CalculateSizeOfAllEntries(cb.callback());
msramekaee01ceb2015-10-07 14:23:33219 return cb.GetResult(rv);
220}
221
Ben Kelly5a16ed02018-09-07 21:28:09222int64_t DiskCacheTestWithCache::CalculateSizeOfEntriesBetween(
dullwebera0030052017-01-16 11:56:04223 const base::Time initial_time,
224 const base::Time end_time) {
Ben Kelly5a16ed02018-09-07 21:28:09225 net::TestInt64CompletionCallback cb;
226 int64_t rv = cache_->CalculateSizeOfEntriesBetween(initial_time, end_time,
227 cb.callback());
dullwebera0030052017-01-16 11:56:04228 return cb.GetResult(rv);
229}
230
danakjd04b92d2016-04-16 01:16:49231std::unique_ptr<DiskCacheTestWithCache::TestIterator>
232DiskCacheTestWithCache::CreateIterator() {
Peter Boström8a7540692021-04-05 20:48:20233 return std::make_unique<TestIterator>(cache_->CreateIterator());
[email protected]bc4fb8e2010-03-18 23:58:17234}
[email protected]fb2622f2010-07-13 18:00:56235
236void DiskCacheTestWithCache::FlushQueueForTest() {
Josh Karlin393a7232020-01-21 19:08:14237 if (memory_only_)
[email protected]fb2622f2010-07-13 18:00:56238 return;
239
Josh Karlin393a7232020-01-21 19:08:14240 if (simple_cache_impl_) {
241 simple_cache_impl_->FlushWorkerPoolForTesting();
242 return;
243 }
244
245 DCHECK(cache_impl_);
[email protected]2a65aceb82011-12-19 20:59:27246 net::TestCompletionCallback cb;
247 int rv = cache_impl_->FlushQueueForTest(cb.callback());
robpercival214763f2016-07-01 23:27:01248 EXPECT_THAT(cb.GetResult(rv), IsOk());
[email protected]fb2622f2010-07-13 18:00:56249}
[email protected]e1fcf142010-08-23 18:47:25250
Anna Malovacb042b642020-03-03 15:50:00251void DiskCacheTestWithCache::RunTaskForTest(base::OnceClosure closure) {
[email protected]65188eb2010-09-16 20:59:29252 if (memory_only_ || !cache_impl_) {
Anna Malovacb042b642020-03-03 15:50:00253 std::move(closure).Run();
[email protected]65188eb2010-09-16 20:59:29254 return;
255 }
256
[email protected]2a65aceb82011-12-19 20:59:27257 net::TestCompletionCallback cb;
Anna Malovacb042b642020-03-03 15:50:00258 int rv = cache_impl_->RunTaskForTest(std::move(closure), cb.callback());
robpercival214763f2016-07-01 23:27:01259 EXPECT_THAT(cb.GetResult(rv), IsOk());
[email protected]65188eb2010-09-16 20:59:29260}
261
Matthew Denton29cbe0ee2019-03-27 00:10:41262int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry,
263 int index,
264 int offset,
265 net::IOBuffer* buf,
266 int len) {
[email protected]2a65aceb82011-12-19 20:59:27267 net::TestCompletionCallback cb;
268 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25269 return cb.GetResult(rv);
270}
271
Matthew Denton29cbe0ee2019-03-27 00:10:41272int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry,
273 int index,
274 int offset,
275 net::IOBuffer* buf,
276 int len,
[email protected]e1fcf142010-08-23 18:47:25277 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27278 net::TestCompletionCallback cb;
279 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25280 return cb.GetResult(rv);
281}
282
283int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46284 int64_t offset,
285 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25286 int len) {
[email protected]2a65aceb82011-12-19 20:59:27287 net::TestCompletionCallback cb;
288 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25289 return cb.GetResult(rv);
290}
291
292int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46293 int64_t offset,
294 net::IOBuffer* buf,
295 int len) {
[email protected]2a65aceb82011-12-19 20:59:27296 net::TestCompletionCallback cb;
297 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25298 return cb.GetResult(rv);
299}
[email protected]7aefb152011-01-21 23:46:49300
Matthew Denton29cbe0ee2019-03-27 00:10:41301int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry,
302 int64_t offset,
303 int len,
304 int64_t* start) {
Maks Orlovich04cd1ad2021-07-02 17:32:24305 TestRangeResultCompletionCallback cb;
306 disk_cache::RangeResult result =
307 cb.GetResult(entry->GetAvailableRange(offset, len, cb.callback()));
308
309 if (result.net_error == net::OK) {
310 *start = result.start;
311 return result.available_len;
312 }
313 return result.net_error;
Matthew Denton29cbe0ee2019-03-27 00:10:41314}
315
[email protected]ceb61da32011-01-25 23:52:02316void DiskCacheTestWithCache::TrimForTest(bool empty) {
Matthew Denton29cbe0ee2019-03-27 00:10:41317 if (memory_only_ || !cache_impl_)
318 return;
319
Anna Malovacb042b642020-03-03 15:50:00320 RunTaskForTest(base::BindOnce(&disk_cache::BackendImpl::TrimForTest,
321 base::Unretained(cache_impl_), empty));
[email protected]ceb61da32011-01-25 23:52:02322}
323
324void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
Matthew Denton29cbe0ee2019-03-27 00:10:41325 if (memory_only_ || !cache_impl_)
326 return;
327
Anna Malovacb042b642020-03-03 15:50:00328 RunTaskForTest(
329 base::BindOnce(&disk_cache::BackendImpl::TrimDeletedListForTest,
330 base::Unretained(cache_impl_), empty));
[email protected]ceb61da32011-01-25 23:52:02331}
332
[email protected]05f8087d2012-06-29 18:58:37333void DiskCacheTestWithCache::AddDelay() {
[email protected]8b2f8a52013-08-28 08:45:35334 if (simple_cache_mode_) {
335 // The simple cache uses second resolution for many timeouts, so it's safest
336 // to advance by at least whole seconds before falling back into the normal
337 // disk cache epsilon advance.
338 const base::Time initial_time = base::Time::Now();
339 do {
340 base::PlatformThread::YieldCurrentThread();
Peter Kastinge5a38ed2021-10-02 03:06:35341 } while (base::Time::Now() - initial_time < base::Seconds(1));
[email protected]8b2f8a52013-08-28 08:45:35342 }
343
[email protected]05f8087d2012-06-29 18:58:37344 base::Time initial = base::Time::Now();
345 while (base::Time::Now() <= initial) {
Peter Kastinge5a38ed2021-10-02 03:06:35346 base::PlatformThread::Sleep(base::Milliseconds(1));
[email protected]05f8087d2012-06-29 18:58:37347 };
348}
349
Matthew Denton29cbe0ee2019-03-27 00:10:41350void DiskCacheTestWithCache::OnExternalCacheHit(const std::string& key) {
351 cache_->OnExternalCacheHit(key);
352}
353
[email protected]7aefb152011-01-21 23:46:49354void DiskCacheTestWithCache::TearDown() {
Bence Béky98447b12018-05-08 03:14:01355 RunUntilIdle();
[email protected]8c3f5a32013-08-01 11:57:53356 cache_.reset();
[email protected]7aefb152011-01-21 23:46:49357
[email protected]fccce902013-04-15 19:00:51358 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
Maks Orlovichf3860652017-12-13 18:03:16359 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
[email protected]7aefb152011-01-21 23:46:49360 }
Bence Béky98447b12018-05-08 03:14:01361 RunUntilIdle();
Maks Orlovichf378b3a2017-10-31 16:27:30362 if (simple_cache_mode_ && simple_file_tracker_)
363 EXPECT_TRUE(simple_file_tracker_->IsEmptyForTesting());
364
[email protected]de677dc2013-09-13 02:12:25365 DiskCacheTest::TearDown();
[email protected]7aefb152011-01-21 23:46:49366}
367
368void DiskCacheTestWithCache::InitMemoryCache() {
Raul Tambre94493c652019-03-11 17:18:35369 mem_cache_ = new disk_cache::MemBackendImpl(nullptr);
[email protected]8c3f5a32013-08-01 11:57:53370 cache_.reset(mem_cache_);
371 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49372
373 if (size_)
374 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
375
376 ASSERT_TRUE(mem_cache_->Init());
377}
378
379void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49380 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47381 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49382
Maks Orlovich463dc502017-07-25 14:52:49383 CreateBackend(disk_cache::kNoRandom);
[email protected]398ad132013-04-02 15:11:01384}
[email protected]7aefb152011-01-21 23:46:49385
Maks Orlovich463dc502017-07-25 14:52:49386void DiskCacheTestWithCache::CreateBackend(uint32_t flags) {
[email protected]81e549a2014-08-21 04:19:55387 scoped_refptr<base::SingleThreadTaskRunner> runner;
[email protected]398ad132013-04-02 15:11:01388 if (use_current_thread_)
[email protected]81e549a2014-08-21 04:19:55389 runner = base::ThreadTaskRunnerHandle::Get();
[email protected]7aefb152011-01-21 23:46:49390 else
Maks Orlovich463dc502017-07-25 14:52:49391 runner = nullptr; // let the backend sort it out.
[email protected]7aefb152011-01-21 23:46:49392
[email protected]398ad132013-04-02 15:11:01393 if (simple_cache_mode_) {
Maks Orlovich922db0c2018-02-21 15:28:46394 DCHECK(!use_current_thread_)
395 << "Using current thread unsupported by SimpleCache";
[email protected]398ad132013-04-02 15:11:01396 net::TestCompletionCallback cb;
Maks Orlovichdb1f0922018-01-31 03:41:17397 // We limit ourselves to 64 fds since OS X by default gives us 256.
398 // (Chrome raises the number on startup, but the test fixture doesn't).
Maks Orlovichf378b3a2017-10-31 16:27:30399 if (!simple_file_tracker_)
Maks Orlovichdb1f0922018-01-31 03:41:17400 simple_file_tracker_ =
401 std::make_unique<disk_cache::SimpleFileTracker>(64);
Maks Orlovichf378b3a2017-10-31 16:27:30402 std::unique_ptr<disk_cache::SimpleBackendImpl> simple_backend =
403 std::make_unique<disk_cache::SimpleBackendImpl>(
404 cache_path_, /* cleanup_tracker = */ nullptr,
Maks Orlovich922db0c2018-02-21 15:28:46405 simple_file_tracker_.get(), size_, type_, /*net_log = */ nullptr);
[email protected]00831c822013-04-10 15:37:12406 int rv = simple_backend->Init(cb.callback());
robpercival214763f2016-07-01 23:27:01407 ASSERT_THAT(cb.GetResult(rv), IsOk());
[email protected]8c3f5a32013-08-01 11:57:53408 simple_cache_impl_ = simple_backend.get();
dchengc7eeda422015-12-26 03:56:48409 cache_ = std::move(simple_backend);
[email protected]7e48c1b2013-07-12 12:15:43410 if (simple_cache_wait_for_index_) {
411 net::TestCompletionCallback wait_for_index_cb;
Maks Orlovich928035b2019-07-29 20:19:45412 simple_cache_impl_->index()->ExecuteWhenReady(
[email protected]7e48c1b2013-07-12 12:15:43413 wait_for_index_cb.callback());
Maks Orlovich928035b2019-07-29 20:19:45414 rv = wait_for_index_cb.WaitForResult();
415 ASSERT_THAT(rv, IsOk());
[email protected]7e48c1b2013-07-12 12:15:43416 }
[email protected]398ad132013-04-02 15:11:01417 return;
418 }
419
420 if (mask_)
Steven Bingler252ffae2019-03-19 17:43:16421 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, type_,
Maks Orlovich036fd1f2017-08-07 17:51:11422 /* net_log = */ nullptr);
[email protected]398ad132013-04-02 15:11:01423 else
Steven Bingler252ffae2019-03-19 17:43:16424 cache_impl_ = new disk_cache::BackendImpl(
425 cache_path_, /* cleanup_tracker = */ nullptr, runner, type_,
426 /* net_log = */ nullptr);
[email protected]8c3f5a32013-08-01 11:57:53427 cache_.reset(cache_impl_);
428 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49429 if (size_)
430 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
[email protected]7aefb152011-01-21 23:46:49431 if (new_eviction_)
432 cache_impl_->SetNewEviction();
[email protected]398ad132013-04-02 15:11:01433 cache_impl_->SetFlags(flags);
[email protected]42c459632011-12-17 02:20:23434 net::TestCompletionCallback cb;
435 int rv = cache_impl_->Init(cb.callback());
robpercival214763f2016-07-01 23:27:01436 ASSERT_THAT(cb.GetResult(rv), IsOk());
[email protected]7aefb152011-01-21 23:46:49437}