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