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

Side by Side Diff: components/rappor/log_uploader_unittest.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 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 unified diff | Download patch
« no previous file with comments | « components/rappor/log_uploader.cc ('k') | components/rappor/proto/rappor_metric.proto » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "components/rappor/log_uploader.h"
6
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "net/url_request/test_url_fetcher_factory.h"
9 #include "net/url_request/url_request_test_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace rappor {
13
14 namespace {
15
16 const char kTestServerURL[] = "http://a.com/";
17 const char kTestMimeType[] = "text/plain";
18
19 class TestLogUploader : public LogUploader {
20 public:
21 TestLogUploader(net::URLRequestContextGetter* request_context) :
22 LogUploader(GURL(kTestServerURL), kTestMimeType, request_context) {
23 }
24
25 base::TimeDelta last_interval_set() const { return last_interval_set_; };
26
27 void StartUpload() {
28 last_interval_set_ = base::TimeDelta();
29 StartScheduledUpload();
30 }
31
32 static base::TimeDelta BackOff(base::TimeDelta t) {
33 return LogUploader::BackOffUploadInterval(t);
34 }
35
36 protected:
37 virtual bool IsUploadScheduled() const OVERRIDE {
38 return last_interval_set() != base::TimeDelta();
39 }
40
41 // Schedules a future call to StartScheduledUpload if one isn't already
42 // pending.
43 virtual void ScheduleNextUpload(base::TimeDelta interval) OVERRIDE {
44 EXPECT_EQ(last_interval_set(), base::TimeDelta());
45 last_interval_set_ = interval;
46 }
47
48 base::TimeDelta last_interval_set_;
49
50 DISALLOW_COPY_AND_ASSIGN(TestLogUploader);
51 };
52
53 } // namespace
54
55 class LogUploaderTest : public testing::Test {
56 public:
57 LogUploaderTest()
58 : request_context_(new net::TestURLRequestContextGetter(
59 base::MessageLoopProxy::current())),
60 factory_(NULL) {}
61
62 protected:
63 // Required for base::MessageLoopProxy::current().
64 base::MessageLoopForUI loop_;
65 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
66 net::FakeURLFetcherFactory factory_;
67
68 DISALLOW_COPY_AND_ASSIGN(LogUploaderTest);
69 };
70
71 TEST_F(LogUploaderTest, Success) {
72 TestLogUploader uploader(request_context_);
73
74 factory_.SetFakeResponse(GURL(kTestServerURL),
75 std::string(),
76 net::HTTP_OK,
77 net::URLRequestStatus::SUCCESS);
78
79 uploader.QueueLog("log1");
80 base::MessageLoop::current()->RunUntilIdle();
81 // Log should be discarded instead of retransmitted.
82 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta());
83 }
84
85 TEST_F(LogUploaderTest, Rejection) {
86 TestLogUploader uploader(request_context_);
87
88 factory_.SetFakeResponse(GURL(kTestServerURL),
89 std::string(),
90 net::HTTP_BAD_REQUEST,
91 net::URLRequestStatus::SUCCESS);
92
93 uploader.QueueLog("log1");
94 base::MessageLoop::current()->RunUntilIdle();
95 // Log should be discarded instead of retransmitted.
96 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta());
97 }
98
99 TEST_F(LogUploaderTest, Failure) {
100 TestLogUploader uploader(request_context_);
101
102 factory_.SetFakeResponse(GURL(kTestServerURL),
103 std::string(),
104 net::HTTP_INTERNAL_SERVER_ERROR,
105 net::URLRequestStatus::SUCCESS);
106
107 uploader.QueueLog("log1");
108 base::MessageLoop::current()->RunUntilIdle();
109 // Log should be scheduled for retransmission.
110 base::TimeDelta error_interval = uploader.last_interval_set();
111 EXPECT_GT(error_interval, base::TimeDelta());
112
113 for (int i = 0; i < 10; i++) {
114 uploader.QueueLog("logX");
115 }
116
117 // A second failure should lead to a longer interval, and the log should
118 // be discarded due to full queue.
119 uploader.StartUpload();
120 base::MessageLoop::current()->RunUntilIdle();
121 EXPECT_GT(uploader.last_interval_set(), error_interval);
122
123 factory_.SetFakeResponse(GURL(kTestServerURL),
124 std::string(),
125 net::HTTP_OK,
126 net::URLRequestStatus::SUCCESS);
127
128 // A success should revert to base interval while queue is not empty.
129 for (int i = 0; i < 9; i++) {
130 uploader.StartUpload();
131 base::MessageLoop::current()->RunUntilIdle();
132 EXPECT_LT(uploader.last_interval_set(), error_interval);
133 }
134
135 // Queue should be empty.
136 uploader.StartUpload();
137 base::MessageLoop::current()->RunUntilIdle();
138 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta());
139 }
140
141 TEST_F(LogUploaderTest, Backoff) {
142 base::TimeDelta current = base::TimeDelta();
143 base::TimeDelta next = base::TimeDelta::FromSeconds(1);
144 // Backoff until the maximum is reached.
145 while (next > current) {
146 current = next;
147 next = TestLogUploader::BackOff(current);
148 }
149 // Maximum backoff should have been reached.
150 EXPECT_EQ(next, current);
151 }
152
153 } // namespace rappor
OLDNEW
« no previous file with comments | « components/rappor/log_uploader.cc ('k') | components/rappor/proto/rappor_metric.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698