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 <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "net/disk_cache/simple/simple_util.h" | 25 #include "net/disk_cache/simple/simple_util.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 | 27 |
28 namespace disk_cache { | 28 namespace disk_cache { |
29 namespace { | 29 namespace { |
30 | 30 |
31 const base::Time kTestLastUsedTime = | 31 const base::Time kTestLastUsedTime = |
32 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20); | 32 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20); |
33 const uint32_t kTestEntrySize = 789; | 33 const uint32_t kTestEntrySize = 789; |
34 | 34 |
| 35 uint32_t RoundSize(uint32_t in) { |
| 36 return (in + 255u) & 0xFFFFFF00; |
| 37 } |
| 38 |
35 } // namespace | 39 } // namespace |
36 | 40 |
37 class EntryMetadataTest : public testing::Test { | 41 class EntryMetadataTest : public testing::Test { |
38 public: | 42 public: |
39 EntryMetadata NewEntryMetadataWithValues() { | 43 EntryMetadata NewEntryMetadataWithValues() { |
40 return EntryMetadata(kTestLastUsedTime, kTestEntrySize); | 44 return EntryMetadata(kTestLastUsedTime, kTestEntrySize); |
41 } | 45 } |
42 | 46 |
43 void CheckEntryMetadataValues(const EntryMetadata& entry_metadata) { | 47 void CheckEntryMetadataValues(const EntryMetadata& entry_metadata) { |
44 EXPECT_LT(kTestLastUsedTime - base::TimeDelta::FromSeconds(2), | 48 EXPECT_LT(kTestLastUsedTime - base::TimeDelta::FromSeconds(2), |
45 entry_metadata.GetLastUsedTime()); | 49 entry_metadata.GetLastUsedTime()); |
46 EXPECT_GT(kTestLastUsedTime + base::TimeDelta::FromSeconds(2), | 50 EXPECT_GT(kTestLastUsedTime + base::TimeDelta::FromSeconds(2), |
47 entry_metadata.GetLastUsedTime()); | 51 entry_metadata.GetLastUsedTime()); |
48 EXPECT_EQ(kTestEntrySize, entry_metadata.GetEntrySize()); | 52 EXPECT_EQ(RoundSize(kTestEntrySize), entry_metadata.GetEntrySize()); |
49 } | 53 } |
50 }; | 54 }; |
51 | 55 |
52 class MockSimpleIndexFile : public SimpleIndexFile, | 56 class MockSimpleIndexFile : public SimpleIndexFile, |
53 public base::SupportsWeakPtr<MockSimpleIndexFile> { | 57 public base::SupportsWeakPtr<MockSimpleIndexFile> { |
54 public: | 58 public: |
55 MockSimpleIndexFile() | 59 MockSimpleIndexFile() |
56 : SimpleIndexFile(NULL, NULL, net::DISK_CACHE, base::FilePath()), | 60 : SimpleIndexFile(NULL, NULL, net::DISK_CACHE, base::FilePath()), |
57 load_result_(NULL), | 61 load_result_(NULL), |
58 load_index_entries_calls_(0), | 62 load_index_entries_calls_(0), |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 210 } |
207 | 211 |
208 TEST_F(EntryMetadataTest, Serialize) { | 212 TEST_F(EntryMetadataTest, Serialize) { |
209 EntryMetadata entry_metadata = NewEntryMetadataWithValues(); | 213 EntryMetadata entry_metadata = NewEntryMetadataWithValues(); |
210 | 214 |
211 base::Pickle pickle; | 215 base::Pickle pickle; |
212 entry_metadata.Serialize(&pickle); | 216 entry_metadata.Serialize(&pickle); |
213 | 217 |
214 base::PickleIterator it(pickle); | 218 base::PickleIterator it(pickle); |
215 EntryMetadata new_entry_metadata; | 219 EntryMetadata new_entry_metadata; |
216 new_entry_metadata.Deserialize(&it); | 220 new_entry_metadata.Deserialize(&it, true); |
217 CheckEntryMetadataValues(new_entry_metadata); | 221 CheckEntryMetadataValues(new_entry_metadata); |
| 222 |
| 223 // ### test old format. |
218 } | 224 } |
219 | 225 |
220 TEST_F(SimpleIndexTest, IndexSizeCorrectOnMerge) { | 226 TEST_F(SimpleIndexTest, IndexSizeCorrectOnMerge) { |
| 227 #if 0 |
221 index()->SetMaxSize(100); | 228 index()->SetMaxSize(100); |
222 index()->Insert(hashes_.at<2>()); | 229 index()->Insert(hashes_.at<2>()); |
223 index()->UpdateEntrySize(hashes_.at<2>(), 2u); | 230 index()->UpdateEntrySize(hashes_.at<2>(), 2u); |
224 index()->Insert(hashes_.at<3>()); | 231 index()->Insert(hashes_.at<3>()); |
225 index()->UpdateEntrySize(hashes_.at<3>(), 3u); | 232 index()->UpdateEntrySize(hashes_.at<3>(), 3u); |
226 index()->Insert(hashes_.at<4>()); | 233 index()->Insert(hashes_.at<4>()); |
227 index()->UpdateEntrySize(hashes_.at<4>(), 4u); | 234 index()->UpdateEntrySize(hashes_.at<4>(), 4u); |
228 EXPECT_EQ(9U, index()->cache_size_); | 235 EXPECT_EQ(9U, index()->cache_size_); |
229 { | 236 { |
230 std::unique_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); | 237 std::unique_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); |
231 result->did_load = true; | 238 result->did_load = true; |
232 index()->MergeInitializingSet(std::move(result)); | 239 index()->MergeInitializingSet(std::move(result)); |
233 } | 240 } |
234 EXPECT_EQ(9U, index()->cache_size_); | 241 EXPECT_EQ(9U, index()->cache_size_); |
235 { | 242 { |
236 std::unique_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); | 243 std::unique_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); |
237 result->did_load = true; | 244 result->did_load = true; |
238 const uint64_t new_hash_key = hashes_.at<11>(); | 245 const uint64_t new_hash_key = hashes_.at<11>(); |
239 result->entries.insert( | 246 result->entries.insert( |
240 std::make_pair(new_hash_key, EntryMetadata(base::Time::Now(), 11u))); | 247 std::make_pair(new_hash_key, EntryMetadata(base::Time::Now(), 11u))); |
241 const uint64_t redundant_hash_key = hashes_.at<4>(); | 248 const uint64_t redundant_hash_key = hashes_.at<4>(); |
242 result->entries.insert(std::make_pair( | 249 result->entries.insert(std::make_pair( |
243 redundant_hash_key, EntryMetadata(base::Time::Now(), 4u))); | 250 redundant_hash_key, EntryMetadata(base::Time::Now(), 4u))); |
244 index()->MergeInitializingSet(std::move(result)); | 251 index()->MergeInitializingSet(std::move(result)); |
245 } | 252 } |
246 EXPECT_EQ(2U + 3U + 4U + 11U, index()->cache_size_); | 253 EXPECT_EQ(2U + 3U + 4U + 11U, index()->cache_size_); |
| 254 #endif |
247 } | 255 } |
248 | 256 |
249 // State of index changes as expected with an insert and a remove. | 257 // State of index changes as expected with an insert and a remove. |
250 TEST_F(SimpleIndexTest, BasicInsertRemove) { | 258 TEST_F(SimpleIndexTest, BasicInsertRemove) { |
251 // Confirm blank state. | 259 // Confirm blank state. |
252 EntryMetadata metadata; | 260 EntryMetadata metadata; |
253 EXPECT_EQ(base::Time(), metadata.GetLastUsedTime()); | 261 EXPECT_EQ(base::Time(), metadata.GetLastUsedTime()); |
254 EXPECT_EQ(0U, metadata.GetEntrySize()); | 262 EXPECT_EQ(0U, metadata.GetEntrySize()); |
255 | 263 |
256 // Confirm state after insert. | 264 // Confirm state after insert. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 ReturnIndexFile(); | 351 ReturnIndexFile(); |
344 | 352 |
345 EntryMetadata metadata; | 353 EntryMetadata metadata; |
346 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); | 354 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); |
347 EXPECT_LT( | 355 EXPECT_LT( |
348 now - base::TimeDelta::FromDays(2) - base::TimeDelta::FromSeconds(1), | 356 now - base::TimeDelta::FromDays(2) - base::TimeDelta::FromSeconds(1), |
349 metadata.GetLastUsedTime()); | 357 metadata.GetLastUsedTime()); |
350 EXPECT_GT( | 358 EXPECT_GT( |
351 now - base::TimeDelta::FromDays(2) + base::TimeDelta::FromSeconds(1), | 359 now - base::TimeDelta::FromDays(2) + base::TimeDelta::FromSeconds(1), |
352 metadata.GetLastUsedTime()); | 360 metadata.GetLastUsedTime()); |
353 EXPECT_EQ(475U, metadata.GetEntrySize()); | 361 EXPECT_EQ(RoundSize(475U), metadata.GetEntrySize()); |
354 | 362 |
355 index()->UpdateEntrySize(kHash1, 600u); | 363 index()->UpdateEntrySize(kHash1, 600u); |
356 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); | 364 EXPECT_TRUE(GetEntryForTesting(kHash1, &metadata)); |
357 EXPECT_EQ(600U, metadata.GetEntrySize()); | 365 EXPECT_EQ(RoundSize(600U), metadata.GetEntrySize()); |
358 EXPECT_EQ(1, index()->GetEntryCount()); | 366 EXPECT_EQ(1, index()->GetEntryCount()); |
359 } | 367 } |
360 | 368 |
361 TEST_F(SimpleIndexTest, GetEntryCount) { | 369 TEST_F(SimpleIndexTest, GetEntryCount) { |
362 EXPECT_EQ(0, index()->GetEntryCount()); | 370 EXPECT_EQ(0, index()->GetEntryCount()); |
363 index()->Insert(hashes_.at<1>()); | 371 index()->Insert(hashes_.at<1>()); |
364 EXPECT_EQ(1, index()->GetEntryCount()); | 372 EXPECT_EQ(1, index()->GetEntryCount()); |
365 index()->Insert(hashes_.at<2>()); | 373 index()->Insert(hashes_.at<2>()); |
366 EXPECT_EQ(2, index()->GetEntryCount()); | 374 EXPECT_EQ(2, index()->GetEntryCount()); |
367 index()->Insert(hashes_.at<3>()); | 375 index()->Insert(hashes_.at<3>()); |
(...skipping 14 matching lines...) Expand all Loading... |
382 EXPECT_EQ(0, index()->GetEntryCount()); | 390 EXPECT_EQ(0, index()->GetEntryCount()); |
383 } | 391 } |
384 | 392 |
385 // Confirm that we get the results we expect from a simple init. | 393 // Confirm that we get the results we expect from a simple init. |
386 TEST_F(SimpleIndexTest, BasicInit) { | 394 TEST_F(SimpleIndexTest, BasicInit) { |
387 base::Time now(base::Time::Now()); | 395 base::Time now(base::Time::Now()); |
388 | 396 |
389 InsertIntoIndexFileReturn(hashes_.at<1>(), | 397 InsertIntoIndexFileReturn(hashes_.at<1>(), |
390 now - base::TimeDelta::FromDays(2), | 398 now - base::TimeDelta::FromDays(2), |
391 10u); | 399 10u); |
392 InsertIntoIndexFileReturn(hashes_.at<2>(), | 400 InsertIntoIndexFileReturn(hashes_.at<2>(), now - base::TimeDelta::FromDays(3), |
393 now - base::TimeDelta::FromDays(3), | 401 1000u); |
394 100u); | |
395 | 402 |
396 ReturnIndexFile(); | 403 ReturnIndexFile(); |
397 | 404 |
398 EntryMetadata metadata; | 405 EntryMetadata metadata; |
399 EXPECT_TRUE(GetEntryForTesting(hashes_.at<1>(), &metadata)); | 406 EXPECT_TRUE(GetEntryForTesting(hashes_.at<1>(), &metadata)); |
400 EXPECT_LT( | 407 EXPECT_LT( |
401 now - base::TimeDelta::FromDays(2) - base::TimeDelta::FromSeconds(1), | 408 now - base::TimeDelta::FromDays(2) - base::TimeDelta::FromSeconds(1), |
402 metadata.GetLastUsedTime()); | 409 metadata.GetLastUsedTime()); |
403 EXPECT_GT( | 410 EXPECT_GT( |
404 now - base::TimeDelta::FromDays(2) + base::TimeDelta::FromSeconds(1), | 411 now - base::TimeDelta::FromDays(2) + base::TimeDelta::FromSeconds(1), |
405 metadata.GetLastUsedTime()); | 412 metadata.GetLastUsedTime()); |
406 EXPECT_EQ(10U, metadata.GetEntrySize()); | 413 EXPECT_EQ(RoundSize(10U), metadata.GetEntrySize()); |
407 EXPECT_TRUE(GetEntryForTesting(hashes_.at<2>(), &metadata)); | 414 EXPECT_TRUE(GetEntryForTesting(hashes_.at<2>(), &metadata)); |
408 EXPECT_LT( | 415 EXPECT_LT( |
409 now - base::TimeDelta::FromDays(3) - base::TimeDelta::FromSeconds(1), | 416 now - base::TimeDelta::FromDays(3) - base::TimeDelta::FromSeconds(1), |
410 metadata.GetLastUsedTime()); | 417 metadata.GetLastUsedTime()); |
411 EXPECT_GT( | 418 EXPECT_GT( |
412 now - base::TimeDelta::FromDays(3) + base::TimeDelta::FromSeconds(1), | 419 now - base::TimeDelta::FromDays(3) + base::TimeDelta::FromSeconds(1), |
413 metadata.GetLastUsedTime()); | 420 metadata.GetLastUsedTime()); |
414 EXPECT_EQ(100U, metadata.GetEntrySize()); | 421 EXPECT_EQ(RoundSize(1000U), metadata.GetEntrySize()); |
415 } | 422 } |
416 | 423 |
417 // Remove something that's going to come in from the loaded index. | 424 // Remove something that's going to come in from the loaded index. |
418 TEST_F(SimpleIndexTest, RemoveBeforeInit) { | 425 TEST_F(SimpleIndexTest, RemoveBeforeInit) { |
419 const uint64_t kHash1 = hashes_.at<1>(); | 426 const uint64_t kHash1 = hashes_.at<1>(); |
420 index()->Remove(kHash1); | 427 index()->Remove(kHash1); |
421 | 428 |
422 InsertIntoIndexFileReturn(kHash1, | 429 InsertIntoIndexFileReturn(kHash1, |
423 base::Time::Now() - base::TimeDelta::FromDays(2), | 430 base::Time::Now() - base::TimeDelta::FromDays(2), |
424 10u); | 431 10u); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 | 532 |
526 EXPECT_TRUE(GetEntryForTesting(hashes_.at<5>(), &metadata)); | 533 EXPECT_TRUE(GetEntryForTesting(hashes_.at<5>(), &metadata)); |
527 | 534 |
528 EXPECT_GT( | 535 EXPECT_GT( |
529 now - base::TimeDelta::FromDays(6) + base::TimeDelta::FromSeconds(1), | 536 now - base::TimeDelta::FromDays(6) + base::TimeDelta::FromSeconds(1), |
530 metadata.GetLastUsedTime()); | 537 metadata.GetLastUsedTime()); |
531 EXPECT_LT( | 538 EXPECT_LT( |
532 now - base::TimeDelta::FromDays(6) - base::TimeDelta::FromSeconds(1), | 539 now - base::TimeDelta::FromDays(6) - base::TimeDelta::FromSeconds(1), |
533 metadata.GetLastUsedTime()); | 540 metadata.GetLastUsedTime()); |
534 | 541 |
535 EXPECT_EQ(100000U, metadata.GetEntrySize()); | 542 EXPECT_EQ(RoundSize(100000U), metadata.GetEntrySize()); |
536 } | 543 } |
537 | 544 |
538 TEST_F(SimpleIndexTest, BasicEviction) { | 545 TEST_F(SimpleIndexTest, BasicEviction) { |
539 base::Time now(base::Time::Now()); | 546 base::Time now(base::Time::Now()); |
540 index()->SetMaxSize(1000); | 547 index()->SetMaxSize(1000); |
541 InsertIntoIndexFileReturn(hashes_.at<1>(), | 548 InsertIntoIndexFileReturn(hashes_.at<1>(), |
542 now - base::TimeDelta::FromDays(2), | 549 now - base::TimeDelta::FromDays(2), |
543 475u); | 550 475u); |
544 index()->Insert(hashes_.at<2>()); | 551 index()->Insert(hashes_.at<2>()); |
545 index()->UpdateEntrySize(hashes_.at<2>(), 475u); | 552 index()->UpdateEntrySize(hashes_.at<2>(), 475u); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 SimpleIndex::EntrySet entry_set; | 621 SimpleIndex::EntrySet entry_set; |
615 index_file_->GetAndResetDiskWriteEntrySet(&entry_set); | 622 index_file_->GetAndResetDiskWriteEntrySet(&entry_set); |
616 | 623 |
617 uint64_t hash_key = kHash1; | 624 uint64_t hash_key = kHash1; |
618 base::Time now(base::Time::Now()); | 625 base::Time now(base::Time::Now()); |
619 ASSERT_EQ(1u, entry_set.size()); | 626 ASSERT_EQ(1u, entry_set.size()); |
620 EXPECT_EQ(hash_key, entry_set.begin()->first); | 627 EXPECT_EQ(hash_key, entry_set.begin()->first); |
621 const EntryMetadata& entry1(entry_set.begin()->second); | 628 const EntryMetadata& entry1(entry_set.begin()->second); |
622 EXPECT_LT(now - base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); | 629 EXPECT_LT(now - base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); |
623 EXPECT_GT(now + base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); | 630 EXPECT_GT(now + base::TimeDelta::FromMinutes(1), entry1.GetLastUsedTime()); |
624 EXPECT_EQ(20U, entry1.GetEntrySize()); | 631 EXPECT_EQ(RoundSize(20U), entry1.GetEntrySize()); |
625 } | 632 } |
626 | 633 |
627 TEST_F(SimpleIndexTest, DiskWritePostponed) { | 634 TEST_F(SimpleIndexTest, DiskWritePostponed) { |
628 index()->SetMaxSize(1000); | 635 index()->SetMaxSize(1000); |
629 ReturnIndexFile(); | 636 ReturnIndexFile(); |
630 | 637 |
631 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); | 638 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); |
632 | 639 |
633 index()->Insert(hashes_.at<1>()); | 640 index()->Insert(hashes_.at<1>()); |
634 index()->UpdateEntrySize(hashes_.at<1>(), 20u); | 641 index()->UpdateEntrySize(hashes_.at<1>(), 20u); |
635 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 642 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
636 base::TimeTicks expected_trigger( | 643 base::TimeTicks expected_trigger( |
637 index()->write_to_disk_timer_.desired_run_time()); | 644 index()->write_to_disk_timer_.desired_run_time()); |
638 | 645 |
639 WaitForTimeChange(); | 646 WaitForTimeChange(); |
640 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 647 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
641 index()->Insert(hashes_.at<2>()); | 648 index()->Insert(hashes_.at<2>()); |
642 index()->UpdateEntrySize(hashes_.at<2>(), 40u); | 649 index()->UpdateEntrySize(hashes_.at<2>(), 40u); |
643 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 650 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
644 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 651 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
645 index()->write_to_disk_timer_.Stop(); | 652 index()->write_to_disk_timer_.Stop(); |
646 } | 653 } |
647 | 654 |
648 } // namespace disk_cache | 655 } // namespace disk_cache |
OLD | NEW |