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 21 matching lines...) Expand all Loading... |
32 beacon.protocol = "HTTP"; | 32 beacon.protocol = "HTTP"; |
33 beacon.http_response_code = 200; | 33 beacon.http_response_code = 200; |
34 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); | 34 beacon.elapsed = base::TimeDelta::FromMilliseconds(250); |
35 beacon.start_time = time->NowTicks() - beacon.elapsed; | 35 beacon.start_time = time->NowTicks() - beacon.elapsed; |
36 return beacon; | 36 return beacon; |
37 } | 37 } |
38 | 38 |
39 class DomainReliabilityContextTest : public testing::Test { | 39 class DomainReliabilityContextTest : public testing::Test { |
40 protected: | 40 protected: |
41 DomainReliabilityContextTest() | 41 DomainReliabilityContextTest() |
42 : dispatcher_(&time_), | 42 : last_network_change_time_(time_.NowTicks()), |
| 43 dispatcher_(&time_), |
43 params_(MakeTestSchedulerParams()), | 44 params_(MakeTestSchedulerParams()), |
44 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, | 45 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest, |
45 base::Unretained(this))), | 46 base::Unretained(this))), |
46 upload_reporter_string_("test-reporter"), | 47 upload_reporter_string_("test-reporter"), |
47 context_(&time_, | 48 context_(&time_, |
48 params_, | 49 params_, |
49 upload_reporter_string_, | 50 upload_reporter_string_, |
| 51 &last_network_change_time_, |
50 &dispatcher_, | 52 &dispatcher_, |
51 &uploader_, | 53 &uploader_, |
52 MakeTestConfig().Pass()), | 54 MakeTestConfig().Pass()), |
53 upload_pending_(false) {} | 55 upload_pending_(false) {} |
54 | 56 |
55 TimeDelta min_delay() const { return params_.minimum_upload_delay; } | 57 TimeDelta min_delay() const { return params_.minimum_upload_delay; } |
56 TimeDelta max_delay() const { return params_.maximum_upload_delay; } | 58 TimeDelta max_delay() const { return params_.maximum_upload_delay; } |
57 TimeDelta retry_interval() const { return params_.upload_retry_interval; } | 59 TimeDelta retry_interval() const { return params_.upload_retry_interval; } |
58 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } | 60 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } |
59 | 61 |
(...skipping 23 matching lines...) Expand all Loading... |
83 | 85 |
84 bool CheckCounts(size_t index, | 86 bool CheckCounts(size_t index, |
85 unsigned expected_successful, | 87 unsigned expected_successful, |
86 unsigned expected_failed) { | 88 unsigned expected_failed) { |
87 unsigned successful, failed; | 89 unsigned successful, failed; |
88 context_.GetRequestCountsForTesting(index, &successful, &failed); | 90 context_.GetRequestCountsForTesting(index, &successful, &failed); |
89 return successful == expected_successful && failed == expected_failed; | 91 return successful == expected_successful && failed == expected_failed; |
90 } | 92 } |
91 | 93 |
92 MockTime time_; | 94 MockTime time_; |
| 95 base::TimeTicks last_network_change_time_; |
93 DomainReliabilityDispatcher dispatcher_; | 96 DomainReliabilityDispatcher dispatcher_; |
94 DomainReliabilityScheduler::Params params_; | 97 DomainReliabilityScheduler::Params params_; |
95 MockUploader uploader_; | 98 MockUploader uploader_; |
96 std::string upload_reporter_string_; | 99 std::string upload_reporter_string_; |
97 DomainReliabilityContext context_; | 100 DomainReliabilityContext context_; |
98 | 101 |
99 private: | 102 private: |
100 void OnUploadRequest( | 103 void OnUploadRequest( |
101 const std::string& report_json, | 104 const std::string& report_json, |
102 const GURL& upload_url, | 105 const GURL& upload_url, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 EXPECT_TRUE(upload_pending()); | 182 EXPECT_TRUE(upload_pending()); |
180 EXPECT_EQ(kExpectedReport, upload_report()); | 183 EXPECT_EQ(kExpectedReport, upload_report()); |
181 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); | 184 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
182 CallUploadCallback(true); | 185 CallUploadCallback(true); |
183 | 186 |
184 EXPECT_TRUE(CheckNoBeacons()); | 187 EXPECT_TRUE(CheckNoBeacons()); |
185 EXPECT_TRUE(CheckCounts(0, 0, 0)); | 188 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
186 EXPECT_TRUE(CheckCounts(1, 0, 0)); | 189 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
187 } | 190 } |
188 | 191 |
| 192 TEST_F(DomainReliabilityContextTest, ReportUpload_NetworkChanged) { |
| 193 GURL url("http://example/always_report"); |
| 194 DomainReliabilityBeacon beacon = MakeBeacon(&time_); |
| 195 context_.OnBeacon(url, beacon); |
| 196 |
| 197 BeaconVector beacons; |
| 198 context_.GetQueuedBeaconsForTesting(&beacons); |
| 199 EXPECT_EQ(1u, beacons.size()); |
| 200 EXPECT_TRUE(CheckCounts(0, 1, 0)); |
| 201 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
| 202 |
| 203 // N.B.: Assumes max_delay is 5 minutes. |
| 204 const char* kExpectedReport = "{" |
| 205 "\"config_version\":\"1\"," |
| 206 "\"entries\":[{\"domain\":\"localhost\"," |
| 207 "\"http_response_code\":200,\"network_changed\":true," |
| 208 "\"protocol\":\"HTTP\",\"request_age_ms\":300250," |
| 209 "\"request_elapsed_ms\":250,\"resource\":\"always_report\"," |
| 210 "\"server_ip\":\"127.0.0.1\",\"status\":\"ok\"}]," |
| 211 "\"reporter\":\"test-reporter\"," |
| 212 "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\"," |
| 213 "\"successful_requests\":1}]}"; |
| 214 |
| 215 time_.Advance(max_delay()); |
| 216 last_network_change_time_ = time_.NowTicks(); |
| 217 EXPECT_TRUE(upload_pending()); |
| 218 EXPECT_EQ(kExpectedReport, upload_report()); |
| 219 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 220 CallUploadCallback(true); |
| 221 |
| 222 EXPECT_TRUE(CheckNoBeacons()); |
| 223 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
| 224 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
| 225 } |
| 226 |
189 } // namespace | 227 } // namespace |
190 } // namespace domain_reliability | 228 } // namespace domain_reliability |
OLD | NEW |