Make castv2 performance test work.
[chromium-blink-merge.git] / components / domain_reliability / context_unittest.cc
blob8633c48ff6f1aa8d99c2dde065162776d1d2c00a
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
3 // found in the LICENSE file.
5 #include "components/domain_reliability/context.h"
7 #include <map>
8 #include <string>
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "components/domain_reliability/beacon.h"
14 #include "components/domain_reliability/dispatcher.h"
15 #include "components/domain_reliability/scheduler.h"
16 #include "components/domain_reliability/test_util.h"
17 #include "components/domain_reliability/uploader.h"
18 #include "net/base/net_errors.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace domain_reliability {
23 namespace {
25 typedef std::vector<DomainReliabilityBeacon> BeaconVector;
27 DomainReliabilityBeacon MakeBeacon(MockableTime* time) {
28 DomainReliabilityBeacon beacon;
29 beacon.domain = "localhost";
30 beacon.status = "ok";
31 beacon.chrome_error = net::OK;
32 beacon.server_ip = "127.0.0.1";
33 beacon.protocol = "HTTP";
34 beacon.http_response_code = 200;
35 beacon.elapsed = base::TimeDelta::FromMilliseconds(250);
36 beacon.start_time = time->NowTicks() - beacon.elapsed;
37 return beacon;
40 class DomainReliabilityContextTest : public testing::Test {
41 protected:
42 DomainReliabilityContextTest()
43 : last_network_change_time_(time_.NowTicks()),
44 dispatcher_(&time_),
45 params_(MakeTestSchedulerParams()),
46 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest,
47 base::Unretained(this))),
48 upload_reporter_string_("test-reporter"),
49 context_(&time_,
50 params_,
51 upload_reporter_string_,
52 &last_network_change_time_,
53 &dispatcher_,
54 &uploader_,
55 MakeTestConfig().Pass()),
56 upload_pending_(false) {
57 // Make sure that the last network change does not overlap requests
58 // made in test cases, which start 250ms in the past (see |MakeBeacon|).
59 last_network_change_time_ = time_.NowTicks();
60 time_.Advance(base::TimeDelta::FromSeconds(1));
63 TimeDelta min_delay() const { return params_.minimum_upload_delay; }
64 TimeDelta max_delay() const { return params_.maximum_upload_delay; }
65 TimeDelta retry_interval() const { return params_.upload_retry_interval; }
66 TimeDelta zero_delta() const { return TimeDelta::FromMicroseconds(0); }
68 bool upload_pending() { return upload_pending_; }
70 const std::string& upload_report() {
71 DCHECK(upload_pending_);
72 return upload_report_;
75 const GURL& upload_url() {
76 DCHECK(upload_pending_);
77 return upload_url_;
80 void CallUploadCallback(DomainReliabilityUploader::UploadResult result) {
81 DCHECK(upload_pending_);
82 upload_callback_.Run(result);
83 upload_pending_ = false;
86 bool CheckNoBeacons() {
87 BeaconVector beacons;
88 context_.GetQueuedBeaconsForTesting(&beacons);
89 return beacons.empty();
92 bool CheckCounts(size_t index,
93 unsigned expected_successful,
94 unsigned expected_failed) {
95 unsigned successful, failed;
96 context_.GetRequestCountsForTesting(index, &successful, &failed);
97 return successful == expected_successful && failed == expected_failed;
100 MockTime time_;
101 base::TimeTicks last_network_change_time_;
102 DomainReliabilityDispatcher dispatcher_;
103 DomainReliabilityScheduler::Params params_;
104 MockUploader uploader_;
105 std::string upload_reporter_string_;
106 DomainReliabilityContext context_;
108 private:
109 void OnUploadRequest(
110 const std::string& report_json,
111 const GURL& upload_url,
112 const DomainReliabilityUploader::UploadCallback& callback) {
113 DCHECK(!upload_pending_);
114 upload_report_ = report_json;
115 upload_url_ = upload_url;
116 upload_callback_ = callback;
117 upload_pending_ = true;
120 bool upload_pending_;
121 std::string upload_report_;
122 GURL upload_url_;
123 DomainReliabilityUploader::UploadCallback upload_callback_;
126 TEST_F(DomainReliabilityContextTest, Create) {
127 EXPECT_TRUE(CheckNoBeacons());
128 EXPECT_TRUE(CheckCounts(0, 0, 0));
129 EXPECT_TRUE(CheckCounts(1, 0, 0));
132 TEST_F(DomainReliabilityContextTest, NoResource) {
133 GURL url("http://example/no_resource");
134 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
135 context_.OnBeacon(url, beacon);
137 EXPECT_TRUE(CheckNoBeacons());
138 EXPECT_TRUE(CheckCounts(0, 0, 0));
139 EXPECT_TRUE(CheckCounts(1, 0, 0));
142 TEST_F(DomainReliabilityContextTest, NeverReport) {
143 GURL url("http://example/never_report");
144 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
145 context_.OnBeacon(url, beacon);
147 EXPECT_TRUE(CheckNoBeacons());
148 EXPECT_TRUE(CheckCounts(0, 0, 0));
149 EXPECT_TRUE(CheckCounts(1, 1, 0));
152 TEST_F(DomainReliabilityContextTest, AlwaysReport) {
153 GURL url("http://example/always_report");
154 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
155 context_.OnBeacon(url, beacon);
157 BeaconVector beacons;
158 context_.GetQueuedBeaconsForTesting(&beacons);
159 EXPECT_EQ(1u, beacons.size());
160 EXPECT_TRUE(CheckCounts(0, 1, 0));
161 EXPECT_TRUE(CheckCounts(1, 0, 0));
164 TEST_F(DomainReliabilityContextTest, ReportUpload) {
165 GURL url("http://example/always_report");
166 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
167 context_.OnBeacon(url, beacon);
169 BeaconVector beacons;
170 context_.GetQueuedBeaconsForTesting(&beacons);
171 EXPECT_EQ(1u, beacons.size());
172 EXPECT_TRUE(CheckCounts(0, 1, 0));
173 EXPECT_TRUE(CheckCounts(1, 0, 0));
175 // N.B.: Assumes max_delay is 5 minutes.
176 const char* kExpectedReport = "{"
177 "\"config_version\":\"1\","
178 "\"entries\":[{\"domain\":\"localhost\","
179 "\"http_response_code\":200,\"network_changed\":false,"
180 "\"protocol\":\"HTTP\",\"request_age_ms\":300250,"
181 "\"request_elapsed_ms\":250,\"resource\":\"always_report\","
182 "\"server_ip\":\"127.0.0.1\",\"status\":\"ok\"}],"
183 "\"reporter\":\"test-reporter\","
184 "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\","
185 "\"successful_requests\":1}]}";
187 time_.Advance(max_delay());
188 EXPECT_TRUE(upload_pending());
189 EXPECT_EQ(kExpectedReport, upload_report());
190 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
192 DomainReliabilityUploader::UploadResult result;
193 result.status = DomainReliabilityUploader::UploadResult::SUCCESS;
194 CallUploadCallback(result);
196 EXPECT_TRUE(CheckNoBeacons());
197 EXPECT_TRUE(CheckCounts(0, 0, 0));
198 EXPECT_TRUE(CheckCounts(1, 0, 0));
201 TEST_F(DomainReliabilityContextTest, ReportUpload_NetworkChanged) {
202 GURL url("http://example/always_report");
203 DomainReliabilityBeacon beacon = MakeBeacon(&time_);
204 context_.OnBeacon(url, beacon);
206 BeaconVector beacons;
207 context_.GetQueuedBeaconsForTesting(&beacons);
208 EXPECT_EQ(1u, beacons.size());
209 EXPECT_TRUE(CheckCounts(0, 1, 0));
210 EXPECT_TRUE(CheckCounts(1, 0, 0));
212 // N.B.: Assumes max_delay is 5 minutes.
213 const char* kExpectedReport = "{"
214 "\"config_version\":\"1\","
215 "\"entries\":[{\"domain\":\"localhost\","
216 "\"http_response_code\":200,\"network_changed\":true,"
217 "\"protocol\":\"HTTP\",\"request_age_ms\":300250,"
218 "\"request_elapsed_ms\":250,\"resource\":\"always_report\","
219 "\"server_ip\":\"127.0.0.1\",\"status\":\"ok\"}],"
220 "\"reporter\":\"test-reporter\","
221 "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\","
222 "\"successful_requests\":1}]}";
224 // Simulate a network change after the request but before the upload.
225 last_network_change_time_ = time_.NowTicks();
226 time_.Advance(max_delay());
228 EXPECT_TRUE(upload_pending());
229 EXPECT_EQ(kExpectedReport, upload_report());
230 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
232 DomainReliabilityUploader::UploadResult result;
233 result.status = DomainReliabilityUploader::UploadResult::SUCCESS;
234 CallUploadCallback(result);
236 EXPECT_TRUE(CheckNoBeacons());
237 EXPECT_TRUE(CheckCounts(0, 0, 0));
238 EXPECT_TRUE(CheckCounts(1, 0, 0));
241 } // namespace
242 } // namespace domain_reliability