| Index: net/base/network_throttle_manager_unittest.cc
|
| diff --git a/net/base/network_throttle_manager_unittest.cc b/net/base/network_throttle_manager_unittest.cc
|
| index c39a7a894fd4a97a904e3fa70b624b44364555f5..bef9c4a04b1e0d919e425b20911f51050cbefd5a 100644
|
| --- a/net/base/network_throttle_manager_unittest.cc
|
| +++ b/net/base/network_throttle_manager_unittest.cc
|
| @@ -2,10 +2,15 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "net/base/network_throttle_manager.h"
|
| +#include "net/base/network_throttle_manager_impl.h"
|
|
|
| #include <memory>
|
|
|
| +#include "base/bind.h"
|
| +#include "base/callback.h"
|
| +#include "base/callback_helpers.h"
|
| +#include "base/memory/scoped_vector.h"
|
| +#include "base/test/simple_test_clock.h"
|
| #include "net/base/request_priority.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| @@ -19,33 +24,400 @@ class NetworkThrottleManagerTest : public testing::Test,
|
| NetworkThrottleManager::ThrottleDelegate {
|
| public:
|
| NetworkThrottleManagerTest()
|
| - : throttler_(NetworkThrottleManager::CreateThrottler()) {}
|
| + : throttle_state_change_count_(0),
|
| + last_throttle_state_change_(nullptr),
|
| + throttle_manager_(new NetworkThrottleManagerImpl) {}
|
|
|
| protected:
|
| std::unique_ptr<NetworkThrottleManager::Throttle> CreateThrottle(
|
| net::RequestPriority priority,
|
| bool expected_throttle_state) {
|
| std::unique_ptr<NetworkThrottleManager::Throttle> throttle(
|
| - throttler_->CreateThrottle(this, priority, false));
|
| - EXPECT_EQ(expected_throttle_state, throttle->IsThrottled());
|
| + throttle_manager_->CreateThrottle(this, priority, false));
|
| + EXPECT_EQ(expected_throttle_state, throttle->IsBlocked());
|
| + throttled_blocked_[throttle.get()] = !throttle->IsBlocked();
|
| return throttle;
|
| }
|
|
|
| + std::unique_ptr<NetworkThrottleManager::Throttle>
|
| + CreateThrottleIgnoringLimits(net::RequestPriority priority) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle(
|
| + throttle_manager_->CreateThrottle(this, priority, true));
|
| + EXPECT_FALSE(throttle->IsBlocked());
|
| + throttled_blocked_[throttle.get()] = !throttle->IsBlocked();
|
| + return throttle;
|
| + }
|
| +
|
| + int throttle_state_change_count() { return throttle_state_change_count_; }
|
| +
|
| + NetworkThrottleManager::Throttle* last_throttle_state_change() {
|
| + return last_throttle_state_change_;
|
| + }
|
| +
|
| + void SetUpcallCallback(const base::Closure& callback) {
|
| + upcall_callback_ = callback;
|
| + }
|
| +
|
| + void SetClock(std::unique_ptr<base::Clock> clock) {
|
| + throttle_manager_->SetClockForTesting(std::move(clock));
|
| + }
|
| +
|
| private:
|
| // NetworkThrottleManager::Delegate
|
| - void OnThrottleStateChanged() override { ADD_FAILURE(); }
|
| + void OnThrottleStateChanged(
|
| + NetworkThrottleManager::Throttle* throttle) override {
|
| + EXPECT_FALSE(throttled_blocked_.find(throttle) == throttled_blocked_.end());
|
| + EXPECT_FALSE(throttled_blocked_[throttle]);
|
| + throttled_blocked_[throttle] = true;
|
| + ++throttle_state_change_count_;
|
| + last_throttle_state_change_ = throttle;
|
| + if (!upcall_callback_.is_null())
|
| + base::ResetAndReturn(&upcall_callback_).Run();
|
| + }
|
|
|
| - std::unique_ptr<NetworkThrottleManager> throttler_;
|
| + int throttle_state_change_count_;
|
| + NetworkThrottleManager::Throttle* last_throttle_state_change_;
|
| + std::unique_ptr<NetworkThrottleManagerImpl> throttle_manager_;
|
| + std::map<NetworkThrottleManager::Throttle*, bool> throttled_blocked_;
|
| + base::Closure upcall_callback_;
|
| };
|
|
|
| -// Check to confirm that all created throttles start unthrottled for the
|
| -// current null implementation.
|
| +// Check to confirm that all created throttles at priorities other than
|
| +// THROTTLED start unblocked.
|
| TEST_F(NetworkThrottleManagerTest, AllUnthrottled) {
|
| for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
|
| + if (i == THROTTLED)
|
| + continue;
|
| CreateThrottle(static_cast<RequestPriority>(i), false);
|
| }
|
| }
|
|
|
| +// Check for basic semantics around the new THROTTLED level.
|
| +TEST_F(NetworkThrottleManagerTest, ThrottledBlocking) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(THROTTLED, true));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, true));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
|
| + CreateThrottle(THROTTLED, true));
|
| +
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| +
|
| + throttle1.reset();
|
| + EXPECT_EQ(1, throttle_state_change_count());
|
| + EXPECT_EQ(throttle3.get(), last_throttle_state_change());
|
| +
|
| + EXPECT_FALSE(throttle3->IsBlocked());
|
| + EXPECT_TRUE(throttle4->IsBlocked());
|
| + EXPECT_TRUE(throttle5->IsBlocked());
|
| +
|
| + throttle2.reset();
|
| + EXPECT_EQ(2, throttle_state_change_count());
|
| + EXPECT_EQ(throttle4.get(), last_throttle_state_change());
|
| +
|
| + EXPECT_FALSE(throttle3->IsBlocked());
|
| + EXPECT_FALSE(throttle4->IsBlocked());
|
| + EXPECT_TRUE(throttle5->IsBlocked());
|
| +}
|
| +
|
| +// Check that THROTTLED semantics are dependent on all outstanding requests.
|
| +TEST_F(NetworkThrottleManagerTest, ThrottledBlockingMultiPriority) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(HIGHEST, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(LOW, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(IDLE, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, true));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
|
| + CreateThrottle(THROTTLED, true));
|
| +
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| +
|
| + throttle1.reset();
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| + EXPECT_FALSE(throttle3->IsBlocked());
|
| + EXPECT_TRUE(throttle4->IsBlocked());
|
| + EXPECT_TRUE(throttle5->IsBlocked());
|
| +
|
| + throttle2.reset();
|
| + EXPECT_EQ(1, throttle_state_change_count());
|
| + EXPECT_EQ(throttle4.get(), last_throttle_state_change());
|
| +
|
| + EXPECT_FALSE(throttle3->IsBlocked());
|
| + EXPECT_FALSE(throttle4->IsBlocked());
|
| + EXPECT_TRUE(throttle5->IsBlocked());
|
| +
|
| + throttle3.reset();
|
| + EXPECT_EQ(2, throttle_state_change_count());
|
| + EXPECT_EQ(throttle5.get(), last_throttle_state_change());
|
| +
|
| + EXPECT_FALSE(throttle4->IsBlocked());
|
| + EXPECT_FALSE(throttle5->IsBlocked());
|
| +}
|
| +
|
| +// Check that a SetPriority() away from THROTTLED results in unblocking
|
| +// and an upcall.
|
| +TEST_F(NetworkThrottleManagerTest, ThrottledSetPriority) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(THROTTLED, true));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, true));
|
| +
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| +
|
| + throttle3->SetPriority(LOW);
|
| + EXPECT_EQ(1, throttle_state_change_count());
|
| + EXPECT_EQ(throttle3.get(), last_throttle_state_change());
|
| + EXPECT_FALSE(throttle3->IsBlocked());
|
| + EXPECT_TRUE(throttle4->IsBlocked());
|
| +}
|
| +
|
| +void ResetThrottles(ScopedVector<NetworkThrottleManager::Throttle> throttles) {
|
| + // All pointers in the vector should be deleted on exit.
|
| +}
|
| +
|
| +// Note that this routine is dependent on priority setting
|
| +// *not* resulting in destruction of any throttle and should only
|
| +// be used in tests where that is true.
|
| +void SetAllToPriority(
|
| + RequestPriority priority,
|
| + std::vector<NetworkThrottleManager::Throttle*> throttles) {
|
| + for (size_t i = 0; i < throttles.size(); ++i)
|
| + throttles[i]->SetPriority(priority);
|
| +}
|
| +
|
| +// Check that tearing down all elements in the NTM on a SetPriority
|
| +// upcall doesn't create any problems.
|
| +TEST_F(NetworkThrottleManagerTest, ThrottleTeardown) {
|
| + ScopedVector<NetworkThrottleManager::Throttle> throttles;
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle_temporary;
|
| +
|
| + throttle_temporary = CreateThrottle(THROTTLED, false);
|
| + throttles.push_back(std::move(throttle_temporary.release()));
|
| +
|
| + throttle_temporary = CreateThrottle(THROTTLED, false);
|
| + throttles.push_back(std::move(throttle_temporary.release()));
|
| +
|
| + // Note that if there is more than one throttle blocked, then the
|
| + // number of throttle state changes is dependent on destruction order.
|
| + // So only one blocked throttle is created.
|
| +
|
| + throttle_temporary = CreateThrottle(THROTTLED, true);
|
| + NetworkThrottleManager::Throttle* throttle3 = throttle_temporary.get();
|
| + throttles.push_back(std::move(throttle_temporary.release()));
|
| +
|
| + SetUpcallCallback(base::Bind(&ResetThrottles, base::Passed(&throttles)));
|
| +
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| +
|
| + throttle3->SetPriority(LOW);
|
| + // If the test is functioning as expected, throttle3 now points to
|
| + // a deleted object and can no longer be indirected through.
|
| +
|
| + EXPECT_EQ(1, throttle_state_change_count());
|
| + EXPECT_EQ(throttle3, last_throttle_state_change());
|
| +}
|
| +
|
| +// Check that modifying all the priorities of the allocated throttles in
|
| +// the callback works properly.
|
| +TEST_F(NetworkThrottleManagerTest, ThrottlePriorityReset) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(THROTTLED, true));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, true));
|
| +
|
| + std::vector<NetworkThrottleManager::Throttle*> throttles;
|
| + throttles.push_back(throttle1.get());
|
| + throttles.push_back(throttle2.get());
|
| + throttles.push_back(throttle3.get());
|
| +
|
| + SetUpcallCallback(
|
| + base::Bind(&SetAllToPriority, MEDIUM, base::Passed(&throttles)));
|
| +
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| + throttle3->SetPriority(HIGHEST);
|
| +
|
| + // Expected result: throttles 1-3 @ medium priority (the callback
|
| + // should have overridden the priority setting above), only throttle 4
|
| + // blocked (throttle3 should have been unblocked by either of
|
| + // the priority changes), and one state changes (the unblocking).
|
| + EXPECT_EQ(MEDIUM, throttle1->Priority());
|
| + EXPECT_EQ(MEDIUM, throttle2->Priority());
|
| + EXPECT_EQ(MEDIUM, throttle3->Priority());
|
| + EXPECT_EQ(THROTTLED, throttle4->Priority());
|
| + EXPECT_FALSE(throttle1->IsBlocked());
|
| + EXPECT_FALSE(throttle2->IsBlocked());
|
| + EXPECT_FALSE(throttle3->IsBlocked());
|
| + EXPECT_TRUE(throttle4->IsBlocked());
|
| + EXPECT_EQ(1, throttle_state_change_count());
|
| +}
|
| +
|
| +// Check that modifying the priority of a request from a non-THROTTLED
|
| +// value to THROTTLED causes no change in behavior.
|
| +TEST_F(NetworkThrottleManagerTest, ThrottlePriorityResetToThrottled) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(THROTTLED, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(LOW, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, true));
|
| +
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| + throttle3->SetPriority(THROTTLED);
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| +
|
| + EXPECT_FALSE(throttle1->IsBlocked());
|
| + EXPECT_FALSE(throttle2->IsBlocked());
|
| + EXPECT_FALSE(throttle3->IsBlocked());
|
| + EXPECT_TRUE(throttle4->IsBlocked());
|
| +
|
| + EXPECT_EQ(THROTTLED, throttle1->Priority());
|
| + EXPECT_EQ(THROTTLED, throttle2->Priority());
|
| + EXPECT_EQ(THROTTLED, throttle3->Priority());
|
| + EXPECT_EQ(THROTTLED, throttle4->Priority());
|
| +}
|
| +
|
| +// Confirm that old requests don't count against the ilmit.
|
| +TEST_F(NetworkThrottleManagerTest, DontCountAgedRequests) {
|
| + base::Time now(base::Time::Now());
|
| + base::SimpleTestClock* clock(new base::SimpleTestClock);
|
| +
|
| + SetClock(std::unique_ptr<base::Clock>(clock));
|
| +
|
| + const int age_in_days_of_old_throttles = 4;
|
| +
|
| + // Confirm default median and timing means that 4 days is long enough ago
|
| + // to be aged out.
|
| + EXPECT_GT(age_in_days_of_old_throttles * 24 * 60 * 60 * 1000,
|
| + NetworkThrottleManagerImpl::kInitialMedianInMS *
|
| + NetworkThrottleManagerImpl::kMedianLifetimeMultiple);
|
| +
|
| + clock->SetNow(now - base::TimeDelta::FromDays(age_in_days_of_old_throttles));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(IDLE, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(IDLE, false));
|
| +
|
| + clock->SetNow(now);
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(LOW, false));
|
| +
|
| + // First throttled request should not be blocked.
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, false));
|
| +
|
| + // Second should be.
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
|
| + CreateThrottle(THROTTLED, true));
|
| +
|
| + // Destroying the old requests should not result in any upcalls.
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| + throttle1.reset();
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| + throttle2.reset();
|
| + EXPECT_EQ(0, throttle_state_change_count());
|
| +
|
| + // But destroying a new request should result in a state change.
|
| + throttle3.reset();
|
| + EXPECT_EQ(1, throttle_state_change_count());
|
| + EXPECT_EQ(throttle5.get(), last_throttle_state_change());
|
| +}
|
| +
|
| +// Confirm that a slew of throttles of a specific age will shift the
|
| +// median for determining "aged requests" to that age.
|
| +TEST_F(NetworkThrottleManagerTest, ShiftMedian) {
|
| + base::Time now(base::Time::Now());
|
| + base::SimpleTestClock* clock(new base::SimpleTestClock);
|
| +
|
| + SetClock(std::unique_ptr<base::Clock>(clock));
|
| +
|
| + // Setup two throttles of age *just short* of aging out; confirm
|
| + // they result in blocked THROTTLED requests.
|
| + clock->SetNow(now -
|
| + base::TimeDelta::FromMilliseconds(
|
| + NetworkThrottleManagerImpl::kInitialMedianInMS *
|
| + NetworkThrottleManagerImpl::kMedianLifetimeMultiple -
|
| + 1));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(IDLE, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(IDLE, false));
|
| + clock->SetNow(now);
|
| +
|
| + // Two throttles need to be created because of an implementation issue;
|
| + // there isn't a way to force recomputation of outstanding requests on a
|
| + // test-provoked time shift, so this test relies on the first throttle
|
| + // creation to do it for the second throttle.
|
| + // First one can't be throttled since that result is indeterminate because
|
| + // of implementation detail (SetNow() doesn't force a recomputation of
|
| + // how many requests have aged out).
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(IDLE, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, true));
|
| +
|
| + throttle1.reset();
|
| + throttle2.reset();
|
| + throttle3.reset();
|
| + throttle4.reset();
|
| +
|
| + // Create 100 throttles and destroy them, effectively with lifetime zero.
|
| + // This should substantially decrease the median age estimate.
|
| + for (int i = 0; i < 100; ++i) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> tmp(
|
| + CreateThrottle(IDLE, false));
|
| + }
|
| +
|
| + // The identical test above should no longer result in blocked throttles.
|
| + clock->SetNow(now -
|
| + base::TimeDelta::FromMilliseconds(
|
| + NetworkThrottleManagerImpl::kInitialMedianInMS *
|
| + NetworkThrottleManagerImpl::kMedianLifetimeMultiple -
|
| + 1));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
|
| + CreateThrottle(IDLE, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle6(
|
| + CreateThrottle(IDLE, false));
|
| + clock->SetNow(now);
|
| + // First one can't be throttled since that result is indeterminate because
|
| + // of implementation detail (SetNow() doesn't force a recomputation of
|
| + // how many requests have aged out).
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle7(
|
| + CreateThrottle(IDLE, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle8(
|
| + CreateThrottle(THROTTLED, false));
|
| +}
|
| +
|
| +// Confirm that "ignore_limits" boolean is respected.
|
| +TEST_F(NetworkThrottleManagerTest, IgnoreLimits) {
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle1(
|
| + CreateThrottle(HIGHEST, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle2(
|
| + CreateThrottle(LOW, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle3(
|
| + CreateThrottle(IDLE, false));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle4(
|
| + CreateThrottle(THROTTLED, true));
|
| + std::unique_ptr<NetworkThrottleManager::Throttle> throttle5(
|
| + CreateThrottleIgnoringLimits(THROTTLED));
|
| +}
|
| +
|
| } // namespace
|
|
|
| } // namespace net
|
|
|