Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: net/disk_cache/simple/simple_index_file_unittest.cc

Issue 2957133002: Implement in-memory byte hints in SimpleCache (Closed)
Patch Set: Adjust to the interface's naming change. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_file.h" 5 #include "net/disk_cache/simple/simple_index_file.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 28 matching lines...) Expand all
39 using disk_cache::SimpleIndexFile; 39 using disk_cache::SimpleIndexFile;
40 using disk_cache::SimpleIndex; 40 using disk_cache::SimpleIndex;
41 41
42 namespace disk_cache { 42 namespace disk_cache {
43 43
44 // The Simple Cache backend requires a few guarantees from the filesystem like 44 // The Simple Cache backend requires a few guarantees from the filesystem like
45 // atomic renaming of recently open files. Those guarantees are not provided in 45 // atomic renaming of recently open files. Those guarantees are not provided in
46 // general on Windows. 46 // general on Windows.
47 #if defined(OS_POSIX) 47 #if defined(OS_POSIX)
48 48
49 namespace {
50
51 uint32_t RoundSize(uint32_t in) {
52 return (in + 0xFFu) & 0xFFFFFF00u;
53 }
54
55 } // namespace
56
49 TEST(IndexMetadataTest, Basics) { 57 TEST(IndexMetadataTest, Basics) {
50 SimpleIndexFile::IndexMetadata index_metadata; 58 SimpleIndexFile::IndexMetadata index_metadata;
51 59
52 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_); 60 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_);
53 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_); 61 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_);
54 EXPECT_EQ(0U, index_metadata.entry_count()); 62 EXPECT_EQ(0U, index_metadata.entry_count());
55 EXPECT_EQ(0U, index_metadata.cache_size_); 63 EXPECT_EQ(0U, index_metadata.cache_size_);
56 64
57 // Without setting a |reason_|, the index metadata isn't valid. 65 // Without setting a |reason_|, the index metadata isn't valid.
58 index_metadata.reason_ = SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN; 66 index_metadata.reason_ = SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 EXPECT_EQ(new_index_metadata.magic_number_, v6_index_metadata.magic_number_); 122 EXPECT_EQ(new_index_metadata.magic_number_, v6_index_metadata.magic_number_);
115 EXPECT_EQ(new_index_metadata.version_, v6_index_metadata.version_); 123 EXPECT_EQ(new_index_metadata.version_, v6_index_metadata.version_);
116 124
117 EXPECT_EQ(new_index_metadata.reason_, SimpleIndex::INDEX_WRITE_REASON_MAX); 125 EXPECT_EQ(new_index_metadata.reason_, SimpleIndex::INDEX_WRITE_REASON_MAX);
118 EXPECT_EQ(new_index_metadata.entry_count(), v6_index_metadata.entry_count()); 126 EXPECT_EQ(new_index_metadata.entry_count(), v6_index_metadata.entry_count());
119 EXPECT_EQ(new_index_metadata.cache_size_, v6_index_metadata.cache_size_); 127 EXPECT_EQ(new_index_metadata.cache_size_, v6_index_metadata.cache_size_);
120 128
121 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); 129 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
122 } 130 }
123 131
132 // This derived index metadata class allows us to serialize the older V7 format
133 // of the index metadata, thus allowing us to test deserializing the old format.
134 class V7IndexMetadataForTest : public SimpleIndexFile::IndexMetadata {
135 public:
136 V7IndexMetadataForTest(uint64_t entry_count, uint64_t cache_size)
137 : SimpleIndexFile::IndexMetadata(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
138 entry_count,
139 cache_size) {
140 version_ = 7;
141 }
142 };
143
124 // This friend derived class is able to reexport its ancestors private methods 144 // This friend derived class is able to reexport its ancestors private methods
125 // as public, for use in tests. 145 // as public, for use in tests.
126 class WrappedSimpleIndexFile : public SimpleIndexFile { 146 class WrappedSimpleIndexFile : public SimpleIndexFile {
127 public: 147 public:
128 using SimpleIndexFile::Deserialize; 148 using SimpleIndexFile::Deserialize;
129 using SimpleIndexFile::LegacyIsIndexFileStale; 149 using SimpleIndexFile::LegacyIsIndexFileStale;
130 using SimpleIndexFile::Serialize; 150 using SimpleIndexFile::Serialize;
131 using SimpleIndexFile::SerializeFinalData; 151 using SimpleIndexFile::SerializeFinalData;
132 152
133 explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory) 153 explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory)
(...skipping 12 matching lines...) Expand all
146 } 166 }
147 167
148 bool CreateIndexFileDirectory() const { 168 bool CreateIndexFileDirectory() const {
149 return base::CreateDirectory(index_file_.DirName()); 169 return base::CreateDirectory(index_file_.DirName());
150 } 170 }
151 }; 171 };
152 172
153 class SimpleIndexFileTest : public testing::Test { 173 class SimpleIndexFileTest : public testing::Test {
154 public: 174 public:
155 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { 175 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) {
156 return 176 return a.last_used_time_seconds_since_epoch_ ==
157 a.last_used_time_seconds_since_epoch_ == 177 b.last_used_time_seconds_since_epoch_ &&
158 b.last_used_time_seconds_since_epoch_ && 178 a.entry_size_256b_chunks_ == b.entry_size_256b_chunks_ &&
159 a.entry_size_ == b.entry_size_; 179 a.in_memory_data_ == b.in_memory_data_;
160 } 180 }
161 }; 181 };
162 182
163 TEST_F(SimpleIndexFileTest, Serialize) { 183 TEST_F(SimpleIndexFileTest, Serialize) {
164 SimpleIndex::EntrySet entries; 184 SimpleIndex::EntrySet entries;
165 static const uint64_t kHashes[] = {11, 22, 33}; 185 static const uint64_t kHashes[] = {11, 22, 33};
166 static const size_t kNumHashes = arraysize(kHashes); 186 static const size_t kNumHashes = arraysize(kHashes);
167 EntryMetadata metadata_entries[kNumHashes]; 187 EntryMetadata metadata_entries[kNumHashes];
168 188
169 SimpleIndexFile::IndexMetadata index_metadata( 189 SimpleIndexFile::IndexMetadata index_metadata(
170 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, 190 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
171 static_cast<uint64_t>(kNumHashes), 456); 191 static_cast<uint64_t>(kNumHashes), 456);
172 for (size_t i = 0; i < kNumHashes; ++i) { 192 for (size_t i = 0; i < kNumHashes; ++i) {
173 uint64_t hash = kHashes[i]; 193 uint64_t hash = kHashes[i];
174 // TODO(eroman): Should restructure the test so no casting here (and same 194 // TODO(eroman): Should restructure the test so no casting here (and same
175 // elsewhere where a hash is cast to an entry size). 195 // elsewhere where a hash is cast to an entry size).
176 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash)); 196 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash));
197 metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
177 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); 198 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
178 } 199 }
179 200
180 std::unique_ptr<base::Pickle> pickle = 201 std::unique_ptr<base::Pickle> pickle =
181 WrappedSimpleIndexFile::Serialize(index_metadata, entries); 202 WrappedSimpleIndexFile::Serialize(index_metadata, entries);
182 EXPECT_TRUE(pickle.get() != NULL); 203 EXPECT_TRUE(pickle.get() != NULL);
183 base::Time now = base::Time::Now(); 204 base::Time now = base::Time::Now();
184 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); 205 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get()));
185 base::Time when_index_last_saw_cache; 206 base::Time when_index_last_saw_cache;
186 SimpleIndexLoadResult deserialize_result; 207 SimpleIndexLoadResult deserialize_result;
187 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), 208 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()),
188 pickle->size(), 209 pickle->size(),
189 &when_index_last_saw_cache, 210 &when_index_last_saw_cache,
190 &deserialize_result); 211 &deserialize_result);
191 EXPECT_TRUE(deserialize_result.did_load); 212 EXPECT_TRUE(deserialize_result.did_load);
192 EXPECT_EQ(now, when_index_last_saw_cache); 213 EXPECT_EQ(now, when_index_last_saw_cache);
193 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries; 214 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
194 EXPECT_EQ(entries.size(), new_entries.size()); 215 EXPECT_EQ(entries.size(), new_entries.size());
195 216
196 for (size_t i = 0; i < kNumHashes; ++i) { 217 for (size_t i = 0; i < kNumHashes; ++i) {
197 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); 218 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]);
198 EXPECT_TRUE(new_entries.end() != it); 219 EXPECT_TRUE(new_entries.end() != it);
199 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i])); 220 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i]));
200 } 221 }
201 } 222 }
202 223
224 TEST_F(SimpleIndexFileTest, ReadV7Format) {
225 static const uint64_t kHashes[] = {11, 22, 33};
226 static const uint32_t kSizes[] = {394, 594, 495940};
227 static_assert(arraysize(kHashes) == arraysize(kSizes),
228 "Need same number of hashes and sizes");
229 static const size_t kNumHashes = arraysize(kHashes);
230
231 V7IndexMetadataForTest v7_metadata(kNumHashes, 100 * 1024 * 1024);
232
233 // We don't have a convenient way of serializing the actual entries in the
234 // V7 format, but we can cheat a bit by using the implementation details: if
235 // we set the 8 lower bits of size as the memory data, and upper bits
236 // as the size, the new serialization will produce what we want.
237 SimpleIndex::EntrySet entries;
238 for (size_t i = 0; i < kNumHashes; ++i) {
239 EntryMetadata entry(Time(), kSizes[i] & 0xFFFFFF00u);
240 entry.SetInMemoryData(static_cast<uint8_t>(kSizes[i] & 0xFFu));
241 SimpleIndex::InsertInEntrySet(kHashes[i], entry, &entries);
242 }
243 std::unique_ptr<base::Pickle> pickle =
244 WrappedSimpleIndexFile::Serialize(v7_metadata, entries);
245 ASSERT_TRUE(pickle.get() != NULL);
246 base::Time now = base::Time::Now();
247 ASSERT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get()));
248
249 // Now read it back. We should get the sizes rounded, and 0 for mem entries.
250 base::Time when_index_last_saw_cache;
251 SimpleIndexLoadResult deserialize_result;
252 WrappedSimpleIndexFile::Deserialize(
253 static_cast<const char*>(pickle->data()), pickle->size(),
254 &when_index_last_saw_cache, &deserialize_result);
255 EXPECT_TRUE(deserialize_result.did_load);
256 EXPECT_EQ(now, when_index_last_saw_cache);
257 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
258 ASSERT_EQ(entries.size(), new_entries.size());
259 for (size_t i = 0; i < kNumHashes; ++i) {
260 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]);
261 ASSERT_TRUE(new_entries.end() != it);
262 EXPECT_EQ(RoundSize(kSizes[i]), it->second.GetEntrySize());
263 EXPECT_EQ(0u, it->second.GetInMemoryData());
264 }
265 }
266
203 TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) { 267 TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) {
204 base::ScopedTempDir cache_dir; 268 base::ScopedTempDir cache_dir;
205 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); 269 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
206 base::Time cache_mtime; 270 base::Time cache_mtime;
207 const base::FilePath cache_path = cache_dir.GetPath(); 271 const base::FilePath cache_path = cache_dir.GetPath();
208 272
209 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); 273 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime));
210 WrappedSimpleIndexFile simple_index_file(cache_path); 274 WrappedSimpleIndexFile simple_index_file(cache_path);
211 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); 275 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
212 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); 276 const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 closure.WaitForResult(); 466 closure.WaitForResult();
403 467
404 // Check that the temporary file was deleted and the index file was created. 468 // Check that the temporary file was deleted and the index file was created.
405 EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath())); 469 EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath()));
406 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); 470 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
407 } 471 }
408 472
409 #endif // defined(OS_POSIX) 473 #endif // defined(OS_POSIX)
410 474
411 } // namespace disk_cache 475 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index_file.cc ('k') | net/disk_cache/simple/simple_index_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698