| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <functional> | 6 #include <functional> |
| 7 | 7 |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/hash.h" | 9 #include "base/hash.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "net/disk_cache/simple/simple_index_file.h" | 21 #include "net/disk_cache/simple/simple_index_file.h" |
| 22 #include "net/disk_cache/simple/simple_test_util.h" | 22 #include "net/disk_cache/simple/simple_test_util.h" |
| 23 #include "net/disk_cache/simple/simple_util.h" | 23 #include "net/disk_cache/simple/simple_util.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 namespace disk_cache { | 26 namespace disk_cache { |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const base::Time kTestLastUsedTime = | 29 const base::Time kTestLastUsedTime = |
| 30 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20); | 30 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20); |
| 31 const uint64 kTestEntrySize = 789; | 31 const uint64_t kTestEntrySize = 789; |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 | 35 |
| 36 class EntryMetadataTest : public testing::Test { | 36 class EntryMetadataTest : public testing::Test { |
| 37 public: | 37 public: |
| 38 EntryMetadata NewEntryMetadataWithValues() { | 38 EntryMetadata NewEntryMetadataWithValues() { |
| 39 return EntryMetadata(kTestLastUsedTime, kTestEntrySize); | 39 return EntryMetadata(kTestLastUsedTime, kTestEntrySize); |
| 40 } | 40 } |
| 41 | 41 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 59 | 59 |
| 60 void LoadIndexEntries(base::Time cache_last_modified, | 60 void LoadIndexEntries(base::Time cache_last_modified, |
| 61 const base::Closure& callback, | 61 const base::Closure& callback, |
| 62 SimpleIndexLoadResult* out_load_result) override { | 62 SimpleIndexLoadResult* out_load_result) override { |
| 63 load_callback_ = callback; | 63 load_callback_ = callback; |
| 64 load_result_ = out_load_result; | 64 load_result_ = out_load_result; |
| 65 ++load_index_entries_calls_; | 65 ++load_index_entries_calls_; |
| 66 } | 66 } |
| 67 | 67 |
| 68 void WriteToDisk(const SimpleIndex::EntrySet& entry_set, | 68 void WriteToDisk(const SimpleIndex::EntrySet& entry_set, |
| 69 uint64 cache_size, | 69 uint64_t cache_size, |
| 70 const base::TimeTicks& start, | 70 const base::TimeTicks& start, |
| 71 bool app_on_background, | 71 bool app_on_background, |
| 72 const base::Closure& callback) override { | 72 const base::Closure& callback) override { |
| 73 disk_writes_++; | 73 disk_writes_++; |
| 74 disk_write_entry_set_ = entry_set; | 74 disk_write_entry_set_ = entry_set; |
| 75 } | 75 } |
| 76 | 76 |
| 77 void GetAndResetDiskWriteEntrySet(SimpleIndex::EntrySet* entry_set) { | 77 void GetAndResetDiskWriteEntrySet(SimpleIndex::EntrySet* entry_set) { |
| 78 entry_set->swap(disk_write_entry_set_); | 78 entry_set->swap(disk_write_entry_set_); |
| 79 } | 79 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 90 int disk_writes_; | 90 int disk_writes_; |
| 91 SimpleIndex::EntrySet disk_write_entry_set_; | 91 SimpleIndex::EntrySet disk_write_entry_set_; |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 class SimpleIndexTest : public testing::Test, public SimpleIndexDelegate { | 94 class SimpleIndexTest : public testing::Test, public SimpleIndexDelegate { |
| 95 protected: | 95 protected: |
| 96 SimpleIndexTest() | 96 SimpleIndexTest() |
| 97 : hashes_(base::Bind(&HashesInitializer)), | 97 : hashes_(base::Bind(&HashesInitializer)), |
| 98 doom_entries_calls_(0) {} | 98 doom_entries_calls_(0) {} |
| 99 | 99 |
| 100 static uint64 HashesInitializer(size_t hash_index) { | 100 static uint64_t HashesInitializer(size_t hash_index) { |
| 101 return disk_cache::simple_util::GetEntryHashKey( | 101 return disk_cache::simple_util::GetEntryHashKey( |
| 102 base::StringPrintf("key%d", static_cast<int>(hash_index))); | 102 base::StringPrintf("key%d", static_cast<int>(hash_index))); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void SetUp() override { | 105 void SetUp() override { |
| 106 scoped_ptr<MockSimpleIndexFile> index_file(new MockSimpleIndexFile()); | 106 scoped_ptr<MockSimpleIndexFile> index_file(new MockSimpleIndexFile()); |
| 107 index_file_ = index_file->AsWeakPtr(); | 107 index_file_ = index_file->AsWeakPtr(); |
| 108 index_.reset( | 108 index_.reset( |
| 109 new SimpleIndex(NULL, this, net::DISK_CACHE, index_file.Pass())); | 109 new SimpleIndex(NULL, this, net::DISK_CACHE, index_file.Pass())); |
| 110 | 110 |
| 111 index_->Initialize(base::Time()); | 111 index_->Initialize(base::Time()); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void WaitForTimeChange() { | 114 void WaitForTimeChange() { |
| 115 const base::Time initial_time = base::Time::Now(); | 115 const base::Time initial_time = base::Time::Now(); |
| 116 do { | 116 do { |
| 117 base::PlatformThread::YieldCurrentThread(); | 117 base::PlatformThread::YieldCurrentThread(); |
| 118 } while (base::Time::Now() - | 118 } while (base::Time::Now() - |
| 119 initial_time < base::TimeDelta::FromSeconds(1)); | 119 initial_time < base::TimeDelta::FromSeconds(1)); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // From SimpleIndexDelegate: | 122 // From SimpleIndexDelegate: |
| 123 void DoomEntries(std::vector<uint64>* entry_hashes, | 123 void DoomEntries(std::vector<uint64_t>* entry_hashes, |
| 124 const net::CompletionCallback& callback) override { | 124 const net::CompletionCallback& callback) override { |
| 125 for (const uint64& entry_hash : *entry_hashes) | 125 for (const uint64_t& entry_hash : *entry_hashes) |
| 126 index_->Remove(entry_hash); | 126 index_->Remove(entry_hash); |
| 127 last_doom_entry_hashes_ = *entry_hashes; | 127 last_doom_entry_hashes_ = *entry_hashes; |
| 128 ++doom_entries_calls_; | 128 ++doom_entries_calls_; |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Redirect to allow single "friend" declaration in base class. | 131 // Redirect to allow single "friend" declaration in base class. |
| 132 bool GetEntryForTesting(uint64 key, EntryMetadata* metadata) { | 132 bool GetEntryForTesting(uint64_t key, EntryMetadata* metadata) { |
| 133 SimpleIndex::EntrySet::iterator it = index_->entries_set_.find(key); | 133 SimpleIndex::EntrySet::iterator it = index_->entries_set_.find(key); |
| 134 if (index_->entries_set_.end() == it) | 134 if (index_->entries_set_.end() == it) |
| 135 return false; | 135 return false; |
| 136 *metadata = it->second; | 136 *metadata = it->second; |
| 137 return true; | 137 return true; |
| 138 } | 138 } |
| 139 | 139 |
| 140 void InsertIntoIndexFileReturn(uint64 hash_key, | 140 void InsertIntoIndexFileReturn(uint64_t hash_key, |
| 141 base::Time last_used_time, | 141 base::Time last_used_time, |
| 142 int entry_size) { | 142 int entry_size) { |
| 143 index_file_->load_result()->entries.insert(std::make_pair( | 143 index_file_->load_result()->entries.insert(std::make_pair( |
| 144 hash_key, EntryMetadata(last_used_time, entry_size))); | 144 hash_key, EntryMetadata(last_used_time, entry_size))); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void ReturnIndexFile() { | 147 void ReturnIndexFile() { |
| 148 index_file_->load_result()->did_load = true; | 148 index_file_->load_result()->did_load = true; |
| 149 index_file_->load_callback().Run(); | 149 index_file_->load_callback().Run(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Non-const for timer manipulation. | 152 // Non-const for timer manipulation. |
| 153 SimpleIndex* index() { return index_.get(); } | 153 SimpleIndex* index() { return index_.get(); } |
| 154 const MockSimpleIndexFile* index_file() const { return index_file_.get(); } | 154 const MockSimpleIndexFile* index_file() const { return index_file_.get(); } |
| 155 | 155 |
| 156 const std::vector<uint64>& last_doom_entry_hashes() const { | 156 const std::vector<uint64_t>& last_doom_entry_hashes() const { |
| 157 return last_doom_entry_hashes_; | 157 return last_doom_entry_hashes_; |
| 158 } | 158 } |
| 159 int doom_entries_calls() const { return doom_entries_calls_; } | 159 int doom_entries_calls() const { return doom_entries_calls_; } |
| 160 | 160 |
| 161 | 161 const simple_util::ImmutableArray<uint64_t, 16> hashes_; |
| 162 const simple_util::ImmutableArray<uint64, 16> hashes_; | |
| 163 scoped_ptr<SimpleIndex> index_; | 162 scoped_ptr<SimpleIndex> index_; |
| 164 base::WeakPtr<MockSimpleIndexFile> index_file_; | 163 base::WeakPtr<MockSimpleIndexFile> index_file_; |
| 165 | 164 |
| 166 std::vector<uint64> last_doom_entry_hashes_; | 165 std::vector<uint64_t> last_doom_entry_hashes_; |
| 167 int doom_entries_calls_; | 166 int doom_entries_calls_; |
| 168 }; | 167 }; |
| 169 | 168 |
| 170 TEST_F(EntryMetadataTest, Basics) { | 169 TEST_F(EntryMetadataTest, Basics) { |
| 171 EntryMetadata entry_metadata; | 170 EntryMetadata entry_metadata; |
| 172 EXPECT_EQ(base::Time(), entry_metadata.GetLastUsedTime()); | 171 EXPECT_EQ(base::Time(), entry_metadata.GetLastUsedTime()); |
| 173 EXPECT_EQ(0U, entry_metadata.GetEntrySize()); | 172 EXPECT_EQ(0U, entry_metadata.GetEntrySize()); |
| 174 | 173 |
| 175 entry_metadata = NewEntryMetadataWithValues(); | 174 entry_metadata = NewEntryMetadataWithValues(); |
| 176 CheckEntryMetadataValues(entry_metadata); | 175 CheckEntryMetadataValues(entry_metadata); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 207 EXPECT_EQ(9U, index()->cache_size_); | 206 EXPECT_EQ(9U, index()->cache_size_); |
| 208 { | 207 { |
| 209 scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); | 208 scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); |
| 210 result->did_load = true; | 209 result->did_load = true; |
| 211 index()->MergeInitializingSet(result.Pass()); | 210 index()->MergeInitializingSet(result.Pass()); |
| 212 } | 211 } |
| 213 EXPECT_EQ(9U, index()->cache_size_); | 212 EXPECT_EQ(9U, index()->cache_size_); |
| 214 { | 213 { |
| 215 scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); | 214 scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); |
| 216 result->did_load = true; | 215 result->did_load = true; |
| 217 const uint64 new_hash_key = hashes_.at<11>(); | 216 const uint64_t new_hash_key = hashes_.at<11>(); |
| 218 result->entries.insert( | 217 result->entries.insert( |
| 219 std::make_pair(new_hash_key, EntryMetadata(base::Time::Now(), 11))); | 218 std::make_pair(new_hash_key, EntryMetadata(base::Time::Now(), 11))); |
| 220 const uint64 redundant_hash_key = hashes_.at<4>(); | 219 const uint64_t redundant_hash_key = hashes_.at<4>(); |
| 221 result->entries.insert(std::make_pair(redundant_hash_key, | 220 result->entries.insert(std::make_pair(redundant_hash_key, |
| 222 EntryMetadata(base::Time::Now(), 4))); | 221 EntryMetadata(base::Time::Now(), 4))); |
| 223 index()->MergeInitializingSet(result.Pass()); | 222 index()->MergeInitializingSet(result.Pass()); |
| 224 } | 223 } |
| 225 EXPECT_EQ(2U + 3U + 4U + 11U, index()->cache_size_); | 224 EXPECT_EQ(2U + 3U + 4U + 11U, index()->cache_size_); |
| 226 } | 225 } |
| 227 | 226 |
| 228 // State of index changes as expected with an insert and a remove. | 227 // State of index changes as expected with an insert and a remove. |
| 229 TEST_F(SimpleIndexTest, BasicInsertRemove) { | 228 TEST_F(SimpleIndexTest, BasicInsertRemove) { |
| 230 // Confirm blank state. | 229 // Confirm blank state. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 247 EXPECT_EQ(base::Time(), metadata.GetLastUsedTime()); | 246 EXPECT_EQ(base::Time(), metadata.GetLastUsedTime()); |
| 248 EXPECT_EQ(0U, metadata.GetEntrySize()); | 247 EXPECT_EQ(0U, metadata.GetEntrySize()); |
| 249 } | 248 } |
| 250 | 249 |
| 251 TEST_F(SimpleIndexTest, Has) { | 250 TEST_F(SimpleIndexTest, Has) { |
| 252 // Confirm the base index has dispatched the request for index entries. | 251 // Confirm the base index has dispatched the request for index entries. |
| 253 EXPECT_TRUE(index_file_.get()); | 252 EXPECT_TRUE(index_file_.get()); |
| 254 EXPECT_EQ(1, index_file_->load_index_entries_calls()); | 253 EXPECT_EQ(1, index_file_->load_index_entries_calls()); |
| 255 | 254 |
| 256 // Confirm "Has()" always returns true before the callback is called. | 255 // Confirm "Has()" always returns true before the callback is called. |
| 257 const uint64 kHash1 = hashes_.at<1>(); | 256 const uint64_t kHash1 = hashes_.at<1>(); |
| 258 EXPECT_TRUE(index()->Has(kHash1)); | 257 EXPECT_TRUE(index()->Has(kHash1)); |
| 259 index()->Insert(kHash1); | 258 index()->Insert(kHash1); |
| 260 EXPECT_TRUE(index()->Has(kHash1)); | 259 EXPECT_TRUE(index()->Has(kHash1)); |
| 261 index()->Remove(kHash1); | 260 index()->Remove(kHash1); |
| 262 // TODO(rdsmith): Maybe return false on explicitly removed entries? | 261 // TODO(rdsmith): Maybe return false on explicitly removed entries? |
| 263 EXPECT_TRUE(index()->Has(kHash1)); | 262 EXPECT_TRUE(index()->Has(kHash1)); |
| 264 | 263 |
| 265 ReturnIndexFile(); | 264 ReturnIndexFile(); |
| 266 | 265 |
| 267 // Confirm "Has() returns conditionally now. | 266 // Confirm "Has() returns conditionally now. |
| 268 EXPECT_FALSE(index()->Has(kHash1)); | 267 EXPECT_FALSE(index()->Has(kHash1)); |
| 269 index()->Insert(kHash1); | 268 index()->Insert(kHash1); |
| 270 EXPECT_TRUE(index()->Has(kHash1)); | 269 EXPECT_TRUE(index()->Has(kHash1)); |
| 271 index()->Remove(kHash1); | 270 index()->Remove(kHash1); |
| 272 } | 271 } |
| 273 | 272 |
| 274 TEST_F(SimpleIndexTest, UseIfExists) { | 273 TEST_F(SimpleIndexTest, UseIfExists) { |
| 275 // Confirm the base index has dispatched the request for index entries. | 274 // Confirm the base index has dispatched the request for index entries. |
| 276 EXPECT_TRUE(index_file_.get()); | 275 EXPECT_TRUE(index_file_.get()); |
| 277 EXPECT_EQ(1, index_file_->load_index_entries_calls()); | 276 EXPECT_EQ(1, index_file_->load_index_entries_calls()); |
| 278 | 277 |
| 279 // Confirm "UseIfExists()" always returns true before the callback is called | 278 // Confirm "UseIfExists()" always returns true before the callback is called |
| 280 // and updates mod time if the entry was really there. | 279 // and updates mod time if the entry was really there. |
| 281 const uint64 kHash1 = hashes_.at<1>(); | 280 const uint64_t kHash1 = hashes_.at<1>(); |
| 282 EntryMetadata metadata1, metadata2; | 281 EntryMetadata metadata1, metadata2; |
| 283 EXPECT_TRUE(index()->UseIfExists(kHash1)); | 282 EXPECT_TRUE(index()->UseIfExists(kHash1)); |
| 284 EXPECT_FALSE(GetEntryForTesting(kHash1, &metadata1)); | 283 EXPECT_FALSE(GetEntryForTesting(kHash1, &metadata1)); |
| 285 index()->Insert(kHash1); | 284 index()->Insert(kHash1); |
| 286 EXPECT_TRUE(index()->UseIfExists(kHash1)); | 285 EXPECT_TRUE(index()->UseIfExists(kHash1)); |
| 287 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata1)); | 286 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata1)); |
| 288 WaitForTimeChange(); | 287 WaitForTimeChange(); |
| 289 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata2)); | 288 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata2)); |
| 290 EXPECT_EQ(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); | 289 EXPECT_EQ(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); |
| 291 EXPECT_TRUE(index()->UseIfExists(kHash1)); | 290 EXPECT_TRUE(index()->UseIfExists(kHash1)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 310 EXPECT_LT(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); | 309 EXPECT_LT(metadata1.GetLastUsedTime(), metadata2.GetLastUsedTime()); |
| 311 index()->Remove(kHash1); | 310 index()->Remove(kHash1); |
| 312 EXPECT_FALSE(index()->UseIfExists(kHash1)); | 311 EXPECT_FALSE(index()->UseIfExists(kHash1)); |
| 313 } | 312 } |
| 314 | 313 |
| 315 TEST_F(SimpleIndexTest, UpdateEntrySize) { | 314 TEST_F(SimpleIndexTest, UpdateEntrySize) { |
| 316 base::Time now(base::Time::Now()); | 315 base::Time now(base::Time::Now()); |
| 317 | 316 |
| 318 index()->SetMaxSize(1000); | 317 index()->SetMaxSize(1000); |
| 319 | 318 |
| 320 const uint64 kHash1 = hashes_.at<1>(); | 319 const uint64_t kHash1 = hashes_.at<1>(); |
| 321 InsertIntoIndexFileReturn(kHash1, now - base::TimeDelta::FromDays(2), 475); | 320 InsertIntoIndexFileReturn(kHash1, now - base::TimeDelta::FromDays(2), 475); |
| 322 ReturnIndexFile(); | 321 ReturnIndexFile(); |
| 323 | 322 |
| 324 EntryMetadata metadata; | 323 EntryMetadata metadata; |
| 325 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); | 324 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); |
| 326 EXPECT_LT( | 325 EXPECT_LT( |
| 327 now - base::TimeDelta::FromDays(2) - base::TimeDelta::FromSeconds(1), | 326 now - base::TimeDelta::FromDays(2) - base::TimeDelta::FromSeconds(1), |
| 328 metadata.GetLastUsedTime()); | 327 metadata.GetLastUsedTime()); |
| 329 EXPECT_GT( | 328 EXPECT_GT( |
| 330 now - base::TimeDelta::FromDays(2) + base::TimeDelta::FromSeconds(1), | 329 now - base::TimeDelta::FromDays(2) + base::TimeDelta::FromSeconds(1), |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 now - base::TimeDelta::FromDays(3) - base::TimeDelta::FromSeconds(1), | 387 now - base::TimeDelta::FromDays(3) - base::TimeDelta::FromSeconds(1), |
| 389 metadata.GetLastUsedTime()); | 388 metadata.GetLastUsedTime()); |
| 390 EXPECT_GT( | 389 EXPECT_GT( |
| 391 now - base::TimeDelta::FromDays(3) + base::TimeDelta::FromSeconds(1), | 390 now - base::TimeDelta::FromDays(3) + base::TimeDelta::FromSeconds(1), |
| 392 metadata.GetLastUsedTime()); | 391 metadata.GetLastUsedTime()); |
| 393 EXPECT_EQ(100U, metadata.GetEntrySize()); | 392 EXPECT_EQ(100U, metadata.GetEntrySize()); |
| 394 } | 393 } |
| 395 | 394 |
| 396 // Remove something that's going to come in from the loaded index. | 395 // Remove something that's going to come in from the loaded index. |
| 397 TEST_F(SimpleIndexTest, RemoveBeforeInit) { | 396 TEST_F(SimpleIndexTest, RemoveBeforeInit) { |
| 398 const uint64 kHash1 = hashes_.at<1>(); | 397 const uint64_t kHash1 = hashes_.at<1>(); |
| 399 index()->Remove(kHash1); | 398 index()->Remove(kHash1); |
| 400 | 399 |
| 401 InsertIntoIndexFileReturn(kHash1, | 400 InsertIntoIndexFileReturn(kHash1, |
| 402 base::Time::Now() - base::TimeDelta::FromDays(2), | 401 base::Time::Now() - base::TimeDelta::FromDays(2), |
| 403 10u); | 402 10u); |
| 404 ReturnIndexFile(); | 403 ReturnIndexFile(); |
| 405 | 404 |
| 406 EXPECT_FALSE(index()->Has(kHash1)); | 405 EXPECT_FALSE(index()->Has(kHash1)); |
| 407 } | 406 } |
| 408 | 407 |
| 409 // Insert something that's going to come in from the loaded index; correct | 408 // Insert something that's going to come in from the loaded index; correct |
| 410 // result? | 409 // result? |
| 411 TEST_F(SimpleIndexTest, InsertBeforeInit) { | 410 TEST_F(SimpleIndexTest, InsertBeforeInit) { |
| 412 const uint64 kHash1 = hashes_.at<1>(); | 411 const uint64_t kHash1 = hashes_.at<1>(); |
| 413 index()->Insert(kHash1); | 412 index()->Insert(kHash1); |
| 414 | 413 |
| 415 InsertIntoIndexFileReturn(kHash1, | 414 InsertIntoIndexFileReturn(kHash1, |
| 416 base::Time::Now() - base::TimeDelta::FromDays(2), | 415 base::Time::Now() - base::TimeDelta::FromDays(2), |
| 417 10u); | 416 10u); |
| 418 ReturnIndexFile(); | 417 ReturnIndexFile(); |
| 419 | 418 |
| 420 EntryMetadata metadata; | 419 EntryMetadata metadata; |
| 421 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); | 420 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); |
| 422 base::Time now(base::Time::Now()); | 421 base::Time now(base::Time::Now()); |
| 423 EXPECT_LT(now - base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); | 422 EXPECT_LT(now - base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); |
| 424 EXPECT_GT(now + base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); | 423 EXPECT_GT(now + base::TimeDelta::FromMinutes(1), metadata.GetLastUsedTime()); |
| 425 EXPECT_EQ(0U, metadata.GetEntrySize()); | 424 EXPECT_EQ(0U, metadata.GetEntrySize()); |
| 426 } | 425 } |
| 427 | 426 |
| 428 // Insert and Remove something that's going to come in from the loaded index. | 427 // Insert and Remove something that's going to come in from the loaded index. |
| 429 TEST_F(SimpleIndexTest, InsertRemoveBeforeInit) { | 428 TEST_F(SimpleIndexTest, InsertRemoveBeforeInit) { |
| 430 const uint64 kHash1 = hashes_.at<1>(); | 429 const uint64_t kHash1 = hashes_.at<1>(); |
| 431 index()->Insert(kHash1); | 430 index()->Insert(kHash1); |
| 432 index()->Remove(kHash1); | 431 index()->Remove(kHash1); |
| 433 | 432 |
| 434 InsertIntoIndexFileReturn(kHash1, | 433 InsertIntoIndexFileReturn(kHash1, |
| 435 base::Time::Now() - base::TimeDelta::FromDays(2), | 434 base::Time::Now() - base::TimeDelta::FromDays(2), |
| 436 10u); | 435 10u); |
| 437 ReturnIndexFile(); | 436 ReturnIndexFile(); |
| 438 | 437 |
| 439 EXPECT_FALSE(index()->Has(kHash1)); | 438 EXPECT_FALSE(index()->Has(kHash1)); |
| 440 } | 439 } |
| 441 | 440 |
| 442 // Insert and Remove something that's going to come in from the loaded index. | 441 // Insert and Remove something that's going to come in from the loaded index. |
| 443 TEST_F(SimpleIndexTest, RemoveInsertBeforeInit) { | 442 TEST_F(SimpleIndexTest, RemoveInsertBeforeInit) { |
| 444 const uint64 kHash1 = hashes_.at<1>(); | 443 const uint64_t kHash1 = hashes_.at<1>(); |
| 445 index()->Remove(kHash1); | 444 index()->Remove(kHash1); |
| 446 index()->Insert(kHash1); | 445 index()->Insert(kHash1); |
| 447 | 446 |
| 448 InsertIntoIndexFileReturn(kHash1, | 447 InsertIntoIndexFileReturn(kHash1, |
| 449 base::Time::Now() - base::TimeDelta::FromDays(2), | 448 base::Time::Now() - base::TimeDelta::FromDays(2), |
| 450 10u); | 449 10u); |
| 451 ReturnIndexFile(); | 450 ReturnIndexFile(); |
| 452 | 451 |
| 453 EntryMetadata metadata; | 452 EntryMetadata metadata; |
| 454 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); | 453 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 } | 547 } |
| 549 | 548 |
| 550 // Confirm all the operations queue a disk write at some point in the | 549 // Confirm all the operations queue a disk write at some point in the |
| 551 // future. | 550 // future. |
| 552 TEST_F(SimpleIndexTest, DiskWriteQueued) { | 551 TEST_F(SimpleIndexTest, DiskWriteQueued) { |
| 553 index()->SetMaxSize(1000); | 552 index()->SetMaxSize(1000); |
| 554 ReturnIndexFile(); | 553 ReturnIndexFile(); |
| 555 | 554 |
| 556 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); | 555 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); |
| 557 | 556 |
| 558 const uint64 kHash1 = hashes_.at<1>(); | 557 const uint64_t kHash1 = hashes_.at<1>(); |
| 559 index()->Insert(kHash1); | 558 index()->Insert(kHash1); |
| 560 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 559 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
| 561 index()->write_to_disk_timer_.Stop(); | 560 index()->write_to_disk_timer_.Stop(); |
| 562 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); | 561 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); |
| 563 | 562 |
| 564 index()->UseIfExists(kHash1); | 563 index()->UseIfExists(kHash1); |
| 565 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 564 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
| 566 index()->write_to_disk_timer_.Stop(); | 565 index()->write_to_disk_timer_.Stop(); |
| 567 | 566 |
| 568 index()->UpdateEntrySize(kHash1, 20); | 567 index()->UpdateEntrySize(kHash1, 20); |
| 569 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 568 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
| 570 index()->write_to_disk_timer_.Stop(); | 569 index()->write_to_disk_timer_.Stop(); |
| 571 | 570 |
| 572 index()->Remove(kHash1); | 571 index()->Remove(kHash1); |
| 573 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 572 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
| 574 index()->write_to_disk_timer_.Stop(); | 573 index()->write_to_disk_timer_.Stop(); |
| 575 } | 574 } |
| 576 | 575 |
| 577 TEST_F(SimpleIndexTest, DiskWriteExecuted) { | 576 TEST_F(SimpleIndexTest, DiskWriteExecuted) { |
| 578 index()->SetMaxSize(1000); | 577 index()->SetMaxSize(1000); |
| 579 ReturnIndexFile(); | 578 ReturnIndexFile(); |
| 580 | 579 |
| 581 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); | 580 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); |
| 582 | 581 |
| 583 const uint64 kHash1 = hashes_.at<1>(); | 582 const uint64_t kHash1 = hashes_.at<1>(); |
| 584 index()->Insert(kHash1); | 583 index()->Insert(kHash1); |
| 585 index()->UpdateEntrySize(kHash1, 20); | 584 index()->UpdateEntrySize(kHash1, 20); |
| 586 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 585 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
| 587 base::Closure user_task(index()->write_to_disk_timer_.user_task()); | 586 base::Closure user_task(index()->write_to_disk_timer_.user_task()); |
| 588 index()->write_to_disk_timer_.Stop(); | 587 index()->write_to_disk_timer_.Stop(); |
| 589 | 588 |
| 590 EXPECT_EQ(0, index_file_->disk_writes()); | 589 EXPECT_EQ(0, index_file_->disk_writes()); |
| 591 user_task.Run(); | 590 user_task.Run(); |
| 592 EXPECT_EQ(1, index_file_->disk_writes()); | 591 EXPECT_EQ(1, index_file_->disk_writes()); |
| 593 SimpleIndex::EntrySet entry_set; | 592 SimpleIndex::EntrySet entry_set; |
| 594 index_file_->GetAndResetDiskWriteEntrySet(&entry_set); | 593 index_file_->GetAndResetDiskWriteEntrySet(&entry_set); |
| 595 | 594 |
| 596 uint64 hash_key = kHash1; | 595 uint64_t hash_key = kHash1; |
| 597 base::Time now(base::Time::Now()); | 596 base::Time now(base::Time::Now()); |
| 598 ASSERT_EQ(1u, entry_set.size()); | 597 ASSERT_EQ(1u, entry_set.size()); |
| 599 EXPECT_EQ(hash_key, entry_set.begin()->first); | 598 EXPECT_EQ(hash_key, entry_set.begin()->first); |
| 600 const EntryMetadata& entry1(entry_set.begin()->second); | 599 const EntryMetadata& entry1(entry_set.begin()->second); |
| 601 EXPECT_LT(now - base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); | 600 EXPECT_LT(now - base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); |
| 602 EXPECT_GT(now + base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); | 601 EXPECT_GT(now + base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); |
| 603 EXPECT_EQ(20U, entry1.GetEntrySize()); | 602 EXPECT_EQ(20U, entry1.GetEntrySize()); |
| 604 } | 603 } |
| 605 | 604 |
| 606 TEST_F(SimpleIndexTest, DiskWritePostponed) { | 605 TEST_F(SimpleIndexTest, DiskWritePostponed) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 618 WaitForTimeChange(); | 617 WaitForTimeChange(); |
| 619 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 618 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
| 620 index()->Insert(hashes_.at<2>()); | 619 index()->Insert(hashes_.at<2>()); |
| 621 index()->UpdateEntrySize(hashes_.at<2>(), 40); | 620 index()->UpdateEntrySize(hashes_.at<2>(), 40); |
| 622 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 621 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
| 623 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 622 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
| 624 index()->write_to_disk_timer_.Stop(); | 623 index()->write_to_disk_timer_.Stop(); |
| 625 } | 624 } |
| 626 | 625 |
| 627 } // namespace disk_cache | 626 } // namespace disk_cache |
| OLD | NEW |