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"
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 "net/base/net_errors.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace domain_reliability
{
24 typedef std::vector
<DomainReliabilityBeacon
> BeaconVector
;
26 DomainReliabilityBeacon
MakeBeacon(MockableTime
* time
) {
27 DomainReliabilityBeacon beacon
;
29 beacon
.chrome_error
= net::OK
;
30 beacon
.server_ip
= "127.0.0.1";
31 beacon
.protocol
= "HTTP";
32 beacon
.http_response_code
= 200;
33 beacon
.elapsed
= base::TimeDelta::FromMilliseconds(250);
34 beacon
.start_time
= time
->NowTicks() - beacon
.elapsed
;
38 class DomainReliabilityContextTest
: public testing::Test
{
40 DomainReliabilityContextTest()
41 : dispatcher_(&time_
),
42 params_(MakeTestSchedulerParams()),
43 uploader_(base::Bind(&DomainReliabilityContextTest::OnUploadRequest
,
44 base::Unretained(this))),
45 upload_reporter_string_("test-reporter"),
48 upload_reporter_string_
,
51 MakeTestConfig().Pass()),
52 upload_pending_(false) {}
54 TimeDelta
min_delay() const { return params_
.minimum_upload_delay
; }
55 TimeDelta
max_delay() const { return params_
.maximum_upload_delay
; }
56 TimeDelta
retry_interval() const { return params_
.upload_retry_interval
; }
57 TimeDelta
zero_delta() const { return TimeDelta::FromMicroseconds(0); }
59 bool upload_pending() { return upload_pending_
; }
61 const std::string
& upload_report() {
62 DCHECK(upload_pending_
);
63 return upload_report_
;
66 const GURL
& upload_url() {
67 DCHECK(upload_pending_
);
71 void CallUploadCallback(bool success
) {
72 DCHECK(upload_pending_
);
73 upload_callback_
.Run(success
);
74 upload_pending_
= false;
77 bool CheckNoBeacons() {
79 context_
.GetQueuedBeaconsForTesting(&beacons
);
80 return beacons
.empty();
83 bool CheckCounts(size_t index
,
84 unsigned expected_successful
,
85 unsigned expected_failed
) {
86 unsigned successful
, failed
;
87 context_
.GetRequestCountsForTesting(index
, &successful
, &failed
);
88 return successful
== expected_successful
&& failed
== expected_failed
;
92 DomainReliabilityDispatcher dispatcher_
;
93 DomainReliabilityScheduler::Params params_
;
94 MockUploader uploader_
;
95 std::string upload_reporter_string_
;
96 DomainReliabilityContext context_
;
100 const std::string
& report_json
,
101 const GURL
& upload_url
,
102 const DomainReliabilityUploader::UploadCallback
& callback
) {
103 DCHECK(!upload_pending_
);
104 upload_report_
= report_json
;
105 upload_url_
= upload_url
;
106 upload_callback_
= callback
;
107 upload_pending_
= true;
110 bool upload_pending_
;
111 std::string upload_report_
;
113 DomainReliabilityUploader::UploadCallback upload_callback_
;
116 TEST_F(DomainReliabilityContextTest
, Create
) {
117 EXPECT_TRUE(CheckNoBeacons());
118 EXPECT_TRUE(CheckCounts(0, 0, 0));
119 EXPECT_TRUE(CheckCounts(1, 0, 0));
122 TEST_F(DomainReliabilityContextTest
, NoResource
) {
123 GURL
url("http://example/no_resource");
124 DomainReliabilityBeacon beacon
= MakeBeacon(&time_
);
125 context_
.OnBeacon(url
, beacon
);
127 EXPECT_TRUE(CheckNoBeacons());
128 EXPECT_TRUE(CheckCounts(0, 0, 0));
129 EXPECT_TRUE(CheckCounts(1, 0, 0));
132 TEST_F(DomainReliabilityContextTest
, NeverReport
) {
133 GURL
url("http://example/never_report");
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, 1, 0));
142 TEST_F(DomainReliabilityContextTest
, AlwaysReport
) {
143 GURL
url("http://example/always_report");
144 DomainReliabilityBeacon beacon
= MakeBeacon(&time_
);
145 context_
.OnBeacon(url
, beacon
);
147 BeaconVector beacons
;
148 context_
.GetQueuedBeaconsForTesting(&beacons
);
149 EXPECT_EQ(1u, beacons
.size());
150 EXPECT_TRUE(CheckCounts(0, 1, 0));
151 EXPECT_TRUE(CheckCounts(1, 0, 0));
154 TEST_F(DomainReliabilityContextTest
, ReportUpload
) {
155 GURL
url("http://example/always_report");
156 DomainReliabilityBeacon beacon
= MakeBeacon(&time_
);
157 context_
.OnBeacon(url
, beacon
);
159 BeaconVector beacons
;
160 context_
.GetQueuedBeaconsForTesting(&beacons
);
161 EXPECT_EQ(1u, beacons
.size());
162 EXPECT_TRUE(CheckCounts(0, 1, 0));
163 EXPECT_TRUE(CheckCounts(1, 0, 0));
165 // N.B.: Assumes max_delay is 5 minutes.
166 const char* kExpectedReport
= "{"
167 "\"config_version\":\"1\","
168 "\"entries\":[{\"http_response_code\":200,\"protocol\":\"HTTP\","
169 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,"
170 "\"resource\":\"always_report\",\"server_ip\":\"127.0.0.1\","
171 "\"status\":\"ok\"}],"
172 "\"reporter\":\"test-reporter\","
173 "\"resources\":[{\"failed_requests\":0,\"name\":\"always_report\","
174 "\"successful_requests\":1}]}";
176 time_
.Advance(max_delay());
177 EXPECT_TRUE(upload_pending());
178 EXPECT_EQ(kExpectedReport
, upload_report());
179 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url());
180 CallUploadCallback(true);
182 EXPECT_TRUE(CheckNoBeacons());
183 EXPECT_TRUE(CheckCounts(0, 0, 0));
184 EXPECT_TRUE(CheckCounts(1, 0, 0));
188 } // namespace domain_reliability