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

Unified Diff: components/ntp_snippets/request_throttler_unittest.cc

Issue 2158843002: Introduce a request throttler for limiting requests in mobile NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tim's comments (renaming) Created 4 years, 5 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
Index: components/ntp_snippets/request_throttler_unittest.cc
diff --git a/components/ntp_snippets/request_throttler_unittest.cc b/components/ntp_snippets/request_throttler_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e75295e996304e3f1414e0fd47bb43eae746264c
--- /dev/null
+++ b/components/ntp_snippets/request_throttler_unittest.cc
@@ -0,0 +1,71 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/ntp_snippets/request_throttler.h"
+
+#include <memory>
+
+#include "base/strings/stringprintf.h"
+#include "components/ntp_snippets/pref_names.h"
+#include "components/prefs/pref_registry_simple.h"
+#include "components/prefs/testing_pref_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+const int kCounterQuota = 2;
+} // namespace
+
+namespace ntp_snippets {
+
+class RequestThrottlerTest : public testing::Test {
+ public:
+ RequestThrottlerTest() {
+ RequestThrottler::RegisterProfilePrefs(test_prefs_.registry());
+ // Use any arbitrary RequestType for this unittest.
+ throttler_.reset(new RequestThrottler(
+ &test_prefs_, RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER,
+ kCounterQuota));
+ }
+
+ protected:
+ TestingPrefServiceSimple test_prefs_;
+ std::unique_ptr<RequestThrottler> throttler_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(RequestThrottlerTest);
+};
+
+TEST_F(RequestThrottlerTest, QuotaExceeded) {
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+ EXPECT_FALSE(throttler_->DemandQuotaForRequest(false));
+}
+
+TEST_F(RequestThrottlerTest, ForcedDoesNotCountInQuota) {
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(true));
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+}
+
+TEST_F(RequestThrottlerTest, ForcedWorksRegardlessOfQuota) {
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(true));
+}
+
+TEST_F(RequestThrottlerTest, QuotaIsPerDay) {
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+
+ // Now fake the day pref so that the counter believes the count comes from
+ // yesterday.
+ int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
+ test_prefs_.SetInteger(ntp_snippets::prefs::kSnippetFetcherQuotaDay,
+ now_day - 1);
+
+ // The quota should get reset as the day has changed.
+ EXPECT_TRUE(throttler_->DemandQuotaForRequest(false));
+}
+
+} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698