Chromium Code Reviews| 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> |
| 11 | 11 |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/hash.h" | 13 #include "base/hash.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/field_trial.h" | |
| 16 #include "base/metrics/field_trial_param_associator.h" | |
| 15 #include "base/pickle.h" | 17 #include "base/pickle.h" |
| 16 #include "base/sha1.h" | 18 #include "base/sha1.h" |
| 17 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 18 #include "base/task_runner.h" | 20 #include "base/task_runner.h" |
| 21 #include "base/test/mock_entropy_provider.h" | |
| 22 #include "base/test/scoped_feature_list.h" | |
| 19 #include "base/threading/platform_thread.h" | 23 #include "base/threading/platform_thread.h" |
| 20 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 21 #include "net/base/cache_type.h" | 25 #include "net/base/cache_type.h" |
| 26 #include "net/disk_cache/simple/simple_experiment.h" | |
| 22 #include "net/disk_cache/simple/simple_index_delegate.h" | 27 #include "net/disk_cache/simple/simple_index_delegate.h" |
| 23 #include "net/disk_cache/simple/simple_index_file.h" | 28 #include "net/disk_cache/simple/simple_index_file.h" |
| 24 #include "net/disk_cache/simple/simple_test_util.h" | 29 #include "net/disk_cache/simple/simple_test_util.h" |
| 25 #include "net/disk_cache/simple/simple_util.h" | 30 #include "net/disk_cache/simple/simple_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 32 |
| 28 namespace disk_cache { | 33 namespace disk_cache { |
| 29 namespace { | 34 namespace { |
| 30 | 35 |
| 31 const base::Time kTestLastUsedTime = | 36 const base::Time kTestLastUsedTime = |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 // that. | 566 // that. |
| 562 index()->UpdateEntrySize(hashes_.at<3>(), 475u); | 567 index()->UpdateEntrySize(hashes_.at<3>(), 475u); |
| 563 EXPECT_EQ(1, doom_entries_calls()); | 568 EXPECT_EQ(1, doom_entries_calls()); |
| 564 EXPECT_EQ(1, index()->GetEntryCount()); | 569 EXPECT_EQ(1, index()->GetEntryCount()); |
| 565 EXPECT_FALSE(index()->Has(hashes_.at<1>())); | 570 EXPECT_FALSE(index()->Has(hashes_.at<1>())); |
| 566 EXPECT_FALSE(index()->Has(hashes_.at<2>())); | 571 EXPECT_FALSE(index()->Has(hashes_.at<2>())); |
| 567 EXPECT_TRUE(index()->Has(hashes_.at<3>())); | 572 EXPECT_TRUE(index()->Has(hashes_.at<3>())); |
| 568 ASSERT_EQ(2u, last_doom_entry_hashes().size()); | 573 ASSERT_EQ(2u, last_doom_entry_hashes().size()); |
| 569 } | 574 } |
| 570 | 575 |
| 576 TEST_F(SimpleIndexTest, EvictBySize) { | |
| 577 // Enable size-based eviction. | |
| 578 const std::string kTrialName = "EvictWithSizeTrial"; | |
| 579 const std::string kGroupName = "GroupFoo"; // Value not used | |
| 580 | |
| 581 std::unique_ptr<base::FieldTrialList> field_trial_list( | |
| 582 base::MakeUnique<base::FieldTrialList>( | |
| 583 base::MakeUnique<base::MockEntropyProvider>())); | |
| 584 scoped_refptr<base::FieldTrial> trial = | |
| 585 base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName); | |
| 586 | |
| 587 std::map<std::string, std::string> params; | |
| 588 params[kSizeEvictionParam] = "1"; | |
| 589 base::FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( | |
| 590 kTrialName, kGroupName, params); | |
| 591 | |
| 592 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 593 feature_list->RegisterFieldTrialOverride( | |
| 594 kSimpleCacheEvictionWithSizeExperiment.name, | |
| 595 base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get()); | |
| 596 base::test::ScopedFeatureList scoped_feature_list; | |
| 597 scoped_feature_list.InitWithFeatureList(std::move(feature_list)); | |
| 598 | |
| 599 // Enabled, now we can run the actual test. | |
| 600 base::Time now(base::Time::Now()); | |
| 601 index()->SetMaxSize(50000); | |
| 602 InsertIntoIndexFileReturn(hashes_.at<1>(), now - base::TimeDelta::FromDays(2), | |
| 603 475u); | |
| 604 InsertIntoIndexFileReturn(hashes_.at<2>(), now - base::TimeDelta::FromDays(1), | |
| 605 40000u); | |
|
pasko
2017/07/18 15:30:01
Perhaps make it depend on the implementation in a
hubbe
2017/07/18 18:27:40
Making the test tightly coupled to the implementat
pasko
2017/07/19 14:42:28
OK
| |
| 606 ReturnIndexFile(); | |
| 607 WaitForTimeChange(); | |
| 608 | |
| 609 index()->Insert(hashes_.at<3>()); | |
| 610 // Confirm index is as expected: No eviction, everything there. | |
| 611 EXPECT_EQ(3, index()->GetEntryCount()); | |
| 612 EXPECT_EQ(0, doom_entries_calls()); | |
| 613 EXPECT_TRUE(index()->Has(hashes_.at<1>())); | |
| 614 EXPECT_TRUE(index()->Has(hashes_.at<2>())); | |
| 615 EXPECT_TRUE(index()->Has(hashes_.at<3>())); | |
| 616 | |
| 617 // Trigger an eviction, and make sure the right things are tossed. | |
| 618 // TODO(rdsmith): This is dependent on the innards of the implementation | |
|
pasko
2017/07/18 15:30:01
rdsmith: what do you mean? Surely eviction trigger
hubbe
2017/07/18 18:27:40
Sure, but what if we PostTask the eviction? Or do
pasko
2017/07/19 14:42:28
Ah, so you mean in the proper design the test woul
hubbe
2017/07/19 17:50:33
I consider it more of an FYI than a TODO to make s
| |
| 619 // as to at exactly what point we trigger eviction. Not sure how to fix | |
| 620 // that. | |
| 621 index()->UpdateEntrySize(hashes_.at<3>(), 40000u); | |
| 622 EXPECT_EQ(1, doom_entries_calls()); | |
| 623 EXPECT_EQ(2, index()->GetEntryCount()); | |
| 624 EXPECT_TRUE(index()->Has(hashes_.at<1>())); | |
| 625 EXPECT_FALSE(index()->Has(hashes_.at<2>())); | |
|
pasko
2017/07/18 15:30:01
maybe also test the case when the smaller and olde
hubbe
2017/07/18 18:27:40
Done.
| |
| 626 EXPECT_TRUE(index()->Has(hashes_.at<3>())); | |
| 627 ASSERT_EQ(1u, last_doom_entry_hashes().size()); | |
| 628 } | |
| 629 | |
| 571 // Confirm all the operations queue a disk write at some point in the | 630 // Confirm all the operations queue a disk write at some point in the |
| 572 // future. | 631 // future. |
| 573 TEST_F(SimpleIndexTest, DiskWriteQueued) { | 632 TEST_F(SimpleIndexTest, DiskWriteQueued) { |
| 574 index()->SetMaxSize(1000); | 633 index()->SetMaxSize(1000); |
| 575 ReturnIndexFile(); | 634 ReturnIndexFile(); |
| 576 | 635 |
| 577 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); | 636 EXPECT_FALSE(index()->write_to_disk_timer_.IsRunning()); |
| 578 | 637 |
| 579 const uint64_t kHash1 = hashes_.at<1>(); | 638 const uint64_t kHash1 = hashes_.at<1>(); |
| 580 index()->Insert(kHash1); | 639 index()->Insert(kHash1); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 639 WaitForTimeChange(); | 698 WaitForTimeChange(); |
| 640 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 699 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
| 641 index()->Insert(hashes_.at<2>()); | 700 index()->Insert(hashes_.at<2>()); |
| 642 index()->UpdateEntrySize(hashes_.at<2>(), 40u); | 701 index()->UpdateEntrySize(hashes_.at<2>(), 40u); |
| 643 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 702 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
| 644 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 703 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
| 645 index()->write_to_disk_timer_.Stop(); | 704 index()->write_to_disk_timer_.Stop(); |
| 646 } | 705 } |
| 647 | 706 |
| 648 } // namespace disk_cache | 707 } // namespace disk_cache |
| OLD | NEW |