blob: 9e36dd91212d798f450dbc7c66b762df450179ae [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"
Patrick Monette643cdf62021-10-15 19:13:4214#include "base/task/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() {
Sergey Ulanoved1f4942022-06-01 00:08:3164 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
Tsuyoshi Horo432981d52022-06-09 09:50:1383DiskCacheTestWithCache::DiskCacheTestWithCache() = default;
initial.commit586acc5fe2008-07-26 22:42:5284
Chris Watkins68b15032017-12-01 03:07:1385DiskCacheTestWithCache::~DiskCacheTestWithCache() = default;
[email protected]7aefb152011-01-21 23:46:4986
[email protected]4d5e0362008-08-28 00:59:0687void DiskCacheTestWithCache::InitCache() {
initial.commit586acc5fe2008-07-26 22:42:5288 if (memory_only_)
89 InitMemoryCache();
90 else
91 InitDiskCache();
92
Raul Tambre94493c652019-03-11 17:18:3593 ASSERT_TRUE(nullptr != cache_);
[email protected]a2553842013-04-19 14:25:1894 if (first_cleanup_)
[email protected]a9da16d2008-07-30 21:41:5495 ASSERT_EQ(0, cache_->GetEntryCount());
initial.commit586acc5fe2008-07-26 22:42:5296}
97
initial.commit586acc5fe2008-07-26 22:42:5298// We are expected to leak memory when simulating crashes.
[email protected]4d5e0362008-08-28 00:59:0699void DiskCacheTestWithCache::SimulateCrash() {
[email protected]398ad132013-04-02 15:11:01100 ASSERT_TRUE(!memory_only_);
[email protected]2a65aceb82011-12-19 20:59:27101 net::TestCompletionCallback cb;
102 int rv = cache_impl_->FlushQueueForTest(cb.callback());
robpercival214763f2016-07-01 23:27:01103 ASSERT_THAT(cb.GetResult(rv), IsOk());
initial.commit586acc5fe2008-07-26 22:42:52104 cache_impl_->ClearRefCountForTest();
105
[email protected]8c3f5a32013-08-01 11:57:53106 cache_.reset();
Maks Orlovichf3860652017-12-13 18:03:16107 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
initial.commit586acc5fe2008-07-26 22:42:52108
Maks Orlovich463dc502017-07-25 14:52:49109 CreateBackend(disk_cache::kNoRandom);
initial.commit586acc5fe2008-07-26 22:42:52110}
[email protected]a9da16d2008-07-30 21:41:54111
[email protected]4d5e0362008-08-28 00:59:06112void DiskCacheTestWithCache::SetTestMode() {
[email protected]398ad132013-04-02 15:11:01113 ASSERT_TRUE(!memory_only_);
[email protected]a9da16d2008-07-30 21:41:54114 cache_impl_->SetUnitTestMode();
115}
[email protected]bc4fb8e2010-03-18 23:58:17116
Ben Kelly5a16ed02018-09-07 21:28:09117void DiskCacheTestWithCache::SetMaxSize(int64_t size, bool should_succeed) {
[email protected]7aefb152011-01-21 23:46:49118 size_ = size;
[email protected]1ed95752013-04-23 00:12:36119 if (simple_cache_impl_)
Ben Kelly5a16ed02018-09-07 21:28:09120 EXPECT_EQ(should_succeed, simple_cache_impl_->SetMaxSize(size));
[email protected]1ed95752013-04-23 00:12:36121
[email protected]7aefb152011-01-21 23:46:49122 if (cache_impl_)
Ben Kelly5a16ed02018-09-07 21:28:09123 EXPECT_EQ(should_succeed, cache_impl_->SetMaxSize(size));
[email protected]7aefb152011-01-21 23:46:49124
125 if (mem_cache_)
Ben Kelly5a16ed02018-09-07 21:28:09126 EXPECT_EQ(should_succeed, mem_cache_->SetMaxSize(size));
[email protected]7aefb152011-01-21 23:46:49127}
128
Maks Orlovich8efea482019-08-20 17:14:53129disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntry(
130 const std::string& key) {
131 return OpenOrCreateEntryWithPriority(key, net::HIGHEST);
Steven Bingler6afedf82019-01-08 19:34:49132}
133
Maks Orlovich8efea482019-08-20 17:14:53134disk_cache::EntryResult DiskCacheTestWithCache::OpenOrCreateEntryWithPriority(
Steven Bingler6afedf82019-01-08 19:34:49135 const std::string& key,
Maks Orlovich8efea482019-08-20 17:14:53136 net::RequestPriority request_priority) {
137 TestEntryResultCompletionCallback cb;
138 disk_cache::EntryResult result =
139 cache_->OpenOrCreateEntry(key, request_priority, cb.callback());
140 return cb.GetResult(std::move(result));
Steven Bingler6afedf82019-01-08 19:34:49141}
142
[email protected]bc4fb8e2010-03-18 23:58:17143int DiskCacheTestWithCache::OpenEntry(const std::string& key,
144 disk_cache::Entry** entry) {
Josh Karlindd9a5d142018-06-06 00:35:48145 return OpenEntryWithPriority(key, net::HIGHEST, entry);
146}
147
148int DiskCacheTestWithCache::OpenEntryWithPriority(
149 const std::string& key,
150 net::RequestPriority request_priority,
151 disk_cache::Entry** entry) {
Maks Orlovich8efea482019-08-20 17:14:53152 TestEntryResultCompletionCallback cb;
153 disk_cache::EntryResult result =
154 cb.GetResult(cache_->OpenEntry(key, request_priority, cb.callback()));
155 int rv = result.net_error();
156 *entry = result.ReleaseEntry();
157 return rv;
[email protected]bc4fb8e2010-03-18 23:58:17158}
159
160int DiskCacheTestWithCache::CreateEntry(const std::string& key,
161 disk_cache::Entry** entry) {
Josh Karlindd9a5d142018-06-06 00:35:48162 return CreateEntryWithPriority(key, net::HIGHEST, entry);
163}
164
165int DiskCacheTestWithCache::CreateEntryWithPriority(
166 const std::string& key,
167 net::RequestPriority request_priority,
168 disk_cache::Entry** entry) {
Maks Orlovich8efea482019-08-20 17:14:53169 TestEntryResultCompletionCallback cb;
170 disk_cache::EntryResult result =
171 cb.GetResult(cache_->CreateEntry(key, request_priority, cb.callback()));
172 int rv = result.net_error();
173 *entry = result.ReleaseEntry();
174 return rv;
[email protected]bc4fb8e2010-03-18 23:58:17175}
176
177int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
[email protected]42c459632011-12-17 02:20:23178 net::TestCompletionCallback cb;
Josh Karlindd9a5d142018-06-06 00:35:48179 int rv = cache_->DoomEntry(key, net::HIGHEST, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17180 return cb.GetResult(rv);
181}
182
183int DiskCacheTestWithCache::DoomAllEntries() {
[email protected]6ad7c0912011-12-15 19:10:19184 net::TestCompletionCallback cb;
185 int rv = cache_->DoomAllEntries(cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17186 return cb.GetResult(rv);
187}
188
189int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
190 const base::Time end_time) {
[email protected]6ad7c0912011-12-15 19:10:19191 net::TestCompletionCallback cb;
192 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17193 return cb.GetResult(rv);
194}
195
196int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
[email protected]2a65aceb82011-12-19 20:59:27197 net::TestCompletionCallback cb;
198 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
[email protected]bc4fb8e2010-03-18 23:58:17199 return cb.GetResult(rv);
200}
201
Ben Kelly5a16ed02018-09-07 21:28:09202int64_t DiskCacheTestWithCache::CalculateSizeOfAllEntries() {
203 net::TestInt64CompletionCallback cb;
204 int64_t rv = cache_->CalculateSizeOfAllEntries(cb.callback());
msramekaee01ceb2015-10-07 14:23:33205 return cb.GetResult(rv);
206}
207
Ben Kelly5a16ed02018-09-07 21:28:09208int64_t DiskCacheTestWithCache::CalculateSizeOfEntriesBetween(
dullwebera0030052017-01-16 11:56:04209 const base::Time initial_time,
210 const base::Time end_time) {
Ben Kelly5a16ed02018-09-07 21:28:09211 net::TestInt64CompletionCallback cb;
212 int64_t rv = cache_->CalculateSizeOfEntriesBetween(initial_time, end_time,
213 cb.callback());
dullwebera0030052017-01-16 11:56:04214 return cb.GetResult(rv);
215}
216
danakjd04b92d2016-04-16 01:16:49217std::unique_ptr<DiskCacheTestWithCache::TestIterator>
218DiskCacheTestWithCache::CreateIterator() {
Peter Boström8a7540692021-04-05 20:48:20219 return std::make_unique<TestIterator>(cache_->CreateIterator());
[email protected]bc4fb8e2010-03-18 23:58:17220}
[email protected]fb2622f2010-07-13 18:00:56221
222void DiskCacheTestWithCache::FlushQueueForTest() {
Josh Karlin393a7232020-01-21 19:08:14223 if (memory_only_)
[email protected]fb2622f2010-07-13 18:00:56224 return;
225
Josh Karlin393a7232020-01-21 19:08:14226 if (simple_cache_impl_) {
Yutaka Hirano2d9ce052022-04-22 02:56:24227 disk_cache::FlushCacheThreadForTesting();
Josh Karlin393a7232020-01-21 19:08:14228 return;
229 }
230
231 DCHECK(cache_impl_);
[email protected]2a65aceb82011-12-19 20:59:27232 net::TestCompletionCallback cb;
233 int rv = cache_impl_->FlushQueueForTest(cb.callback());
robpercival214763f2016-07-01 23:27:01234 EXPECT_THAT(cb.GetResult(rv), IsOk());
[email protected]fb2622f2010-07-13 18:00:56235}
[email protected]e1fcf142010-08-23 18:47:25236
Anna Malovacb042b642020-03-03 15:50:00237void DiskCacheTestWithCache::RunTaskForTest(base::OnceClosure closure) {
[email protected]65188eb2010-09-16 20:59:29238 if (memory_only_ || !cache_impl_) {
Anna Malovacb042b642020-03-03 15:50:00239 std::move(closure).Run();
[email protected]65188eb2010-09-16 20:59:29240 return;
241 }
242
[email protected]2a65aceb82011-12-19 20:59:27243 net::TestCompletionCallback cb;
Anna Malovacb042b642020-03-03 15:50:00244 int rv = cache_impl_->RunTaskForTest(std::move(closure), cb.callback());
robpercival214763f2016-07-01 23:27:01245 EXPECT_THAT(cb.GetResult(rv), IsOk());
[email protected]65188eb2010-09-16 20:59:29246}
247
Matthew Denton29cbe0ee2019-03-27 00:10:41248int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry,
249 int index,
250 int offset,
251 net::IOBuffer* buf,
252 int len) {
[email protected]2a65aceb82011-12-19 20:59:27253 net::TestCompletionCallback cb;
254 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25255 return cb.GetResult(rv);
256}
257
Matthew Denton29cbe0ee2019-03-27 00:10:41258int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry,
259 int index,
260 int offset,
261 net::IOBuffer* buf,
262 int len,
[email protected]e1fcf142010-08-23 18:47:25263 bool truncate) {
[email protected]2a65aceb82011-12-19 20:59:27264 net::TestCompletionCallback cb;
265 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
[email protected]e1fcf142010-08-23 18:47:25266 return cb.GetResult(rv);
267}
268
269int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46270 int64_t offset,
271 net::IOBuffer* buf,
[email protected]e1fcf142010-08-23 18:47:25272 int len) {
[email protected]2a65aceb82011-12-19 20:59:27273 net::TestCompletionCallback cb;
274 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25275 return cb.GetResult(rv);
276}
277
278int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
Avi Drissman13fc8932015-12-20 04:40:46279 int64_t offset,
280 net::IOBuffer* buf,
281 int len) {
[email protected]2a65aceb82011-12-19 20:59:27282 net::TestCompletionCallback cb;
283 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
[email protected]e1fcf142010-08-23 18:47:25284 return cb.GetResult(rv);
285}
[email protected]7aefb152011-01-21 23:46:49286
Matthew Denton29cbe0ee2019-03-27 00:10:41287int DiskCacheTestWithCache::GetAvailableRange(disk_cache::Entry* entry,
288 int64_t offset,
289 int len,
290 int64_t* start) {
Maks Orlovich04cd1ad2021-07-02 17:32:24291 TestRangeResultCompletionCallback cb;
292 disk_cache::RangeResult result =
293 cb.GetResult(entry->GetAvailableRange(offset, len, cb.callback()));
294
295 if (result.net_error == net::OK) {
296 *start = result.start;
297 return result.available_len;
298 }
299 return result.net_error;
Matthew Denton29cbe0ee2019-03-27 00:10:41300}
301
[email protected]ceb61da32011-01-25 23:52:02302void DiskCacheTestWithCache::TrimForTest(bool empty) {
Matthew Denton29cbe0ee2019-03-27 00:10:41303 if (memory_only_ || !cache_impl_)
304 return;
305
Anna Malovacb042b642020-03-03 15:50:00306 RunTaskForTest(base::BindOnce(&disk_cache::BackendImpl::TrimForTest,
307 base::Unretained(cache_impl_), empty));
[email protected]ceb61da32011-01-25 23:52:02308}
309
310void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
Matthew Denton29cbe0ee2019-03-27 00:10:41311 if (memory_only_ || !cache_impl_)
312 return;
313
Anna Malovacb042b642020-03-03 15:50:00314 RunTaskForTest(
315 base::BindOnce(&disk_cache::BackendImpl::TrimDeletedListForTest,
316 base::Unretained(cache_impl_), empty));
[email protected]ceb61da32011-01-25 23:52:02317}
318
[email protected]05f8087d2012-06-29 18:58:37319void DiskCacheTestWithCache::AddDelay() {
[email protected]8b2f8a52013-08-28 08:45:35320 if (simple_cache_mode_) {
321 // The simple cache uses second resolution for many timeouts, so it's safest
322 // to advance by at least whole seconds before falling back into the normal
323 // disk cache epsilon advance.
324 const base::Time initial_time = base::Time::Now();
325 do {
326 base::PlatformThread::YieldCurrentThread();
Peter Kastinge5a38ed2021-10-02 03:06:35327 } while (base::Time::Now() - initial_time < base::Seconds(1));
[email protected]8b2f8a52013-08-28 08:45:35328 }
329
[email protected]05f8087d2012-06-29 18:58:37330 base::Time initial = base::Time::Now();
331 while (base::Time::Now() <= initial) {
Peter Kastinge5a38ed2021-10-02 03:06:35332 base::PlatformThread::Sleep(base::Milliseconds(1));
[email protected]05f8087d2012-06-29 18:58:37333 };
334}
335
Matthew Denton29cbe0ee2019-03-27 00:10:41336void DiskCacheTestWithCache::OnExternalCacheHit(const std::string& key) {
337 cache_->OnExternalCacheHit(key);
338}
339
[email protected]7aefb152011-01-21 23:46:49340void DiskCacheTestWithCache::TearDown() {
Bence Béky98447b12018-05-08 03:14:01341 RunUntilIdle();
[email protected]8c3f5a32013-08-01 11:57:53342 cache_.reset();
[email protected]7aefb152011-01-21 23:46:49343
[email protected]fccce902013-04-15 19:00:51344 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
Maks Orlovichf3860652017-12-13 18:03:16345 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, size_, mask_));
[email protected]7aefb152011-01-21 23:46:49346 }
Bence Béky98447b12018-05-08 03:14:01347 RunUntilIdle();
Maks Orlovichf378b3a2017-10-31 16:27:30348 if (simple_cache_mode_ && simple_file_tracker_)
349 EXPECT_TRUE(simple_file_tracker_->IsEmptyForTesting());
350
[email protected]de677dc2013-09-13 02:12:25351 DiskCacheTest::TearDown();
[email protected]7aefb152011-01-21 23:46:49352}
353
354void DiskCacheTestWithCache::InitMemoryCache() {
Tsuyoshi Horof8861cb2022-07-05 23:50:20355 auto cache = std::make_unique<disk_cache::MemBackendImpl>(nullptr);
356 mem_cache_ = cache.get();
357 cache_ = std::move(cache);
[email protected]8c3f5a32013-08-01 11:57:53358 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49359
360 if (size_)
361 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
362
363 ASSERT_TRUE(mem_cache_->Init());
364}
365
366void DiskCacheTestWithCache::InitDiskCache() {
[email protected]7aefb152011-01-21 23:46:49367 if (first_cleanup_)
[email protected]79b3fec2011-11-15 22:33:47368 ASSERT_TRUE(CleanupCacheDir());
[email protected]7aefb152011-01-21 23:46:49369
Maks Orlovich463dc502017-07-25 14:52:49370 CreateBackend(disk_cache::kNoRandom);
[email protected]398ad132013-04-02 15:11:01371}
[email protected]7aefb152011-01-21 23:46:49372
Maks Orlovich463dc502017-07-25 14:52:49373void DiskCacheTestWithCache::CreateBackend(uint32_t flags) {
[email protected]81e549a2014-08-21 04:19:55374 scoped_refptr<base::SingleThreadTaskRunner> runner;
[email protected]398ad132013-04-02 15:11:01375 if (use_current_thread_)
[email protected]81e549a2014-08-21 04:19:55376 runner = base::ThreadTaskRunnerHandle::Get();
[email protected]7aefb152011-01-21 23:46:49377 else
Maks Orlovich463dc502017-07-25 14:52:49378 runner = nullptr; // let the backend sort it out.
[email protected]7aefb152011-01-21 23:46:49379
[email protected]398ad132013-04-02 15:11:01380 if (simple_cache_mode_) {
Maks Orlovich922db0c2018-02-21 15:28:46381 DCHECK(!use_current_thread_)
382 << "Using current thread unsupported by SimpleCache";
[email protected]398ad132013-04-02 15:11:01383 net::TestCompletionCallback cb;
Maks Orlovichdb1f0922018-01-31 03:41:17384 // We limit ourselves to 64 fds since OS X by default gives us 256.
385 // (Chrome raises the number on startup, but the test fixture doesn't).
Maks Orlovichf378b3a2017-10-31 16:27:30386 if (!simple_file_tracker_)
Maks Orlovichdb1f0922018-01-31 03:41:17387 simple_file_tracker_ =
388 std::make_unique<disk_cache::SimpleFileTracker>(64);
Maks Orlovichf378b3a2017-10-31 16:27:30389 std::unique_ptr<disk_cache::SimpleBackendImpl> simple_backend =
390 std::make_unique<disk_cache::SimpleBackendImpl>(
Yutaka Hirano23a590342022-03-10 13:31:42391 /*file_operations=*/nullptr, cache_path_,
392 /* cleanup_tracker = */ nullptr, simple_file_tracker_.get(), size_,
393 type_, /*net_log = */ nullptr);
Maks Orlovich2690c352022-06-24 15:48:44394 simple_backend->Init(cb.callback());
395 ASSERT_THAT(cb.WaitForResult(), 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 Orlovich2690c352022-06-24 15:48:44402 int rv = wait_for_index_cb.WaitForResult();
Maks Orlovich928035b2019-07-29 20:19:45403 ASSERT_THAT(rv, IsOk());
[email protected]7e48c1b2013-07-12 12:15:43404 }
[email protected]398ad132013-04-02 15:11:01405 return;
406 }
407
Tsuyoshi Horof8861cb2022-07-05 23:50:20408 std::unique_ptr<disk_cache::BackendImpl> cache;
409 if (mask_) {
410 cache = std::make_unique<disk_cache::BackendImpl>(cache_path_, mask_,
411 runner, type_,
412 /* net_log = */ nullptr);
413 } else {
414 cache = std::make_unique<disk_cache::BackendImpl>(
Steven Bingler252ffae2019-03-19 17:43:16415 cache_path_, /* cleanup_tracker = */ nullptr, runner, type_,
416 /* net_log = */ nullptr);
Tsuyoshi Horof8861cb2022-07-05 23:50:20417 }
418 cache_impl_ = cache.get();
419 cache_ = std::move(cache);
[email protected]8c3f5a32013-08-01 11:57:53420 ASSERT_TRUE(cache_);
[email protected]7aefb152011-01-21 23:46:49421 if (size_)
422 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
[email protected]7aefb152011-01-21 23:46:49423 if (new_eviction_)
424 cache_impl_->SetNewEviction();
[email protected]398ad132013-04-02 15:11:01425 cache_impl_->SetFlags(flags);
[email protected]42c459632011-12-17 02:20:23426 net::TestCompletionCallback cb;
Maks Orlovich2690c352022-06-24 15:48:44427 cache_impl_->Init(cb.callback());
428 ASSERT_THAT(cb.WaitForResult(), IsOk());
[email protected]7aefb152011-01-21 23:46:49429}