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

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: Added (currently failing) URLRequest unit test. Created 4 years, 3 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/test/simple_test_tick_clock.h"
12 #include "net/base/network_throttle_manager_impl.h"
9 #include "net/base/request_priority.h" 13 #include "net/base/request_priority.h"
10 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
11 15
12 namespace net { 16 namespace net {
13 17
14 namespace { 18 namespace {
15 19
16 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
17 21
22 const int kInitialAgeHorizonForUncountedRequests =
23 (NetworkThrottleManagerImpl::kInitialMedianInMs *
24 NetworkThrottleManagerImpl::kMedianLifetimeMultiple);
25
26 // Test fixture for throttle manager tests.
27 // Note that the manager owned and managed by this fixture has a clock that
Charlie Harrison 2016/09/01 18:17:52 nitty nit: more words on lines.
Randy Smith (Not in Mondays) 2016/09/18 19:12:34 Done.
28 // is set to base::TimeTicks::Now() (which value is also exposed via an
29 // accessor) on creation but does not change without intervention
30 // by tests (to make the tests more predictable).
18 class NetworkThrottleManagerTest : public testing::Test, 31 class NetworkThrottleManagerTest : public testing::Test,
19 NetworkThrottleManager::ThrottleDelegate { 32 NetworkThrottleManager::ThrottleDelegate {
20 public: 33 public:
21 NetworkThrottleManagerTest() 34 NetworkThrottleManagerTest()
22 : throttler_(NetworkThrottleManager::CreateThrottler()) {} 35 : clock_(new base::SimpleTestTickClock),
36 now_(base::TimeTicks::Now()),
37 throttle_state_change_count_(0),
38 last_throttle_state_change_(nullptr),
39 throttle_manager_(new NetworkThrottleManagerImpl) {
40 clock_->SetNowTicks(now_);
41 throttle_manager_->SetTickClockForTesting(
42 std::unique_ptr<base::TickClock>(clock_));
43 }
23 44
24 protected: 45 protected:
46 base::SimpleTestTickClock* clock() const { return clock_; }
47 base::TimeTicks now() { return now_; }
48 NetworkThrottleManagerImpl* throttle_manager() {
49 return throttle_manager_.get();
50 }
51
52 // Throttle creation
25 std::unique_ptr<NetworkThrottleManager::Throttle> CreateThrottle( 53 std::unique_ptr<NetworkThrottleManager::Throttle> CreateThrottle(
26 net::RequestPriority priority, 54 net::RequestPriority priority,
27 bool expected_throttle_state) { 55 bool expected_throttle_state) {
28 std::unique_ptr<NetworkThrottleManager::Throttle> throttle( 56 std::unique_ptr<NetworkThrottleManager::Throttle> throttle(
29 throttler_->CreateThrottle(this, priority, false)); 57 throttle_manager_->CreateThrottle(this, priority, false));
30 EXPECT_EQ(expected_throttle_state, throttle->IsThrottled()); 58 EXPECT_EQ(expected_throttle_state, throttle->IsBlocked());
31 return throttle; 59 return throttle;
32 } 60 }
61 std::unique_ptr<NetworkThrottleManager::Throttle>
62 CreateThrottleIgnoringLimits(net::RequestPriority priority) {
63 std::unique_ptr<NetworkThrottleManager::Throttle> throttle(
64 throttle_manager_->CreateThrottle(this, priority, true));
65 EXPECT_FALSE(throttle->IsBlocked());
66 return throttle;
67 }
68
69 // Throttle state change information.
70 int throttle_state_change_count() { return throttle_state_change_count_; }
71 NetworkThrottleManager::Throttle* last_throttle_state_change() {
Charlie Harrison 2016/09/01 18:17:52 nit: The method name isn't very clear that this is
Randy Smith (Not in Mondays) 2016/09/18 19:12:35 last_throttle_to_change_state() ok?
72 return last_throttle_state_change_;
73 }
74
75 // Setting a callback to be invoked when a throttle's state changes.
76 void SetThrottleStateChangedCallback(const base::Closure& callback) {
77 throttle_state_changed_callback_ = callback;
78 }
33 79
34 private: 80 private:
35 // NetworkThrottleManager::Delegate 81 // NetworkThrottleManager::Delegate
36 void OnThrottleStateChanged() override { ADD_FAILURE(); } 82 void OnThrottleStateChanged(
37 83 NetworkThrottleManager::Throttle* throttle) override {
38 std::unique_ptr<NetworkThrottleManager> throttler_; 84 ++throttle_state_change_count_;
85 last_throttle_state_change_ = throttle;
86 if (!throttle_state_changed_callback_.is_null())
87 base::ResetAndReturn(&throttle_state_changed_callback_).Run();
88 }
89
90 base::SimpleTestTickClock* clock_;
91 base::TimeTicks now_;
92 int throttle_state_change_count_;
93 NetworkThrottleManager::Throttle* last_throttle_state_change_;
94 std::unique_ptr<NetworkThrottleManagerImpl> throttle_manager_;
95 base::Closure throttle_state_changed_callback_;
39 }; 96 };
Charlie Harrison 2016/09/01 18:17:52 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManagerTes
Randy Smith (Not in Mondays) 2016/09/18 19:12:35 Done.
40 97
41 // Check to confirm that all created throttles start unthrottled for the 98 // Check to confirm that all created throttles at priorities other than
42 // current null implementation. 99 // THROTTLED start unblocked.
43 TEST_F(NetworkThrottleManagerTest, AllUnthrottled) { 100 TEST_F(NetworkThrottleManagerTest, AllUnthrottled) {
44 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { 101 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
102 if (i == THROTTLED)
103 continue;
45 CreateThrottle(static_cast<RequestPriority>(i), false); 104 CreateThrottle(static_cast<RequestPriority>(i), false);
Charlie Harrison 2016/09/01 18:17:52 Optional: These opaque bools are kind of hard to u
Randy Smith (Not in Mondays) 2016/09/18 19:12:34 Nah, you make a good argument. Done.
46 } 105 }
47 } 106 }
48 107
108 // Check for basic semantics around the new THROTTLED level.
109 TEST_F(NetworkThrottleManagerTest, ThrottledBlocking) {
110 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
111 CreateThrottle(THROTTLED, false));
112 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
113 CreateThrottle(THROTTLED, false));
114 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
115 CreateThrottle(THROTTLED, true));
116 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
117 CreateThrottle(THROTTLED, true));
118 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
119 CreateThrottle(THROTTLED, true));
120
121 EXPECT_EQ(0, throttle_state_change_count());
122
123 throttle1.reset();
124 EXPECT_EQ(1, throttle_state_change_count());
125 EXPECT_EQ(throttle3.get(), last_throttle_state_change());
126
127 EXPECT_FALSE(throttle3->IsBlocked());
128 EXPECT_TRUE(throttle4->IsBlocked());
129 EXPECT_TRUE(throttle5->IsBlocked());
130
131 throttle2.reset();
132 EXPECT_EQ(2, throttle_state_change_count());
133 EXPECT_EQ(throttle4.get(), last_throttle_state_change());
134
135 EXPECT_FALSE(throttle3->IsBlocked());
136 EXPECT_FALSE(throttle4->IsBlocked());
137 EXPECT_TRUE(throttle5->IsBlocked());
138 }
139
140 // Check that THROTTLED semantics are dependent on all outstanding requests.
141 TEST_F(NetworkThrottleManagerTest, ThrottledBlockingMultiPriority) {
142 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
143 CreateThrottle(HIGHEST, false));
144 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
145 CreateThrottle(LOW, false));
146 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
147 CreateThrottle(IDLE, false));
148 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
149 CreateThrottle(THROTTLED, true));
150 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
151 CreateThrottle(THROTTLED, true));
152
153 EXPECT_EQ(0, throttle_state_change_count());
154
155 throttle1.reset();
156 EXPECT_EQ(0, throttle_state_change_count());
157 EXPECT_FALSE(throttle3->IsBlocked());
158 EXPECT_TRUE(throttle4->IsBlocked());
159 EXPECT_TRUE(throttle5->IsBlocked());
160
161 throttle2.reset();
162 EXPECT_EQ(1, throttle_state_change_count());
163 EXPECT_EQ(throttle4.get(), last_throttle_state_change());
164
165 EXPECT_FALSE(throttle3->IsBlocked());
166 EXPECT_FALSE(throttle4->IsBlocked());
167 EXPECT_TRUE(throttle5->IsBlocked());
168
169 throttle3.reset();
170 EXPECT_EQ(2, throttle_state_change_count());
171 EXPECT_EQ(throttle5.get(), last_throttle_state_change());
172
173 EXPECT_FALSE(throttle4->IsBlocked());
174 EXPECT_FALSE(throttle5->IsBlocked());
175 }
176
177 // Check that a SetPriority() away from THROTTLED results in unblocking
178 // and an upcall.
179 TEST_F(NetworkThrottleManagerTest, ThrottledSetPriority) {
180 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
181 CreateThrottle(THROTTLED, false));
182 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
183 CreateThrottle(THROTTLED, false));
184 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
185 CreateThrottle(THROTTLED, true));
186 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
187 CreateThrottle(THROTTLED, true));
188
189 EXPECT_EQ(0, throttle_state_change_count());
190
191 throttle3->SetPriority(LOW);
192 EXPECT_EQ(1, throttle_state_change_count());
193 EXPECT_EQ(throttle3.get(), last_throttle_state_change());
194 EXPECT_FALSE(throttle3->IsBlocked());
195 EXPECT_TRUE(throttle4->IsBlocked());
196 }
197
198 void ResetThrottles(ScopedVector<NetworkThrottleManager::Throttle> throttles) {
199 // All pointers in the vector should be deleted on exit.
200 }
201
202 // Note that this routine is dependent on priority setting *not* resulting in
203 // destruction of any throttle and should only be used in tests where that is
204 // true.
205 void SetAllToPriority(
206 RequestPriority priority,
207 std::vector<NetworkThrottleManager::Throttle*> throttles) {
208 for (size_t i = 0; i < throttles.size(); ++i)
209 throttles[i]->SetPriority(priority);
210 }
211
212 // Check that tearing down all elements in the NTM on a SetPriority
213 // upcall doesn't create any problems.
214 TEST_F(NetworkThrottleManagerTest, ThrottleTeardown) {
215 ScopedVector<NetworkThrottleManager::Throttle> throttles;
216 std::unique_ptr<NetworkThrottleManager::Throttle> throttle_temporary;
217
218 throttles.push_back(std::unique_ptr<NetworkThrottleManager::Throttle>(
219 CreateThrottle(THROTTLED, false)));
220 throttles.push_back(std::unique_ptr<NetworkThrottleManager::Throttle>(
221 CreateThrottle(THROTTLED, false)));
222
223 // Note that if there is more than one throttle blocked, then the
224 // number of throttle state changes is dependent on destruction order.
225 // So only one blocked throttle is created.
226
227 throttle_temporary = CreateThrottle(THROTTLED, true);
228 NetworkThrottleManager::Throttle* throttle3 = throttle_temporary.get();
229 throttles.push_back(std::move(throttle_temporary.release()));
230
231 SetThrottleStateChangedCallback(
232 base::Bind(&ResetThrottles, base::Passed(&throttles)));
233
234 EXPECT_EQ(0, throttle_state_change_count());
235
236 throttle3->SetPriority(LOW);
237 // If the test is functioning as expected, throttle3 now points to
238 // a deleted object and can no longer be indirected through.
239
240 EXPECT_EQ(1, throttle_state_change_count());
241 EXPECT_EQ(throttle3, last_throttle_state_change());
242 }
243
244 // Check that modifying all the priorities of the allocated throttles in
245 // the callback works properly.
246 TEST_F(NetworkThrottleManagerTest, ThrottlePriorityReset) {
247 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
248 CreateThrottle(THROTTLED, false));
249 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
250 CreateThrottle(THROTTLED, false));
251 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
252 CreateThrottle(THROTTLED, true));
253 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
254 CreateThrottle(THROTTLED, true));
255
256 std::vector<NetworkThrottleManager::Throttle*> throttles;
257 throttles.push_back(throttle1.get());
258 throttles.push_back(throttle2.get());
259 throttles.push_back(throttle3.get());
260
261 SetThrottleStateChangedCallback(
262 base::Bind(&SetAllToPriority, MEDIUM, base::Passed(&throttles)));
263
264 EXPECT_EQ(0, throttle_state_change_count());
265 throttle3->SetPriority(HIGHEST);
266
267 // Expected result: throttles 1-3 @ medium priority (the callback should
268 // have overridden the priority setting above), only throttle 4 blocked
269 // (throttle3 should have been unblocked by either of the priority changes),
270 // and one state changes (the unblocking).
271 EXPECT_EQ(MEDIUM, throttle1->Priority());
272 EXPECT_EQ(MEDIUM, throttle2->Priority());
273 EXPECT_EQ(MEDIUM, throttle3->Priority());
274 EXPECT_EQ(THROTTLED, throttle4->Priority());
275 EXPECT_FALSE(throttle1->IsBlocked());
276 EXPECT_FALSE(throttle2->IsBlocked());
277 EXPECT_FALSE(throttle3->IsBlocked());
278 EXPECT_TRUE(throttle4->IsBlocked());
279 EXPECT_EQ(1, throttle_state_change_count());
280 }
281
282 // Check that modifying the priority of a request from a non-THROTTLED
283 // value to THROTTLED causes no change in behavior.
284 TEST_F(NetworkThrottleManagerTest, ThrottlePriorityResetToThrottled) {
285 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
286 CreateThrottle(THROTTLED, false));
287 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
288 CreateThrottle(THROTTLED, false));
289 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
290 CreateThrottle(LOW, false));
291 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
292 CreateThrottle(THROTTLED, true));
293
294 EXPECT_EQ(0, throttle_state_change_count());
295 throttle3->SetPriority(THROTTLED);
296 EXPECT_EQ(0, throttle_state_change_count());
297
298 EXPECT_FALSE(throttle1->IsBlocked());
299 EXPECT_FALSE(throttle2->IsBlocked());
300 EXPECT_FALSE(throttle3->IsBlocked());
301 EXPECT_TRUE(throttle4->IsBlocked());
302
303 EXPECT_EQ(THROTTLED, throttle1->Priority());
304 EXPECT_EQ(THROTTLED, throttle2->Priority());
305 EXPECT_EQ(THROTTLED, throttle3->Priority());
306 EXPECT_EQ(THROTTLED, throttle4->Priority());
307 }
308
309 // Confirm that old requests don't count against the limit.
310 TEST_F(NetworkThrottleManagerTest, DontCountAgedRequests) {
311 const int age_in_days_of_old_throttles = 4;
312
313 // Confirm default median and timing means that 4 days is long enough ago
314 // to be aged out.
315 EXPECT_GT(age_in_days_of_old_throttles * 24 * 60 * 60 * 1000,
316 kInitialAgeHorizonForUncountedRequests);
317
318 clock()->SetNowTicks(now() -
319 base::TimeDelta::FromDays(age_in_days_of_old_throttles));
320 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
321 CreateThrottle(IDLE, false));
322 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
323 CreateThrottle(IDLE, false));
324
325 clock()->SetNowTicks(now());
326 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
327 CreateThrottle(LOW, false));
328
329 // First throttled request should not be blocked.
330 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
331 CreateThrottle(THROTTLED, false));
332
333 // Second should be.
334 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
335 CreateThrottle(THROTTLED, true));
336
337 // Destroying the old requests should not result in any upcalls.
338 EXPECT_EQ(0, throttle_state_change_count());
339 throttle1.reset();
340 EXPECT_EQ(0, throttle_state_change_count());
341 throttle2.reset();
342 EXPECT_EQ(0, throttle_state_change_count());
343
344 // But destroying a new request should result in a state change.
345 throttle3.reset();
346 EXPECT_EQ(1, throttle_state_change_count());
347 EXPECT_EQ(throttle5.get(), last_throttle_state_change());
348 }
349
350 // Confirm that a slew of throttles of a specific age will shift the
351 // median for determining "aged requests" to that age.
352 TEST_F(NetworkThrottleManagerTest, ShiftMedian) {
353 // Setup two throttles of age *just short* of aging out; confirm
354 // they result in blocked THROTTLED requests.
355 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
356 CreateThrottle(IDLE, false));
357 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
358 CreateThrottle(IDLE, false));
359 clock()->SetNowTicks(now() + base::TimeDelta::FromMilliseconds(
360 kInitialAgeHorizonForUncountedRequests - 1));
361 throttle_manager()->ConditionallyTriggerTimerForTesting();
362
363 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
364 CreateThrottle(THROTTLED, true));
365
366 throttle1.reset();
367 throttle2.reset();
368 throttle3.reset();
369
370 // Create 100 throttles and destroy them, effectively with lifetime zero.
371 // This should substantially decrease the median age estimate.
372 for (int i = 0; i < 100; ++i) {
373 std::unique_ptr<NetworkThrottleManager::Throttle> tmp(
374 CreateThrottle(IDLE, false));
375 }
376
377 // The identical test above should no longer result in blocked throttles.
378 clock()->SetNowTicks(now());
379 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
380 CreateThrottle(IDLE, false));
381 std::unique_ptr<NetworkThrottleManager::Throttle> throttle6(
382 CreateThrottle(IDLE, false));
383 clock()->SetNowTicks(now() + base::TimeDelta::FromMilliseconds(
384 kInitialAgeHorizonForUncountedRequests - 1));
385 throttle_manager()->ConditionallyTriggerTimerForTesting();
386 std::unique_ptr<NetworkThrottleManager::Throttle> throttle7(
387 CreateThrottle(THROTTLED, false));
388 }
389
390 // Confirm that just "aging out" requests will result in unblocking
391 // blocked requests.
392 TEST_F(NetworkThrottleManagerTest, AgeInvalidThrottles) {
393 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
394 CreateThrottle(IDLE, false));
395 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
396 CreateThrottle(IDLE, false));
397 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
398 CreateThrottle(THROTTLED, true));
399
400 EXPECT_EQ(0, throttle_state_change_count());
401 clock()->SetNowTicks(now() +
402 base::TimeDelta::FromMilliseconds(
403 (kInitialAgeHorizonForUncountedRequests +
404 NetworkThrottleManagerImpl::kInitialMedianInMs)));
405 throttle_manager()->ConditionallyTriggerTimerForTesting();
406 EXPECT_EQ(1, throttle_state_change_count());
407 EXPECT_EQ(throttle3.get(), last_throttle_state_change());
408 EXPECT_FALSE(throttle3->IsBlocked());
409 }
410
411 // Confirm that throttles that are blocked for a while and then
412 // unblocked don't "age out".
413 TEST_F(NetworkThrottleManagerTest, AgeBlockedThrottles) {
414 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
415 CreateThrottle(IDLE, false));
416 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
417 CreateThrottle(IDLE, false));
418 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
419 CreateThrottle(THROTTLED, true));
420 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
421 CreateThrottle(THROTTLED, true));
422 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
423 CreateThrottle(THROTTLED, true));
424
425 EXPECT_EQ(0, throttle_state_change_count());
426 clock()->SetNowTicks(now() +
427 base::TimeDelta::FromMilliseconds(
428 (kInitialAgeHorizonForUncountedRequests +
429 NetworkThrottleManagerImpl::kInitialMedianInMs)));
430 throttle_manager()->ConditionallyTriggerTimerForTesting();
431
432 // If blocked throttles aged out, all three throttles should have been
433 // unblocked. If not, only the two replacing the IDLE throttles should
434 // have.
435 EXPECT_EQ(2, throttle_state_change_count());
436 }
437
438 // Confirm that "ignore_limits" boolean is respected.
439 TEST_F(NetworkThrottleManagerTest, IgnoreLimits) {
440 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
441 CreateThrottle(HIGHEST, false));
442 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
443 CreateThrottle(LOW, false));
444 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
445 CreateThrottle(IDLE, false));
446 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
447 CreateThrottle(THROTTLED, true));
448 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
449 CreateThrottleIgnoringLimits(THROTTLED));
450 }
451
49 } // namespace 452 } // namespace
50 453
51 } // namespace net 454 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698