OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/rappor/log_uploader.h" | 5 #include "components/rappor/log_uploader.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/run_loop.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
10 #include "net/url_request/test_url_fetcher_factory.h" | 11 #include "net/url_request/test_url_fetcher_factory.h" |
11 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace rappor { | 15 namespace rappor { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 const char kTestServerURL[] = "http://a.com/"; | 19 const char kTestServerURL[] = "http://a.com/"; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 76 |
76 TEST_F(LogUploaderTest, Success) { | 77 TEST_F(LogUploaderTest, Success) { |
77 TestLogUploader uploader(request_context_.get()); | 78 TestLogUploader uploader(request_context_.get()); |
78 | 79 |
79 factory_.SetFakeResponse(GURL(kTestServerURL), | 80 factory_.SetFakeResponse(GURL(kTestServerURL), |
80 std::string(), | 81 std::string(), |
81 net::HTTP_OK, | 82 net::HTTP_OK, |
82 net::URLRequestStatus::SUCCESS); | 83 net::URLRequestStatus::SUCCESS); |
83 | 84 |
84 uploader.QueueLog("log1"); | 85 uploader.QueueLog("log1"); |
85 base::MessageLoop::current()->RunUntilIdle(); | 86 base::RunLoop().RunUntilIdle(); |
86 // Log should be discarded instead of retransmitted. | 87 // Log should be discarded instead of retransmitted. |
87 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); | 88 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); |
88 } | 89 } |
89 | 90 |
90 TEST_F(LogUploaderTest, Rejection) { | 91 TEST_F(LogUploaderTest, Rejection) { |
91 TestLogUploader uploader(request_context_.get()); | 92 TestLogUploader uploader(request_context_.get()); |
92 | 93 |
93 factory_.SetFakeResponse(GURL(kTestServerURL), | 94 factory_.SetFakeResponse(GURL(kTestServerURL), |
94 std::string(), | 95 std::string(), |
95 net::HTTP_BAD_REQUEST, | 96 net::HTTP_BAD_REQUEST, |
96 net::URLRequestStatus::SUCCESS); | 97 net::URLRequestStatus::SUCCESS); |
97 | 98 |
98 uploader.QueueLog("log1"); | 99 uploader.QueueLog("log1"); |
99 base::MessageLoop::current()->RunUntilIdle(); | 100 base::RunLoop().RunUntilIdle(); |
100 // Log should be discarded instead of retransmitted. | 101 // Log should be discarded instead of retransmitted. |
101 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); | 102 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); |
102 } | 103 } |
103 | 104 |
104 TEST_F(LogUploaderTest, Failure) { | 105 TEST_F(LogUploaderTest, Failure) { |
105 TestLogUploader uploader(request_context_.get()); | 106 TestLogUploader uploader(request_context_.get()); |
106 | 107 |
107 factory_.SetFakeResponse(GURL(kTestServerURL), | 108 factory_.SetFakeResponse(GURL(kTestServerURL), |
108 std::string(), | 109 std::string(), |
109 net::HTTP_INTERNAL_SERVER_ERROR, | 110 net::HTTP_INTERNAL_SERVER_ERROR, |
110 net::URLRequestStatus::SUCCESS); | 111 net::URLRequestStatus::SUCCESS); |
111 | 112 |
112 uploader.QueueLog("log1"); | 113 uploader.QueueLog("log1"); |
113 base::MessageLoop::current()->RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
114 // Log should be scheduled for retransmission. | 115 // Log should be scheduled for retransmission. |
115 base::TimeDelta error_interval = uploader.last_interval_set(); | 116 base::TimeDelta error_interval = uploader.last_interval_set(); |
116 EXPECT_GT(error_interval, base::TimeDelta()); | 117 EXPECT_GT(error_interval, base::TimeDelta()); |
117 | 118 |
118 for (int i = 0; i < 10; i++) { | 119 for (int i = 0; i < 10; i++) { |
119 uploader.QueueLog("logX"); | 120 uploader.QueueLog("logX"); |
120 } | 121 } |
121 | 122 |
122 // A second failure should lead to a longer interval, and the log should | 123 // A second failure should lead to a longer interval, and the log should |
123 // be discarded due to full queue. | 124 // be discarded due to full queue. |
124 uploader.StartUpload(); | 125 uploader.StartUpload(); |
125 base::MessageLoop::current()->RunUntilIdle(); | 126 base::RunLoop().RunUntilIdle(); |
126 EXPECT_GT(uploader.last_interval_set(), error_interval); | 127 EXPECT_GT(uploader.last_interval_set(), error_interval); |
127 | 128 |
128 factory_.SetFakeResponse(GURL(kTestServerURL), | 129 factory_.SetFakeResponse(GURL(kTestServerURL), |
129 std::string(), | 130 std::string(), |
130 net::HTTP_OK, | 131 net::HTTP_OK, |
131 net::URLRequestStatus::SUCCESS); | 132 net::URLRequestStatus::SUCCESS); |
132 | 133 |
133 // A success should revert to base interval while queue is not empty. | 134 // A success should revert to base interval while queue is not empty. |
134 for (int i = 0; i < 9; i++) { | 135 for (int i = 0; i < 9; i++) { |
135 uploader.StartUpload(); | 136 uploader.StartUpload(); |
136 base::MessageLoop::current()->RunUntilIdle(); | 137 base::RunLoop().RunUntilIdle(); |
137 EXPECT_LT(uploader.last_interval_set(), error_interval); | 138 EXPECT_LT(uploader.last_interval_set(), error_interval); |
138 } | 139 } |
139 | 140 |
140 // Queue should be empty. | 141 // Queue should be empty. |
141 uploader.StartUpload(); | 142 uploader.StartUpload(); |
142 base::MessageLoop::current()->RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
143 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); | 144 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); |
144 } | 145 } |
145 | 146 |
146 TEST_F(LogUploaderTest, Backoff) { | 147 TEST_F(LogUploaderTest, Backoff) { |
147 base::TimeDelta current = base::TimeDelta(); | 148 base::TimeDelta current = base::TimeDelta(); |
148 base::TimeDelta next = base::TimeDelta::FromSeconds(1); | 149 base::TimeDelta next = base::TimeDelta::FromSeconds(1); |
149 // Backoff until the maximum is reached. | 150 // Backoff until the maximum is reached. |
150 while (next > current) { | 151 while (next > current) { |
151 current = next; | 152 current = next; |
152 next = TestLogUploader::BackOff(current); | 153 next = TestLogUploader::BackOff(current); |
153 } | 154 } |
154 // Maximum backoff should have been reached. | 155 // Maximum backoff should have been reached. |
155 EXPECT_EQ(next, current); | 156 EXPECT_EQ(next, current); |
156 } | 157 } |
157 | 158 |
158 } // namespace rappor | 159 } // namespace rappor |
OLD | NEW |