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) { |
| 56 // Make sure that the last network change does not overlap requests |
| 57 // made in test cases, which start 250ms in the past (see |MakeBeacon|). |
| 58 last_network_change_time_ = time_.NowTicks(); |
| 59 time_.Advance(base::TimeDelta::FromSeconds(1)); |
| 60 } |
54 | 61 |
55 TimeDelta min_delay() const { return params_.minimum_upload_delay; } | 62 TimeDelta min_delay() const { return params_.minimum_upload_delay; } |
56 TimeDelta max_delay() const { return params_.maximum_upload_delay; } | 63 TimeDelta max_delay() const { return params_.maximum_upload_delay; } |
57 TimeDelta retry_interval() const { return params_.upload_retry_interval; } | 64 TimeDelta retry_interval() const { return params_.upload_retry_interval; } |
58 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } | 65 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); } |
59 | 66 |
60 bool upload_pending() { return upload_pending_; } | 67 bool upload_pending() { return upload_pending_; } |
61 | 68 |
62 const std::string& upload_report() { | 69 const std::string& upload_report() { |
63 DCHECK(upload_pending_); | 70 DCHECK(upload_pending_); |
(...skipping 19 matching lines...) Expand all Loading... |
83 | 90 |
84 bool CheckCounts(size_t index, | 91 bool CheckCounts(size_t index, |
85 unsigned expected_successful, | 92 unsigned expected_successful, |
86 unsigned expected_failed) { | 93 unsigned expected_failed) { |
87 unsigned successful, failed; | 94 unsigned successful, failed; |
88 context_.GetRequestCountsForTesting(index, &successful, &failed); | 95 context_.GetRequestCountsForTesting(index, &successful, &failed); |
89 return successful == expected_successful && failed == expected_failed; | 96 return successful == expected_successful && failed == expected_failed; |
90 } | 97 } |
91 | 98 |
92 MockTime time_; | 99 MockTime time_; |
| 100 base::TimeTicks last_network_change_time_; |
93 DomainReliabilityDispatcher dispatcher_; | 101 DomainReliabilityDispatcher dispatcher_; |
94 DomainReliabilityScheduler::Params params_; | 102 DomainReliabilityScheduler::Params params_; |
95 MockUploader uploader_; | 103 MockUploader uploader_; |
96 std::string upload_reporter_string_; | 104 std::string upload_reporter_string_; |
97 DomainReliabilityContext context_; | 105 DomainReliabilityContext context_; |
98 | 106 |
99 private: | 107 private: |
100 void OnUploadRequest( | 108 void OnUploadRequest( |
101 const std::string& report_json, | 109 const std::string& report_json, |
102 const GURL& upload_url, | 110 const GURL& upload_url, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 EXPECT_TRUE(upload_pending()); | 187 EXPECT_TRUE(upload_pending()); |
180 EXPECT_EQ(kExpectedReport, upload_report()); | 188 EXPECT_EQ(kExpectedReport, upload_report()); |
181 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); | 189 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
182 CallUploadCallback(true); | 190 CallUploadCallback(true); |
183 | 191 |
184 EXPECT_TRUE(CheckNoBeacons()); | 192 EXPECT_TRUE(CheckNoBeacons()); |
185 EXPECT_TRUE(CheckCounts(0, 0, 0)); | 193 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
186 EXPECT_TRUE(CheckCounts(1, 0, 0)); | 194 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
187 } | 195 } |
188 | 196 |
| 197 TEST_F(DomainReliabilityContextTest, ReportUpload_NetworkChanged) { |
| 198 GURL url("http://example/always_report"); |
| 199 DomainReliabilityBeacon beacon = MakeBeacon(&time_); |
| 200 context_.OnBeacon(url, beacon); |
| 201 |
| 202 BeaconVector beacons; |
| 203 context_.GetQueuedBeaconsForTesting(&beacons); |
| 204 EXPECT_EQ(1u, beacons.size()); |
| 205 EXPECT_TRUE(CheckCounts(0, 1, 0)); |
| 206 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
| 207 |
| 208 // N.B.: Assumes max_delay is 5 minutes. |
| 209 const char* kExpectedReport = "{" |
| 210 "\"config_version\":\"1\"," |
| 211 "\"entries\":[{\"domain\":\"localhost\"," |
| 212 "\"http_response_code\":200,\"network_changed\":true," |
| 213 "\"protocol\":\"HTTP\",\"request_age_ms\":300250," |
| 214 "\"request_elapsed_ms\":250,\"resource\":\"always_report\"," |
| 215 "\"server_ip\":\"127.0.0.1\",\"status\":\"ok\"}]," |
| 216 "\"reporter\":\"test-reporter\"," |
| 217 "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\"," |
| 218 "\"successful_requests\":1}]}"; |
| 219 |
| 220 // Simulate a network change after the request but before the upload. |
| 221 last_network_change_time_ = time_.NowTicks(); |
| 222 time_.Advance(max_delay()); |
| 223 |
| 224 EXPECT_TRUE(upload_pending()); |
| 225 EXPECT_EQ(kExpectedReport, upload_report()); |
| 226 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 227 CallUploadCallback(true); |
| 228 |
| 229 EXPECT_TRUE(CheckNoBeacons()); |
| 230 EXPECT_TRUE(CheckCounts(0, 0, 0)); |
| 231 EXPECT_TRUE(CheckCounts(1, 0, 0)); |
| 232 } |
| 233 |
189 } // namespace | 234 } // namespace |
190 } // namespace domain_reliability | 235 } // namespace domain_reliability |
OLD | NEW |