OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/base/backoff_entry.h" | 5 #include "net/base/backoff_entry.h" |
6 | |
7 #include "base/time/tick_clock.h" | |
6 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
7 | 9 |
8 namespace { | 10 namespace { |
9 | 11 |
10 using base::TimeDelta; | 12 using base::TimeDelta; |
11 using base::TimeTicks; | 13 using base::TimeTicks; |
12 using net::BackoffEntry; | 14 using net::BackoffEntry; |
13 | 15 |
14 BackoffEntry::Policy base_policy = { 0, 1000, 2.0, 0.0, 20000, 2000, false }; | 16 BackoffEntry::Policy base_policy = { 0, 1000, 2.0, 0.0, 20000, 2000, false }; |
15 | 17 |
16 class TestBackoffEntry : public BackoffEntry { | 18 class TestTickClock : public base::TickClock { |
17 public: | 19 public: |
18 explicit TestBackoffEntry(const Policy* const policy) | 20 TimeTicks NowTicks() override { return now_ticks_; } |
19 : BackoffEntry(policy), | 21 void set_now(TimeTicks now) { now_ticks_ = now; } |
20 now_(TimeTicks()) { | |
21 // Work around initialization in constructor not picking up | |
22 // fake time. | |
23 SetCustomReleaseTime(TimeTicks()); | |
24 } | |
25 | |
26 ~TestBackoffEntry() override {} | |
27 | |
28 TimeTicks ImplGetTimeNow() const override { return now_; } | |
29 | |
30 void set_now(const TimeTicks& now) { | |
31 now_ = now; | |
32 } | |
33 | 22 |
34 private: | 23 private: |
35 TimeTicks now_; | 24 TimeTicks now_ticks_; |
mmenke
2015/04/10 15:50:43
DISALLOW_COPY_AND_ASSIGN? (+ include base/macros.
johnme
2015/04/10 16:23:36
Done.
| |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(TestBackoffEntry); | |
38 }; | 25 }; |
39 | 26 |
40 TEST(BackoffEntryTest, BaseTest) { | 27 TEST(BackoffEntryTest, BaseTest) { |
41 TestBackoffEntry entry(&base_policy); | 28 TestTickClock now_ticks; |
29 BackoffEntry entry(&base_policy, &now_ticks); | |
42 EXPECT_FALSE(entry.ShouldRejectRequest()); | 30 EXPECT_FALSE(entry.ShouldRejectRequest()); |
43 EXPECT_EQ(TimeDelta(), entry.GetTimeUntilRelease()); | 31 EXPECT_EQ(TimeDelta(), entry.GetTimeUntilRelease()); |
44 | 32 |
45 entry.InformOfRequest(false); | 33 entry.InformOfRequest(false); |
46 EXPECT_TRUE(entry.ShouldRejectRequest()); | 34 EXPECT_TRUE(entry.ShouldRejectRequest()); |
47 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); | 35 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
48 } | 36 } |
49 | 37 |
50 TEST(BackoffEntryTest, CanDiscardNeverExpires) { | 38 TEST(BackoffEntryTest, CanDiscardNeverExpires) { |
51 BackoffEntry::Policy never_expires_policy = base_policy; | 39 BackoffEntry::Policy never_expires_policy = base_policy; |
52 never_expires_policy.entry_lifetime_ms = -1; | 40 never_expires_policy.entry_lifetime_ms = -1; |
53 TestBackoffEntry never_expires(&never_expires_policy); | 41 TestTickClock now_ticks; |
42 BackoffEntry never_expires(&never_expires_policy, &now_ticks); | |
54 EXPECT_FALSE(never_expires.CanDiscard()); | 43 EXPECT_FALSE(never_expires.CanDiscard()); |
55 never_expires.set_now(TimeTicks() + TimeDelta::FromDays(100)); | 44 now_ticks.set_now(TimeTicks() + TimeDelta::FromDays(100)); |
56 EXPECT_FALSE(never_expires.CanDiscard()); | 45 EXPECT_FALSE(never_expires.CanDiscard()); |
57 } | 46 } |
58 | 47 |
59 TEST(BackoffEntryTest, CanDiscard) { | 48 TEST(BackoffEntryTest, CanDiscard) { |
60 TestBackoffEntry entry(&base_policy); | 49 TestTickClock now_ticks; |
50 BackoffEntry entry(&base_policy, &now_ticks); | |
61 // Because lifetime is non-zero, we shouldn't be able to discard yet. | 51 // Because lifetime is non-zero, we shouldn't be able to discard yet. |
62 EXPECT_FALSE(entry.CanDiscard()); | 52 EXPECT_FALSE(entry.CanDiscard()); |
63 | 53 |
64 // Test the "being used" case. | 54 // Test the "being used" case. |
65 entry.InformOfRequest(false); | 55 entry.InformOfRequest(false); |
66 EXPECT_FALSE(entry.CanDiscard()); | 56 EXPECT_FALSE(entry.CanDiscard()); |
67 | 57 |
68 // Test the case where there are errors but we can time out. | 58 // Test the case where there are errors but we can time out. |
69 entry.set_now( | 59 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1)); |
70 entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1)); | |
71 EXPECT_FALSE(entry.CanDiscard()); | 60 EXPECT_FALSE(entry.CanDiscard()); |
72 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | 61 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
73 base_policy.maximum_backoff_ms + 1)); | 62 base_policy.maximum_backoff_ms + 1)); |
74 EXPECT_TRUE(entry.CanDiscard()); | 63 EXPECT_TRUE(entry.CanDiscard()); |
75 | 64 |
76 // Test the final case (no errors, dependent only on specified lifetime). | 65 // Test the final case (no errors, dependent only on specified lifetime). |
77 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | 66 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
78 base_policy.entry_lifetime_ms - 1)); | 67 base_policy.entry_lifetime_ms - 1)); |
79 entry.InformOfRequest(true); | 68 entry.InformOfRequest(true); |
80 EXPECT_FALSE(entry.CanDiscard()); | 69 EXPECT_FALSE(entry.CanDiscard()); |
81 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | 70 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
82 base_policy.entry_lifetime_ms)); | 71 base_policy.entry_lifetime_ms)); |
83 EXPECT_TRUE(entry.CanDiscard()); | 72 EXPECT_TRUE(entry.CanDiscard()); |
84 } | 73 } |
85 | 74 |
86 TEST(BackoffEntryTest, CanDiscardAlwaysDelay) { | 75 TEST(BackoffEntryTest, CanDiscardAlwaysDelay) { |
87 BackoffEntry::Policy always_delay_policy = base_policy; | 76 BackoffEntry::Policy always_delay_policy = base_policy; |
88 always_delay_policy.always_use_initial_delay = true; | 77 always_delay_policy.always_use_initial_delay = true; |
89 always_delay_policy.entry_lifetime_ms = 0; | 78 always_delay_policy.entry_lifetime_ms = 0; |
90 | 79 |
91 TestBackoffEntry entry(&always_delay_policy); | 80 TestTickClock now_ticks; |
81 BackoffEntry entry(&always_delay_policy, &now_ticks); | |
92 | 82 |
93 // Because lifetime is non-zero, we shouldn't be able to discard yet. | 83 // Because lifetime is non-zero, we shouldn't be able to discard yet. |
94 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); | 84 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); |
95 EXPECT_TRUE(entry.CanDiscard()); | 85 EXPECT_TRUE(entry.CanDiscard()); |
96 | 86 |
97 // Even with no failures, we wait until the delay before we allow discard. | 87 // Even with no failures, we wait until the delay before we allow discard. |
98 entry.InformOfRequest(true); | 88 entry.InformOfRequest(true); |
99 EXPECT_FALSE(entry.CanDiscard()); | 89 EXPECT_FALSE(entry.CanDiscard()); |
100 | 90 |
101 // Wait until the delay expires, and we can discard the entry again. | 91 // Wait until the delay expires, and we can discard the entry again. |
102 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1000)); | 92 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1000)); |
103 EXPECT_TRUE(entry.CanDiscard()); | 93 EXPECT_TRUE(entry.CanDiscard()); |
104 } | 94 } |
105 | 95 |
106 TEST(BackoffEntryTest, CanDiscardNotStored) { | 96 TEST(BackoffEntryTest, CanDiscardNotStored) { |
107 BackoffEntry::Policy no_store_policy = base_policy; | 97 BackoffEntry::Policy no_store_policy = base_policy; |
108 no_store_policy.entry_lifetime_ms = 0; | 98 no_store_policy.entry_lifetime_ms = 0; |
109 TestBackoffEntry not_stored(&no_store_policy); | 99 TestTickClock now_ticks; |
100 BackoffEntry not_stored(&no_store_policy, &now_ticks); | |
110 EXPECT_TRUE(not_stored.CanDiscard()); | 101 EXPECT_TRUE(not_stored.CanDiscard()); |
111 } | 102 } |
112 | 103 |
113 TEST(BackoffEntryTest, ShouldIgnoreFirstTwo) { | 104 TEST(BackoffEntryTest, ShouldIgnoreFirstTwo) { |
114 BackoffEntry::Policy lenient_policy = base_policy; | 105 BackoffEntry::Policy lenient_policy = base_policy; |
115 lenient_policy.num_errors_to_ignore = 2; | 106 lenient_policy.num_errors_to_ignore = 2; |
116 | 107 |
117 BackoffEntry entry(&lenient_policy); | 108 BackoffEntry entry(&lenient_policy); |
118 | 109 |
119 entry.InformOfRequest(false); | 110 entry.InformOfRequest(false); |
120 EXPECT_FALSE(entry.ShouldRejectRequest()); | 111 EXPECT_FALSE(entry.ShouldRejectRequest()); |
121 | 112 |
122 entry.InformOfRequest(false); | 113 entry.InformOfRequest(false); |
123 EXPECT_FALSE(entry.ShouldRejectRequest()); | 114 EXPECT_FALSE(entry.ShouldRejectRequest()); |
124 | 115 |
125 entry.InformOfRequest(false); | 116 entry.InformOfRequest(false); |
126 EXPECT_TRUE(entry.ShouldRejectRequest()); | 117 EXPECT_TRUE(entry.ShouldRejectRequest()); |
127 } | 118 } |
128 | 119 |
129 TEST(BackoffEntryTest, ReleaseTimeCalculation) { | 120 TEST(BackoffEntryTest, ReleaseTimeCalculation) { |
130 TestBackoffEntry entry(&base_policy); | 121 TestTickClock now_ticks; |
122 BackoffEntry entry(&base_policy, &now_ticks); | |
131 | 123 |
132 // With zero errors, should return "now". | 124 // With zero errors, should return "now". |
133 TimeTicks result = entry.GetReleaseTime(); | 125 TimeTicks result = entry.GetReleaseTime(); |
134 EXPECT_EQ(entry.ImplGetTimeNow(), result); | 126 EXPECT_EQ(now_ticks.NowTicks(), result); |
135 | 127 |
136 // 1 error. | 128 // 1 error. |
137 entry.InformOfRequest(false); | 129 entry.InformOfRequest(false); |
138 result = entry.GetReleaseTime(); | 130 result = entry.GetReleaseTime(); |
139 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(1000), result); | 131 EXPECT_EQ(now_ticks.NowTicks() + TimeDelta::FromMilliseconds(1000), result); |
140 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); | 132 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
141 | 133 |
142 // 2 errors. | 134 // 2 errors. |
143 entry.InformOfRequest(false); | 135 entry.InformOfRequest(false); |
144 result = entry.GetReleaseTime(); | 136 result = entry.GetReleaseTime(); |
145 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(2000), result); | 137 EXPECT_EQ(now_ticks.NowTicks() + TimeDelta::FromMilliseconds(2000), result); |
146 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); | 138 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); |
147 | 139 |
148 // 3 errors. | 140 // 3 errors. |
149 entry.InformOfRequest(false); | 141 entry.InformOfRequest(false); |
150 result = entry.GetReleaseTime(); | 142 result = entry.GetReleaseTime(); |
151 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); | 143 EXPECT_EQ(now_ticks.NowTicks() + TimeDelta::FromMilliseconds(4000), result); |
152 EXPECT_EQ(TimeDelta::FromMilliseconds(4000), entry.GetTimeUntilRelease()); | 144 EXPECT_EQ(TimeDelta::FromMilliseconds(4000), entry.GetTimeUntilRelease()); |
153 | 145 |
154 // 6 errors (to check it doesn't pass maximum). | 146 // 6 errors (to check it doesn't pass maximum). |
155 entry.InformOfRequest(false); | 147 entry.InformOfRequest(false); |
156 entry.InformOfRequest(false); | 148 entry.InformOfRequest(false); |
157 entry.InformOfRequest(false); | 149 entry.InformOfRequest(false); |
158 result = entry.GetReleaseTime(); | 150 result = entry.GetReleaseTime(); |
159 EXPECT_EQ( | 151 EXPECT_EQ(now_ticks.NowTicks() + TimeDelta::FromMilliseconds(20000), result); |
160 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(20000), result); | |
161 } | 152 } |
162 | 153 |
163 TEST(BackoffEntryTest, ReleaseTimeCalculationAlwaysDelay) { | 154 TEST(BackoffEntryTest, ReleaseTimeCalculationAlwaysDelay) { |
164 BackoffEntry::Policy always_delay_policy = base_policy; | 155 BackoffEntry::Policy always_delay_policy = base_policy; |
165 always_delay_policy.always_use_initial_delay = true; | 156 always_delay_policy.always_use_initial_delay = true; |
166 always_delay_policy.num_errors_to_ignore = 2; | 157 always_delay_policy.num_errors_to_ignore = 2; |
167 | 158 |
168 TestBackoffEntry entry(&always_delay_policy); | 159 TestTickClock now_ticks; |
160 BackoffEntry entry(&always_delay_policy, &now_ticks); | |
169 | 161 |
170 // With previous requests, should return "now". | 162 // With previous requests, should return "now". |
171 TimeTicks result = entry.GetReleaseTime(); | 163 TimeTicks result = entry.GetReleaseTime(); |
172 EXPECT_EQ(TimeDelta(), entry.GetTimeUntilRelease()); | 164 EXPECT_EQ(TimeDelta(), entry.GetTimeUntilRelease()); |
173 | 165 |
174 // 1 error. | 166 // 1 error. |
175 entry.InformOfRequest(false); | 167 entry.InformOfRequest(false); |
176 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); | 168 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
177 | 169 |
178 // 2 errors. | 170 // 2 errors. |
(...skipping 15 matching lines...) Expand all Loading... | |
194 entry.InformOfRequest(false); | 186 entry.InformOfRequest(false); |
195 result = entry.GetReleaseTime(); | 187 result = entry.GetReleaseTime(); |
196 EXPECT_EQ(TimeDelta::FromMilliseconds(20000), entry.GetTimeUntilRelease()); | 188 EXPECT_EQ(TimeDelta::FromMilliseconds(20000), entry.GetTimeUntilRelease()); |
197 } | 189 } |
198 | 190 |
199 TEST(BackoffEntryTest, ReleaseTimeCalculationWithJitter) { | 191 TEST(BackoffEntryTest, ReleaseTimeCalculationWithJitter) { |
200 for (int i = 0; i < 10; ++i) { | 192 for (int i = 0; i < 10; ++i) { |
201 BackoffEntry::Policy jittery_policy = base_policy; | 193 BackoffEntry::Policy jittery_policy = base_policy; |
202 jittery_policy.jitter_factor = 0.2; | 194 jittery_policy.jitter_factor = 0.2; |
203 | 195 |
204 TestBackoffEntry entry(&jittery_policy); | 196 TestTickClock now_ticks; |
197 BackoffEntry entry(&jittery_policy, &now_ticks); | |
205 | 198 |
206 entry.InformOfRequest(false); | 199 entry.InformOfRequest(false); |
207 entry.InformOfRequest(false); | 200 entry.InformOfRequest(false); |
208 entry.InformOfRequest(false); | 201 entry.InformOfRequest(false); |
209 TimeTicks result = entry.GetReleaseTime(); | 202 TimeTicks result = entry.GetReleaseTime(); |
210 EXPECT_LE( | 203 EXPECT_LE(now_ticks.NowTicks() + TimeDelta::FromMilliseconds(3200), result); |
211 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(3200), result); | 204 EXPECT_GE(now_ticks.NowTicks() + TimeDelta::FromMilliseconds(4000), result); |
212 EXPECT_GE( | |
213 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); | |
214 } | 205 } |
215 } | 206 } |
216 | 207 |
217 TEST(BackoffEntryTest, FailureThenSuccess) { | 208 TEST(BackoffEntryTest, FailureThenSuccess) { |
218 TestBackoffEntry entry(&base_policy); | 209 TestTickClock now_ticks; |
210 BackoffEntry entry(&base_policy, &now_ticks); | |
219 | 211 |
220 // Failure count 1, establishes horizon. | 212 // Failure count 1, establishes horizon. |
221 entry.InformOfRequest(false); | 213 entry.InformOfRequest(false); |
222 TimeTicks release_time = entry.GetReleaseTime(); | 214 TimeTicks release_time = entry.GetReleaseTime(); |
223 EXPECT_EQ(TimeTicks() + TimeDelta::FromMilliseconds(1000), release_time); | 215 EXPECT_EQ(TimeTicks() + TimeDelta::FromMilliseconds(1000), release_time); |
224 | 216 |
225 // Success, failure count 0, should not advance past | 217 // Success, failure count 0, should not advance past |
226 // the horizon that was already set. | 218 // the horizon that was already set. |
227 entry.set_now(release_time - TimeDelta::FromMilliseconds(200)); | 219 now_ticks.set_now(release_time - TimeDelta::FromMilliseconds(200)); |
228 entry.InformOfRequest(true); | 220 entry.InformOfRequest(true); |
229 EXPECT_EQ(release_time, entry.GetReleaseTime()); | 221 EXPECT_EQ(release_time, entry.GetReleaseTime()); |
230 | 222 |
231 // Failure, failure count 1. | 223 // Failure, failure count 1. |
232 entry.InformOfRequest(false); | 224 entry.InformOfRequest(false); |
233 EXPECT_EQ(release_time + TimeDelta::FromMilliseconds(800), | 225 EXPECT_EQ(release_time + TimeDelta::FromMilliseconds(800), |
234 entry.GetReleaseTime()); | 226 entry.GetReleaseTime()); |
235 } | 227 } |
236 | 228 |
237 TEST(BackoffEntryTest, FailureThenSuccessAlwaysDelay) { | 229 TEST(BackoffEntryTest, FailureThenSuccessAlwaysDelay) { |
238 BackoffEntry::Policy always_delay_policy = base_policy; | 230 BackoffEntry::Policy always_delay_policy = base_policy; |
239 always_delay_policy.always_use_initial_delay = true; | 231 always_delay_policy.always_use_initial_delay = true; |
240 always_delay_policy.num_errors_to_ignore = 1; | 232 always_delay_policy.num_errors_to_ignore = 1; |
241 | 233 |
242 TestBackoffEntry entry(&always_delay_policy); | 234 TestTickClock now_ticks; |
235 BackoffEntry entry(&always_delay_policy, &now_ticks); | |
243 | 236 |
244 // Failure count 1. | 237 // Failure count 1. |
245 entry.InformOfRequest(false); | 238 entry.InformOfRequest(false); |
246 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); | 239 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
247 | 240 |
248 // Failure count 2. | 241 // Failure count 2. |
249 entry.InformOfRequest(false); | 242 entry.InformOfRequest(false); |
250 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); | 243 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); |
251 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); | 244 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); |
252 | 245 |
253 // Success. We should go back to the original delay. | 246 // Success. We should go back to the original delay. |
254 entry.InformOfRequest(true); | 247 entry.InformOfRequest(true); |
255 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); | 248 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
256 | 249 |
257 // Failure count reaches 2 again. We should increase the delay once more. | 250 // Failure count reaches 2 again. We should increase the delay once more. |
258 entry.InformOfRequest(false); | 251 entry.InformOfRequest(false); |
259 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); | 252 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); |
260 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); | 253 now_ticks.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); |
261 } | 254 } |
262 | 255 |
263 TEST(BackoffEntryTest, RetainCustomHorizon) { | 256 TEST(BackoffEntryTest, RetainCustomHorizon) { |
264 TestBackoffEntry custom(&base_policy); | 257 TestTickClock now_ticks; |
258 BackoffEntry custom(&base_policy, &now_ticks); | |
265 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); | 259 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); |
266 custom.SetCustomReleaseTime(custom_horizon); | 260 custom.SetCustomReleaseTime(custom_horizon); |
267 custom.InformOfRequest(false); | 261 custom.InformOfRequest(false); |
268 custom.InformOfRequest(true); | 262 custom.InformOfRequest(true); |
269 custom.set_now(TimeTicks() + TimeDelta::FromDays(2)); | 263 now_ticks.set_now(TimeTicks() + TimeDelta::FromDays(2)); |
270 custom.InformOfRequest(false); | 264 custom.InformOfRequest(false); |
271 custom.InformOfRequest(true); | 265 custom.InformOfRequest(true); |
272 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); | 266 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); |
273 | 267 |
274 // Now check that once we are at or past the custom horizon, | 268 // Now check that once we are at or past the custom horizon, |
275 // we get normal behavior. | 269 // we get normal behavior. |
276 custom.set_now(TimeTicks() + TimeDelta::FromDays(3)); | 270 now_ticks.set_now(TimeTicks() + TimeDelta::FromDays(3)); |
277 custom.InformOfRequest(false); | 271 custom.InformOfRequest(false); |
278 EXPECT_EQ( | 272 EXPECT_EQ( |
279 TimeTicks() + TimeDelta::FromDays(3) + TimeDelta::FromMilliseconds(1000), | 273 TimeTicks() + TimeDelta::FromDays(3) + TimeDelta::FromMilliseconds(1000), |
280 custom.GetReleaseTime()); | 274 custom.GetReleaseTime()); |
281 } | 275 } |
282 | 276 |
283 TEST(BackoffEntryTest, RetainCustomHorizonWhenInitialErrorsIgnored) { | 277 TEST(BackoffEntryTest, RetainCustomHorizonWhenInitialErrorsIgnored) { |
284 // Regression test for a bug discovered during code review. | 278 // Regression test for a bug discovered during code review. |
285 BackoffEntry::Policy lenient_policy = base_policy; | 279 BackoffEntry::Policy lenient_policy = base_policy; |
286 lenient_policy.num_errors_to_ignore = 1; | 280 lenient_policy.num_errors_to_ignore = 1; |
287 TestBackoffEntry custom(&lenient_policy); | 281 TestTickClock now_ticks; |
282 BackoffEntry custom(&lenient_policy, &now_ticks); | |
288 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); | 283 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); |
289 custom.SetCustomReleaseTime(custom_horizon); | 284 custom.SetCustomReleaseTime(custom_horizon); |
290 custom.InformOfRequest(false); // This must not reset the horizon. | 285 custom.InformOfRequest(false); // This must not reset the horizon. |
291 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); | 286 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); |
292 } | 287 } |
293 | 288 |
294 TEST(BackoffEntryTest, OverflowProtection) { | 289 TEST(BackoffEntryTest, OverflowProtection) { |
295 BackoffEntry::Policy large_multiply_policy = base_policy; | 290 BackoffEntry::Policy large_multiply_policy = base_policy; |
296 large_multiply_policy.multiply_factor = 256; | 291 large_multiply_policy.multiply_factor = 256; |
297 TestBackoffEntry custom(&large_multiply_policy); | 292 TestTickClock now_ticks; |
293 BackoffEntry custom(&large_multiply_policy, &now_ticks); | |
298 | 294 |
299 // Trigger enough failures such that more than 11 bits of exponent are used | 295 // Trigger enough failures such that more than 11 bits of exponent are used |
300 // to represent the exponential backoff intermediate values. Given a multiply | 296 // to represent the exponential backoff intermediate values. Given a multiply |
301 // factor of 256 (2^8), 129 iterations is enough: 2^(8*(129-1)) = 2^1024. | 297 // factor of 256 (2^8), 129 iterations is enough: 2^(8*(129-1)) = 2^1024. |
302 for (int i = 0; i < 129; ++i) { | 298 for (int i = 0; i < 129; ++i) { |
303 custom.set_now(custom.ImplGetTimeNow() + custom.GetTimeUntilRelease()); | 299 now_ticks.set_now(now_ticks.NowTicks() + custom.GetTimeUntilRelease()); |
304 custom.InformOfRequest(false); | 300 custom.InformOfRequest(false); |
305 ASSERT_TRUE(custom.ShouldRejectRequest()); | 301 ASSERT_TRUE(custom.ShouldRejectRequest()); |
306 } | 302 } |
307 | 303 |
308 // Max delay should still be respected. | 304 // Max delay should still be respected. |
309 EXPECT_EQ(20000, custom.GetTimeUntilRelease().InMilliseconds()); | 305 EXPECT_EQ(20000, custom.GetTimeUntilRelease().InMilliseconds()); |
310 } | 306 } |
311 | 307 |
312 } // namespace | 308 } // namespace |
OLD | NEW |