Chromium Code Reviews| Index: net/reporting/reporting_cache_unittest.cc |
| diff --git a/net/reporting/reporting_cache_unittest.cc b/net/reporting/reporting_cache_unittest.cc |
| index 73023400392fa0f02b19c874ec057e609842176f..19229db22e766c6f1edaceed8aca2c9c8238b4d9 100644 |
| --- a/net/reporting/reporting_cache_unittest.cc |
| +++ b/net/reporting/reporting_cache_unittest.cc |
| @@ -7,6 +7,7 @@ |
| #include <string> |
| #include "base/memory/ptr_util.h" |
| +#include "base/test/simple_test_tick_clock.h" |
| #include "base/time/time.h" |
| #include "base/values.h" |
| #include "net/reporting/reporting_client.h" |
| @@ -42,6 +43,12 @@ class ReportingCacheTest : public ReportingTestBase { |
| TestReportingObserver* observer() { return &observer_; } |
| + size_t report_count() { |
| + std::vector<const ReportingReport*> reports; |
| + cache()->GetReports(&reports); |
| + return reports.size(); |
| + } |
| + |
| const GURL kUrl1_ = GURL("https://origin1/path"); |
| const url::Origin kOrigin1_ = url::Origin(GURL("https://origin1/")); |
| const url::Origin kOrigin2_ = url::Origin(GURL("https://origin2/")); |
| @@ -400,5 +407,65 @@ TEST_F(ReportingCacheTest, IncludeSubdomainsPreferMoreSpecificSuperdomain) { |
| EXPECT_EQ(kSuperOrigin, clients[0]->origin); |
| } |
| +TEST_F(ReportingCacheTest, EvictOldest) { |
| + ASSERT_LT(0u, policy().max_report_count); |
| + ASSERT_GT(std::numeric_limits<size_t>::max(), policy().max_report_count); |
| + |
| + base::TimeTicks earliest_queued = tick_clock()->NowTicks(); |
| + |
| + // Enqueue the maximum number of reports, spaced apart in time. |
| + for (size_t i = 0; i < policy().max_report_count; ++i) { |
| + cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| + base::MakeUnique<base::DictionaryValue>(), |
| + tick_clock()->NowTicks(), 0); |
|
shivanisha
2017/05/09 13:58:27
Its possible for earliest_queued to be different t
Julia Tuttle
2017/05/09 14:37:22
tick_clock() refers to the SimpleTestTickClock cre
|
| + tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); |
| + } |
| + EXPECT_EQ(policy().max_report_count, report_count()); |
| + |
| + // Add one more report to force the cache to evict one. |
| + cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| + base::MakeUnique<base::DictionaryValue>(), kNow_, 0); |
| + |
| + // Make sure the cache evicted a report to make room for the new one, and make |
| + // sure the report evicted was the earliest-queued one. |
| + std::vector<const ReportingReport*> reports; |
| + cache()->GetReports(&reports); |
| + EXPECT_EQ(policy().max_report_count, reports.size()); |
| + for (const ReportingReport* report : reports) |
| + EXPECT_NE(earliest_queued, report->queued); |
| +} |
| + |
| +TEST_F(ReportingCacheTest, DontEvictPendingReports) { |
| + ASSERT_LT(0u, policy().max_report_count); |
| + ASSERT_GT(std::numeric_limits<size_t>::max(), policy().max_report_count); |
| + |
| + // Enqueue the maximum number of reports, spaced apart in time. |
| + for (size_t i = 0; i < policy().max_report_count; ++i) { |
| + cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| + base::MakeUnique<base::DictionaryValue>(), |
| + tick_clock()->NowTicks(), 0); |
| + tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); |
| + } |
| + EXPECT_EQ(policy().max_report_count, report_count()); |
| + |
| + // Mark all of the queued reports pending. |
| + std::vector<const ReportingReport*> queued_reports; |
| + cache()->GetReports(&queued_reports); |
| + cache()->SetReportsPending(queued_reports); |
| + |
| + // Add one more report to force the cache to evict one. Since the cache has |
| + // only pending reports, it will be forced to evict the *new* report! |
| + cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| + base::MakeUnique<base::DictionaryValue>(), kNow_, 0); |
| + |
| + // Make sure the cache evicted a report, and make sure the report evicted was |
| + // the new, non-pending one. |
| + std::vector<const ReportingReport*> reports; |
| + cache()->GetReports(&reports); |
| + EXPECT_EQ(policy().max_report_count, reports.size()); |
| + for (const ReportingReport* report : reports) |
| + EXPECT_TRUE(cache()->IsReportPendingForTesting(report)); |
| +} |
| + |
| } // namespace |
| } // namespace net |