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

Side by Side Diff: components/domain_reliability/context_unittest.cc

Issue 1180223006: Domain Reliability: Simplify configs and reports (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make requested changes Created 5 years, 1 month 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
OLDNEW
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/domain_reliability/context.h" 5 #include "components/domain_reliability/context.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "components/domain_reliability/beacon.h" 12 #include "components/domain_reliability/beacon.h"
13 #include "components/domain_reliability/dispatcher.h" 13 #include "components/domain_reliability/dispatcher.h"
14 #include "components/domain_reliability/scheduler.h" 14 #include "components/domain_reliability/scheduler.h"
15 #include "components/domain_reliability/test_util.h" 15 #include "components/domain_reliability/test_util.h"
16 #include "components/domain_reliability/uploader.h" 16 #include "components/domain_reliability/uploader.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace domain_reliability { 21 namespace domain_reliability {
22 namespace { 22 namespace {
23 23
24 typedef std::vector<DomainReliabilityBeacon> BeaconVector; 24 typedef std::vector<const DomainReliabilityBeacon*> BeaconVector;
25 25
26 DomainReliabilityBeacon MakeBeacon(MockableTime* time) { 26 scoped_ptr<DomainReliabilityBeacon> MakeBeacon(MockableTime* time) {
27 DomainReliabilityBeacon beacon; 27 scoped_ptr<DomainReliabilityBeacon> beacon(new DomainReliabilityBeacon());
28 beacon.domain = "localhost"; 28 beacon->url = GURL("https://localhost/");
29 beacon.status = "ok"; 29 beacon->status = "tcp.connection_reset";
30 beacon.chrome_error = net::OK; 30 beacon->chrome_error = net::ERR_CONNECTION_RESET;
31 beacon.server_ip = "127.0.0.1"; 31 beacon->server_ip = "127.0.0.1";
32 beacon.was_proxied = false; 32 beacon->was_proxied = false;
33 beacon.protocol = "HTTP"; 33 beacon->protocol = "HTTP";
34 beacon.http_response_code = 200; 34 beacon->http_response_code = -1;
35 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); 35 beacon->elapsed = base::TimeDelta::FromMilliseconds(250);
36 beacon.start_time = time->NowTicks() - beacon.elapsed; 36 beacon->start_time = time->NowTicks() - beacon->elapsed;
37 return beacon; 37 return beacon.Pass();
38 } 38 }
39 39
40 class DomainReliabilityContextTest : public testing::Test { 40 class DomainReliabilityContextTest : public testing::Test {
41 protected: 41 protected:
42 DomainReliabilityContextTest() 42 DomainReliabilityContextTest()
43 : last_network_change_time_(time_.NowTicks()), 43 : last_network_change_time_(time_.NowTicks()),
44 dispatcher_(&time_), 44 dispatcher_(&time_),
45 params_(MakeTestSchedulerParams()), 45 params_(MakeTestSchedulerParams()),
46 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, 46 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest,
47 base::Unretained(this))), 47 base::Unretained(this))),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 upload_callback_.Run(result); 82 upload_callback_.Run(result);
83 upload_pending_ = false; 83 upload_pending_ = false;
84 } 84 }
85 85
86 bool CheckNoBeacons() { 86 bool CheckNoBeacons() {
87 BeaconVector beacons; 87 BeaconVector beacons;
88 context_.GetQueuedBeaconsForTesting(&beacons); 88 context_.GetQueuedBeaconsForTesting(&beacons);
89 return beacons.empty(); 89 return beacons.empty();
90 } 90 }
91 91
92 bool CheckCounts(size_t index,
93 unsigned expected_successful,
94 unsigned expected_failed) {
95 unsigned successful, failed;
96 context_.GetRequestCountsForTesting(index, &successful, &failed);
97 return successful == expected_successful && failed == expected_failed;
98 }
99
100 MockTime time_; 92 MockTime time_;
101 base::TimeTicks last_network_change_time_; 93 base::TimeTicks last_network_change_time_;
102 DomainReliabilityDispatcher dispatcher_; 94 DomainReliabilityDispatcher dispatcher_;
103 DomainReliabilityScheduler::Params params_; 95 DomainReliabilityScheduler::Params params_;
104 MockUploader uploader_; 96 MockUploader uploader_;
105 std::string upload_reporter_string_; 97 std::string upload_reporter_string_;
106 DomainReliabilityContext context_; 98 DomainReliabilityContext context_;
107 99
108 private: 100 private:
109 void OnUploadRequest( 101 void OnUploadRequest(
110 const std::string& report_json, 102 const std::string& report_json,
111 const GURL& upload_url, 103 const GURL& upload_url,
112 const DomainReliabilityUploader::UploadCallback& callback) { 104 const DomainReliabilityUploader::UploadCallback& callback) {
113 DCHECK(!upload_pending_); 105 DCHECK(!upload_pending_);
114 upload_report_ = report_json; 106 upload_report_ = report_json;
115 upload_url_ = upload_url; 107 upload_url_ = upload_url;
116 upload_callback_ = callback; 108 upload_callback_ = callback;
117 upload_pending_ = true; 109 upload_pending_ = true;
118 } 110 }
119 111
120 bool upload_pending_; 112 bool upload_pending_;
121 std::string upload_report_; 113 std::string upload_report_;
122 GURL upload_url_; 114 GURL upload_url_;
123 DomainReliabilityUploader::UploadCallback upload_callback_; 115 DomainReliabilityUploader::UploadCallback upload_callback_;
124 }; 116 };
125 117
126 TEST_F(DomainReliabilityContextTest, Create) { 118 TEST_F(DomainReliabilityContextTest, Create) {
127 EXPECT_TRUE(CheckNoBeacons()); 119 EXPECT_TRUE(CheckNoBeacons());
128 EXPECT_TRUE(CheckCounts(0, 0, 0));
129 EXPECT_TRUE(CheckCounts(1, 0, 0));
130 } 120 }
131 121
132 TEST_F(DomainReliabilityContextTest, NoResource) { 122 TEST_F(DomainReliabilityContextTest, Report) {
133 GURL url("http://example/no_resource"); 123 context_.OnBeacon(MakeBeacon(&time_));
134 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
135 context_.OnBeacon(url, beacon);
136
137 EXPECT_TRUE(CheckNoBeacons());
138 EXPECT_TRUE(CheckCounts(0, 0, 0));
139 EXPECT_TRUE(CheckCounts(1, 0, 0));
140 }
141
142 TEST_F(DomainReliabilityContextTest, NeverReport) {
143 GURL url("http://example/never_report");
144 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
145 context_.OnBeacon(url, beacon);
146
147 EXPECT_TRUE(CheckNoBeacons());
148 EXPECT_TRUE(CheckCounts(0, 0, 0));
149 EXPECT_TRUE(CheckCounts(1, 1, 0));
150 }
151
152 TEST_F(DomainReliabilityContextTest, AlwaysReport) {
153 GURL url("http://example/always_report");
154 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
155 context_.OnBeacon(url, beacon);
156 124
157 BeaconVector beacons; 125 BeaconVector beacons;
158 context_.GetQueuedBeaconsForTesting(&beacons); 126 context_.GetQueuedBeaconsForTesting(&beacons);
159 EXPECT_EQ(1u, beacons.size()); 127 EXPECT_EQ(1u, beacons.size());
160 EXPECT_TRUE(CheckCounts(0, 1, 0));
161 EXPECT_TRUE(CheckCounts(1, 0, 0));
162 } 128 }
163 129
164 TEST_F(DomainReliabilityContextTest, ReportUpload) { 130 TEST_F(DomainReliabilityContextTest, Upload) {
165 GURL url("http://example/always_report"); 131 context_.OnBeacon(MakeBeacon(&time_));
166 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
167 context_.OnBeacon(url, beacon);
168 132
169 BeaconVector beacons; 133 BeaconVector beacons;
170 context_.GetQueuedBeaconsForTesting(&beacons); 134 context_.GetQueuedBeaconsForTesting(&beacons);
171 EXPECT_EQ(1u, beacons.size()); 135 EXPECT_EQ(1u, beacons.size());
172 EXPECT_TRUE(CheckCounts(0, 1, 0));
173 EXPECT_TRUE(CheckCounts(1, 0, 0));
174 136
175 // N.B.: Assumes max_delay is 5 minutes. 137 // N.B.: Assumes max_delay is 5 minutes.
176 const char* kExpectedReport = "{" 138 const char* kExpectedReport = "{"
177 "\"config_version\":\"1\"," 139 "\"entries\":["
178 "\"entries\":[{\"domain\":\"localhost\"," 140 "{\"failure_data\":{\"custom_error\":\"net::ERR_CONNECTION_RESET\"},"
179 "\"http_response_code\":200,\"network_changed\":false," 141 "\"network_changed\":false,\"protocol\":\"HTTP\","
180 "\"protocol\":\"HTTP\",\"request_age_ms\":300250," 142 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,"
181 "\"request_elapsed_ms\":250,\"resource\":\"always_report\"," 143 "\"server_ip\":\"127.0.0.1\",\"status\":\"tcp.connection_reset\","
182 "\"server_ip\":\"127.0.0.1\",\"status\":\"ok\"," 144 "\"url\":\"https://localhost/\",\"was_proxied\":false}],"
183 "\"was_proxied\":false}]," 145 "\"reporter\":\"test-reporter\"}";
184 "\"reporter\":\"test-reporter\","
185 "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\","
186 "\"successful_requests\":1}]}";
187 146
188 time_.Advance(max_delay()); 147 time_.Advance(max_delay());
189 EXPECT_TRUE(upload_pending()); 148 EXPECT_TRUE(upload_pending());
190 EXPECT_EQ(kExpectedReport, upload_report()); 149 EXPECT_EQ(kExpectedReport, upload_report());
191 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); 150 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
192 151
193 DomainReliabilityUploader::UploadResult result; 152 DomainReliabilityUploader::UploadResult result;
194 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; 153 result.status = DomainReliabilityUploader::UploadResult::SUCCESS;
195 CallUploadCallback(result); 154 CallUploadCallback(result);
196 155
197 EXPECT_TRUE(CheckNoBeacons()); 156 EXPECT_TRUE(CheckNoBeacons());
198 EXPECT_TRUE(CheckCounts(0, 0, 0));
199 EXPECT_TRUE(CheckCounts(1, 0, 0));
200 } 157 }
201 158
202 TEST_F(DomainReliabilityContextTest, ReportUpload_NetworkChanged) { 159 TEST_F(DomainReliabilityContextTest, Upload_NetworkChanged) {
203 GURL url("http://example/always_report"); 160 context_.OnBeacon(MakeBeacon(&time_));
204 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
205 context_.OnBeacon(url, beacon);
206 161
207 BeaconVector beacons; 162 BeaconVector beacons;
208 context_.GetQueuedBeaconsForTesting(&beacons); 163 context_.GetQueuedBeaconsForTesting(&beacons);
209 EXPECT_EQ(1u, beacons.size()); 164 EXPECT_EQ(1u, beacons.size());
210 EXPECT_TRUE(CheckCounts(0, 1, 0));
211 EXPECT_TRUE(CheckCounts(1, 0, 0));
212 165
213 // N.B.: Assumes max_delay is 5 minutes. 166 // N.B.: Assumes max_delay is 5 minutes.
214 const char* kExpectedReport = "{" 167 const char* kExpectedReport = "{"
215 "\"config_version\":\"1\"," 168 "\"entries\":["
216 "\"entries\":[{\"domain\":\"localhost\"," 169 "{\"failure_data\":{\"custom_error\":\"net::ERR_CONNECTION_RESET\"},"
217 "\"http_response_code\":200,\"network_changed\":true," 170 "\"network_changed\":true,\"protocol\":\"HTTP\","
218 "\"protocol\":\"HTTP\",\"request_age_ms\":300250," 171 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,"
219 "\"request_elapsed_ms\":250,\"resource\":\"always_report\"," 172 "\"server_ip\":\"127.0.0.1\",\"status\":\"tcp.connection_reset\","
220 "\"server_ip\":\"127.0.0.1\",\"status\":\"ok\"," 173 "\"url\":\"https://localhost/\",\"was_proxied\":false}],"
221 "\"was_proxied\":false}]," 174 "\"reporter\":\"test-reporter\"}";
222 "\"reporter\":\"test-reporter\","
223 "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\","
224 "\"successful_requests\":1}]}";
225 175
226 // Simulate a network change after the request but before the upload. 176 // Simulate a network change after the request but before the upload.
227 last_network_change_time_ = time_.NowTicks(); 177 last_network_change_time_ = time_.NowTicks();
228 time_.Advance(max_delay()); 178 time_.Advance(max_delay());
229
230 EXPECT_TRUE(upload_pending()); 179 EXPECT_TRUE(upload_pending());
231 EXPECT_EQ(kExpectedReport, upload_report()); 180 EXPECT_EQ(kExpectedReport, upload_report());
232 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); 181 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
233 182
234 DomainReliabilityUploader::UploadResult result; 183 DomainReliabilityUploader::UploadResult result;
235 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; 184 result.status = DomainReliabilityUploader::UploadResult::SUCCESS;
236 CallUploadCallback(result); 185 CallUploadCallback(result);
237 186
238 EXPECT_TRUE(CheckNoBeacons()); 187 EXPECT_TRUE(CheckNoBeacons());
239 EXPECT_TRUE(CheckCounts(0, 0, 0));
240 EXPECT_TRUE(CheckCounts(1, 0, 0));
241 } 188 }
242 189
190 // TODO(ttuttle): Add beacon_unittest.cc to test serialization.
Randy Smith (Not in Mondays) 2015/11/03 21:48:12 Why not in this CL?
Deprecated (see juliatuttle) 2015/11/06 17:22:29 No rush; we test it above as part of uploads. I ju
Randy Smith (Not in Mondays) 2015/11/09 21:23:24 Acknowledged.
191
243 } // namespace 192 } // namespace
244 } // namespace domain_reliability 193 } // namespace domain_reliability
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698