Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Unified Diff: net/reporting/reporting_cache_unittest.cc

Issue 2847813002: Reporting: Cap number of reports in cache. (Closed)
Patch Set: Make requested change. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/reporting/reporting_cache.cc ('k') | net/reporting/reporting_policy.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+ 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
« no previous file with comments | « net/reporting/reporting_cache.cc ('k') | net/reporting/reporting_policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698