| 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/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" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 beacon->status = "tcp.connection_reset"; | 34 beacon->status = "tcp.connection_reset"; |
| 35 beacon->chrome_error = net::ERR_CONNECTION_RESET; | 35 beacon->chrome_error = net::ERR_CONNECTION_RESET; |
| 36 beacon->server_ip = "127.0.0.1"; | 36 beacon->server_ip = "127.0.0.1"; |
| 37 beacon->was_proxied = false; | 37 beacon->was_proxied = false; |
| 38 beacon->protocol = "HTTP"; | 38 beacon->protocol = "HTTP"; |
| 39 beacon->details.quic_broken = true; | 39 beacon->details.quic_broken = true; |
| 40 beacon->http_response_code = -1; | 40 beacon->http_response_code = -1; |
| 41 beacon->elapsed = base::TimeDelta::FromMilliseconds(250); | 41 beacon->elapsed = base::TimeDelta::FromMilliseconds(250); |
| 42 beacon->start_time = time->NowTicks() - beacon->elapsed; | 42 beacon->start_time = time->NowTicks() - beacon->elapsed; |
| 43 beacon->upload_depth = 0; | 43 beacon->upload_depth = 0; |
| 44 beacon->sample_rate = 1.0; |
| 44 return beacon.Pass(); | 45 return beacon.Pass(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 template <typename ValueType, | 48 template <typename ValueType, |
| 48 bool (DictionaryValue::* GetValueType)(const std::string&, | 49 bool (DictionaryValue::* GetValueType)(const std::string&, |
| 49 ValueType*) const> | 50 ValueType*) const> |
| 50 struct HasValue { | 51 struct HasValue { |
| 51 bool operator()(const DictionaryValue& dict, | 52 bool operator()(const DictionaryValue& dict, |
| 52 const std::string& key, | 53 const std::string& key, |
| 53 ValueType expected_value) { | 54 ValueType expected_value) { |
| 54 ValueType actual_value; | 55 ValueType actual_value; |
| 55 bool got_value = (dict.*GetValueType)(key, &actual_value); | 56 bool got_value = (dict.*GetValueType)(key, &actual_value); |
| 56 EXPECT_TRUE(got_value); | 57 EXPECT_TRUE(got_value); |
| 57 if (got_value) | 58 if (got_value) |
| 58 EXPECT_EQ(expected_value, actual_value); | 59 EXPECT_EQ(expected_value, actual_value); |
| 59 return got_value && (expected_value == actual_value); | 60 return got_value && (expected_value == actual_value); |
| 60 } | 61 } |
| 61 }; | 62 }; |
| 62 | 63 |
| 64 HasValue<bool, &DictionaryValue::GetBoolean> HasBooleanValue; |
| 65 HasValue<double, &DictionaryValue::GetDouble> HasDoubleValue; |
| 66 HasValue<int, &DictionaryValue::GetInteger> HasIntegerValue; |
| 63 HasValue<std::string, &DictionaryValue::GetString> HasStringValue; | 67 HasValue<std::string, &DictionaryValue::GetString> HasStringValue; |
| 64 HasValue<int, &DictionaryValue::GetInteger> HasIntegerValue; | |
| 65 HasValue<bool, &DictionaryValue::GetBoolean> HasBooleanValue; | |
| 66 | 68 |
| 67 bool GetEntryFromReport(const Value* report, | 69 bool GetEntryFromReport(const Value* report, |
| 68 size_t index, | 70 size_t index, |
| 69 const DictionaryValue** entry_out) { | 71 const DictionaryValue** entry_out) { |
| 70 const DictionaryValue* report_dict; | 72 const DictionaryValue* report_dict; |
| 71 const ListValue* entries; | 73 const ListValue* entries; |
| 72 | 74 |
| 73 return report && | 75 return report && |
| 74 report->GetAsDictionary(&report_dict) && | 76 report->GetAsDictionary(&report_dict) && |
| 75 report_dict->GetList("entries", &entries) && | 77 report_dict->GetList("entries", &entries) && |
| 76 entries->GetDictionary(index, entry_out); | 78 entries->GetDictionary(index, entry_out); |
| 77 } | 79 } |
| 78 | 80 |
| 79 class DomainReliabilityContextTest : public testing::Test { | 81 class DomainReliabilityContextTest : public testing::Test { |
| 80 protected: | 82 protected: |
| 81 DomainReliabilityContextTest() | 83 DomainReliabilityContextTest() |
| 82 : last_network_change_time_(time_.NowTicks()), | 84 : last_network_change_time_(time_.NowTicks()), |
| 83 dispatcher_(&time_), | 85 dispatcher_(&time_), |
| 84 params_(MakeTestSchedulerParams()), | 86 params_(MakeTestSchedulerParams()), |
| 85 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, | 87 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, |
| 86 base::Unretained(this))), | 88 base::Unretained(this))), |
| 87 upload_reporter_string_("test-reporter"), | 89 upload_reporter_string_("test-reporter"), |
| 88 context_(&time_, | |
| 89 params_, | |
| 90 upload_reporter_string_, | |
| 91 &last_network_change_time_, | |
| 92 &dispatcher_, | |
| 93 &uploader_, | |
| 94 MakeTestConfig().Pass()), | |
| 95 upload_pending_(false) { | 90 upload_pending_(false) { |
| 96 // Make sure that the last network change does not overlap requests | 91 // Make sure that the last network change does not overlap requests |
| 97 // made in test cases, which start 250ms in the past (see |MakeBeacon|). | 92 // made in test cases, which start 250ms in the past (see |MakeBeacon|). |
| 98 last_network_change_time_ = time_.NowTicks(); | 93 last_network_change_time_ = time_.NowTicks(); |
| 99 time_.Advance(base::TimeDelta::FromSeconds(1)); | 94 time_.Advance(base::TimeDelta::FromSeconds(1)); |
| 100 } | 95 } |
| 101 | 96 |
| 97 void InitContext(scoped_ptr<const DomainReliabilityConfig> config) { |
| 98 context_.reset(new DomainReliabilityContext( |
| 99 &time_, |
| 100 params_, |
| 101 upload_reporter_string_, |
| 102 &last_network_change_time_, |
| 103 &dispatcher_, |
| 104 &uploader_, |
| 105 config.Pass())); |
| 106 } |
| 107 |
| 102 TimeDelta min_delay() const { return params_.minimum_upload_delay; } | 108 TimeDelta min_delay() const { return params_.minimum_upload_delay; } |
| 103 TimeDelta max_delay() const { return params_.maximum_upload_delay; } | 109 TimeDelta max_delay() const { return params_.maximum_upload_delay; } |
| 104 TimeDelta retry_interval() const { return params_.upload_retry_interval; } | 110 TimeDelta retry_interval() const { return params_.upload_retry_interval; } |
| 105 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } | 111 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } |
| 106 | 112 |
| 107 bool upload_pending() const { return upload_pending_; } | 113 bool upload_pending() const { return upload_pending_; } |
| 108 | 114 |
| 109 const std::string& upload_report() const { | 115 const std::string& upload_report() const { |
| 110 EXPECT_TRUE(upload_pending_); | 116 EXPECT_TRUE(upload_pending_); |
| 111 return upload_report_; | 117 return upload_report_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 122 } | 128 } |
| 123 | 129 |
| 124 void CallUploadCallback(DomainReliabilityUploader::UploadResult result) { | 130 void CallUploadCallback(DomainReliabilityUploader::UploadResult result) { |
| 125 ASSERT_TRUE(upload_pending_); | 131 ASSERT_TRUE(upload_pending_); |
| 126 upload_callback_.Run(result); | 132 upload_callback_.Run(result); |
| 127 upload_pending_ = false; | 133 upload_pending_ = false; |
| 128 } | 134 } |
| 129 | 135 |
| 130 bool CheckNoBeacons() { | 136 bool CheckNoBeacons() { |
| 131 BeaconVector beacons; | 137 BeaconVector beacons; |
| 132 context_.GetQueuedBeaconsForTesting(&beacons); | 138 context_->GetQueuedBeaconsForTesting(&beacons); |
| 133 return beacons.empty(); | 139 return beacons.empty(); |
| 134 } | 140 } |
| 135 | 141 |
| 136 MockTime time_; | 142 MockTime time_; |
| 137 base::TimeTicks last_network_change_time_; | 143 base::TimeTicks last_network_change_time_; |
| 138 DomainReliabilityDispatcher dispatcher_; | 144 DomainReliabilityDispatcher dispatcher_; |
| 139 DomainReliabilityScheduler::Params params_; | 145 DomainReliabilityScheduler::Params params_; |
| 140 MockUploader uploader_; | 146 MockUploader uploader_; |
| 141 std::string upload_reporter_string_; | 147 std::string upload_reporter_string_; |
| 142 DomainReliabilityContext context_; | 148 scoped_ptr<DomainReliabilityContext> context_; |
| 143 | 149 |
| 144 private: | 150 private: |
| 145 void OnUploadRequest( | 151 void OnUploadRequest( |
| 146 const std::string& report_json, | 152 const std::string& report_json, |
| 147 int max_upload_depth, | 153 int max_upload_depth, |
| 148 const GURL& upload_url, | 154 const GURL& upload_url, |
| 149 const DomainReliabilityUploader::UploadCallback& callback) { | 155 const DomainReliabilityUploader::UploadCallback& callback) { |
| 150 ASSERT_FALSE(upload_pending_); | 156 ASSERT_FALSE(upload_pending_); |
| 151 upload_report_ = report_json; | 157 upload_report_ = report_json; |
| 152 upload_max_depth_ = max_upload_depth; | 158 upload_max_depth_ = max_upload_depth; |
| 153 upload_url_ = upload_url; | 159 upload_url_ = upload_url; |
| 154 upload_callback_ = callback; | 160 upload_callback_ = callback; |
| 155 upload_pending_ = true; | 161 upload_pending_ = true; |
| 156 } | 162 } |
| 157 | 163 |
| 158 bool upload_pending_; | 164 bool upload_pending_; |
| 159 std::string upload_report_; | 165 std::string upload_report_; |
| 160 int upload_max_depth_; | 166 int upload_max_depth_; |
| 161 GURL upload_url_; | 167 GURL upload_url_; |
| 162 DomainReliabilityUploader::UploadCallback upload_callback_; | 168 DomainReliabilityUploader::UploadCallback upload_callback_; |
| 163 }; | 169 }; |
| 164 | 170 |
| 165 TEST_F(DomainReliabilityContextTest, Create) { | 171 TEST_F(DomainReliabilityContextTest, Create) { |
| 172 InitContext(MakeTestConfig()); |
| 166 EXPECT_TRUE(CheckNoBeacons()); | 173 EXPECT_TRUE(CheckNoBeacons()); |
| 167 } | 174 } |
| 168 | 175 |
| 169 TEST_F(DomainReliabilityContextTest, Report) { | 176 TEST_F(DomainReliabilityContextTest, Report) { |
| 170 context_.OnBeacon(MakeBeacon(&time_)); | 177 InitContext(MakeTestConfig()); |
| 178 context_->OnBeacon(MakeBeacon(&time_)); |
| 171 | 179 |
| 172 BeaconVector beacons; | 180 BeaconVector beacons; |
| 173 context_.GetQueuedBeaconsForTesting(&beacons); | 181 context_->GetQueuedBeaconsForTesting(&beacons); |
| 174 EXPECT_EQ(1u, beacons.size()); | 182 EXPECT_EQ(1u, beacons.size()); |
| 175 } | 183 } |
| 176 | 184 |
| 177 TEST_F(DomainReliabilityContextTest, MaxNestedBeaconSchedules) { | 185 TEST_F(DomainReliabilityContextTest, MaxNestedBeaconSchedules) { |
| 186 InitContext(MakeTestConfig()); |
| 178 GURL url("http://example/always_report"); | 187 GURL url("http://example/always_report"); |
| 179 scoped_ptr<DomainReliabilityBeacon> beacon = MakeBeacon(&time_); | 188 scoped_ptr<DomainReliabilityBeacon> beacon = MakeBeacon(&time_); |
| 180 beacon->upload_depth = DomainReliabilityContext::kMaxUploadDepthToSchedule; | 189 beacon->upload_depth = DomainReliabilityContext::kMaxUploadDepthToSchedule; |
| 181 context_.OnBeacon(beacon.Pass()); | 190 context_->OnBeacon(beacon.Pass()); |
| 182 | 191 |
| 183 BeaconVector beacons; | 192 BeaconVector beacons; |
| 184 context_.GetQueuedBeaconsForTesting(&beacons); | 193 context_->GetQueuedBeaconsForTesting(&beacons); |
| 185 EXPECT_EQ(1u, beacons.size()); | 194 EXPECT_EQ(1u, beacons.size()); |
| 186 | 195 |
| 187 time_.Advance(max_delay()); | 196 time_.Advance(max_delay()); |
| 188 EXPECT_TRUE(upload_pending()); | 197 EXPECT_TRUE(upload_pending()); |
| 189 } | 198 } |
| 190 | 199 |
| 191 TEST_F(DomainReliabilityContextTest, OverlyNestedBeaconDoesNotSchedule) { | 200 TEST_F(DomainReliabilityContextTest, OverlyNestedBeaconDoesNotSchedule) { |
| 201 InitContext(MakeTestConfig()); |
| 192 GURL url("http://example/always_report"); | 202 GURL url("http://example/always_report"); |
| 193 scoped_ptr<DomainReliabilityBeacon> beacon = MakeBeacon(&time_); | 203 scoped_ptr<DomainReliabilityBeacon> beacon = MakeBeacon(&time_); |
| 194 beacon->upload_depth = | 204 beacon->upload_depth = |
| 195 DomainReliabilityContext::kMaxUploadDepthToSchedule + 1; | 205 DomainReliabilityContext::kMaxUploadDepthToSchedule + 1; |
| 196 context_.OnBeacon(beacon.Pass()); | 206 context_->OnBeacon(beacon.Pass()); |
| 197 | 207 |
| 198 BeaconVector beacons; | 208 BeaconVector beacons; |
| 199 context_.GetQueuedBeaconsForTesting(&beacons); | 209 context_->GetQueuedBeaconsForTesting(&beacons); |
| 200 EXPECT_EQ(1u, beacons.size()); | 210 EXPECT_EQ(1u, beacons.size()); |
| 201 | 211 |
| 202 time_.Advance(max_delay()); | 212 time_.Advance(max_delay()); |
| 203 EXPECT_FALSE(upload_pending()); | 213 EXPECT_FALSE(upload_pending()); |
| 204 } | 214 } |
| 205 | 215 |
| 206 TEST_F(DomainReliabilityContextTest, | 216 TEST_F(DomainReliabilityContextTest, |
| 207 MaxNestedBeaconAfterOverlyNestedBeaconSchedules) { | 217 MaxNestedBeaconAfterOverlyNestedBeaconSchedules) { |
| 218 InitContext(MakeTestConfig()); |
| 208 // Add a beacon for a report that's too nested to schedule a beacon. | 219 // Add a beacon for a report that's too nested to schedule a beacon. |
| 209 scoped_ptr<DomainReliabilityBeacon> beacon = MakeBeacon(&time_); | 220 scoped_ptr<DomainReliabilityBeacon> beacon = MakeBeacon(&time_); |
| 210 beacon->upload_depth = | 221 beacon->upload_depth = |
| 211 DomainReliabilityContext::kMaxUploadDepthToSchedule + 1; | 222 DomainReliabilityContext::kMaxUploadDepthToSchedule + 1; |
| 212 context_.OnBeacon(beacon.Pass()); | 223 context_->OnBeacon(beacon.Pass()); |
| 213 | 224 |
| 214 BeaconVector beacons; | 225 BeaconVector beacons; |
| 215 context_.GetQueuedBeaconsForTesting(&beacons); | 226 context_->GetQueuedBeaconsForTesting(&beacons); |
| 216 EXPECT_EQ(1u, beacons.size()); | 227 EXPECT_EQ(1u, beacons.size()); |
| 217 | 228 |
| 218 time_.Advance(max_delay()); | 229 time_.Advance(max_delay()); |
| 219 EXPECT_FALSE(upload_pending()); | 230 EXPECT_FALSE(upload_pending()); |
| 220 | 231 |
| 221 // Add a beacon for a report that should schedule a beacon, and make sure it | 232 // Add a beacon for a report that should schedule a beacon, and make sure it |
| 222 // doesn't schedule until the deadline. | 233 // doesn't schedule until the deadline. |
| 223 beacon = MakeBeacon(&time_); | 234 beacon = MakeBeacon(&time_); |
| 224 beacon->upload_depth = DomainReliabilityContext::kMaxUploadDepthToSchedule; | 235 beacon->upload_depth = DomainReliabilityContext::kMaxUploadDepthToSchedule; |
| 225 context_.OnBeacon(beacon.Pass()); | 236 context_->OnBeacon(beacon.Pass()); |
| 226 | 237 |
| 227 context_.GetQueuedBeaconsForTesting(&beacons); | 238 context_->GetQueuedBeaconsForTesting(&beacons); |
| 228 EXPECT_EQ(2u, beacons.size()); | 239 EXPECT_EQ(2u, beacons.size()); |
| 229 | 240 |
| 230 time_.Advance(max_delay()); | 241 time_.Advance(max_delay()); |
| 231 EXPECT_TRUE(upload_pending()); | 242 EXPECT_TRUE(upload_pending()); |
| 232 | 243 |
| 233 // Check that both beacons were uploaded. | 244 // Check that both beacons were uploaded. |
| 234 DomainReliabilityUploader::UploadResult result; | 245 DomainReliabilityUploader::UploadResult result; |
| 235 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; | 246 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; |
| 236 CallUploadCallback(result); | 247 CallUploadCallback(result); |
| 237 | 248 |
| 238 EXPECT_TRUE(CheckNoBeacons()); | 249 EXPECT_TRUE(CheckNoBeacons()); |
| 239 } | 250 } |
| 240 | 251 |
| 241 TEST_F(DomainReliabilityContextTest, ReportUpload) { | 252 TEST_F(DomainReliabilityContextTest, ReportUpload) { |
| 242 context_.OnBeacon(MakeBeacon(&time_)); | 253 InitContext(MakeTestConfig()); |
| 254 context_->OnBeacon(MakeBeacon(&time_)); |
| 243 | 255 |
| 244 BeaconVector beacons; | 256 BeaconVector beacons; |
| 245 context_.GetQueuedBeaconsForTesting(&beacons); | 257 context_->GetQueuedBeaconsForTesting(&beacons); |
| 246 EXPECT_EQ(1u, beacons.size()); | 258 EXPECT_EQ(1u, beacons.size()); |
| 247 | 259 |
| 248 time_.Advance(max_delay()); | 260 time_.Advance(max_delay()); |
| 249 EXPECT_TRUE(upload_pending()); | 261 EXPECT_TRUE(upload_pending()); |
| 250 EXPECT_EQ(0, upload_max_depth()); | 262 EXPECT_EQ(0, upload_max_depth()); |
| 251 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); | 263 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 252 | 264 |
| 253 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); | 265 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); |
| 254 const DictionaryValue* entry; | 266 const DictionaryValue* entry; |
| 255 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); | 267 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); |
| 256 EXPECT_TRUE(HasStringValue(*entry, "failure_data.custom_error", | 268 EXPECT_TRUE(HasStringValue(*entry, "failure_data.custom_error", |
| 257 "net::ERR_CONNECTION_RESET")); | 269 "net::ERR_CONNECTION_RESET")); |
| 258 EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", false)); | 270 EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", false)); |
| 259 EXPECT_TRUE(HasStringValue(*entry, "protocol", "HTTP")); | 271 EXPECT_TRUE(HasStringValue(*entry, "protocol", "HTTP")); |
| 260 EXPECT_TRUE(HasBooleanValue(*entry, "quic_broken", true)); | 272 EXPECT_TRUE(HasBooleanValue(*entry, "quic_broken", true)); |
| 261 // N.B.: Assumes max_delay is 5 minutes. | 273 // N.B.: Assumes max_delay is 5 minutes. |
| 262 EXPECT_TRUE(HasIntegerValue(*entry, "request_age_ms", 300250)); | 274 EXPECT_TRUE(HasIntegerValue(*entry, "request_age_ms", 300250)); |
| 263 EXPECT_TRUE(HasIntegerValue(*entry, "request_elapsed_ms", 250)); | 275 EXPECT_TRUE(HasIntegerValue(*entry, "request_elapsed_ms", 250)); |
| 276 EXPECT_TRUE(HasDoubleValue(*entry, "sample_rate", 1.0)); |
| 264 EXPECT_TRUE(HasStringValue(*entry, "server_ip", "127.0.0.1")); | 277 EXPECT_TRUE(HasStringValue(*entry, "server_ip", "127.0.0.1")); |
| 265 EXPECT_TRUE(HasStringValue(*entry, "status", "tcp.connection_reset")); | 278 EXPECT_TRUE(HasStringValue(*entry, "status", "tcp.connection_reset")); |
| 266 EXPECT_TRUE(HasStringValue(*entry, "url", "https://localhost/")); | 279 EXPECT_TRUE(HasStringValue(*entry, "url", "https://localhost/")); |
| 267 EXPECT_TRUE(HasBooleanValue(*entry, "was_proxied", false)); | 280 EXPECT_TRUE(HasBooleanValue(*entry, "was_proxied", false)); |
| 268 | 281 |
| 269 DomainReliabilityUploader::UploadResult result; | 282 DomainReliabilityUploader::UploadResult result; |
| 270 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; | 283 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; |
| 271 CallUploadCallback(result); | 284 CallUploadCallback(result); |
| 272 | 285 |
| 273 EXPECT_TRUE(CheckNoBeacons()); | 286 EXPECT_TRUE(CheckNoBeacons()); |
| 274 } | 287 } |
| 275 | 288 |
| 276 TEST_F(DomainReliabilityContextTest, NetworkChanged) { | 289 TEST_F(DomainReliabilityContextTest, NetworkChanged) { |
| 277 context_.OnBeacon(MakeBeacon(&time_)); | 290 InitContext(MakeTestConfig()); |
| 291 context_->OnBeacon(MakeBeacon(&time_)); |
| 278 | 292 |
| 279 BeaconVector beacons; | 293 BeaconVector beacons; |
| 280 context_.GetQueuedBeaconsForTesting(&beacons); | 294 context_->GetQueuedBeaconsForTesting(&beacons); |
| 281 EXPECT_EQ(1u, beacons.size()); | 295 EXPECT_EQ(1u, beacons.size()); |
| 282 | 296 |
| 283 // Simulate a network change after the request but before the upload. | 297 // Simulate a network change after the request but before the upload. |
| 284 last_network_change_time_ = time_.NowTicks(); | 298 last_network_change_time_ = time_.NowTicks(); |
| 285 time_.Advance(max_delay()); | 299 time_.Advance(max_delay()); |
| 286 EXPECT_TRUE(upload_pending()); | 300 EXPECT_TRUE(upload_pending()); |
| 287 EXPECT_EQ(0, upload_max_depth()); | 301 EXPECT_EQ(0, upload_max_depth()); |
| 288 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); | 302 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 289 | 303 |
| 290 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); | 304 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); |
| 291 const DictionaryValue* entry; | 305 const DictionaryValue* entry; |
| 292 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); | 306 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); |
| 293 EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", true)); | 307 EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", true)); |
| 294 | 308 |
| 295 DomainReliabilityUploader::UploadResult result; | 309 DomainReliabilityUploader::UploadResult result; |
| 296 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; | 310 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; |
| 297 CallUploadCallback(result); | 311 CallUploadCallback(result); |
| 298 | 312 |
| 299 EXPECT_TRUE(CheckNoBeacons()); | 313 EXPECT_TRUE(CheckNoBeacons()); |
| 300 } | 314 } |
| 301 | 315 |
| 316 TEST_F(DomainReliabilityContextTest, ZeroSampleRate) { |
| 317 scoped_ptr<DomainReliabilityConfig> config(MakeTestConfig()); |
| 318 config->failure_sample_rate = 0.0; |
| 319 InitContext(config.Pass()); |
| 320 |
| 321 BeaconVector beacons; |
| 322 for (int i = 0; i < 100; i++) { |
| 323 context_->OnBeacon(MakeBeacon(&time_)); |
| 324 EXPECT_TRUE(CheckNoBeacons()); |
| 325 } |
| 326 } |
| 327 |
| 328 TEST_F(DomainReliabilityContextTest, FractionalSampleRate) { |
| 329 scoped_ptr<DomainReliabilityConfig> config(MakeTestConfig()); |
| 330 config->failure_sample_rate = 0.5; |
| 331 InitContext(config.Pass()); |
| 332 |
| 333 BeaconVector beacons; |
| 334 do { |
| 335 context_->OnBeacon(MakeBeacon(&time_)); |
| 336 context_->GetQueuedBeaconsForTesting(&beacons); |
| 337 } while (beacons.empty()); |
| 338 EXPECT_EQ(1u, beacons.size()); |
| 339 |
| 340 time_.Advance(max_delay()); |
| 341 EXPECT_TRUE(upload_pending()); |
| 342 EXPECT_EQ(0, upload_max_depth()); |
| 343 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 344 |
| 345 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); |
| 346 const DictionaryValue* entry; |
| 347 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); |
| 348 EXPECT_TRUE(HasDoubleValue(*entry, "sample_rate", 0.5)); |
| 349 |
| 350 DomainReliabilityUploader::UploadResult result; |
| 351 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; |
| 352 CallUploadCallback(result); |
| 353 |
| 354 EXPECT_TRUE(CheckNoBeacons()); |
| 355 } |
| 356 |
| 357 TEST_F(DomainReliabilityContextTest, FailureSampleOnly) { |
| 358 scoped_ptr<DomainReliabilityConfig> config(MakeTestConfig()); |
| 359 config->success_sample_rate = 0.0; |
| 360 config->failure_sample_rate = 1.0; |
| 361 InitContext(config.Pass()); |
| 362 |
| 363 BeaconVector beacons; |
| 364 |
| 365 context_->OnBeacon(MakeBeacon(&time_)); |
| 366 context_->GetQueuedBeaconsForTesting(&beacons); |
| 367 EXPECT_EQ(1u, beacons.size()); |
| 368 |
| 369 scoped_ptr<DomainReliabilityBeacon> beacon(MakeBeacon(&time_)); |
| 370 beacon->status = "ok"; |
| 371 beacon->chrome_error = net::OK; |
| 372 context_->OnBeacon(beacon.Pass()); |
| 373 context_->GetQueuedBeaconsForTesting(&beacons); |
| 374 EXPECT_EQ(1u, beacons.size()); |
| 375 } |
| 376 |
| 377 TEST_F(DomainReliabilityContextTest, SuccessSampleOnly) { |
| 378 scoped_ptr<DomainReliabilityConfig> config(MakeTestConfig()); |
| 379 config->success_sample_rate = 1.0; |
| 380 config->failure_sample_rate = 0.0; |
| 381 InitContext(config.Pass()); |
| 382 |
| 383 BeaconVector beacons; |
| 384 |
| 385 context_->OnBeacon(MakeBeacon(&time_)); |
| 386 context_->GetQueuedBeaconsForTesting(&beacons); |
| 387 EXPECT_EQ(0u, beacons.size()); |
| 388 |
| 389 scoped_ptr<DomainReliabilityBeacon> beacon(MakeBeacon(&time_)); |
| 390 beacon->status = "ok"; |
| 391 beacon->chrome_error = net::OK; |
| 392 context_->OnBeacon(beacon.Pass()); |
| 393 context_->GetQueuedBeaconsForTesting(&beacons); |
| 394 EXPECT_EQ(1u, beacons.size()); |
| 395 } |
| 396 |
| 397 TEST_F(DomainReliabilityContextTest, SampleAllBeacons) { |
| 398 scoped_ptr<DomainReliabilityConfig> config(MakeTestConfig()); |
| 399 config->success_sample_rate = 1.0; |
| 400 config->failure_sample_rate = 1.0; |
| 401 InitContext(config.Pass()); |
| 402 |
| 403 BeaconVector beacons; |
| 404 |
| 405 context_->OnBeacon(MakeBeacon(&time_)); |
| 406 context_->GetQueuedBeaconsForTesting(&beacons); |
| 407 EXPECT_EQ(1u, beacons.size()); |
| 408 |
| 409 scoped_ptr<DomainReliabilityBeacon> beacon(MakeBeacon(&time_)); |
| 410 beacon->status = "ok"; |
| 411 beacon->chrome_error = net::OK; |
| 412 context_->OnBeacon(beacon.Pass()); |
| 413 context_->GetQueuedBeaconsForTesting(&beacons); |
| 414 EXPECT_EQ(2u, beacons.size()); |
| 415 } |
| 416 |
| 417 TEST_F(DomainReliabilityContextTest, SampleNoBeacons) { |
| 418 scoped_ptr<DomainReliabilityConfig> config(MakeTestConfig()); |
| 419 config->success_sample_rate = 0.0; |
| 420 config->failure_sample_rate = 0.0; |
| 421 InitContext(config.Pass()); |
| 422 |
| 423 BeaconVector beacons; |
| 424 |
| 425 context_->OnBeacon(MakeBeacon(&time_)); |
| 426 context_->GetQueuedBeaconsForTesting(&beacons); |
| 427 EXPECT_EQ(0u, beacons.size()); |
| 428 |
| 429 scoped_ptr<DomainReliabilityBeacon> beacon(MakeBeacon(&time_)); |
| 430 beacon->status = "ok"; |
| 431 beacon->chrome_error = net::OK; |
| 432 context_->OnBeacon(beacon.Pass()); |
| 433 context_->GetQueuedBeaconsForTesting(&beacons); |
| 434 EXPECT_EQ(0u, beacons.size()); |
| 435 } |
| 436 |
| 302 // TODO(ttuttle): Add beacon_unittest.cc to test serialization. | 437 // TODO(ttuttle): Add beacon_unittest.cc to test serialization. |
| 303 | 438 |
| 304 } // namespace | 439 } // namespace |
| 305 } // namespace domain_reliability | 440 } // namespace domain_reliability |
| OLD | NEW |