Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(424)

Side by Side Diff: net/base/backoff_entry_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698