| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.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 "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace domain_reliability { | 20 namespace domain_reliability { |
| 21 | |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 typedef std::vector<DomainReliabilityBeacon> BeaconVector; | 23 typedef std::vector<DomainReliabilityBeacon> BeaconVector; |
| 25 | 24 |
| 26 DomainReliabilityBeacon MakeBeacon(MockableTime* time) { | 25 DomainReliabilityBeacon MakeBeacon(MockableTime* time) { |
| 27 DomainReliabilityBeacon beacon; | 26 DomainReliabilityBeacon beacon; |
| 28 beacon.status = "ok"; | 27 beacon.status = "ok"; |
| 29 beacon.chrome_error = net::OK; | 28 beacon.chrome_error = net::OK; |
| 30 beacon.server_ip = "127.0.0.1"; | 29 beacon.server_ip = "127.0.0.1"; |
| 31 beacon.http_response_code = 200; | 30 beacon.http_response_code = 200; |
| 32 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); | 31 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); |
| 33 beacon.start_time = time->NowTicks() - beacon.elapsed; | 32 beacon.start_time = time->NowTicks() - beacon.elapsed; |
| 34 return beacon; | 33 return beacon; |
| 35 } | 34 } |
| 36 | 35 |
| 37 } // namespace | 36 } // namespace |
| 38 | 37 |
| 39 class DomainReliabilityContextTest : public testing::Test { | 38 class DomainReliabilityContextTest : public testing::Test { |
| 40 protected: | 39 protected: |
| 41 DomainReliabilityContextTest() | 40 DomainReliabilityContextTest() |
| 42 : dispatcher_(&time_), | 41 : dispatcher_(&time_), |
| 43 params_(CreateParams()), | 42 params_(CreateParams()), |
| 44 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, | 43 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, |
| 45 base::Unretained(this))), | 44 base::Unretained(this))), |
| 46 context_(&time_, | 45 context_(&time_, |
| 47 params_, | 46 params_, |
| 47 "test-reporter", |
| 48 &dispatcher_, | 48 &dispatcher_, |
| 49 &uploader_, | 49 &uploader_, |
| 50 CreateConfig().Pass()), | 50 CreateConfig().Pass()), |
| 51 upload_pending_(false) {} | 51 upload_pending_(false) {} |
| 52 | 52 |
| 53 TimeDelta min_delay() const { return params_.minimum_upload_delay; } | 53 TimeDelta min_delay() const { return params_.minimum_upload_delay; } |
| 54 TimeDelta max_delay() const { return params_.maximum_upload_delay; } | 54 TimeDelta max_delay() const { return params_.maximum_upload_delay; } |
| 55 TimeDelta retry_interval() const { return params_.upload_retry_interval; } | 55 TimeDelta retry_interval() const { return params_.upload_retry_interval; } |
| 56 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } | 56 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } |
| 57 | 57 |
| 58 bool upload_pending() { return upload_pending_; } | 58 bool upload_pending() { return upload_pending_; } |
| 59 | 59 |
| 60 const std::string& upload_report() { | 60 const std::string& upload_report() { |
| 61 DCHECK(upload_pending_); | 61 DCHECK(upload_pending_); |
| 62 return upload_report_; | 62 return upload_report_; |
| 63 } | 63 } |
| 64 | 64 |
| 65 const GURL& upload_url() { | 65 const GURL& upload_url() { |
| 66 DCHECK(upload_pending_); | 66 DCHECK(upload_pending_); |
| 67 return upload_url_; | 67 return upload_url_; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void CallUploadCallback(bool success) { | 70 void CallUploadCallback(bool success) { |
| 71 DCHECK(upload_pending_); | 71 DCHECK(upload_pending_); |
| 72 upload_callback_.Run(success); | 72 upload_callback_.Run(success); |
| 73 upload_pending_ = false; | 73 upload_pending_ = false; |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool CheckNoBeacons(int index) { | 76 bool CheckNoBeacons(size_t index) { |
| 77 BeaconVector beacons; | 77 BeaconVector beacons; |
| 78 context_.GetQueuedDataForTesting(index, &beacons, NULL, NULL); | 78 context_.GetQueuedDataForTesting(index, &beacons, NULL, NULL); |
| 79 return beacons.empty(); | 79 return beacons.empty(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool CheckCounts(int index, int expected_successful, int expected_failed) { | 82 bool CheckCounts(size_t index, |
| 83 int successful, failed; | 83 unsigned expected_successful, |
| 84 unsigned expected_failed) { |
| 85 unsigned successful, failed; |
| 84 context_.GetQueuedDataForTesting(index, NULL, &successful, &failed); | 86 context_.GetQueuedDataForTesting(index, NULL, &successful, &failed); |
| 85 return successful == expected_successful && failed == expected_failed; | 87 return successful == expected_successful && failed == expected_failed; |
| 86 } | 88 } |
| 87 | 89 |
| 88 MockTime time_; | 90 MockTime time_; |
| 89 DomainReliabilityDispatcher dispatcher_; | 91 DomainReliabilityDispatcher dispatcher_; |
| 90 DomainReliabilityScheduler::Params params_; | 92 DomainReliabilityScheduler::Params params_; |
| 91 MockUploader uploader_; | 93 MockUploader uploader_; |
| 92 DomainReliabilityContext context_; | 94 DomainReliabilityContext context_; |
| 93 | 95 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 }; | 148 }; |
| 147 | 149 |
| 148 TEST_F(DomainReliabilityContextTest, Create) { | 150 TEST_F(DomainReliabilityContextTest, Create) { |
| 149 EXPECT_TRUE(CheckNoBeacons(0)); | 151 EXPECT_TRUE(CheckNoBeacons(0)); |
| 150 EXPECT_TRUE(CheckCounts(0, 0, 0)); | 152 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
| 151 EXPECT_TRUE(CheckNoBeacons(1)); | 153 EXPECT_TRUE(CheckNoBeacons(1)); |
| 152 EXPECT_TRUE(CheckCounts(1, 0, 0)); | 154 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
| 153 } | 155 } |
| 154 | 156 |
| 155 TEST_F(DomainReliabilityContextTest, NoResource) { | 157 TEST_F(DomainReliabilityContextTest, NoResource) { |
| 158 GURL url("http://example/no_resource"); |
| 156 DomainReliabilityBeacon beacon = MakeBeacon(&time_); | 159 DomainReliabilityBeacon beacon = MakeBeacon(&time_); |
| 157 context_.AddBeacon(beacon, GURL("http://example/no_resource")); | 160 context_.OnBeacon(url, beacon); |
| 158 | 161 |
| 159 EXPECT_TRUE(CheckNoBeacons(0)); | 162 EXPECT_TRUE(CheckNoBeacons(0)); |
| 160 EXPECT_TRUE(CheckCounts(0, 0, 0)); | 163 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
| 161 EXPECT_TRUE(CheckNoBeacons(1)); | 164 EXPECT_TRUE(CheckNoBeacons(1)); |
| 162 EXPECT_TRUE(CheckCounts(1, 0, 0)); | 165 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
| 163 } | 166 } |
| 164 | 167 |
| 165 TEST_F(DomainReliabilityContextTest, NeverReport) { | 168 TEST_F(DomainReliabilityContextTest, NeverReport) { |
| 169 GURL url("http://example/never_report"); |
| 166 DomainReliabilityBeacon beacon = MakeBeacon(&time_); | 170 DomainReliabilityBeacon beacon = MakeBeacon(&time_); |
| 167 context_.AddBeacon(beacon, GURL("http://example/never_report")); | 171 context_.OnBeacon(url, beacon); |
| 168 | 172 |
| 169 EXPECT_TRUE(CheckNoBeacons(0)); | 173 EXPECT_TRUE(CheckNoBeacons(0)); |
| 170 EXPECT_TRUE(CheckCounts(0, 0, 0)); | 174 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
| 171 EXPECT_TRUE(CheckNoBeacons(1)); | 175 EXPECT_TRUE(CheckNoBeacons(1)); |
| 172 EXPECT_TRUE(CheckCounts(1, 1, 0)); | 176 EXPECT_TRUE(CheckCounts(1, 1, 0)); |
| 173 } | 177 } |
| 174 | 178 |
| 175 TEST_F(DomainReliabilityContextTest, AlwaysReport) { | 179 TEST_F(DomainReliabilityContextTest, AlwaysReport) { |
| 180 GURL url("http://example/always_report"); |
| 176 DomainReliabilityBeacon beacon = MakeBeacon(&time_); | 181 DomainReliabilityBeacon beacon = MakeBeacon(&time_); |
| 177 context_.AddBeacon(beacon, GURL("http://example/always_report")); | 182 context_.OnBeacon(url, beacon); |
| 178 | 183 |
| 179 BeaconVector beacons; | 184 BeaconVector beacons; |
| 180 context_.GetQueuedDataForTesting(0, &beacons, NULL, NULL); | 185 context_.GetQueuedDataForTesting(0, &beacons, NULL, NULL); |
| 181 EXPECT_EQ(1u, beacons.size()); | 186 EXPECT_EQ(1u, beacons.size()); |
| 182 EXPECT_TRUE(CheckCounts(0, 1, 0)); | 187 EXPECT_TRUE(CheckCounts(0, 1, 0)); |
| 183 EXPECT_TRUE(CheckNoBeacons(1)); | 188 EXPECT_TRUE(CheckNoBeacons(1)); |
| 184 EXPECT_TRUE(CheckCounts(1, 0, 0)); | 189 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
| 185 } | 190 } |
| 186 | 191 |
| 187 TEST_F(DomainReliabilityContextTest, ReportUpload) { | 192 TEST_F(DomainReliabilityContextTest, ReportUpload) { |
| 193 GURL url("http://example/always_report"); |
| 188 DomainReliabilityBeacon beacon = MakeBeacon(&time_); | 194 DomainReliabilityBeacon beacon = MakeBeacon(&time_); |
| 189 context_.AddBeacon(beacon, GURL("http://example/always_report")); | 195 context_.OnBeacon(url, beacon); |
| 190 | 196 |
| 191 const char* kExpectedReport = "{\"reporter\":\"chrome\"," | 197 const char* kExpectedReport = "{\"reporter\":\"chrome\"," |
| 192 "\"resource_reports\":[{\"beacons\":[{\"http_response_code\":200," | 198 "\"resource_reports\":[{\"beacons\":[{\"http_response_code\":200," |
| 193 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,\"server_ip\":" | 199 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,\"server_ip\":" |
| 194 "\"127.0.0.1\",\"status\":\"ok\"}],\"failed_requests\":0," | 200 "\"127.0.0.1\",\"status\":\"ok\"}],\"failed_requests\":0," |
| 195 "\"resource_name\":\"always_report\",\"successful_requests\":1}," | 201 "\"resource_name\":\"always_report\",\"successful_requests\":1}," |
| 196 "{\"beacons\":[],\"failed_requests\":0,\"resource_name\":" | 202 "{\"beacons\":[],\"failed_requests\":0,\"resource_name\":" |
| 197 "\"never_report\",\"successful_requests\":0}]}"; | 203 "\"never_report\",\"successful_requests\":0}]}"; |
| 198 | 204 |
| 199 time_.Advance(max_delay()); | 205 time_.Advance(max_delay()); |
| 200 EXPECT_TRUE(upload_pending()); | 206 EXPECT_TRUE(upload_pending()); |
| 201 EXPECT_EQ(kExpectedReport, upload_report()); | 207 EXPECT_EQ(kExpectedReport, upload_report()); |
| 202 EXPECT_EQ(GURL("https://example/upload"), upload_url()); | 208 EXPECT_EQ(GURL("https://example/upload"), upload_url()); |
| 203 CallUploadCallback(true); | 209 CallUploadCallback(true); |
| 204 } | 210 } |
| 205 | 211 |
| 206 } // namespace domain_reliability | 212 } // namespace domain_reliability |
| OLD | NEW |