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

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

Issue 2130493002: Implement THROTTLED priority semantics. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@NetworkStreamThrottler
Patch Set: Incorporated comments, simplified timing code. Created 4 years, 2 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/network_throttle_manager.h"
6
7 #include <memory> 5 #include <memory>
8 6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/callback_helpers.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/run_loop.h"
12 #include "base/test/simple_test_tick_clock.h"
13 #include "base/test/test_message_loop.h"
14 #include "net/base/network_throttle_manager_impl.h"
mmenke 2016/10/07 16:51:47 Rename this file to network_throttle_manager_impl_
Randy Smith (Not in Mondays) 2016/10/11 21:43:35 I think we're using the ABC for tests on HttpNetwo
9 #include "net/base/request_priority.h" 15 #include "net/base/request_priority.h"
10 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
11 17
12 namespace net { 18 namespace net {
13 19
14 namespace { 20 namespace {
15 21
16 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
17 23
24 const int kInitialAgeHorizonForUncountedRequests =
25 (NetworkThrottleManagerImpl::kInitialMedianInMs *
26 NetworkThrottleManagerImpl::kMedianLifetimeMultiple);
27
28 // Test fixture for throttle manager tests.
29
30 // Note that the manager owned and managed by this fixture has a clock
31 // that is set to base::TimeTicks::Now() (which value is also exposed
32 // via an accessor) on creation but does not change without
33 // intervention by tests (to make the tests more predictable).
34 //
35 // HOWEVER, also note that that manager uses the base::Timer class, which
36 // uses the system clock, which isn't affected by the setting of the
37 // test fixture clock. So test should be written to a) avoid situations
38 // in which the manager's timer will actually go off based on the system
39 // clock, and b) call ConditionallyTriggerTimerForTesting() (which does
40 // evaluate the manager's clock) when timer based tests are necessary.
18 class NetworkThrottleManagerTest : public testing::Test, 41 class NetworkThrottleManagerTest : public testing::Test,
19 NetworkThrottleManager::ThrottleDelegate { 42 NetworkThrottleManager::ThrottleDelegate {
20 public: 43 public:
21 NetworkThrottleManagerTest() 44 NetworkThrottleManagerTest()
22 : throttler_(NetworkThrottleManager::CreateThrottler()) {} 45 : clock_(new base::SimpleTestTickClock),
46 now_(base::TimeTicks::Now()),
47 throttle_state_change_count_(0),
48 last_throttle_to_change_state_(nullptr),
49 throttle_manager_(new NetworkThrottleManagerImpl) {
50 clock_->SetNowTicks(now_);
51 throttle_manager_->SetTickClockForTesting(
52 std::unique_ptr<base::TickClock>(clock_));
53 }
23 54
24 protected: 55 protected:
56 enum ThrottleState { BLOCKED, UNBLOCKED };
Charlie Harrison 2016/10/11 14:50:52 This is pretty confusing given the enum declared i
Randy Smith (Not in Mondays) 2016/10/11 21:43:35 ExpectedThrottleBlockState. Done.
57
58 base::TimeTicks now() { return now_; }
59 NetworkThrottleManagerImpl* throttle_manager() {
60 return throttle_manager_.get();
61 }
62
63 // Set the offset of the test clock from now_.
64 void SetClockDelta(base::TimeDelta time_delta) {
65 clock_->SetNowTicks(now_ + time_delta);
66 }
67
68 // Throttle creation
25 std::unique_ptr<NetworkThrottleManager::Throttle> CreateThrottle( 69 std::unique_ptr<NetworkThrottleManager::Throttle> CreateThrottle(
26 net::RequestPriority priority, 70 net::RequestPriority priority,
27 bool expected_throttle_state) { 71 ThrottleState throttle_state) {
28 std::unique_ptr<NetworkThrottleManager::Throttle> throttle( 72 std::unique_ptr<NetworkThrottleManager::Throttle> throttle(
29 throttler_->CreateThrottle(this, priority, false)); 73 throttle_manager_->CreateThrottle(this, priority, false));
30 EXPECT_EQ(expected_throttle_state, throttle->IsThrottled()); 74 EXPECT_EQ(throttle_state == BLOCKED, throttle->IsBlocked());
31 return throttle; 75 return throttle;
32 } 76 }
77 std::unique_ptr<NetworkThrottleManager::Throttle>
78 CreateThrottleIgnoringLimits(net::RequestPriority priority) {
79 std::unique_ptr<NetworkThrottleManager::Throttle> throttle(
80 throttle_manager_->CreateThrottle(this, priority, true));
81 EXPECT_FALSE(throttle->IsBlocked());
82 return throttle;
83 }
84
85 // Throttle state change information.
86 int throttle_state_change_count() { return throttle_state_change_count_; }
87 NetworkThrottleManager::Throttle* last_throttle_to_change_state() {
88 return last_throttle_to_change_state_;
89 }
90
91 // Setting a callback to be invoked when a throttle's state changes.
92 void SetThrottleStateChangedCallback(const base::Closure& callback) {
93 throttle_state_changed_callback_ = callback;
94 }
33 95
34 private: 96 private:
35 // NetworkThrottleManager::Delegate 97 // NetworkThrottleManager::Delegate
36 void OnThrottleStateChanged() override { ADD_FAILURE(); } 98 void OnThrottleUnblocked(
37 99 NetworkThrottleManager::Throttle* throttle) override {
38 std::unique_ptr<NetworkThrottleManager> throttler_; 100 ++throttle_state_change_count_;
101 last_throttle_to_change_state_ = throttle;
102 if (!throttle_state_changed_callback_.is_null())
103 base::ResetAndReturn(&throttle_state_changed_callback_).Run();
104 }
105
106 base::SimpleTestTickClock* clock_;
107 base::TimeTicks now_;
108 int throttle_state_change_count_;
109 NetworkThrottleManager::Throttle* last_throttle_to_change_state_;
110 std::unique_ptr<NetworkThrottleManagerImpl> throttle_manager_;
111 base::Closure throttle_state_changed_callback_;
112
113 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManagerTest);
39 }; 114 };
40 115
41 // Check to confirm that all created throttles start unthrottled for the 116 // Check to confirm that all created throttles at priorities other than
42 // current null implementation. 117 // THROTTLED start unblocked.
43 TEST_F(NetworkThrottleManagerTest, AllUnthrottled) { 118 TEST_F(NetworkThrottleManagerTest, AllUnthrottled) {
44 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { 119 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
45 CreateThrottle(static_cast<RequestPriority>(i), false); 120 if (i == THROTTLED)
46 } 121 continue;
122 CreateThrottle(static_cast<RequestPriority>(i), UNBLOCKED);
123 }
124 }
125
126 // Check for basic semantics around the new THROTTLED level.
127 TEST_F(NetworkThrottleManagerTest, ThrottledBlocking) {
128 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
129 CreateThrottle(THROTTLED, UNBLOCKED));
130 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
131 CreateThrottle(THROTTLED, UNBLOCKED));
132 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
133 CreateThrottle(THROTTLED, BLOCKED));
134 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
135 CreateThrottle(THROTTLED, BLOCKED));
136 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
137 CreateThrottle(THROTTLED, BLOCKED));
138
139 EXPECT_EQ(0, throttle_state_change_count());
140
141 throttle1.reset();
142 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
143 EXPECT_EQ(1, throttle_state_change_count());
144 EXPECT_EQ(throttle3.get(), last_throttle_to_change_state());
145
146 EXPECT_FALSE(throttle3->IsBlocked());
147 EXPECT_TRUE(throttle4->IsBlocked());
148 EXPECT_TRUE(throttle5->IsBlocked());
149
150 throttle2.reset();
151 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
152 EXPECT_EQ(2, throttle_state_change_count());
153 EXPECT_EQ(throttle4.get(), last_throttle_to_change_state());
154
155 EXPECT_FALSE(throttle3->IsBlocked());
156 EXPECT_FALSE(throttle4->IsBlocked());
157 EXPECT_TRUE(throttle5->IsBlocked());
158 }
159
160 // Check that THROTTLED semantics are dependent on all outstanding requests.
161 TEST_F(NetworkThrottleManagerTest, ThrottledBlockingMultiPriority) {
162 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
163 CreateThrottle(HIGHEST, UNBLOCKED));
164 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
165 CreateThrottle(LOW, UNBLOCKED));
166 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
167 CreateThrottle(IDLE, UNBLOCKED));
168 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
169 CreateThrottle(THROTTLED, BLOCKED));
170 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
171 CreateThrottle(THROTTLED, BLOCKED));
172
173 EXPECT_EQ(0, throttle_state_change_count());
174
175 throttle1.reset();
176 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
177 EXPECT_EQ(0, throttle_state_change_count());
178 EXPECT_FALSE(throttle3->IsBlocked());
179 EXPECT_TRUE(throttle4->IsBlocked());
180 EXPECT_TRUE(throttle5->IsBlocked());
181
182 throttle2.reset();
183 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
184 EXPECT_EQ(1, throttle_state_change_count());
185 EXPECT_EQ(throttle4.get(), last_throttle_to_change_state());
186
187 EXPECT_FALSE(throttle3->IsBlocked());
188 EXPECT_FALSE(throttle4->IsBlocked());
189 EXPECT_TRUE(throttle5->IsBlocked());
190
191 throttle3.reset();
192 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
193 EXPECT_EQ(2, throttle_state_change_count());
194 EXPECT_EQ(throttle5.get(), last_throttle_to_change_state());
195
196 EXPECT_FALSE(throttle4->IsBlocked());
197 EXPECT_FALSE(throttle5->IsBlocked());
198 }
199
200 // Check that a SetPriority() away from THROTTLED results in unblocking
201 // and an upcall.
202 TEST_F(NetworkThrottleManagerTest, ThrottledSetPriority) {
203 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
204 CreateThrottle(THROTTLED, UNBLOCKED));
205 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
206 CreateThrottle(THROTTLED, UNBLOCKED));
207 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
208 CreateThrottle(THROTTLED, BLOCKED));
209 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
210 CreateThrottle(THROTTLED, BLOCKED));
211
212 EXPECT_EQ(0, throttle_state_change_count());
213
214 throttle3->SetPriority(LOW);
215 EXPECT_EQ(1, throttle_state_change_count());
216 EXPECT_EQ(throttle3.get(), last_throttle_to_change_state());
217 EXPECT_FALSE(throttle3->IsBlocked());
218 EXPECT_TRUE(throttle4->IsBlocked());
219 }
220
221 void ResetThrottles(ScopedVector<NetworkThrottleManager::Throttle> throttles) {
222 // All pointers in the vector should be deleted on exit.
223 }
224
225 // Check that tearing down all elements in the NTM on a SetPriority
226 // upcall doesn't create any problems.
227 TEST_F(NetworkThrottleManagerTest, ThrottleTeardown) {
228 ScopedVector<NetworkThrottleManager::Throttle> throttles;
229 std::unique_ptr<NetworkThrottleManager::Throttle> throttle_temporary;
230
231 throttles.push_back(std::unique_ptr<NetworkThrottleManager::Throttle>(
232 CreateThrottle(THROTTLED, UNBLOCKED)));
233 throttles.push_back(std::unique_ptr<NetworkThrottleManager::Throttle>(
234 CreateThrottle(THROTTLED, UNBLOCKED)));
235
236 // Note that if there is more than one throttle blocked, then the
237 // number of throttle state changes is dependent on destruction order.
238 // So only one blocked throttle is created.
239
240 throttle_temporary = CreateThrottle(THROTTLED, BLOCKED);
241 NetworkThrottleManager::Throttle* throttle3 = throttle_temporary.get();
242 throttles.push_back(std::move(throttle_temporary.release()));
Charlie Harrison 2016/10/11 14:50:52 I could be wrong but I don't think .release() is n
Randy Smith (Not in Mondays) 2016/10/11 21:43:35 Right you are. Done.
243
244 SetThrottleStateChangedCallback(
245 base::Bind(&ResetThrottles, base::Passed(&throttles)));
246
247 EXPECT_EQ(0, throttle_state_change_count());
248
249 throttle3->SetPriority(LOW);
250 // If the test is functioning as expected, throttle3 now points to
251 // a deleted object and can no longer be indirected through.
252
253 EXPECT_EQ(1, throttle_state_change_count());
Charlie Harrison 2016/10/11 14:50:52 Optional: It would be nice if you could find a way
Randy Smith (Not in Mondays) 2016/10/11 21:43:35 I (now) assert that the callback ran. Given the n
254 EXPECT_EQ(throttle3, last_throttle_to_change_state());
255 }
256
257 // Note that this routine is dependent on priority setting *not* resulting in
258 // destruction of any throttle and should only be used in tests where that is
259 // true.
260 void SetAllToPriority(
261 RequestPriority priority,
262 std::vector<NetworkThrottleManager::Throttle*> throttles) {
263 for (size_t i = 0; i < throttles.size(); ++i)
264 throttles[i]->SetPriority(priority);
265 }
266
267 // Check that modifying all the priorities of the allocated throttles in
268 // the callback works properly.
269 TEST_F(NetworkThrottleManagerTest, ThrottlePriorityReset) {
270 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
271 CreateThrottle(THROTTLED, UNBLOCKED));
272 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
273 CreateThrottle(THROTTLED, UNBLOCKED));
274 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
275 CreateThrottle(THROTTLED, BLOCKED));
276 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
277 CreateThrottle(THROTTLED, BLOCKED));
278
279 std::vector<NetworkThrottleManager::Throttle*> throttles;
Charlie Harrison 2016/10/11 14:50:52 Optional: use an initializer list?
Randy Smith (Not in Mondays) 2016/10/11 21:43:35 After looking at the constructor documentation (ht
280 throttles.push_back(throttle1.get());
281 throttles.push_back(throttle2.get());
282 throttles.push_back(throttle3.get());
283
284 SetThrottleStateChangedCallback(
285 base::Bind(&SetAllToPriority, MEDIUM, base::Passed(&throttles)));
286
287 EXPECT_EQ(0, throttle_state_change_count());
288 throttle3->SetPriority(HIGHEST);
289
290 // Expected result: throttles 1-3 @ medium priority (the callback should
291 // have overridden the priority setting above), only throttle 4 blocked
292 // (throttle3 should have been unblocked by either of the priority changes),
293 // and one state changes (the unblocking).
294 EXPECT_EQ(MEDIUM, throttle1->Priority());
295 EXPECT_EQ(MEDIUM, throttle2->Priority());
296 EXPECT_EQ(MEDIUM, throttle3->Priority());
297 EXPECT_EQ(THROTTLED, throttle4->Priority());
298 EXPECT_FALSE(throttle1->IsBlocked());
299 EXPECT_FALSE(throttle2->IsBlocked());
300 EXPECT_FALSE(throttle3->IsBlocked());
301 EXPECT_TRUE(throttle4->IsBlocked());
302 EXPECT_EQ(1, throttle_state_change_count());
303 }
304
305 // Check that modifying the priority of a request from a non-THROTTLED
306 // value to THROTTLED causes no change in behavior.
307 TEST_F(NetworkThrottleManagerTest, ThrottlePriorityResetToThrottled) {
308 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
309 CreateThrottle(THROTTLED, UNBLOCKED));
310 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
311 CreateThrottle(THROTTLED, UNBLOCKED));
312 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
313 CreateThrottle(LOW, UNBLOCKED));
314 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
315 CreateThrottle(THROTTLED, BLOCKED));
316
317 EXPECT_EQ(0, throttle_state_change_count());
318 throttle3->SetPriority(THROTTLED);
319 EXPECT_EQ(0, throttle_state_change_count());
320
321 EXPECT_FALSE(throttle1->IsBlocked());
322 EXPECT_FALSE(throttle2->IsBlocked());
323 EXPECT_FALSE(throttle3->IsBlocked());
324 EXPECT_TRUE(throttle4->IsBlocked());
325
326 EXPECT_EQ(THROTTLED, throttle1->Priority());
327 EXPECT_EQ(THROTTLED, throttle2->Priority());
328 EXPECT_EQ(THROTTLED, throttle3->Priority());
329 EXPECT_EQ(THROTTLED, throttle4->Priority());
330 }
331
332 // Confirm that old requests don't count against the limit.
333 TEST_F(NetworkThrottleManagerTest, DontCountAgedRequests) {
334 const int age_in_days_of_old_throttles = 4;
335
336 // Confirm default median and timing means that 4 days is long enough ago
337 // to be aged out.
338 EXPECT_GT(age_in_days_of_old_throttles * 24 * 60 * 60 * 1000,
339 kInitialAgeHorizonForUncountedRequests);
340
341 SetClockDelta(-base::TimeDelta::FromDays(age_in_days_of_old_throttles));
342 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
343 CreateThrottle(IDLE, UNBLOCKED));
344 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
345 CreateThrottle(IDLE, UNBLOCKED));
346
347 SetClockDelta(base::TimeDelta());
348 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
349 CreateThrottle(LOW, UNBLOCKED));
350
351 // First throttled request should not be blocked.
352 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
353 CreateThrottle(THROTTLED, UNBLOCKED));
354
355 // Second should be.
356 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
357 CreateThrottle(THROTTLED, BLOCKED));
358
359 // Destroying the old requests should not result in any upcalls.
360 EXPECT_EQ(0, throttle_state_change_count());
361 throttle1.reset();
362 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
363 EXPECT_EQ(0, throttle_state_change_count());
364 throttle2.reset();
365 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
366 EXPECT_EQ(0, throttle_state_change_count());
367
368 // But destroying a new request should result in a state change.
369 throttle3.reset();
370 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
371 EXPECT_EQ(1, throttle_state_change_count());
372 EXPECT_EQ(throttle5.get(), last_throttle_to_change_state());
373 }
374
375 // Confirm that a slew of throttles of a specific age will shift the
376 // median for determining "aged requests" to that age.
377 TEST_F(NetworkThrottleManagerTest, ShiftMedian) {
378 // Setup two throttles of age *just short* of aging out; confirm
379 // they result in blocked THROTTLED requests.
380 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
381 CreateThrottle(IDLE, UNBLOCKED));
382 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
383 CreateThrottle(IDLE, UNBLOCKED));
384 SetClockDelta(base::TimeDelta::FromMilliseconds(
385 kInitialAgeHorizonForUncountedRequests - 1));
386 throttle_manager()->ConditionallyTriggerTimerForTesting();
387
388 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
389 CreateThrottle(THROTTLED, BLOCKED));
390
391 throttle1.reset();
392 throttle2.reset();
393 throttle3.reset();
394 base::RunLoop().RunUntilIdle(); // Allow posttasks to run.
395
396 // Create 100 throttles and destroy them, effectively with lifetime zero.
397 // This should substantially decrease the median age estimate.
398 SetClockDelta(base::TimeDelta());
399 for (int i = 0; i < 100; ++i) {
400 std::unique_ptr<NetworkThrottleManager::Throttle> tmp(
401 CreateThrottle(IDLE, UNBLOCKED));
402 }
403
404 // Clear out the timer
405 SetClockDelta(base::TimeDelta::FromMilliseconds(
Charlie Harrison 2016/10/11 14:50:52 Can you add to this comment, this is a really subt
Randy Smith (Not in Mondays) 2016/10/11 21:43:35 Done.
406 2 * kInitialAgeHorizonForUncountedRequests + 1));
407 throttle_manager()->ConditionallyTriggerTimerForTesting();
408
409 // The identical test above should no longer result in blocked throttles.
410 SetClockDelta(base::TimeDelta());
411 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
412 CreateThrottle(IDLE, UNBLOCKED));
413 std::unique_ptr<NetworkThrottleManager::Throttle> throttle6(
414 CreateThrottle(IDLE, UNBLOCKED));
415 SetClockDelta(base::TimeDelta::FromMilliseconds(
416 kInitialAgeHorizonForUncountedRequests - 1));
417 throttle_manager()->ConditionallyTriggerTimerForTesting();
418 std::unique_ptr<NetworkThrottleManager::Throttle> throttle7(
419 CreateThrottle(THROTTLED, UNBLOCKED));
420 }
421
422 // Confirm that just "aging out" requests will result in unblocking
423 // blocked requests.
424 TEST_F(NetworkThrottleManagerTest, AgeInvalidThrottles) {
425 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
426 CreateThrottle(IDLE, UNBLOCKED));
427 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
428 CreateThrottle(IDLE, UNBLOCKED));
429 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
430 CreateThrottle(THROTTLED, BLOCKED));
431
432 EXPECT_EQ(0, throttle_state_change_count());
433 SetClockDelta(base::TimeDelta::FromMilliseconds(
434 (kInitialAgeHorizonForUncountedRequests +
Charlie Harrison 2016/10/11 14:50:52 Why do you have to add kInitialMedianInMs? Can you
Randy Smith (Not in Mondays) 2016/10/11 21:43:35 Done, but it requires me to add in a fudge factor
435 NetworkThrottleManagerImpl::kInitialMedianInMs)));
436 throttle_manager()->ConditionallyTriggerTimerForTesting();
437 EXPECT_EQ(1, throttle_state_change_count());
438 EXPECT_EQ(throttle3.get(), last_throttle_to_change_state());
439 EXPECT_FALSE(throttle3->IsBlocked());
440 }
441
442 // Confirm that if throttles are unblocked and made active by all
443 // existing outstanding throttles aging out, they will also eventually
444 // age out and let new throttles through.
445 TEST_F(NetworkThrottleManagerTest, NewlyUnblockedThrottlesAlsoAge) {
446 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
447 CreateThrottle(IDLE, UNBLOCKED));
448 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
449 CreateThrottle(IDLE, UNBLOCKED));
450 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
451 CreateThrottle(THROTTLED, BLOCKED));
452 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
453 CreateThrottle(THROTTLED, BLOCKED));
454 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
455 CreateThrottle(THROTTLED, BLOCKED));
456 std::unique_ptr<NetworkThrottleManager::Throttle> throttle6(
457 CreateThrottle(THROTTLED, BLOCKED));
458
459 // Age the first two throttles out of the outstanding, which should
460 // result in the next two throttles becoming unblocked (and in the
461 // oustanding list). (The internal implementation will zero out
462 // the outstanding queue and then add in the two new unblocked throttles.)
463 EXPECT_EQ(0, throttle_state_change_count());
464 SetClockDelta(base::TimeDelta::FromMilliseconds(
465 (kInitialAgeHorizonForUncountedRequests +
466 NetworkThrottleManagerImpl::kInitialMedianInMs)));
467 throttle_manager()->ConditionallyTriggerTimerForTesting();
468 EXPECT_EQ(2, throttle_state_change_count());
469 EXPECT_FALSE(throttle3->IsBlocked());
470 EXPECT_FALSE(throttle4->IsBlocked());
471
472 // Age the next two throttles out of the outstanding queue, which
473 // should result in the next two throttles becoming unblocked (and
474 // in the oustanding list). This will only happen if a timer was properly
475 // set in the above age process as the oustanding queue went through
476 // the empty state.
477 SetClockDelta(base::TimeDelta::FromMilliseconds(
478 2 * (kInitialAgeHorizonForUncountedRequests +
479 NetworkThrottleManagerImpl::kInitialMedianInMs)));
480 throttle_manager()->ConditionallyTriggerTimerForTesting();
481 EXPECT_EQ(4, throttle_state_change_count());
482 EXPECT_FALSE(throttle5->IsBlocked());
483 EXPECT_FALSE(throttle6->IsBlocked());
484 }
485
486 // Confirm that throttles that are blocked for a while and then
487 // unblocked don't "age out".
488 TEST_F(NetworkThrottleManagerTest, AgeBlockedThrottles) {
489 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
490 CreateThrottle(IDLE, UNBLOCKED));
491 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
492 CreateThrottle(IDLE, UNBLOCKED));
493 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
494 CreateThrottle(THROTTLED, BLOCKED));
495 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
496 CreateThrottle(THROTTLED, BLOCKED));
497 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
498 CreateThrottle(THROTTLED, BLOCKED));
499
500 EXPECT_EQ(0, throttle_state_change_count());
501 SetClockDelta(base::TimeDelta::FromMilliseconds(
502 (kInitialAgeHorizonForUncountedRequests +
503 NetworkThrottleManagerImpl::kInitialMedianInMs)));
504 throttle_manager()->ConditionallyTriggerTimerForTesting();
505
506 // If blocked throttles aged out, all three throttles should have been
507 // unblocked. If not, only the two replacing the IDLE throttles should
508 // have.
509 EXPECT_EQ(2, throttle_state_change_count());
510 }
511
512 // Confirm that deleting old throttles before they age out doesn't
513 // interfere with the aging out of more recent throttles.
514 TEST_F(NetworkThrottleManagerTest, DeletionAgingInterference) {
515 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
516 CreateThrottle(IDLE, UNBLOCKED));
517 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
518 CreateThrottle(IDLE, UNBLOCKED));
519 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
520 CreateThrottle(THROTTLED, BLOCKED));
521 EXPECT_EQ(0, throttle_state_change_count());
522
523 SetClockDelta(base::TimeDelta::FromMilliseconds(
524 kInitialAgeHorizonForUncountedRequests / 2));
525 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
526 CreateThrottle(IDLE, UNBLOCKED));
527 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
528 CreateThrottle(IDLE, UNBLOCKED));
529 throttle_manager()->ConditionallyTriggerTimerForTesting();
530 EXPECT_EQ(0, throttle_state_change_count());
531
532 throttle1.reset();
533 throttle2.reset();
534 throttle_manager()->ConditionallyTriggerTimerForTesting();
535 EXPECT_EQ(0, throttle_state_change_count());
536
537 SetClockDelta(base::TimeDelta::FromMilliseconds(
538 (3 * kInitialAgeHorizonForUncountedRequests / 2 +
539 NetworkThrottleManagerImpl::kInitialMedianInMs)));
540 throttle_manager()->ConditionallyTriggerTimerForTesting();
541 EXPECT_EQ(1, throttle_state_change_count());
542 EXPECT_EQ(throttle3.get(), last_throttle_to_change_state());
543 EXPECT_FALSE(throttle3->IsBlocked());
544 }
545
546 // Confirm that "ignore_limits" boolean is respected.
547 TEST_F(NetworkThrottleManagerTest, IgnoreLimits) {
548 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
549 CreateThrottle(HIGHEST, UNBLOCKED));
550 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
551 CreateThrottle(LOW, UNBLOCKED));
552 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
553 CreateThrottle(IDLE, UNBLOCKED));
554 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
555 CreateThrottle(THROTTLED, BLOCKED));
556 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
557 CreateThrottleIgnoringLimits(THROTTLED));
47 } 558 }
48 559
49 } // namespace 560 } // namespace
50 561
51 } // namespace net 562 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698