Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / rappor / log_uploader_unittest.cc
blob1bd9d62da48d34a7ff6e9198e419946676ebe394
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/rappor/log_uploader.h"
7 #include "base/compiler_specific.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "net/url_request/test_url_fetcher_factory.h"
10 #include "net/url_request/url_request_test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace rappor {
15 namespace {
17 const char kTestServerURL[] = "http://a.com/";
18 const char kTestMimeType[] = "text/plain";
20 class TestLogUploader : public LogUploader {
21 public:
22 TestLogUploader(net::URLRequestContextGetter* request_context) :
23 LogUploader(GURL(kTestServerURL), kTestMimeType, request_context) {
24 Start();
27 base::TimeDelta last_interval_set() const { return last_interval_set_; };
29 void StartUpload() {
30 last_interval_set_ = base::TimeDelta();
31 StartScheduledUpload();
34 static base::TimeDelta BackOff(base::TimeDelta t) {
35 return LogUploader::BackOffUploadInterval(t);
38 protected:
39 bool IsUploadScheduled() const override {
40 return last_interval_set() != base::TimeDelta();
43 // Schedules a future call to StartScheduledUpload if one isn't already
44 // pending.
45 void ScheduleNextUpload(base::TimeDelta interval) override {
46 EXPECT_EQ(last_interval_set(), base::TimeDelta());
47 last_interval_set_ = interval;
50 base::TimeDelta last_interval_set_;
52 DISALLOW_COPY_AND_ASSIGN(TestLogUploader);
55 } // namespace
57 class LogUploaderTest : public testing::Test {
58 public:
59 LogUploaderTest()
60 : request_context_(new net::TestURLRequestContextGetter(
61 base::ThreadTaskRunnerHandle::Get())),
62 factory_(NULL) {}
64 protected:
65 // Required for base::ThreadTaskRunnerHandle::Get().
66 base::MessageLoopForUI loop_;
67 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
68 net::FakeURLFetcherFactory factory_;
70 DISALLOW_COPY_AND_ASSIGN(LogUploaderTest);
73 TEST_F(LogUploaderTest, Success) {
74 TestLogUploader uploader(request_context_.get());
76 factory_.SetFakeResponse(GURL(kTestServerURL),
77 std::string(),
78 net::HTTP_OK,
79 net::URLRequestStatus::SUCCESS);
81 uploader.QueueLog("log1");
82 base::MessageLoop::current()->RunUntilIdle();
83 // Log should be discarded instead of retransmitted.
84 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta());
87 TEST_F(LogUploaderTest, Rejection) {
88 TestLogUploader uploader(request_context_.get());
90 factory_.SetFakeResponse(GURL(kTestServerURL),
91 std::string(),
92 net::HTTP_BAD_REQUEST,
93 net::URLRequestStatus::SUCCESS);
95 uploader.QueueLog("log1");
96 base::MessageLoop::current()->RunUntilIdle();
97 // Log should be discarded instead of retransmitted.
98 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta());
101 TEST_F(LogUploaderTest, Failure) {
102 TestLogUploader uploader(request_context_.get());
104 factory_.SetFakeResponse(GURL(kTestServerURL),
105 std::string(),
106 net::HTTP_INTERNAL_SERVER_ERROR,
107 net::URLRequestStatus::SUCCESS);
109 uploader.QueueLog("log1");
110 base::MessageLoop::current()->RunUntilIdle();
111 // Log should be scheduled for retransmission.
112 base::TimeDelta error_interval = uploader.last_interval_set();
113 EXPECT_GT(error_interval, base::TimeDelta());
115 for (int i = 0; i < 10; i++) {
116 uploader.QueueLog("logX");
119 // A second failure should lead to a longer interval, and the log should
120 // be discarded due to full queue.
121 uploader.StartUpload();
122 base::MessageLoop::current()->RunUntilIdle();
123 EXPECT_GT(uploader.last_interval_set(), error_interval);
125 factory_.SetFakeResponse(GURL(kTestServerURL),
126 std::string(),
127 net::HTTP_OK,
128 net::URLRequestStatus::SUCCESS);
130 // A success should revert to base interval while queue is not empty.
131 for (int i = 0; i < 9; i++) {
132 uploader.StartUpload();
133 base::MessageLoop::current()->RunUntilIdle();
134 EXPECT_LT(uploader.last_interval_set(), error_interval);
137 // Queue should be empty.
138 uploader.StartUpload();
139 base::MessageLoop::current()->RunUntilIdle();
140 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta());
143 TEST_F(LogUploaderTest, Backoff) {
144 base::TimeDelta current = base::TimeDelta();
145 base::TimeDelta next = base::TimeDelta::FromSeconds(1);
146 // Backoff until the maximum is reached.
147 while (next > current) {
148 current = next;
149 next = TestLogUploader::BackOff(current);
151 // Maximum backoff should have been reached.
152 EXPECT_EQ(next, current);
155 } // namespace rappor