OLD | NEW |
---|---|
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" | 5 #include "net/base/network_throttle_manager_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | |
10 #include "base/callback.h" | |
11 #include "base/callback_helpers.h" | |
12 #include "base/memory/scoped_vector.h" | |
13 #include "base/test/simple_test_tick_clock.h" | |
9 #include "net/base/request_priority.h" | 14 #include "net/base/request_priority.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
11 | 16 |
12 namespace net { | 17 namespace net { |
13 | 18 |
14 namespace { | 19 namespace { |
15 | 20 |
16 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
17 | 22 |
18 class NetworkThrottleManagerTest : public testing::Test, | 23 class NetworkThrottleManagerTest : public testing::Test, |
19 NetworkThrottleManager::ThrottleDelegate { | 24 NetworkThrottleManager::ThrottleDelegate { |
20 public: | 25 public: |
21 NetworkThrottleManagerTest() | 26 NetworkThrottleManagerTest() |
22 : throttler_(NetworkThrottleManager::CreateThrottler()) {} | 27 : throttle_state_change_count_(0), |
28 last_throttle_state_change_(nullptr), | |
29 throttle_manager_(new NetworkThrottleManagerImpl) {} | |
23 | 30 |
24 protected: | 31 protected: |
25 std::unique_ptr<NetworkThrottleManager::Throttle> CreateThrottle( | 32 std::unique_ptr<NetworkThrottleManager::Throttle> CreateThrottle( |
26 net::RequestPriority priority, | 33 net::RequestPriority priority, |
27 bool expected_throttle_state) { | 34 bool expected_throttle_state) { |
28 std::unique_ptr<NetworkThrottleManager::Throttle> throttle( | 35 std::unique_ptr<NetworkThrottleManager::Throttle> throttle( |
29 throttler_->CreateThrottle(this, priority, false)); | 36 throttle_manager_->CreateThrottle(this, priority, false)); |
30 EXPECT_EQ(expected_throttle_state, throttle->IsThrottled()); | 37 EXPECT_EQ(expected_throttle_state, throttle->IsBlocked()); |
38 throttled_blocked_[throttle.get()] = !throttle->IsBlocked(); | |
Charlie Harrison
2016/07/08 22:36:05
Hmm. So I am only looking at this from top to bott
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
Wow, I wonder what sequence of global replaces res
| |
31 return throttle; | 39 return throttle; |
32 } | 40 } |
33 | 41 |
42 std::unique_ptr<NetworkThrottleManager::Throttle> | |
43 CreateThrottleIgnoringLimits(net::RequestPriority priority) { | |
44 std::unique_ptr<NetworkThrottleManager::Throttle> throttle( | |
45 throttle_manager_->CreateThrottle(this, priority, true)); | |
46 EXPECT_FALSE(throttle->IsBlocked()); | |
47 throttled_blocked_[throttle.get()] = !throttle->IsBlocked(); | |
48 return throttle; | |
49 } | |
50 | |
51 int throttle_state_change_count() { return throttle_state_change_count_; } | |
52 | |
53 NetworkThrottleManager::Throttle* last_throttle_state_change() { | |
54 return last_throttle_state_change_; | |
55 } | |
56 | |
57 void SetUpcallCallback(const base::Closure& callback) { | |
Charlie Harrison
2016/07/08 22:36:06
Can you document the upcall callback? It's a very
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
Good point; done.
| |
58 upcall_callback_ = callback; | |
59 } | |
60 | |
61 NetworkThrottleManagerImpl* throttle_manager() { | |
62 return throttle_manager_.get(); | |
63 } | |
64 | |
34 private: | 65 private: |
35 // NetworkThrottleManager::Delegate | 66 // NetworkThrottleManager::Delegate |
36 void OnThrottleStateChanged() override { ADD_FAILURE(); } | 67 void OnThrottleStateChanged( |
37 | 68 NetworkThrottleManager::Throttle* throttle) override { |
38 std::unique_ptr<NetworkThrottleManager> throttler_; | 69 EXPECT_FALSE(throttled_blocked_.find(throttle) == throttled_blocked_.end()); |
70 EXPECT_FALSE(throttled_blocked_[throttle]); | |
71 throttled_blocked_[throttle] = true; | |
72 ++throttle_state_change_count_; | |
73 last_throttle_state_change_ = throttle; | |
74 if (!upcall_callback_.is_null()) | |
75 base::ResetAndReturn(&upcall_callback_).Run(); | |
76 } | |
77 | |
78 int throttle_state_change_count_; | |
79 NetworkThrottleManager::Throttle* last_throttle_state_change_; | |
80 std::unique_ptr<NetworkThrottleManagerImpl> throttle_manager_; | |
81 std::map<NetworkThrottleManager::Throttle*, bool> throttled_blocked_; | |
82 base::Closure upcall_callback_; | |
39 }; | 83 }; |
40 | 84 |
41 // Check to confirm that all created throttles start unthrottled for the | 85 // Check to confirm that all created throttles at priorities other than |
42 // current null implementation. | 86 // THROTTLED start unblocked. |
43 TEST_F(NetworkThrottleManagerTest, AllUnthrottled) { | 87 TEST_F(NetworkThrottleManagerTest, AllUnthrottled) { |
44 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { | 88 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { |
89 if (i == THROTTLED) | |
90 continue; | |
45 CreateThrottle(static_cast<RequestPriority>(i), false); | 91 CreateThrottle(static_cast<RequestPriority>(i), false); |
46 } | 92 } |
47 } | 93 } |
48 | 94 |
95 // Check for basic semantics around the new THROTTLED level. | |
96 TEST_F(NetworkThrottleManagerTest, ThrottledBlocking) { | |
97 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
98 CreateThrottle(THROTTLED, false)); | |
99 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
100 CreateThrottle(THROTTLED, false)); | |
101 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
102 CreateThrottle(THROTTLED, true)); | |
103 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
104 CreateThrottle(THROTTLED, true)); | |
105 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( | |
106 CreateThrottle(THROTTLED, true)); | |
107 | |
108 EXPECT_EQ(0, throttle_state_change_count()); | |
109 | |
110 throttle1.reset(); | |
111 EXPECT_EQ(1, throttle_state_change_count()); | |
112 EXPECT_EQ(throttle3.get(), last_throttle_state_change()); | |
113 | |
114 EXPECT_FALSE(throttle3->IsBlocked()); | |
115 EXPECT_TRUE(throttle4->IsBlocked()); | |
116 EXPECT_TRUE(throttle5->IsBlocked()); | |
117 | |
118 throttle2.reset(); | |
119 EXPECT_EQ(2, throttle_state_change_count()); | |
120 EXPECT_EQ(throttle4.get(), last_throttle_state_change()); | |
121 | |
122 EXPECT_FALSE(throttle3->IsBlocked()); | |
123 EXPECT_FALSE(throttle4->IsBlocked()); | |
124 EXPECT_TRUE(throttle5->IsBlocked()); | |
125 } | |
126 | |
127 // Check that THROTTLED semantics are dependent on all outstanding requests. | |
128 TEST_F(NetworkThrottleManagerTest, ThrottledBlockingMultiPriority) { | |
129 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
130 CreateThrottle(HIGHEST, false)); | |
131 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
132 CreateThrottle(LOW, false)); | |
133 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
134 CreateThrottle(IDLE, false)); | |
135 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
136 CreateThrottle(THROTTLED, true)); | |
137 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( | |
138 CreateThrottle(THROTTLED, true)); | |
139 | |
140 EXPECT_EQ(0, throttle_state_change_count()); | |
141 | |
142 throttle1.reset(); | |
143 EXPECT_EQ(0, throttle_state_change_count()); | |
144 EXPECT_FALSE(throttle3->IsBlocked()); | |
145 EXPECT_TRUE(throttle4->IsBlocked()); | |
146 EXPECT_TRUE(throttle5->IsBlocked()); | |
147 | |
148 throttle2.reset(); | |
149 EXPECT_EQ(1, throttle_state_change_count()); | |
150 EXPECT_EQ(throttle4.get(), last_throttle_state_change()); | |
151 | |
152 EXPECT_FALSE(throttle3->IsBlocked()); | |
153 EXPECT_FALSE(throttle4->IsBlocked()); | |
154 EXPECT_TRUE(throttle5->IsBlocked()); | |
155 | |
156 throttle3.reset(); | |
157 EXPECT_EQ(2, throttle_state_change_count()); | |
158 EXPECT_EQ(throttle5.get(), last_throttle_state_change()); | |
159 | |
160 EXPECT_FALSE(throttle4->IsBlocked()); | |
161 EXPECT_FALSE(throttle5->IsBlocked()); | |
162 } | |
163 | |
164 // Check that a SetPriority() away from THROTTLED results in unblocking | |
165 // and an upcall. | |
166 TEST_F(NetworkThrottleManagerTest, ThrottledSetPriority) { | |
167 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
168 CreateThrottle(THROTTLED, false)); | |
169 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
170 CreateThrottle(THROTTLED, false)); | |
171 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
172 CreateThrottle(THROTTLED, true)); | |
173 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
174 CreateThrottle(THROTTLED, true)); | |
175 | |
176 EXPECT_EQ(0, throttle_state_change_count()); | |
177 | |
178 throttle3->SetPriority(LOW); | |
179 EXPECT_EQ(1, throttle_state_change_count()); | |
180 EXPECT_EQ(throttle3.get(), last_throttle_state_change()); | |
181 EXPECT_FALSE(throttle3->IsBlocked()); | |
182 EXPECT_TRUE(throttle4->IsBlocked()); | |
183 } | |
184 | |
185 void ResetThrottles(ScopedVector<NetworkThrottleManager::Throttle> throttles) { | |
186 // All pointers in the vector should be deleted on exit. | |
187 } | |
188 | |
189 // Note that this routine is dependent on priority setting | |
Charlie Harrison
2016/07/08 22:36:05
nit: Can fit more words on this line.
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Done.
| |
190 // *not* resulting in destruction of any throttle and should only | |
191 // be used in tests where that is true. | |
192 void SetAllToPriority( | |
193 RequestPriority priority, | |
194 std::vector<NetworkThrottleManager::Throttle*> throttles) { | |
195 for (size_t i = 0; i < throttles.size(); ++i) | |
196 throttles[i]->SetPriority(priority); | |
197 } | |
198 | |
199 // Check that tearing down all elements in the NTM on a SetPriority | |
200 // upcall doesn't create any problems. | |
201 TEST_F(NetworkThrottleManagerTest, ThrottleTeardown) { | |
202 ScopedVector<NetworkThrottleManager::Throttle> throttles; | |
203 std::unique_ptr<NetworkThrottleManager::Throttle> throttle_temporary; | |
204 | |
205 throttle_temporary = CreateThrottle(THROTTLED, false); | |
Charlie Harrison
2016/07/08 22:36:06
Not sure the temporary variable is needed until th
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Good point; done.
| |
206 throttles.push_back(std::move(throttle_temporary.release())); | |
207 | |
208 throttle_temporary = CreateThrottle(THROTTLED, false); | |
209 throttles.push_back(std::move(throttle_temporary.release())); | |
210 | |
211 // Note that if there is more than one throttle blocked, then the | |
212 // number of throttle state changes is dependent on destruction order. | |
213 // So only one blocked throttle is created. | |
214 | |
215 throttle_temporary = CreateThrottle(THROTTLED, true); | |
216 NetworkThrottleManager::Throttle* throttle3 = throttle_temporary.get(); | |
217 throttles.push_back(std::move(throttle_temporary.release())); | |
218 | |
219 SetUpcallCallback(base::Bind(&ResetThrottles, base::Passed(&throttles))); | |
220 | |
221 EXPECT_EQ(0, throttle_state_change_count()); | |
222 | |
223 throttle3->SetPriority(LOW); | |
224 // If the test is functioning as expected, throttle3 now points to | |
225 // a deleted object and can no longer be indirected through. | |
226 | |
227 EXPECT_EQ(1, throttle_state_change_count()); | |
228 EXPECT_EQ(throttle3, last_throttle_state_change()); | |
229 } | |
230 | |
231 // Check that modifying all the priorities of the allocated throttles in | |
232 // the callback works properly. | |
233 TEST_F(NetworkThrottleManagerTest, ThrottlePriorityReset) { | |
234 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
235 CreateThrottle(THROTTLED, false)); | |
236 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
237 CreateThrottle(THROTTLED, false)); | |
238 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
239 CreateThrottle(THROTTLED, true)); | |
240 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
241 CreateThrottle(THROTTLED, true)); | |
242 | |
243 std::vector<NetworkThrottleManager::Throttle*> throttles; | |
244 throttles.push_back(throttle1.get()); | |
245 throttles.push_back(throttle2.get()); | |
246 throttles.push_back(throttle3.get()); | |
247 | |
248 SetUpcallCallback( | |
249 base::Bind(&SetAllToPriority, MEDIUM, base::Passed(&throttles))); | |
250 | |
251 EXPECT_EQ(0, throttle_state_change_count()); | |
252 throttle3->SetPriority(HIGHEST); | |
253 | |
254 // Expected result: throttles 1-3 @ medium priority (the callback | |
Charlie Harrison
2016/07/08 22:36:06
nit: more words can fit on the line.
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
Done.
| |
255 // should have overridden the priority setting above), only throttle 4 | |
256 // blocked (throttle3 should have been unblocked by either of | |
257 // the priority changes), and one state changes (the unblocking). | |
258 EXPECT_EQ(MEDIUM, throttle1->Priority()); | |
259 EXPECT_EQ(MEDIUM, throttle2->Priority()); | |
260 EXPECT_EQ(MEDIUM, throttle3->Priority()); | |
261 EXPECT_EQ(THROTTLED, throttle4->Priority()); | |
262 EXPECT_FALSE(throttle1->IsBlocked()); | |
263 EXPECT_FALSE(throttle2->IsBlocked()); | |
264 EXPECT_FALSE(throttle3->IsBlocked()); | |
265 EXPECT_TRUE(throttle4->IsBlocked()); | |
266 EXPECT_EQ(1, throttle_state_change_count()); | |
267 } | |
268 | |
269 // Check that modifying the priority of a request from a non-THROTTLED | |
270 // value to THROTTLED causes no change in behavior. | |
271 TEST_F(NetworkThrottleManagerTest, ThrottlePriorityResetToThrottled) { | |
272 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
273 CreateThrottle(THROTTLED, false)); | |
274 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
275 CreateThrottle(THROTTLED, false)); | |
276 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
277 CreateThrottle(LOW, false)); | |
278 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
279 CreateThrottle(THROTTLED, true)); | |
280 | |
281 EXPECT_EQ(0, throttle_state_change_count()); | |
282 throttle3->SetPriority(THROTTLED); | |
283 EXPECT_EQ(0, throttle_state_change_count()); | |
284 | |
285 EXPECT_FALSE(throttle1->IsBlocked()); | |
286 EXPECT_FALSE(throttle2->IsBlocked()); | |
287 EXPECT_FALSE(throttle3->IsBlocked()); | |
288 EXPECT_TRUE(throttle4->IsBlocked()); | |
289 | |
290 EXPECT_EQ(THROTTLED, throttle1->Priority()); | |
291 EXPECT_EQ(THROTTLED, throttle2->Priority()); | |
292 EXPECT_EQ(THROTTLED, throttle3->Priority()); | |
293 EXPECT_EQ(THROTTLED, throttle4->Priority()); | |
294 } | |
295 | |
296 // Confirm that old requests don't count against the ilmit. | |
Charlie Harrison
2016/07/08 22:36:05
s/ilmit/limit
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
Done.
| |
297 TEST_F(NetworkThrottleManagerTest, DontCountAgedRequests) { | |
298 base::TimeTicks now(base::TimeTicks::Now()); | |
299 base::SimpleTestTickClock* clock(new base::SimpleTestTickClock); | |
300 | |
301 throttle_manager()->SetTickClockForTesting( | |
Charlie Harrison
2016/07/08 22:36:05
Optional: eek I don't like the passing ownership o
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
I'm feeling an aversion against that, which I gues
Charlie Harrison
2016/08/29 21:10:25
I will defer to you here, I don't care too much.
| |
302 std::unique_ptr<base::TickClock>(clock)); | |
303 | |
304 const int age_in_days_of_old_throttles = 4; | |
305 | |
306 // Confirm default median and timing means that 4 days is long enough ago | |
307 // to be aged out. | |
308 EXPECT_GT(age_in_days_of_old_throttles * 24 * 60 * 60 * 1000, | |
309 NetworkThrottleManagerImpl::kInitialMedianInMS * | |
Charlie Harrison
2016/07/08 22:36:05
Just noticed this but shouldn't it be "Ms" instead
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Sure. Done.
| |
310 NetworkThrottleManagerImpl::kMedianLifetimeMultiple); | |
Charlie Harrison
2016/07/08 22:36:06
This check seems wrong. Can the median*median_mult
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
Just confirming: Your only concern is about using
Charlie Harrison
2016/08/29 21:10:25
Sorry this was a very unclear comment :(
I think
| |
311 | |
312 clock->SetNowTicks(now - | |
313 base::TimeDelta::FromDays(age_in_days_of_old_throttles)); | |
314 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
315 CreateThrottle(IDLE, false)); | |
316 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
317 CreateThrottle(IDLE, false)); | |
318 | |
319 clock->SetNowTicks(now); | |
320 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
321 CreateThrottle(LOW, false)); | |
322 | |
323 // First throttled request should not be blocked. | |
324 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
325 CreateThrottle(THROTTLED, false)); | |
326 | |
327 // Second should be. | |
328 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( | |
329 CreateThrottle(THROTTLED, true)); | |
330 | |
331 // Destroying the old requests should not result in any upcalls. | |
332 EXPECT_EQ(0, throttle_state_change_count()); | |
333 throttle1.reset(); | |
334 EXPECT_EQ(0, throttle_state_change_count()); | |
Charlie Harrison
2016/07/08 22:36:06
This is because they weren't blocked right? Can yo
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
Sorry, I don't think I understand your thinking, s
Charlie Harrison
2016/08/29 21:10:25
Again I was being unclear. I'm not quite sure what
| |
335 throttle2.reset(); | |
336 EXPECT_EQ(0, throttle_state_change_count()); | |
337 | |
338 // But destroying a new request should result in a state change. | |
339 throttle3.reset(); | |
340 EXPECT_EQ(1, throttle_state_change_count()); | |
341 EXPECT_EQ(throttle5.get(), last_throttle_state_change()); | |
342 } | |
343 | |
344 // Confirm that a slew of throttles of a specific age will shift the | |
345 // median for determining "aged requests" to that age. | |
346 TEST_F(NetworkThrottleManagerTest, ShiftMedian) { | |
347 base::TimeTicks now(base::TimeTicks::Now()); | |
348 base::SimpleTestTickClock* clock(new base::SimpleTestTickClock); | |
349 | |
350 throttle_manager()->SetTickClockForTesting( | |
351 std::unique_ptr<base::TickClock>(clock)); | |
352 | |
353 // Setup two throttles of age *just short* of aging out; confirm | |
354 // they result in blocked THROTTLED requests. | |
355 clock->SetNowTicks( | |
356 now - base::TimeDelta::FromMilliseconds( | |
357 NetworkThrottleManagerImpl::kInitialMedianInMS * | |
358 NetworkThrottleManagerImpl::kMedianLifetimeMultiple - | |
359 1)); | |
360 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
361 CreateThrottle(IDLE, false)); | |
362 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
363 CreateThrottle(IDLE, false)); | |
364 clock->SetNowTicks(now); | |
365 | |
366 // Two throttles need to be created because of an implementation issue; | |
367 // there isn't a way to force recomputation of outstanding requests on a | |
368 // test-provoked time shift, so this test relies on the first throttle | |
369 // creation to do it for the second throttle. | |
370 // First one can't be throttled since that result is indeterminate because | |
Charlie Harrison
2016/07/08 22:36:06
The first one .... an implementation detail
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Done.
| |
371 // of implementation detail (SetNowTicks() doesn't force a recomputation of | |
372 // how many requests have aged out). | |
Charlie Harrison
2016/07/08 22:36:06
You could expose another for testing method...
Randy Smith (Not in Mondays)
2016/08/29 19:44:51
Actually, I already did. Let's see if I can incor
| |
373 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
374 CreateThrottle(IDLE, false)); | |
375 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
376 CreateThrottle(THROTTLED, true)); | |
377 | |
378 throttle1.reset(); | |
379 throttle2.reset(); | |
380 throttle3.reset(); | |
381 throttle4.reset(); | |
382 | |
383 // Create 100 throttles and destroy them, effectively with lifetime zero. | |
384 // This should substantially decrease the median age estimate. | |
Charlie Harrison
2016/07/08 22:36:06
This test makes me nervous about fast subresource
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Heh. That would be a plumbing nightmare (i.e. pro
Charlie Harrison
2016/08/29 21:10:25
Fast subresource aborts occur with the js api wind
| |
385 for (int i = 0; i < 100; ++i) { | |
386 std::unique_ptr<NetworkThrottleManager::Throttle> tmp( | |
387 CreateThrottle(IDLE, false)); | |
388 } | |
389 | |
390 // The identical test above should no longer result in blocked throttles. | |
391 clock->SetNowTicks( | |
392 now - base::TimeDelta::FromMilliseconds( | |
393 NetworkThrottleManagerImpl::kInitialMedianInMS * | |
394 NetworkThrottleManagerImpl::kMedianLifetimeMultiple - | |
395 1)); | |
396 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( | |
397 CreateThrottle(IDLE, false)); | |
398 std::unique_ptr<NetworkThrottleManager::Throttle> throttle6( | |
399 CreateThrottle(IDLE, false)); | |
400 clock->SetNowTicks(now); | |
401 // First one can't be throttled since that result is indeterminate because | |
402 // of implementation detail (SetNowTicks() doesn't force a recomputation of | |
403 // how many requests have aged out). | |
404 std::unique_ptr<NetworkThrottleManager::Throttle> throttle7( | |
405 CreateThrottle(IDLE, false)); | |
406 std::unique_ptr<NetworkThrottleManager::Throttle> throttle8( | |
407 CreateThrottle(THROTTLED, false)); | |
408 } | |
409 | |
410 // Confirm that just "aging out" reuqests will result in unblocking | |
Charlie Harrison
2016/07/08 22:36:06
s/reuqests/requests
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Done.
| |
411 // blocked requests. | |
412 TEST_F(NetworkThrottleManagerTest, AgeInvalidThrottles) { | |
413 base::TimeTicks now(base::TimeTicks::Now()); | |
414 base::SimpleTestTickClock* clock(new base::SimpleTestTickClock); | |
415 | |
416 throttle_manager()->SetTickClockForTesting( | |
417 std::unique_ptr<base::TickClock>(clock)); | |
418 clock->SetNowTicks(base::TimeTicks::Now()); | |
419 | |
420 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
421 CreateThrottle(IDLE, false)); | |
422 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
423 CreateThrottle(IDLE, false)); | |
424 clock->SetNowTicks(now); | |
425 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
426 CreateThrottle(THROTTLED, true)); | |
427 | |
428 EXPECT_EQ(0, throttle_state_change_count()); | |
429 clock->SetNowTicks( | |
430 now + base::TimeDelta::FromMilliseconds( | |
431 NetworkThrottleManagerImpl::kInitialMedianInMS * | |
432 (NetworkThrottleManagerImpl::kMedianLifetimeMultiple + 1))); | |
433 throttle_manager()->ConditionallyTriggerTimerForTesting(); | |
434 EXPECT_EQ(1, throttle_state_change_count()); | |
435 EXPECT_EQ(throttle3.get(), last_throttle_state_change()); | |
436 EXPECT_FALSE(throttle3->IsBlocked()); | |
437 } | |
438 | |
439 // Confirm that "ignore_limits" boolean is respected. | |
440 TEST_F(NetworkThrottleManagerTest, IgnoreLimits) { | |
441 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( | |
442 CreateThrottle(HIGHEST, false)); | |
443 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( | |
444 CreateThrottle(LOW, false)); | |
445 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( | |
446 CreateThrottle(IDLE, false)); | |
447 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( | |
448 CreateThrottle(THROTTLED, true)); | |
449 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( | |
450 CreateThrottleIgnoringLimits(THROTTLED)); | |
451 } | |
452 | |
49 } // namespace | 453 } // namespace |
50 | 454 |
51 } // namespace net | 455 } // namespace net |
OLD | NEW |