| 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 19 matching lines...) Expand all Loading... |
| 30 beacon.http_response_code = 200; | 30 beacon.http_response_code = 200; |
| 31 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); | 31 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); |
| 32 beacon.start_time = time->NowTicks() - beacon.elapsed; | 32 beacon.start_time = time->NowTicks() - beacon.elapsed; |
| 33 return beacon; | 33 return beacon; |
| 34 } | 34 } |
| 35 | 35 |
| 36 class DomainReliabilityContextTest : public testing::Test { | 36 class DomainReliabilityContextTest : public testing::Test { |
| 37 protected: | 37 protected: |
| 38 DomainReliabilityContextTest() | 38 DomainReliabilityContextTest() |
| 39 : dispatcher_(&time_), | 39 : dispatcher_(&time_), |
| 40 params_(CreateParams()), | 40 params_(MakeTestSchedulerParams()), |
| 41 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, | 41 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, |
| 42 base::Unretained(this))), | 42 base::Unretained(this))), |
| 43 upload_reporter_string_("test-reporter"), | 43 upload_reporter_string_("test-reporter"), |
| 44 context_(&time_, | 44 context_(&time_, |
| 45 params_, | 45 params_, |
| 46 upload_reporter_string_, | 46 upload_reporter_string_, |
| 47 &dispatcher_, | 47 &dispatcher_, |
| 48 &uploader_, | 48 &uploader_, |
| 49 CreateConfig().Pass()), | 49 MakeTestConfig().Pass()), |
| 50 upload_pending_(false) {} | 50 upload_pending_(false) {} |
| 51 | 51 |
| 52 TimeDelta min_delay() const { return params_.minimum_upload_delay; } | 52 TimeDelta min_delay() const { return params_.minimum_upload_delay; } |
| 53 TimeDelta max_delay() const { return params_.maximum_upload_delay; } | 53 TimeDelta max_delay() const { return params_.maximum_upload_delay; } |
| 54 TimeDelta retry_interval() const { return params_.upload_retry_interval; } | 54 TimeDelta retry_interval() const { return params_.upload_retry_interval; } |
| 55 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } | 55 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } |
| 56 | 56 |
| 57 bool upload_pending() { return upload_pending_; } | 57 bool upload_pending() { return upload_pending_; } |
| 58 | 58 |
| 59 const std::string& upload_report() { | 59 const std::string& upload_report() { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 const std::string& report_json, | 98 const std::string& report_json, |
| 99 const GURL& upload_url, | 99 const GURL& upload_url, |
| 100 const DomainReliabilityUploader::UploadCallback& callback) { | 100 const DomainReliabilityUploader::UploadCallback& callback) { |
| 101 DCHECK(!upload_pending_); | 101 DCHECK(!upload_pending_); |
| 102 upload_report_ = report_json; | 102 upload_report_ = report_json; |
| 103 upload_url_ = upload_url; | 103 upload_url_ = upload_url; |
| 104 upload_callback_ = callback; | 104 upload_callback_ = callback; |
| 105 upload_pending_ = true; | 105 upload_pending_ = true; |
| 106 } | 106 } |
| 107 | 107 |
| 108 static DomainReliabilityScheduler::Params CreateParams() { | |
| 109 DomainReliabilityScheduler::Params params; | |
| 110 params.minimum_upload_delay = base::TimeDelta::FromSeconds(60); | |
| 111 params.maximum_upload_delay = base::TimeDelta::FromSeconds(300); | |
| 112 params.upload_retry_interval = base::TimeDelta::FromSeconds(15); | |
| 113 return params; | |
| 114 } | |
| 115 | |
| 116 static scoped_ptr<const DomainReliabilityConfig> CreateConfig() { | |
| 117 DomainReliabilityConfig* config = new DomainReliabilityConfig(); | |
| 118 DomainReliabilityConfig::Resource* resource; | |
| 119 | |
| 120 resource = new DomainReliabilityConfig::Resource(); | |
| 121 resource->name = "always_report"; | |
| 122 resource->url_patterns.push_back( | |
| 123 new std::string("http://example/always_report")); | |
| 124 resource->success_sample_rate = 1.0; | |
| 125 resource->failure_sample_rate = 1.0; | |
| 126 config->resources.push_back(resource); | |
| 127 | |
| 128 resource = new DomainReliabilityConfig::Resource(); | |
| 129 resource->name = "never_report"; | |
| 130 resource->url_patterns.push_back( | |
| 131 new std::string("http://example/never_report")); | |
| 132 resource->success_sample_rate = 0.0; | |
| 133 resource->failure_sample_rate = 0.0; | |
| 134 config->resources.push_back(resource); | |
| 135 | |
| 136 DomainReliabilityConfig::Collector* collector; | |
| 137 collector = new DomainReliabilityConfig::Collector(); | |
| 138 collector->upload_url = GURL("https://example/upload"); | |
| 139 config->collectors.push_back(collector); | |
| 140 | |
| 141 return scoped_ptr<const DomainReliabilityConfig>(config); | |
| 142 } | |
| 143 | |
| 144 bool upload_pending_; | 108 bool upload_pending_; |
| 145 std::string upload_report_; | 109 std::string upload_report_; |
| 146 GURL upload_url_; | 110 GURL upload_url_; |
| 147 DomainReliabilityUploader::UploadCallback upload_callback_; | 111 DomainReliabilityUploader::UploadCallback upload_callback_; |
| 148 }; | 112 }; |
| 149 | 113 |
| 150 TEST_F(DomainReliabilityContextTest, Create) { | 114 TEST_F(DomainReliabilityContextTest, Create) { |
| 151 EXPECT_TRUE(CheckNoBeacons(0)); | 115 EXPECT_TRUE(CheckNoBeacons(0)); |
| 152 EXPECT_TRUE(CheckCounts(0, 0, 0)); | 116 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
| 153 EXPECT_TRUE(CheckNoBeacons(1)); | 117 EXPECT_TRUE(CheckNoBeacons(1)); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 168 |
| 205 time_.Advance(max_delay()); | 169 time_.Advance(max_delay()); |
| 206 EXPECT_TRUE(upload_pending()); | 170 EXPECT_TRUE(upload_pending()); |
| 207 EXPECT_EQ(kExpectedReport, upload_report()); | 171 EXPECT_EQ(kExpectedReport, upload_report()); |
| 208 EXPECT_EQ(GURL("https://example/upload"), upload_url()); | 172 EXPECT_EQ(GURL("https://example/upload"), upload_url()); |
| 209 CallUploadCallback(true); | 173 CallUploadCallback(true); |
| 210 } | 174 } |
| 211 | 175 |
| 212 } // namespace | 176 } // namespace |
| 213 } // namespace domain_reliability | 177 } // namespace domain_reliability |
| OLD | NEW |