OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/domain_reliability/scheduler.h" | 5 #include "components/domain_reliability/scheduler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "components/domain_reliability/config.h" | 9 #include "components/domain_reliability/config.h" |
10 #include "components/domain_reliability/test_util.h" | 10 #include "components/domain_reliability/test_util.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 << callback_max_.InSeconds() << " seconds from now"; | 56 << callback_max_.InSeconds() << " seconds from now"; |
57 } | 57 } |
58 | 58 |
59 ::testing::AssertionResult CheckPendingUpload(TimeDelta expected_min, | 59 ::testing::AssertionResult CheckPendingUpload(TimeDelta expected_min, |
60 TimeDelta expected_max) { | 60 TimeDelta expected_max) { |
61 DCHECK(scheduler_); | 61 DCHECK(scheduler_); |
62 DCHECK_LE(expected_min.InMicroseconds(), expected_max.InMicroseconds()); | 62 DCHECK_LE(expected_min.InMicroseconds(), expected_max.InMicroseconds()); |
63 | 63 |
64 if (callback_called_ && expected_min == callback_min_ | 64 if (callback_called_ && expected_min == callback_min_ |
65 && expected_max == callback_max_) { | 65 && expected_max == callback_max_) { |
| 66 callback_called_ = false; |
66 return ::testing::AssertionSuccess(); | 67 return ::testing::AssertionSuccess(); |
67 } | 68 } |
68 | 69 |
69 if (callback_called_) { | 70 if (callback_called_) { |
70 return ::testing::AssertionFailure() | 71 return ::testing::AssertionFailure() |
71 << "expected upload between " << expected_min.InSeconds() | 72 << "expected upload between " << expected_min.InSeconds() |
72 << " and " << expected_max.InSeconds() << " seconds from now, " | 73 << " and " << expected_max.InSeconds() << " seconds from now, " |
73 << "got upload between " << callback_min_.InSeconds() | 74 << "got upload between " << callback_min_.InSeconds() |
74 << " and " << callback_max_.InSeconds() << " seconds from now"; | 75 << " and " << callback_max_.InSeconds() << " seconds from now"; |
75 } else { | 76 } else { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 scheduler_->OnUploadComplete(false); | 204 scheduler_->OnUploadComplete(false); |
204 | 205 |
205 ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay())); | 206 ASSERT_TRUE(CheckPendingUpload(zero_delta(), max_delay() - min_delay())); |
206 time_.Advance(retry_interval()); | 207 time_.Advance(retry_interval()); |
207 | 208 |
208 // T = min_delay + retry_interval; collector 0 should be active again. | 209 // T = min_delay + retry_interval; collector 0 should be active again. |
209 ASSERT_TRUE(CheckStartingUpload(0)); | 210 ASSERT_TRUE(CheckStartingUpload(0)); |
210 scheduler_->OnUploadComplete(true); | 211 scheduler_->OnUploadComplete(true); |
211 } | 212 } |
212 | 213 |
| 214 TEST_F(DomainReliabilitySchedulerTest, BeaconWhilePending) { |
| 215 CreateScheduler(1); |
| 216 |
| 217 scheduler_->OnBeaconAdded(); |
| 218 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay())); |
| 219 |
| 220 // Second beacon should not call callback again. |
| 221 scheduler_->OnBeaconAdded(); |
| 222 ASSERT_TRUE(CheckNoPendingUpload()); |
| 223 time_.Advance(min_delay()); |
| 224 |
| 225 // No pending upload after beacon. |
| 226 ASSERT_TRUE(CheckStartingUpload(0)); |
| 227 scheduler_->OnUploadComplete(true); |
| 228 ASSERT_TRUE(CheckNoPendingUpload()); |
| 229 } |
| 230 |
| 231 TEST_F(DomainReliabilitySchedulerTest, BeaconWhileUploading) { |
| 232 CreateScheduler(1); |
| 233 |
| 234 scheduler_->OnBeaconAdded(); |
| 235 ASSERT_TRUE(CheckPendingUpload(min_delay(), max_delay())); |
| 236 time_.Advance(min_delay()); |
| 237 |
| 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())); |
| 243 |
| 244 time_.Advance(min_delay()); |
| 245 ASSERT_TRUE(CheckStartingUpload(0)); |
| 246 scheduler_->OnUploadComplete(true); |
| 247 ASSERT_TRUE(CheckNoPendingUpload()); |
| 248 } |
| 249 |
213 } // namespace domain_reliability | 250 } // namespace domain_reliability |
OLD | NEW |