Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1821)

Unified Diff: base/memory/discardable_memory_manager_unittest.cc

Issue 336273003: base: Add soft memory limit to DiscardableMemoryManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix typo in comment Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/memory/discardable_memory_manager_unittest.cc
diff --git a/base/memory/discardable_memory_manager_unittest.cc b/base/memory/discardable_memory_manager_unittest.cc
index 8b08cf62c48f13cefff73a97b9729089617919ec..badb5277b2feccf520ecc85b783b201e5e70c786 100644
--- a/base/memory/discardable_memory_manager_unittest.cc
+++ b/base/memory/discardable_memory_manager_unittest.cc
@@ -45,13 +45,31 @@ class TestAllocationImpl : public internal::DiscardableMemoryManagerAllocation {
// Tests can assume that the default limit is at least 1024. Tests that rely on
// something else needs to explicit set the limit.
const size_t kDefaultMemoryLimit = 1024;
+const size_t kDefaultSoftMemoryLimit = kDefaultMemoryLimit;
const size_t kDefaultBytesToKeepUnderModeratePressure = kDefaultMemoryLimit;
+class TestDiscardableMemoryManagerImpl
+ : public internal::DiscardableMemoryManager {
+ public:
+ TestDiscardableMemoryManagerImpl()
+ : DiscardableMemoryManager(kDefaultMemoryLimit,
+ kDefaultSoftMemoryLimit,
+ kDefaultBytesToKeepUnderModeratePressure,
+ TimeDelta::Max()) {}
+
+ void SetNow(TimeTicks now) { now_ = now; }
+
+ protected:
+ // Overriden from internal::DiscardableMemoryManager:
+ virtual TimeTicks Now() const OVERRIDE { return now_; }
+
+ private:
+ TimeTicks now_;
+};
+
class DiscardableMemoryManagerTestBase {
public:
- DiscardableMemoryManagerTestBase()
- : manager_(kDefaultMemoryLimit,
- kDefaultBytesToKeepUnderModeratePressure) {
+ DiscardableMemoryManagerTestBase() {
manager_.RegisterMemoryPressureListener();
}
@@ -66,10 +84,16 @@ class DiscardableMemoryManagerTestBase {
void SetMemoryLimit(size_t bytes) { manager_.SetMemoryLimit(bytes); }
+ void SetSoftMemoryLimit(size_t bytes) { manager_.SetSoftMemoryLimit(bytes); }
+
void SetBytesToKeepUnderModeratePressure(size_t bytes) {
manager_.SetBytesToKeepUnderModeratePressure(bytes);
}
+ void SetHardMemoryLimitExpirationTime(TimeDelta time) {
+ manager_.SetHardMemoryLimitExpirationTime(time);
+ }
+
void Register(TestAllocationImpl* allocation, size_t bytes) {
manager_.Register(allocation, bytes);
}
@@ -102,9 +126,13 @@ class DiscardableMemoryManagerTestBase {
return manager_.CanBePurgedForTest(allocation);
}
+ void SetNow(TimeTicks now) { manager_.SetNow(now); }
+
+ bool IdleNotification() { return manager_.IdleNotification(); }
+
private:
MessageLoopForIO message_loop_;
- internal::DiscardableMemoryManager manager_;
+ TestDiscardableMemoryManagerImpl manager_;
};
class DiscardableMemoryManagerTest : public DiscardableMemoryManagerTestBase,
@@ -392,6 +420,51 @@ TEST_F(DiscardableMemoryManagerTest, DestructionAfterPurged) {
EXPECT_EQ(0u, BytesAllocated());
}
+TEST_F(DiscardableMemoryManagerTest, IdleNotification) {
+ SetMemoryLimit(3072);
+ SetSoftMemoryLimit(1024);
+ SetHardMemoryLimitExpirationTime(TimeDelta::FromInternalValue(1));
+
+ size_t size = 1024;
+ TestAllocationImpl allocation[3];
+ RegisterAndLock(&allocation[0], size);
+ RegisterAndLock(&allocation[1], size);
+ RegisterAndLock(&allocation[2], size);
+ EXPECT_EQ(3072u, BytesAllocated());
+
+ // Above soft limit but nothing that can be purged.
+ EXPECT_FALSE(IdleNotification());
+
+ SetNow(TimeTicks::FromInternalValue(0));
+ Unlock(&allocation[0]);
+
+ // Above soft limit but still nothing that can be purged as all unlocked
+ // allocations are within the hard limit cutoff time.
+ EXPECT_FALSE(IdleNotification());
+
+ SetNow(TimeTicks::FromInternalValue(1));
+ Unlock(&allocation[1]);
+
+ // One unlocked allocation is no longer within the hard limit cutoff time. It
+ // should be purged and IdleNotification() should return false as we're not
+ // yet within the soft memory limit.
+ EXPECT_FALSE(IdleNotification());
+ EXPECT_EQ(2048u, BytesAllocated());
+
+ // One more unlocked allocation is no longer within the hard limit cutoff
+ // time. It should be purged and IdleNotification() should return true as
+ // we're now within the soft memory limit.
+ SetNow(TimeTicks::FromInternalValue(2));
+ EXPECT_TRUE(IdleNotification());
+ EXPECT_EQ(1024u, BytesAllocated());
+
+ Unlock(&allocation[2]);
+
+ Unregister(&allocation[0]);
+ Unregister(&allocation[1]);
+ Unregister(&allocation[2]);
+}
+
class ThreadedDiscardableMemoryManagerTest
: public DiscardableMemoryManagerTest {
public:

Powered by Google App Engine
This is Rietveld 408576698