Commit 1a32b862 authored by chrisha's avatar chrisha Committed by Commit bot

Make FilteredMemoryPressureCalculator injected dependencies externally owned.

This makes final integration with the MemoryPressureMonitor easier, as it will own a single TickClock which can be injected down the entire stack of dependencies.

BUG=520962

Review URL: https://codereview.chromium.org/1317353006

Cr-Commit-Position: refs/heads/master@{#347850}
parent d6eca98d
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "components/memory_pressure/filtered_memory_pressure_calculator.h" #include "components/memory_pressure/filtered_memory_pressure_calculator.h"
#include "base/time/default_tick_clock.h" #include "base/time/tick_clock.h"
namespace memory_pressure { namespace memory_pressure {
...@@ -24,13 +24,17 @@ const int FilteredMemoryPressureCalculator::kModeratePressureCooldownPeriodMs = ...@@ -24,13 +24,17 @@ const int FilteredMemoryPressureCalculator::kModeratePressureCooldownPeriodMs =
5000; 5000;
FilteredMemoryPressureCalculator::FilteredMemoryPressureCalculator( FilteredMemoryPressureCalculator::FilteredMemoryPressureCalculator(
scoped_ptr<MemoryPressureCalculator> pressure_calculator) MemoryPressureCalculator* pressure_calculator,
: tick_clock_(new base::DefaultTickClock()), base::TickClock* tick_clock)
pressure_calculator_(pressure_calculator.Pass()), : pressure_calculator_(pressure_calculator),
tick_clock_(tick_clock),
current_pressure_level_( current_pressure_level_(
MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
samples_taken_(false), samples_taken_(false),
cooldown_in_progress_(false) {} cooldown_in_progress_(false) {
DCHECK(pressure_calculator);
DCHECK(tick_clock);
}
FilteredMemoryPressureCalculator::~FilteredMemoryPressureCalculator() { FilteredMemoryPressureCalculator::~FilteredMemoryPressureCalculator() {
} }
......
...@@ -5,9 +5,14 @@ ...@@ -5,9 +5,14 @@
#ifndef COMPONENTS_MEMORY_PRESSURE_FILTERED_MEMORY_PRESSURE_CALCULATOR_H_ #ifndef COMPONENTS_MEMORY_PRESSURE_FILTERED_MEMORY_PRESSURE_CALCULATOR_H_
#define COMPONENTS_MEMORY_PRESSURE_FILTERED_MEMORY_PRESSURE_CALCULATOR_H_ #define COMPONENTS_MEMORY_PRESSURE_FILTERED_MEMORY_PRESSURE_CALCULATOR_H_
#include "base/time/tick_clock.h"
#include "components/memory_pressure/memory_pressure_calculator.h" #include "components/memory_pressure/memory_pressure_calculator.h"
#include "base/time/time.h"
namespace base {
class TickClock;
} // namespace base
namespace memory_pressure { namespace memory_pressure {
#if defined(MEMORY_PRESSURE_IS_POLLING) #if defined(MEMORY_PRESSURE_IS_POLLING)
...@@ -26,18 +31,16 @@ class FilteredMemoryPressureCalculator : public MemoryPressureCalculator { ...@@ -26,18 +31,16 @@ class FilteredMemoryPressureCalculator : public MemoryPressureCalculator {
static const int kCriticalPressureCooldownPeriodMs; static const int kCriticalPressureCooldownPeriodMs;
static const int kModeratePressureCooldownPeriodMs; static const int kModeratePressureCooldownPeriodMs;
explicit FilteredMemoryPressureCalculator( // The provided |pressure_calculator| and |tick_clock| must outlive this
scoped_ptr<MemoryPressureCalculator> pressure_calculator); // object.
FilteredMemoryPressureCalculator(
MemoryPressureCalculator* pressure_calculator,
base::TickClock* tick_clock);
~FilteredMemoryPressureCalculator() override; ~FilteredMemoryPressureCalculator() override;
// Calculates the current pressure level. // Calculates the current pressure level.
MemoryPressureLevel CalculateCurrentPressureLevel() override; MemoryPressureLevel CalculateCurrentPressureLevel() override;
// Testing seam for configuring the tick clock in use.
void set_tick_clock(scoped_ptr<base::TickClock> tick_clock) {
tick_clock_ = tick_clock.Pass();
}
// Accessors for unittesting. // Accessors for unittesting.
bool cooldown_in_progress() const { return cooldown_in_progress_; } bool cooldown_in_progress() const { return cooldown_in_progress_; }
base::TimeTicks cooldown_start_time() const { return cooldown_start_time_; } base::TimeTicks cooldown_start_time() const { return cooldown_start_time_; }
...@@ -46,11 +49,11 @@ class FilteredMemoryPressureCalculator : public MemoryPressureCalculator { ...@@ -46,11 +49,11 @@ class FilteredMemoryPressureCalculator : public MemoryPressureCalculator {
private: private:
friend class TestFilteredMemoryPressureCalculator; friend class TestFilteredMemoryPressureCalculator;
// The delegate tick clock. This is settable as a testing seam.
scoped_ptr<base::TickClock> tick_clock_;
// The delegate pressure calculator. Provided by the constructor. // The delegate pressure calculator. Provided by the constructor.
scoped_ptr<MemoryPressureCalculator> pressure_calculator_; MemoryPressureCalculator* pressure_calculator_;
// The delegate tick clock. Provided by the constructor.
base::TickClock* tick_clock_;
// The memory pressure currently being reported. // The memory pressure currently being reported.
MemoryPressureLevel current_pressure_level_; MemoryPressureLevel current_pressure_level_;
......
...@@ -15,282 +15,267 @@ namespace memory_pressure { ...@@ -15,282 +15,267 @@ namespace memory_pressure {
class FilteredMemoryPressureCalculatorTest : public testing::Test { class FilteredMemoryPressureCalculatorTest : public testing::Test {
public: public:
FilteredMemoryPressureCalculatorTest() FilteredMemoryPressureCalculatorTest()
: calculator_(nullptr), tick_clock_(nullptr) {} : filter_(&calculator_, &tick_clock_) {}
void SetUp() override {
// Ownership of the calculator and tick clock are both passed to the filter.
calculator_ = new TestMemoryPressureCalculator();
tick_clock_ = new base::SimpleTestTickClock();
filter_.reset(new FilteredMemoryPressureCalculator(
scoped_ptr<MemoryPressureCalculator>(calculator_)));
filter_->set_tick_clock(scoped_ptr<base::TickClock>(tick_clock_));
}
void TearDown() override {
calculator_ = nullptr;
tick_clock_ = nullptr;
filter_.reset();
}
// Advances the tick clock by the given number of milliseconds. // Advances the tick clock by the given number of milliseconds.
void Tick(int ms) { void Tick(int ms) {
tick_clock_->Advance(base::TimeDelta::FromMilliseconds(ms)); tick_clock_.Advance(base::TimeDelta::FromMilliseconds(ms));
} }
// Delegates that are inject into the filter under test. // Delegates that are injected into the object under test.
TestMemoryPressureCalculator* calculator_; TestMemoryPressureCalculator calculator_;
base::SimpleTestTickClock* tick_clock_; base::SimpleTestTickClock tick_clock_;
// The object under test. // The object under test.
scoped_ptr<FilteredMemoryPressureCalculator> filter_; FilteredMemoryPressureCalculator filter_;
private: private:
DISALLOW_COPY_AND_ASSIGN(FilteredMemoryPressureCalculatorTest); DISALLOW_COPY_AND_ASSIGN(FilteredMemoryPressureCalculatorTest);
}; };
TEST_F(FilteredMemoryPressureCalculatorTest, FirstCallInvokesCalculator) { TEST_F(FilteredMemoryPressureCalculatorTest, FirstCallInvokesCalculator) {
calculator_->SetCritical(); calculator_.SetCritical();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
TEST_F(FilteredMemoryPressureCalculatorTest, CalculatorNotAlwaysInvoked) { TEST_F(FilteredMemoryPressureCalculatorTest, CalculatorNotAlwaysInvoked) {
calculator_->SetModerate(); calculator_.SetModerate();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// Change the pressure reported by the caculator, but don't expect the filter // Change the pressure reported by the caculator, but don't expect the filter
// to report the critical value until a minimum sampling period has passed. // to report the critical value until a minimum sampling period has passed.
// The level has to be increasing so that hysteresis doesn't come into play. // The level has to be increasing so that hysteresis doesn't come into play.
calculator_->SetCritical(); calculator_.SetCritical();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs / 2); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs / 2);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// A sufficient time has passed so now it should report the new level. // A sufficient time has passed so now it should report the new level.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(2, calculator_->calls()); EXPECT_EQ(2, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
TEST_F(FilteredMemoryPressureCalculatorTest, CooldownCriticalToModerate) { TEST_F(FilteredMemoryPressureCalculatorTest, CooldownCriticalToModerate) {
calculator_->SetCritical(); calculator_.SetCritical();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// Initiate a cooldown period by jumping sufficiently far ahead for another // Initiate a cooldown period by jumping sufficiently far ahead for another
// sample. // sample.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetModerate(); calculator_.SetModerate();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(2, calculator_->calls()); EXPECT_EQ(2, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(tick_clock_->NowTicks(), filter_->cooldown_start_time()); EXPECT_EQ(tick_clock_.NowTicks(), filter_.cooldown_start_time());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step another sample interval and it should still not have changed. // Step another sample interval and it should still not have changed.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(3, calculator_->calls()); EXPECT_EQ(3, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step the cooldown period and it should change state. // Step the cooldown period and it should change state.
Tick(FilteredMemoryPressureCalculator::kCriticalPressureCooldownPeriodMs); Tick(FilteredMemoryPressureCalculator::kCriticalPressureCooldownPeriodMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(4, calculator_->calls()); EXPECT_EQ(4, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
TEST_F(FilteredMemoryPressureCalculatorTest, TEST_F(FilteredMemoryPressureCalculatorTest,
CooldownCriticalToModerateViaNone) { CooldownCriticalToModerateViaNone) {
calculator_->SetCritical(); calculator_.SetCritical();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// Initiate a cooldown period by jumping sufficiently far ahead for another // Initiate a cooldown period by jumping sufficiently far ahead for another
// sample. First go directly to no memory pressure before passing back through // sample. First go directly to no memory pressure before passing back through
// moderate. The final result should be a moderate memory pressure. // moderate. The final result should be a moderate memory pressure.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetNone(); calculator_.SetNone();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(2, calculator_->calls()); EXPECT_EQ(2, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(tick_clock_->NowTicks(), filter_->cooldown_start_time()); EXPECT_EQ(tick_clock_.NowTicks(), filter_.cooldown_start_time());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step another sample interval and it should still not have changed. // Step another sample interval and it should still not have changed.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetModerate(); calculator_.SetModerate();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(3, calculator_->calls()); EXPECT_EQ(3, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step the cooldown period and it should change state. // Step the cooldown period and it should change state.
Tick(FilteredMemoryPressureCalculator::kCriticalPressureCooldownPeriodMs); Tick(FilteredMemoryPressureCalculator::kCriticalPressureCooldownPeriodMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(4, calculator_->calls()); EXPECT_EQ(4, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
TEST_F(FilteredMemoryPressureCalculatorTest, CooldownCriticalToNone) { TEST_F(FilteredMemoryPressureCalculatorTest, CooldownCriticalToNone) {
calculator_->SetCritical(); calculator_.SetCritical();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// Initiate a cooldown period by jumping sufficiently far ahead for another // Initiate a cooldown period by jumping sufficiently far ahead for another
// sample. // sample.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetNone(); calculator_.SetNone();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(2, calculator_->calls()); EXPECT_EQ(2, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(tick_clock_->NowTicks(), filter_->cooldown_start_time()); EXPECT_EQ(tick_clock_.NowTicks(), filter_.cooldown_start_time());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step another sample interval and it should still not have changed. // Step another sample interval and it should still not have changed.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(3, calculator_->calls()); EXPECT_EQ(3, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step the cooldown period and it should change state. // Step the cooldown period and it should change state.
Tick(FilteredMemoryPressureCalculator::kCriticalPressureCooldownPeriodMs); Tick(FilteredMemoryPressureCalculator::kCriticalPressureCooldownPeriodMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(4, calculator_->calls()); EXPECT_EQ(4, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
TEST_F(FilteredMemoryPressureCalculatorTest, CooldownModerateToNone) { TEST_F(FilteredMemoryPressureCalculatorTest, CooldownModerateToNone) {
calculator_->SetModerate(); calculator_.SetModerate();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// Initiate a cooldown period by jumping sufficiently far ahead for another // Initiate a cooldown period by jumping sufficiently far ahead for another
// sample. // sample.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetNone(); calculator_.SetNone();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(2, calculator_->calls()); EXPECT_EQ(2, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(tick_clock_->NowTicks(), filter_->cooldown_start_time()); EXPECT_EQ(tick_clock_.NowTicks(), filter_.cooldown_start_time());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step another sample interval and it should still not have changed. // Step another sample interval and it should still not have changed.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(3, calculator_->calls()); EXPECT_EQ(3, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step the cooldown period and it should change state. // Step the cooldown period and it should change state.
Tick(FilteredMemoryPressureCalculator::kModeratePressureCooldownPeriodMs); Tick(FilteredMemoryPressureCalculator::kModeratePressureCooldownPeriodMs);
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(4, calculator_->calls()); EXPECT_EQ(4, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
TEST_F(FilteredMemoryPressureCalculatorTest, InterruptedCooldownModerate) { TEST_F(FilteredMemoryPressureCalculatorTest, InterruptedCooldownModerate) {
calculator_->SetModerate(); calculator_.SetModerate();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// Initiate a cooldown period by jumping sufficiently far ahead for another // Initiate a cooldown period by jumping sufficiently far ahead for another
// sample. // sample.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetNone(); calculator_.SetNone();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(2, calculator_->calls()); EXPECT_EQ(2, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(tick_clock_->NowTicks(), filter_->cooldown_start_time()); EXPECT_EQ(tick_clock_.NowTicks(), filter_.cooldown_start_time());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step another sample interval and it should still not have changed. Since // Step another sample interval and it should still not have changed. Since
// the pressure level has increased back to moderate it should also end the // the pressure level has increased back to moderate it should also end the
// cooldown period. // cooldown period.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetModerate(); calculator_.SetModerate();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(3, calculator_->calls()); EXPECT_EQ(3, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
TEST_F(FilteredMemoryPressureCalculatorTest, InterruptedCooldownCritical) { TEST_F(FilteredMemoryPressureCalculatorTest, InterruptedCooldownCritical) {
calculator_->SetModerate(); calculator_.SetModerate();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(1, calculator_->calls()); EXPECT_EQ(1, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
// Initiate a cooldown period by jumping sufficiently far ahead for another // Initiate a cooldown period by jumping sufficiently far ahead for another
// sample. // sample.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetNone(); calculator_.SetNone();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(2, calculator_->calls()); EXPECT_EQ(2, calculator_.calls());
EXPECT_TRUE(filter_->cooldown_in_progress()); EXPECT_TRUE(filter_.cooldown_in_progress());
EXPECT_EQ(tick_clock_->NowTicks(), filter_->cooldown_start_time()); EXPECT_EQ(tick_clock_.NowTicks(), filter_.cooldown_start_time());
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
filter_->cooldown_high_tide()); filter_.cooldown_high_tide());
// Step another sample interval and it should still not have changed. Since // Step another sample interval and it should still not have changed. Since
// the pressure level has increased to critical it ends the cooldown period // the pressure level has increased to critical it ends the cooldown period
// and immediately reports the critical memory pressure. // and immediately reports the critical memory pressure.
Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs); Tick(FilteredMemoryPressureCalculator::kMinimumTimeBetweenSamplesMs);
calculator_->SetCritical(); calculator_.SetCritical();
EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
filter_->CalculateCurrentPressureLevel()); filter_.CalculateCurrentPressureLevel());
EXPECT_EQ(3, calculator_->calls()); EXPECT_EQ(3, calculator_.calls());
EXPECT_FALSE(filter_->cooldown_in_progress()); EXPECT_FALSE(filter_.cooldown_in_progress());
} }
#endif // defined(MEMORY_PRESSURE_IS_POLLING) #endif // defined(MEMORY_PRESSURE_IS_POLLING)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment