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(size_t index
) {
79 context_
.GetQueuedDataForTesting(index
, &beacons
, NULL
, NULL
);
80 return beacons
.empty();
83 bool CheckCounts(size_t index
,
84 unsigned expected_successful
,
85 unsigned expected_failed
) {
86 unsigned successful
, failed
;
87 context_
.GetQueuedDataForTesting(index
, NULL
, &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(0));
118 EXPECT_TRUE(CheckCounts(0, 0, 0));
119 EXPECT_TRUE(CheckNoBeacons(1));
120 EXPECT_TRUE(CheckCounts(1, 0, 0));
123 TEST_F(DomainReliabilityContextTest
, NoResource
) {
124 GURL
url("http://example/no_resource");
125 DomainReliabilityBeacon beacon
= MakeBeacon(&time_
);
126 context_
.OnBeacon(url
, beacon
);
128 EXPECT_TRUE(CheckNoBeacons(0));
129 EXPECT_TRUE(CheckCounts(0, 0, 0));
130 EXPECT_TRUE(CheckNoBeacons(1));
131 EXPECT_TRUE(CheckCounts(1, 0, 0));
134 TEST_F(DomainReliabilityContextTest
, NeverReport
) {
135 GURL
url("http://example/never_report");
136 DomainReliabilityBeacon beacon
= MakeBeacon(&time_
);
137 context_
.OnBeacon(url
, beacon
);
139 EXPECT_TRUE(CheckNoBeacons(0));
140 EXPECT_TRUE(CheckCounts(0, 0, 0));
141 EXPECT_TRUE(CheckNoBeacons(1));
142 EXPECT_TRUE(CheckCounts(1, 1, 0));
145 TEST_F(DomainReliabilityContextTest
, AlwaysReport
) {
146 GURL
url("http://example/always_report");
147 DomainReliabilityBeacon beacon
= MakeBeacon(&time_
);
148 context_
.OnBeacon(url
, beacon
);
150 BeaconVector beacons
;
151 context_
.GetQueuedDataForTesting(0, &beacons
, NULL
, NULL
);
152 EXPECT_EQ(1u, beacons
.size());
153 EXPECT_TRUE(CheckCounts(0, 1, 0));
154 EXPECT_TRUE(CheckNoBeacons(1));
155 EXPECT_TRUE(CheckCounts(1, 0, 0));
158 TEST_F(DomainReliabilityContextTest
, ReportUpload
) {
159 GURL
url("http://example/always_report");
160 DomainReliabilityBeacon beacon
= MakeBeacon(&time_
);
161 context_
.OnBeacon(url
, beacon
);
163 // N.B.: Assumes max_delay is 5 minutes.
164 const char* kExpectedReport
= "{\"config_version\":\"1\","
165 "\"reporter\":\"test-reporter\","
166 "\"resource_reports\":[{\"beacons\":[{\"http_response_code\":200,"
167 "\"protocol\":\"HTTP\","
168 "\"request_age_ms\":300250,\"request_elapsed_ms\":250,\"server_ip\":"
169 "\"127.0.0.1\",\"status\":\"ok\"}],\"failed_requests\":0,"
170 "\"resource_name\":\"always_report\",\"successful_requests\":1}]}";
172 time_
.Advance(max_delay());
173 EXPECT_TRUE(upload_pending());
174 EXPECT_EQ(kExpectedReport
, upload_report());
175 EXPECT_EQ(GURL("https://example/upload"), upload_url());
176 CallUploadCallback(true);
180 } // namespace domain_reliability