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/scheduler.h"
8 #include "base/time/time.h"
9 #include "components/domain_reliability/config.h"
10 #include "components/domain_reliability/test_util.h"
11 #include "components/domain_reliability/util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::TimeDelta
;
15 using base::TimeTicks
;
17 namespace domain_reliability
{
19 class DomainReliabilitySchedulerTest
: public testing::Test
{
21 DomainReliabilitySchedulerTest()
23 params_(CreateDefaultParams()),
24 callback_called_(false) {}
26 void CreateScheduler(int num_collectors
) {
27 DCHECK_LT(0, num_collectors
);
30 num_collectors_
= num_collectors
;
31 scheduler_
.reset(new DomainReliabilityScheduler(
35 base::Bind(&DomainReliabilitySchedulerTest::ScheduleUploadCallback
,
36 base::Unretained(this))));
39 static DomainReliabilityScheduler::Params
CreateDefaultParams() {
40 DomainReliabilityScheduler::Params params
;
41 params
.minimum_upload_delay
= base::TimeDelta::FromSeconds(60);
42 params
.maximum_upload_delay
= base::TimeDelta::FromSeconds(300);
43 params
.upload_retry_interval
= base::TimeDelta::FromSeconds(15);
47 ::testing::AssertionResult
CheckNoPendingUpload() {
50 if (!callback_called_
)
51 return ::testing::AssertionSuccess();
53 return ::testing::AssertionFailure()
54 << "expected no upload, got upload between "
55 << callback_min_
.InSeconds() << " and "
56 << callback_max_
.InSeconds() << " seconds from now";
59 ::testing::AssertionResult
CheckPendingUpload(TimeDelta expected_min
,
60 TimeDelta expected_max
) {
62 DCHECK_LE(expected_min
.InMicroseconds(), expected_max
.InMicroseconds());
64 if (callback_called_
&& expected_min
== callback_min_
65 && expected_max
== callback_max_
) {
66 callback_called_
= false;
67 return ::testing::AssertionSuccess();
70 if (callback_called_
) {
71 return ::testing::AssertionFailure()
72 << "expected upload between " << expected_min
.InSeconds()
73 << " and " << expected_max
.InSeconds() << " seconds from now, "
74 << "got upload between " << callback_min_
.InSeconds()
75 << " and " << callback_max_
.InSeconds() << " seconds from now";
77 return ::testing::AssertionFailure()
78 << "expected upload between " << expected_min
.InSeconds()
79 << " and " << expected_max
.InSeconds() << " seconds from now, "
84 ::testing::AssertionResult
CheckStartingUpload(size_t expected_collector
) {
86 DCHECK_GT(num_collectors_
, expected_collector
);
88 size_t collector
= scheduler_
->OnUploadStart();
89 if (collector
== expected_collector
)
90 return ::testing::AssertionSuccess();
92 return ::testing::AssertionFailure()
93 << "expected upload to collector " << expected_collector
94 << ", got upload to collector " << collector
;
97 TimeDelta
min_delay() const { return params_
.minimum_upload_delay
; }
98 TimeDelta
max_delay() const { return params_
.maximum_upload_delay
; }
99 TimeDelta
retry_interval() const { return params_
.upload_retry_interval
; }
100 TimeDelta
zero_delta() const { return base::TimeDelta::FromMicroseconds(0); }
103 void ScheduleUploadCallback(TimeDelta min
, TimeDelta max
) {
104 callback_called_
= true;
110 size_t num_collectors_
;
111 DomainReliabilityScheduler::Params params_
;
112 scoped_ptr
<DomainReliabilityScheduler
> scheduler_
;
114 bool callback_called_
;
115 TimeDelta callback_min_
;
116 TimeDelta callback_max_
;
119 TEST_F(DomainReliabilitySchedulerTest
, Create
) {
123 TEST_F(DomainReliabilitySchedulerTest
, UploadNotPendingWithoutBeacon
) {
126 ASSERT_TRUE(CheckNoPendingUpload());
129 TEST_F(DomainReliabilitySchedulerTest
, SuccessfulUploads
) {
132 scheduler_
->OnBeaconAdded();
133 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
134 time_
.Advance(min_delay());
135 ASSERT_TRUE(CheckStartingUpload(0));
136 scheduler_
->OnUploadComplete(true);
138 scheduler_
->OnBeaconAdded();
139 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
140 time_
.Advance(min_delay());
141 ASSERT_TRUE(CheckStartingUpload(0));
142 scheduler_
->OnUploadComplete(true);
145 TEST_F(DomainReliabilitySchedulerTest
, Failover
) {
148 scheduler_
->OnBeaconAdded();
149 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
150 time_
.Advance(min_delay());
151 ASSERT_TRUE(CheckStartingUpload(0));
152 scheduler_
->OnUploadComplete(false);
154 scheduler_
->OnBeaconAdded();
155 ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay()));
156 // Don't need to advance; should retry immediately.
157 ASSERT_TRUE(CheckStartingUpload(1));
158 scheduler_
->OnUploadComplete(true);
161 TEST_F(DomainReliabilitySchedulerTest
, FailedAllCollectors
) {
165 scheduler_
->OnBeaconAdded();
166 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
167 time_
.Advance(min_delay());
170 ASSERT_TRUE(CheckStartingUpload(0));
171 scheduler_
->OnUploadComplete(false);
173 ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay()));
174 // Don't need to advance; should retry immediately.
175 ASSERT_TRUE(CheckStartingUpload(1));
176 scheduler_
->OnUploadComplete(false);
178 ASSERT_TRUE(CheckPendingUpload(retry_interval(), max_delay() - min_delay()));
179 time_
.Advance(retry_interval());
181 // T = min_delay + retry_interval
182 ASSERT_TRUE(CheckStartingUpload(0));
183 scheduler_
->OnUploadComplete(false);
185 ASSERT_TRUE(CheckPendingUpload(
187 max_delay() - min_delay() - retry_interval()));
188 ASSERT_TRUE(CheckStartingUpload(1));
189 scheduler_
->OnUploadComplete(false);
192 // Make sure that the scheduler uses the first available collector at upload
193 // time, even if it wasn't available at scheduling time.
194 TEST_F(DomainReliabilitySchedulerTest
, DetermineCollectorAtUpload
) {
198 scheduler_
->OnBeaconAdded();
199 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
200 time_
.Advance(min_delay());
203 ASSERT_TRUE(CheckStartingUpload(0));
204 scheduler_
->OnUploadComplete(false);
206 ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay()));
207 time_
.Advance(retry_interval());
209 // T = min_delay + retry_interval; collector 0 should be active again.
210 ASSERT_TRUE(CheckStartingUpload(0));
211 scheduler_
->OnUploadComplete(true);
214 TEST_F(DomainReliabilitySchedulerTest
, BeaconWhilePending
) {
217 scheduler_
->OnBeaconAdded();
218 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
220 // Second beacon should not call callback again.
221 scheduler_
->OnBeaconAdded();
222 ASSERT_TRUE(CheckNoPendingUpload());
223 time_
.Advance(min_delay());
225 // No pending upload after beacon.
226 ASSERT_TRUE(CheckStartingUpload(0));
227 scheduler_
->OnUploadComplete(true);
228 ASSERT_TRUE(CheckNoPendingUpload());
231 TEST_F(DomainReliabilitySchedulerTest
, BeaconWhileUploading
) {
234 scheduler_
->OnBeaconAdded();
235 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
236 time_
.Advance(min_delay());
238 // If a beacon arrives during the upload, a new upload should be pending.
239 ASSERT_TRUE(CheckStartingUpload(0));
240 scheduler_
->OnBeaconAdded();
241 scheduler_
->OnUploadComplete(true);
242 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
244 time_
.Advance(min_delay());
245 ASSERT_TRUE(CheckStartingUpload(0));
246 scheduler_
->OnUploadComplete(true);
247 ASSERT_TRUE(CheckNoPendingUpload());
250 } // namespace domain_reliability