OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/simple/simple_index.h" | 5 #include "net/disk_cache/simple/simple_index.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
| 9 #include <memory> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
12 #include "base/hash.h" | 13 #include "base/hash.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/pickle.h" | 15 #include "base/pickle.h" |
16 #include "base/sha1.h" | 16 #include "base/sha1.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
19 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "net/base/cache_type.h" | 21 #include "net/base/cache_type.h" |
22 #include "net/disk_cache/simple/simple_index_delegate.h" | 22 #include "net/disk_cache/simple/simple_index_delegate.h" |
23 #include "net/disk_cache/simple/simple_index_file.h" | 23 #include "net/disk_cache/simple/simple_index_file.h" |
24 #include "net/disk_cache/simple/simple_test_util.h" | 24 #include "net/disk_cache/simple/simple_test_util.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 SimpleIndexTest() | 98 SimpleIndexTest() |
99 : hashes_(base::Bind(&HashesInitializer)), | 99 : hashes_(base::Bind(&HashesInitializer)), |
100 doom_entries_calls_(0) {} | 100 doom_entries_calls_(0) {} |
101 | 101 |
102 static uint64_t HashesInitializer(size_t hash_index) { | 102 static uint64_t HashesInitializer(size_t hash_index) { |
103 return disk_cache::simple_util::GetEntryHashKey( | 103 return disk_cache::simple_util::GetEntryHashKey( |
104 base::StringPrintf("key%d", static_cast<int>(hash_index))); | 104 base::StringPrintf("key%d", static_cast<int>(hash_index))); |
105 } | 105 } |
106 | 106 |
107 void SetUp() override { | 107 void SetUp() override { |
108 scoped_ptr<MockSimpleIndexFile> index_file(new MockSimpleIndexFile()); | 108 std::unique_ptr<MockSimpleIndexFile> index_file(new MockSimpleIndexFile()); |
109 index_file_ = index_file->AsWeakPtr(); | 109 index_file_ = index_file->AsWeakPtr(); |
110 index_.reset( | 110 index_.reset( |
111 new SimpleIndex(NULL, this, net::DISK_CACHE, std::move(index_file))); | 111 new SimpleIndex(NULL, this, net::DISK_CACHE, std::move(index_file))); |
112 | 112 |
113 index_->Initialize(base::Time()); | 113 index_->Initialize(base::Time()); |
114 } | 114 } |
115 | 115 |
116 void WaitForTimeChange() { | 116 void WaitForTimeChange() { |
117 const base::Time initial_time = base::Time::Now(); | 117 const base::Time initial_time = base::Time::Now(); |
118 do { | 118 do { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // Non-const for timer manipulation. | 154 // Non-const for timer manipulation. |
155 SimpleIndex* index() { return index_.get(); } | 155 SimpleIndex* index() { return index_.get(); } |
156 const MockSimpleIndexFile* index_file() const { return index_file_.get(); } | 156 const MockSimpleIndexFile* index_file() const { return index_file_.get(); } |
157 | 157 |
158 const std::vector<uint64_t>& last_doom_entry_hashes() const { | 158 const std::vector<uint64_t>& last_doom_entry_hashes() const { |
159 return last_doom_entry_hashes_; | 159 return last_doom_entry_hashes_; |
160 } | 160 } |
161 int doom_entries_calls() const { return doom_entries_calls_; } | 161 int doom_entries_calls() const { return doom_entries_calls_; } |
162 | 162 |
163 const simple_util::ImmutableArray<uint64_t, 16> hashes_; | 163 const simple_util::ImmutableArray<uint64_t, 16> hashes_; |
164 scoped_ptr<SimpleIndex> index_; | 164 std::unique_ptr<SimpleIndex> index_; |
165 base::WeakPtr<MockSimpleIndexFile> index_file_; | 165 base::WeakPtr<MockSimpleIndexFile> index_file_; |
166 | 166 |
167 std::vector<uint64_t> last_doom_entry_hashes_; | 167 std::vector<uint64_t> last_doom_entry_hashes_; |
168 int doom_entries_calls_; | 168 int doom_entries_calls_; |
169 }; | 169 }; |
170 | 170 |
171 TEST_F(EntryMetadataTest, Basics) { | 171 TEST_F(EntryMetadataTest, Basics) { |
172 EntryMetadata entry_metadata; | 172 EntryMetadata entry_metadata; |
173 EXPECT_EQ(base::Time(), entry_metadata.GetLastUsedTime()); | 173 EXPECT_EQ(base::Time(), entry_metadata.GetLastUsedTime()); |
174 EXPECT_EQ(0U, entry_metadata.GetEntrySize()); | 174 EXPECT_EQ(0U, entry_metadata.GetEntrySize()); |
(...skipping 25 matching lines...) Expand all Loading... |
200 TEST_F(SimpleIndexTest, IndexSizeCorrectOnMerge) { | 200 TEST_F(SimpleIndexTest, IndexSizeCorrectOnMerge) { |
201 index()->SetMaxSize(100); | 201 index()->SetMaxSize(100); |
202 index()->Insert(hashes_.at<2>()); | 202 index()->Insert(hashes_.at<2>()); |
203 index()->UpdateEntrySize(hashes_.at<2>(), 2); | 203 index()->UpdateEntrySize(hashes_.at<2>(), 2); |
204 index()->Insert(hashes_.at<3>()); | 204 index()->Insert(hashes_.at<3>()); |
205 index()->UpdateEntrySize(hashes_.at<3>(), 3); | 205 index()->UpdateEntrySize(hashes_.at<3>(), 3); |
206 index()->Insert(hashes_.at<4>()); | 206 index()->Insert(hashes_.at<4>()); |
207 index()->UpdateEntrySize(hashes_.at<4>(), 4); | 207 index()->UpdateEntrySize(hashes_.at<4>(), 4); |
208 EXPECT_EQ(9U, index()->cache_size_); | 208 EXPECT_EQ(9U, index()->cache_size_); |
209 { | 209 { |
210 scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); | 210 std::unique_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); |
211 result->did_load = true; | 211 result->did_load = true; |
212 index()->MergeInitializingSet(std::move(result)); | 212 index()->MergeInitializingSet(std::move(result)); |
213 } | 213 } |
214 EXPECT_EQ(9U, index()->cache_size_); | 214 EXPECT_EQ(9U, index()->cache_size_); |
215 { | 215 { |
216 scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); | 216 std::unique_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); |
217 result->did_load = true; | 217 result->did_load = true; |
218 const uint64_t new_hash_key = hashes_.at<11>(); | 218 const uint64_t new_hash_key = hashes_.at<11>(); |
219 result->entries.insert( | 219 result->entries.insert( |
220 std::make_pair(new_hash_key, EntryMetadata(base::Time::Now(), 11))); | 220 std::make_pair(new_hash_key, EntryMetadata(base::Time::Now(), 11))); |
221 const uint64_t redundant_hash_key = hashes_.at<4>(); | 221 const uint64_t redundant_hash_key = hashes_.at<4>(); |
222 result->entries.insert(std::make_pair(redundant_hash_key, | 222 result->entries.insert(std::make_pair(redundant_hash_key, |
223 EntryMetadata(base::Time::Now(), 4))); | 223 EntryMetadata(base::Time::Now(), 4))); |
224 index()->MergeInitializingSet(std::move(result)); | 224 index()->MergeInitializingSet(std::move(result)); |
225 } | 225 } |
226 EXPECT_EQ(2U + 3U + 4U + 11U, index()->cache_size_); | 226 EXPECT_EQ(2U + 3U + 4U + 11U, index()->cache_size_); |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 WaitForTimeChange(); | 619 WaitForTimeChange(); |
620 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 620 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
621 index()->Insert(hashes_.at<2>()); | 621 index()->Insert(hashes_.at<2>()); |
622 index()->UpdateEntrySize(hashes_.at<2>(), 40); | 622 index()->UpdateEntrySize(hashes_.at<2>(), 40); |
623 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 623 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
624 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 624 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
625 index()->write_to_disk_timer_.Stop(); | 625 index()->write_to_disk_timer_.Stop(); |
626 } | 626 } |
627 | 627 |
628 } // namespace disk_cache | 628 } // namespace disk_cache |
OLD | NEW |