1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/domain_reliability/scheduler.h"
#include <stddef.h>
#include <memory>
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "components/domain_reliability/config.h"
#include "components/domain_reliability/test_util.h"
#include "components/domain_reliability/uploader.h"
#include "components/domain_reliability/util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace domain_reliability {
namespace {
using base::TimeTicks;
class DomainReliabilitySchedulerTest : public testing::Test {
public:
DomainReliabilitySchedulerTest()
: num_collectors_(0),
params_(MakeTestSchedulerParams()),
callback_called_(false) {}
void CreateScheduler(int num_collectors) {
DCHECK_LT(0, num_collectors);
DCHECK(!scheduler_);
num_collectors_ = num_collectors;
scheduler_ = std::make_unique<DomainReliabilityScheduler>(
&time_, num_collectors_, params_,
base::BindRepeating(
&DomainReliabilitySchedulerTest::ScheduleUploadCallback,
base::Unretained(this)));
scheduler_->MakeDeterministicForTesting();
}
void NotifySuccessfulUpload() {
DomainReliabilityUploader::UploadResult result;
result.status = DomainReliabilityUploader::UploadResult::SUCCESS;
scheduler_->OnUploadComplete(result);
}
void NotifyFailedUpload() {
DomainReliabilityUploader::UploadResult result;
result.status = DomainReliabilityUploader::UploadResult::FAILURE;
scheduler_->OnUploadComplete(result);
}
void NotifyRetryAfterUpload(base::TimeDelta retry_after) {
DomainReliabilityUploader::UploadResult result;
result.status = DomainReliabilityUploader::UploadResult::RETRY_AFTER;
result.retry_after = retry_after;
scheduler_->OnUploadComplete(result);
}
::testing::AssertionResult CheckNoPendingUpload() {
DCHECK(scheduler_);
if (!callback_called_)
return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< "expected no upload, got upload between "
<< callback_min_.InSeconds() << " and "
<< callback_max_.InSeconds() << " seconds from now";
}
::testing::AssertionResult CheckPendingUpload(base::TimeDelta expected_min,
base::TimeDelta expected_max) {
DCHECK(scheduler_);
DCHECK_LE(expected_min.InMicroseconds(), expected_max.InMicroseconds());
if (callback_called_ && expected_min == callback_min_
&& expected_max == callback_max_) {
callback_called_ = false;
return ::testing::AssertionSuccess();
}
if (callback_called_) {
return ::testing::AssertionFailure()
<< "expected upload between " << expected_min.InSeconds()
<< " and " << expected_max.InSeconds() << " seconds from now, "
<< "got upload between " << callback_min_.InSeconds()
<< " and " << callback_max_.InSeconds() << " seconds from now";
} else {
return ::testing::AssertionFailure()
<< "expected upload between " << expected_min.InSeconds()
<< " and " << expected_max.InSeconds() << " seconds from now, "
<< "got no upload";
}
}
::testing::AssertionResult CheckStartingUpload(size_t expected_collector) {
DCHECK(scheduler_);
DCHECK_GT(num_collectors_, expected_collector);
size_t collector = scheduler_->OnUploadStart();
if (collector == expected_collector)
return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< "expected upload to collector " << expected_collector
<< ", got upload to collector " << collector;
}
base::TimeDelta min_delay() const { return params_.minimum_upload_delay; }
base::TimeDelta max_delay() const { return params_.maximum_upload_delay; }
base::TimeDelta retry_interval() const {
return params_.upload_retry_interval;
}
base::TimeDelta zero_delta() const { return base::Microseconds(0); }
protected:
void ScheduleUploadCallback(base::TimeDelta min, base::TimeDelta max) {
callback_called_ = true;
callback_min_ = min;
callback_max_ = max;
}
MockTime time_;
size_t num_collectors_;
DomainReliabilityScheduler::Params params_;
std::unique_ptr<DomainReliabilityScheduler> scheduler_;
bool callback_called_;
base::TimeDelta callback_min_;
base::TimeDelta callback_max_;
};
TEST_F(DomainReliabilitySchedulerTest, Create) {
CreateScheduler(1);
}
TEST_F(DomainReliabilitySchedulerTest, UploadNotPendingWithoutBeacon) {
CreateScheduler(1);
ASSERT_TRUE(CheckNoPendingUpload());
}
TEST_F(DomainReliabilitySchedulerTest, SuccessfulUploads) {
CreateScheduler(1);
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
ASSERT_TRUE(CheckStartingUpload(0));
NotifySuccessfulUpload();
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
ASSERT_TRUE(CheckStartingUpload(0));
NotifySuccessfulUpload();
}
TEST_F(DomainReliabilitySchedulerTest, RetryAfter) {
CreateScheduler(1);
base::TimeDelta retry_after_interval = base::Minutes(30);
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
ASSERT_TRUE(CheckStartingUpload(0));
NotifyRetryAfterUpload(retry_after_interval);
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(retry_after_interval, retry_after_interval));
time_.Advance(retry_after_interval);
ASSERT_TRUE(CheckStartingUpload(0));
NotifySuccessfulUpload();
}
TEST_F(DomainReliabilitySchedulerTest, Failover) {
CreateScheduler(2);
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
ASSERT_TRUE(CheckStartingUpload(0));
NotifyFailedUpload();
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay()));
// Don't need to advance; should retry immediately.
ASSERT_TRUE(CheckStartingUpload(1));
NotifySuccessfulUpload();
}
TEST_F(DomainReliabilitySchedulerTest, FailedAllCollectors) {
CreateScheduler(2);
// T = 0
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
// T = min_delay
ASSERT_TRUE(CheckStartingUpload(0));
NotifyFailedUpload();
ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay()));
// Don't need to advance; should retry immediately.
ASSERT_TRUE(CheckStartingUpload(1));
NotifyFailedUpload();
ASSERT_TRUE(CheckPendingUpload(retry_interval(), max_delay() - min_delay()));
time_.Advance(retry_interval());
// T = min_delay + retry_interval
ASSERT_TRUE(CheckStartingUpload(0));
NotifyFailedUpload();
ASSERT_TRUE(CheckPendingUpload(
zero_delta(),
max_delay() - min_delay() - retry_interval()));
ASSERT_TRUE(CheckStartingUpload(1));
NotifyFailedUpload();
}
// Make sure that the scheduler uses the first available collector at upload
// time, even if it wasn't available at scheduling time.
TEST_F(DomainReliabilitySchedulerTest, DetermineCollectorAtUpload) {
CreateScheduler(2);
// T = 0
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
// T = min_delay
ASSERT_TRUE(CheckStartingUpload(0));
NotifyFailedUpload();
ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay()));
time_.Advance(retry_interval());
// T = min_delay + retry_interval; collector 0 should be active again.
ASSERT_TRUE(CheckStartingUpload(0));
NotifySuccessfulUpload();
}
TEST_F(DomainReliabilitySchedulerTest, BeaconWhilePending) {
CreateScheduler(1);
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
// Second beacon should not call callback again.
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckNoPendingUpload());
time_.Advance(min_delay());
// No pending upload after beacon.
ASSERT_TRUE(CheckStartingUpload(0));
NotifySuccessfulUpload();
ASSERT_TRUE(CheckNoPendingUpload());
}
TEST_F(DomainReliabilitySchedulerTest, BeaconWhileUploading) {
CreateScheduler(1);
scheduler_->OnBeaconAdded();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
// If a beacon arrives during the upload, a new upload should be pending.
ASSERT_TRUE(CheckStartingUpload(0));
scheduler_->OnBeaconAdded();
NotifySuccessfulUpload();
ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay()));
time_.Advance(min_delay());
ASSERT_TRUE(CheckStartingUpload(0));
NotifySuccessfulUpload();
ASSERT_TRUE(CheckNoPendingUpload());
}
} // namespace
} // namespace domain_reliability
|