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" | |
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" |
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 }; |
| 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 OnThrottleStateChanged( |
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())); |
| 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()); |
| 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; |
| 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 for (int i = 0; i < 100; ++i) { |
| 399 std::unique_ptr<NetworkThrottleManager::Throttle> tmp( |
| 400 CreateThrottle(IDLE, UNBLOCKED)); |
| 401 } |
| 402 |
| 403 // The identical test above should no longer result in blocked throttles. |
| 404 SetClockDelta(base::TimeDelta()); |
| 405 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( |
| 406 CreateThrottle(IDLE, UNBLOCKED)); |
| 407 std::unique_ptr<NetworkThrottleManager::Throttle> throttle6( |
| 408 CreateThrottle(IDLE, UNBLOCKED)); |
| 409 SetClockDelta(base::TimeDelta::FromMilliseconds( |
| 410 kInitialAgeHorizonForUncountedRequests - 1)); |
| 411 throttle_manager()->ConditionallyTriggerTimerForTesting(); |
| 412 std::unique_ptr<NetworkThrottleManager::Throttle> throttle7( |
| 413 CreateThrottle(THROTTLED, UNBLOCKED)); |
| 414 } |
| 415 |
| 416 // Confirm that just "aging out" requests will result in unblocking |
| 417 // blocked requests. |
| 418 TEST_F(NetworkThrottleManagerTest, AgeInvalidThrottles) { |
| 419 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( |
| 420 CreateThrottle(IDLE, UNBLOCKED)); |
| 421 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( |
| 422 CreateThrottle(IDLE, UNBLOCKED)); |
| 423 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( |
| 424 CreateThrottle(THROTTLED, BLOCKED)); |
| 425 |
| 426 EXPECT_EQ(0, throttle_state_change_count()); |
| 427 SetClockDelta(base::TimeDelta::FromMilliseconds( |
| 428 (kInitialAgeHorizonForUncountedRequests + |
| 429 NetworkThrottleManagerImpl::kInitialMedianInMs))); |
| 430 throttle_manager()->ConditionallyTriggerTimerForTesting(); |
| 431 EXPECT_EQ(1, throttle_state_change_count()); |
| 432 EXPECT_EQ(throttle3.get(), last_throttle_to_change_state()); |
| 433 EXPECT_FALSE(throttle3->IsBlocked()); |
| 434 } |
| 435 |
| 436 // Confirm that throttles that are blocked for a while and then |
| 437 // unblocked don't "age out". |
| 438 TEST_F(NetworkThrottleManagerTest, AgeBlockedThrottles) { |
| 439 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( |
| 440 CreateThrottle(IDLE, UNBLOCKED)); |
| 441 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( |
| 442 CreateThrottle(IDLE, UNBLOCKED)); |
| 443 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( |
| 444 CreateThrottle(THROTTLED, BLOCKED)); |
| 445 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( |
| 446 CreateThrottle(THROTTLED, BLOCKED)); |
| 447 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( |
| 448 CreateThrottle(THROTTLED, BLOCKED)); |
| 449 |
| 450 EXPECT_EQ(0, throttle_state_change_count()); |
| 451 SetClockDelta(base::TimeDelta::FromMilliseconds( |
| 452 (kInitialAgeHorizonForUncountedRequests + |
| 453 NetworkThrottleManagerImpl::kInitialMedianInMs))); |
| 454 throttle_manager()->ConditionallyTriggerTimerForTesting(); |
| 455 |
| 456 // If blocked throttles aged out, all three throttles should have been |
| 457 // unblocked. If not, only the two replacing the IDLE throttles should |
| 458 // have. |
| 459 EXPECT_EQ(2, throttle_state_change_count()); |
| 460 } |
| 461 |
| 462 // Confirm that deleting old throttles before they age out doesn't |
| 463 // interfere with the aging out of more recent throttles. |
| 464 TEST_F(NetworkThrottleManagerTest, DeletionAgingInterference) { |
| 465 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( |
| 466 CreateThrottle(IDLE, UNBLOCKED)); |
| 467 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( |
| 468 CreateThrottle(IDLE, UNBLOCKED)); |
| 469 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( |
| 470 CreateThrottle(THROTTLED, BLOCKED)); |
| 471 EXPECT_EQ(0, throttle_state_change_count()); |
| 472 |
| 473 SetClockDelta(base::TimeDelta::FromMilliseconds( |
| 474 kInitialAgeHorizonForUncountedRequests / 2)); |
| 475 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( |
| 476 CreateThrottle(IDLE, UNBLOCKED)); |
| 477 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( |
| 478 CreateThrottle(IDLE, UNBLOCKED)); |
| 479 throttle_manager()->ConditionallyTriggerTimerForTesting(); |
| 480 EXPECT_EQ(0, throttle_state_change_count()); |
| 481 |
| 482 throttle1.reset(); |
| 483 throttle2.reset(); |
| 484 throttle_manager()->ConditionallyTriggerTimerForTesting(); |
| 485 EXPECT_EQ(0, throttle_state_change_count()); |
| 486 |
| 487 SetClockDelta(base::TimeDelta::FromMilliseconds( |
| 488 (3 * kInitialAgeHorizonForUncountedRequests / 2 + |
| 489 NetworkThrottleManagerImpl::kInitialMedianInMs))); |
| 490 throttle_manager()->ConditionallyTriggerTimerForTesting(); |
| 491 EXPECT_EQ(1, throttle_state_change_count()); |
| 492 EXPECT_EQ(throttle3.get(), last_throttle_to_change_state()); |
| 493 EXPECT_FALSE(throttle3->IsBlocked()); |
| 494 } |
| 495 |
| 496 // Confirm that "ignore_limits" boolean is respected. |
| 497 TEST_F(NetworkThrottleManagerTest, IgnoreLimits) { |
| 498 std::unique_ptr<NetworkThrottleManager::Throttle> throttle1( |
| 499 CreateThrottle(HIGHEST, UNBLOCKED)); |
| 500 std::unique_ptr<NetworkThrottleManager::Throttle> throttle2( |
| 501 CreateThrottle(LOW, UNBLOCKED)); |
| 502 std::unique_ptr<NetworkThrottleManager::Throttle> throttle3( |
| 503 CreateThrottle(IDLE, UNBLOCKED)); |
| 504 std::unique_ptr<NetworkThrottleManager::Throttle> throttle4( |
| 505 CreateThrottle(THROTTLED, BLOCKED)); |
| 506 std::unique_ptr<NetworkThrottleManager::Throttle> throttle5( |
| 507 CreateThrottleIgnoringLimits(THROTTLED)); |
47 } | 508 } |
48 | 509 |
49 } // namespace | 510 } // namespace |
50 | 511 |
51 } // namespace net | 512 } // namespace net |
OLD | NEW |