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

Side by Side Diff: athena/resource_manager/memory_pressure_notifier_unittest.cc

Issue 863033002: Delete athena/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 "athena/resource_manager/memory_pressure_notifier.h"
6 #include "athena/resource_manager/public/resource_manager_delegate.h"
7 #include "athena/test/base/athena_test_base.h"
8
9 namespace athena {
10 namespace test {
11
12 namespace {
13
14 // Our OS delegate abstraction class to override the memory fill level.
15 class TestResourceManagerDelegate : public ResourceManagerDelegate {
16 public:
17 TestResourceManagerDelegate() : memory_fill_level_percent_(0) {}
18 ~TestResourceManagerDelegate() override {}
19
20 virtual int GetUsedMemoryInPercent() override {
21 timer_called_++;
22 return memory_fill_level_percent_;
23 }
24
25 virtual int MemoryPressureIntervalInMS() override {
26 return 5;
27 }
28
29 void set_memory_fill_level_percent(int memory_fill_level_percent) {
30 memory_fill_level_percent_ = memory_fill_level_percent;
31 }
32
33 // Returns the number of timer calls to the GetMemoryInPercent() calls.
34 int timer_called() { return timer_called_; }
35
36 private:
37 // The to be returned memory fill level value in percent.
38 int memory_fill_level_percent_;
39
40 // How often was the timer calling the GetUsedMemoryInPercent() function.
41 int timer_called_;
42
43 DISALLOW_COPY_AND_ASSIGN(TestResourceManagerDelegate);
44 };
45
46 // Our memory pressure observer class.
47 class TestMemoryPressureObserver : public MemoryPressureObserver {
48 public:
49 TestMemoryPressureObserver(ResourceManagerDelegate* delegate)
50 : delegate_(delegate),
51 number_of_calls_(0),
52 pressure_(ResourceManager::MEMORY_PRESSURE_UNKNOWN) {}
53 ~TestMemoryPressureObserver() override {}
54
55 // The observer.
56 virtual void OnMemoryPressure(
57 ResourceManager::MemoryPressure pressure) override {
58 number_of_calls_++;
59 pressure_ = pressure;
60 }
61
62 virtual ResourceManagerDelegate* GetDelegate() override {
63 return delegate_.get();
64 }
65
66 int number_of_calls() { return number_of_calls_; }
67 ResourceManager::MemoryPressure pressure() { return pressure_; }
68
69 private:
70 scoped_ptr<ResourceManagerDelegate> delegate_;
71
72 // Number of calls received.
73 int number_of_calls_;
74
75 // Last posted memory pressure.
76 ResourceManager::MemoryPressure pressure_;
77
78 DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureObserver);
79 };
80
81 } // namespace
82
83 // Our testing base.
84 class MemoryPressureTest : public AthenaTestBase {
85 public:
86 MemoryPressureTest() : test_resource_manager_delegate_(nullptr) {}
87 ~MemoryPressureTest() override {}
88
89 // AthenaTestBase:
90 virtual void SetUp() override {
91 AthenaTestBase::SetUp();
92 // Create and install our TestAppContentDelegate with instrumentation.
93 test_resource_manager_delegate_ =
94 new TestResourceManagerDelegate();
95 test_memory_pressure_observer_.reset(new TestMemoryPressureObserver(
96 test_resource_manager_delegate_));
97 memory_pressure_notifier_.reset(
98 new MemoryPressureNotifier(test_memory_pressure_observer_.get()));
99 }
100
101 virtual void TearDown() override {
102 memory_pressure_notifier_.reset();
103 RunAllPendingInMessageLoop();
104 test_memory_pressure_observer_.reset();
105 AthenaTestBase::TearDown();
106 }
107
108 protected:
109 TestResourceManagerDelegate* test_resource_manager_delegate() {
110 return test_resource_manager_delegate_;
111 }
112
113 TestMemoryPressureObserver* test_memory_pressure_observer() {
114 return test_memory_pressure_observer_.get();
115 }
116
117 // Waits until a timer interrupt occurs. Returns false if no timer is
118 // registered.
119 bool WaitForTimer() {
120 int first_counter = test_resource_manager_delegate()->timer_called();
121 // Wait up to 500ms for any poll on our memory status function from the
122 // MemoryPressureNotifier.
123 for (int i = 0; i < 500; ++i) {
124 if (test_resource_manager_delegate()->timer_called() != first_counter)
125 return true;
126 usleep(1);
127 RunAllPendingInMessageLoop();
128 }
129 return false;
130 }
131
132 private:
133 // Not owned: the resource manager delegate.
134 TestResourceManagerDelegate* test_resource_manager_delegate_;
135 scoped_ptr<TestMemoryPressureObserver> test_memory_pressure_observer_;
136 scoped_ptr<MemoryPressureNotifier> memory_pressure_notifier_;
137 DISALLOW_COPY_AND_ASSIGN(MemoryPressureTest);
138 };
139
140 // Only creates and destroys it to see that the system gets properly shut down.
141 TEST_F(MemoryPressureTest, SimpleTest) {
142 }
143
144 // Test that we get only a single call while the memory pressure is low.
145 TEST_F(MemoryPressureTest, OneEventOnLowPressure) {
146 ASSERT_TRUE(WaitForTimer());
147 // No call should have happened at this time to the
148 EXPECT_FALSE(test_memory_pressure_observer()->number_of_calls());
149 // Set to something below 50% and check that we still get no call.
150 test_resource_manager_delegate()->set_memory_fill_level_percent(49);
151 ASSERT_TRUE(WaitForTimer());
152 EXPECT_EQ(1, test_memory_pressure_observer()->number_of_calls());
153 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_LOW,
154 test_memory_pressure_observer()->pressure());
155 ASSERT_TRUE(WaitForTimer());
156 EXPECT_EQ(1, test_memory_pressure_observer()->number_of_calls());
157 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_LOW,
158 test_memory_pressure_observer()->pressure());
159 }
160
161 // Test that we get a |MEMORY_PRESSURE_UNKNOWN| if it cannot be determined.
162 TEST_F(MemoryPressureTest, TestNoCallsOnMemoryPressureUnknown) {
163 test_resource_manager_delegate()->set_memory_fill_level_percent(0);
164 ASSERT_TRUE(WaitForTimer());
165 // We shouldn't have gotten a single call.
166 EXPECT_FALSE(test_memory_pressure_observer()->number_of_calls());
167 // And the memory pressure should be unknown.
168 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_UNKNOWN,
169 test_memory_pressure_observer()->pressure());
170 }
171
172 // Test that we get a change to MODERATE if the memory pressure is at 60%.
173 TEST_F(MemoryPressureTest, TestModeratePressure) {
174 test_resource_manager_delegate()->set_memory_fill_level_percent(60);
175 ASSERT_TRUE(WaitForTimer());
176 // At least one call should have happened.
177 int calls = test_memory_pressure_observer()->number_of_calls();
178 EXPECT_TRUE(calls);
179 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_MODERATE,
180 test_memory_pressure_observer()->pressure());
181 // Even if the value does not change, we should get more calls.
182 ASSERT_TRUE(WaitForTimer());
183 EXPECT_LT(calls, test_memory_pressure_observer()->number_of_calls());
184 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_MODERATE,
185 test_memory_pressure_observer()->pressure());
186 }
187
188 // Test that increasing and decreasing the memory pressure does the right thing.
189 TEST_F(MemoryPressureTest, TestPressureUpAndDown) {
190 test_resource_manager_delegate()->set_memory_fill_level_percent(60);
191 ASSERT_TRUE(WaitForTimer());
192 // At least one call should have happened.
193 int calls1 = test_memory_pressure_observer()->number_of_calls();
194 EXPECT_TRUE(calls1);
195 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_MODERATE,
196 test_memory_pressure_observer()->pressure());
197
198 // Check to the next level.
199 test_resource_manager_delegate()->set_memory_fill_level_percent(80);
200 ASSERT_TRUE(WaitForTimer());
201 int calls2 = test_memory_pressure_observer()->number_of_calls();
202 EXPECT_LT(calls1, calls2);
203 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_HIGH,
204 test_memory_pressure_observer()->pressure());
205
206 // Check to no pressure again.
207 test_resource_manager_delegate()->set_memory_fill_level_percent(20);
208 ASSERT_TRUE(WaitForTimer());
209 int calls3 = test_memory_pressure_observer()->number_of_calls();
210 EXPECT_LT(calls2, calls3);
211 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_LOW,
212 test_memory_pressure_observer()->pressure());
213
214 // Even if the value does not change, we should not get any more calls.
215 ASSERT_TRUE(WaitForTimer());
216 EXPECT_EQ(calls3, test_memory_pressure_observer()->number_of_calls());
217 EXPECT_EQ(ResourceManager::MEMORY_PRESSURE_LOW,
218 test_memory_pressure_observer()->pressure());
219 }
220
221 } // namespace test
222 } // namespace athena
OLDNEW
« no previous file with comments | « athena/resource_manager/memory_pressure_notifier.cc ('k') | athena/resource_manager/public/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698