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