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

Side by Side Diff: base/chromeos/memory_pressure_observer_chromeos_unittest.cc

Issue 815183002: Using the new MemoryPressureListener instead of the LowMemoryObserver when the enhanced memory mana… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed flaky unit test Created 5 years, 11 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 "base/basictypes.h"
6 #include "base/chromeos/memory_pressure_observer_chromeos.h"
7 #include "base/memory/memory_pressure_listener.h"
8 #include "base/message_loop/message_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12
13 namespace {
14
15 // True if the memory notifier got called.
16 // Do not read/modify value directly.
17 bool on_memory_pressure_called = false;
18
19 // If the memory notifier got called, this is the memory pressure reported.
20 MemoryPressureListener::MemoryPressureLevel on_memory_pressure_level =
21 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
22
23 // Processes OnMemoryPressure calls.
24 void OnMemoryPressure(MemoryPressureListener::MemoryPressureLevel level) {
25 on_memory_pressure_called = true;
26 on_memory_pressure_level = level;
27 }
28
29 // Resets the indicator for memory pressure.
30 void ResetOnMemoryPressureCalled() {
31 on_memory_pressure_called = false;
32 }
33
34 // Returns true when OnMemoryPressure was called (and resets it).
35 bool WasOnMemoryPressureCalled() {
36 bool b = on_memory_pressure_called;
37 ResetOnMemoryPressureCalled();
38 return b;
39 }
40
41 } // namespace
42
43 class TestMemoryPressureObserver : public MemoryPressureObserverChromeOS {
44 public:
45 TestMemoryPressureObserver() : memory_in_percent_override_(0) {
46 // Disable any timers which are going on and set a special memory reporting
47 // function.
48 StopObserving();
49 }
50 ~TestMemoryPressureObserver() override {}
51
52 void SetMemoryInPercentOverride(int percent) {
53 memory_in_percent_override_ = percent;
54 }
55
56 void CheckMemoryPressureForTest() {
57 CheckMemoryPressure();
58 }
59
60 private:
61 int GetUsedMemoryInPercent() override {
62 return memory_in_percent_override_;
63 }
64
65 int memory_in_percent_override_;
66 DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureObserver);
67 };
68
69 // This test tests the various transition states from memory pressure, looking
70 // for the correct behavior on event reposting as well as state updates.
71 TEST(MemoryPressureObserverChromeOSTest, CheckMemoryPressure) {
72 base::MessageLoopForUI message_loop;
73 scoped_ptr<TestMemoryPressureObserver> observer(
74 new TestMemoryPressureObserver);
75 scoped_ptr<MemoryPressureListener> listener(
76 new MemoryPressureListener(base::Bind(&OnMemoryPressure)));
77 // Checking the memory pressure while 0% are used should not produce any
78 // events.
79 observer->SetMemoryInPercentOverride(0);
80 ResetOnMemoryPressureCalled();
81
82 observer->CheckMemoryPressureForTest();
83 message_loop.RunUntilIdle();
84 EXPECT_FALSE(WasOnMemoryPressureCalled());
85 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
86 observer->GetCurrentPressureLevel());
87
88 // Setting the memory level to 80% should produce a moderate pressure level.
89 observer->SetMemoryInPercentOverride(80);
90 observer->CheckMemoryPressureForTest();
91 message_loop.RunUntilIdle();
92 EXPECT_TRUE(WasOnMemoryPressureCalled());
93 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
94 observer->GetCurrentPressureLevel());
95 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
96 on_memory_pressure_level);
97
98 // We need to check that the event gets reposted after a while.
99 int i = 0;
100 for (; i < 100; i++) {
101 observer->CheckMemoryPressureForTest();
102 message_loop.RunUntilIdle();
103 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
104 observer->GetCurrentPressureLevel());
105 if (WasOnMemoryPressureCalled()) {
106 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
107 on_memory_pressure_level);
108 break;
109 }
110 }
111 // Should be more then 5 and less then 100.
112 EXPECT_LE(5, i);
113 EXPECT_GE(99, i);
114
115 // Setting the memory usage to 99% should produce critical levels.
116 observer->SetMemoryInPercentOverride(99);
117 observer->CheckMemoryPressureForTest();
118 message_loop.RunUntilIdle();
119 EXPECT_TRUE(WasOnMemoryPressureCalled());
120 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
121 on_memory_pressure_level);
122 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
123 observer->GetCurrentPressureLevel());
124
125 // Calling it again should immediately produce a second call.
126 observer->CheckMemoryPressureForTest();
127 message_loop.RunUntilIdle();
128 EXPECT_TRUE(WasOnMemoryPressureCalled());
129 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
130 on_memory_pressure_level);
131 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
132 observer->GetCurrentPressureLevel());
133
134 // When lowering the pressure again we should not get an event, but the
135 // pressure should go back to moderate.
136 observer->SetMemoryInPercentOverride(80);
137 observer->CheckMemoryPressureForTest();
138 message_loop.RunUntilIdle();
139 EXPECT_FALSE(WasOnMemoryPressureCalled());
140 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
141 observer->GetCurrentPressureLevel());
142
143 // We should need exactly the same amount of calls as before, before the next
144 // call comes in.
145 int j = 0;
146 for (; j < 100; j++) {
147 observer->CheckMemoryPressureForTest();
148 message_loop.RunUntilIdle();
149 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
150 observer->GetCurrentPressureLevel());
151 if (WasOnMemoryPressureCalled()) {
152 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
153 on_memory_pressure_level);
154 break;
155 }
156 }
157 // We should have needed exactly the same amount of checks as before.
158 EXPECT_EQ(j, i);
159 }
160
161 } // namespace base
OLDNEW
« no previous file with comments | « base/chromeos/memory_pressure_observer_chromeos.cc ('k') | chrome/browser/chromeos/memory/oom_priority_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698