Chromium Code Reviews| Index: net/disk_cache/simple/simple_index_unittest.cc |
| diff --git a/net/disk_cache/simple/simple_index_unittest.cc b/net/disk_cache/simple/simple_index_unittest.cc |
| index 9f7d4094e10e6fcded31e3fbb189b8b2334de86d..bfe892cc572bb3b5847ea9b84d5101d8c113cede 100644 |
| --- a/net/disk_cache/simple/simple_index_unittest.cc |
| +++ b/net/disk_cache/simple/simple_index_unittest.cc |
| @@ -12,13 +12,18 @@ |
| #include "base/files/scoped_temp_dir.h" |
| #include "base/hash.h" |
| #include "base/logging.h" |
| +#include "base/metrics/field_trial.h" |
| +#include "base/metrics/field_trial_param_associator.h" |
| #include "base/pickle.h" |
| #include "base/sha1.h" |
| #include "base/strings/stringprintf.h" |
| #include "base/task_runner.h" |
| +#include "base/test/mock_entropy_provider.h" |
| +#include "base/test/scoped_feature_list.h" |
| #include "base/threading/platform_thread.h" |
| #include "base/time/time.h" |
| #include "net/base/cache_type.h" |
| +#include "net/disk_cache/simple/simple_experiment.h" |
| #include "net/disk_cache/simple/simple_index_delegate.h" |
| #include "net/disk_cache/simple/simple_index_file.h" |
| #include "net/disk_cache/simple/simple_test_util.h" |
| @@ -568,6 +573,60 @@ TEST_F(SimpleIndexTest, BasicEviction) { |
| ASSERT_EQ(2u, last_doom_entry_hashes().size()); |
| } |
| +TEST_F(SimpleIndexTest, EvictBySize) { |
| + // Enable size-based eviction. |
| + const std::string kTrialName = "EvictWithSizeTrial"; |
| + const std::string kGroupName = "GroupFoo"; // Value not used |
| + |
| + std::unique_ptr<base::FieldTrialList> field_trial_list( |
| + base::MakeUnique<base::FieldTrialList>( |
| + base::MakeUnique<base::MockEntropyProvider>())); |
| + scoped_refptr<base::FieldTrial> trial = |
| + base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName); |
| + |
| + std::map<std::string, std::string> params; |
| + params[kSizeEvictionParam] = "1"; |
| + base::FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams( |
| + kTrialName, kGroupName, params); |
| + |
| + std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| + feature_list->RegisterFieldTrialOverride( |
| + kSimpleCacheEvictionWithSizeExperiment.name, |
| + base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get()); |
| + base::test::ScopedFeatureList scoped_feature_list; |
| + scoped_feature_list.InitWithFeatureList(std::move(feature_list)); |
| + |
| + // Enabled, now we can run the actual test. |
| + base::Time now(base::Time::Now()); |
| + index()->SetMaxSize(50000); |
| + InsertIntoIndexFileReturn(hashes_.at<1>(), now - base::TimeDelta::FromDays(2), |
| + 475u); |
| + InsertIntoIndexFileReturn(hashes_.at<2>(), now - base::TimeDelta::FromDays(1), |
| + 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
|
| + ReturnIndexFile(); |
| + WaitForTimeChange(); |
| + |
| + index()->Insert(hashes_.at<3>()); |
| + // Confirm index is as expected: No eviction, everything there. |
| + EXPECT_EQ(3, index()->GetEntryCount()); |
| + EXPECT_EQ(0, doom_entries_calls()); |
| + EXPECT_TRUE(index()->Has(hashes_.at<1>())); |
| + EXPECT_TRUE(index()->Has(hashes_.at<2>())); |
| + EXPECT_TRUE(index()->Has(hashes_.at<3>())); |
| + |
| + // Trigger an eviction, and make sure the right things are tossed. |
| + // 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
|
| + // as to at exactly what point we trigger eviction. Not sure how to fix |
| + // that. |
| + index()->UpdateEntrySize(hashes_.at<3>(), 40000u); |
| + EXPECT_EQ(1, doom_entries_calls()); |
| + EXPECT_EQ(2, index()->GetEntryCount()); |
| + EXPECT_TRUE(index()->Has(hashes_.at<1>())); |
| + 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.
|
| + EXPECT_TRUE(index()->Has(hashes_.at<3>())); |
| + ASSERT_EQ(1u, last_doom_entry_hashes().size()); |
| +} |
| + |
| // Confirm all the operations queue a disk write at some point in the |
| // future. |
| TEST_F(SimpleIndexTest, DiskWriteQueued) { |