OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/discardable_memory_provider.h" | 5 #include "base/memory/discardable_memory_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/discardable_memory.h" | 8 #include "base/memory/discardable_memory.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using base::internal::DiscardableMemoryProvider; | |
15 | |
16 namespace base { | 14 namespace base { |
17 | 15 |
18 class DiscardableMemoryProviderTestBase { | 16 class DiscardableMemoryProviderTestBase { |
19 public: | 17 public: |
| 18 class TestDiscardableMemory : public DiscardableMemory { |
| 19 public: |
| 20 TestDiscardableMemory( |
| 21 internal::DiscardableMemoryProvider* provider, size_t size) |
| 22 : provider_(provider), |
| 23 is_locked_(false) { |
| 24 provider_->Register(this, size); |
| 25 } |
| 26 |
| 27 virtual ~TestDiscardableMemory() { |
| 28 if (is_locked_) |
| 29 Unlock(); |
| 30 provider_->Unregister(this); |
| 31 } |
| 32 |
| 33 // Overridden from DiscardableMemory: |
| 34 virtual LockDiscardableMemoryStatus Lock() OVERRIDE { |
| 35 DCHECK(!is_locked_); |
| 36 |
| 37 bool purged = false; |
| 38 memory_ = provider_->Acquire(this, &purged); |
| 39 if (!memory_) |
| 40 return DISCARDABLE_MEMORY_FAILED; |
| 41 |
| 42 is_locked_ = true; |
| 43 return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; |
| 44 } |
| 45 virtual void Unlock() OVERRIDE { |
| 46 DCHECK(is_locked_); |
| 47 provider_->Release(this, memory_.Pass()); |
| 48 is_locked_ = false; |
| 49 } |
| 50 virtual void* Memory() const OVERRIDE { |
| 51 DCHECK(memory_); |
| 52 return memory_.get(); |
| 53 } |
| 54 |
| 55 private: |
| 56 internal::DiscardableMemoryProvider* provider_; |
| 57 scoped_ptr<uint8, FreeDeleter> memory_; |
| 58 bool is_locked_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(TestDiscardableMemory); |
| 61 }; |
| 62 |
20 DiscardableMemoryProviderTestBase() | 63 DiscardableMemoryProviderTestBase() |
21 : message_loop_(MessageLoop::TYPE_IO), | 64 : message_loop_(MessageLoop::TYPE_IO), |
22 provider_(new DiscardableMemoryProvider) { | 65 provider_(new internal::DiscardableMemoryProvider) { |
23 // We set a provider here for two reasons: | |
24 // 1. It ensures that one test cannot affect the next, and | |
25 // 2. Since the provider listens for pressure notifications on the thread | |
26 // it was created on, if we create it on the test thread, we can run | |
27 // the test thread's message loop until idle when we want to process | |
28 // one of these notifications. | |
29 DiscardableMemoryProvider::SetInstanceForTest(provider_.get()); | |
30 } | |
31 | |
32 virtual ~DiscardableMemoryProviderTestBase() { | |
33 DiscardableMemoryProvider::SetInstanceForTest(NULL); | |
34 } | 66 } |
35 | 67 |
36 protected: | 68 protected: |
37 bool IsRegistered(const DiscardableMemory* discardable) { | 69 bool IsRegistered(const DiscardableMemory* discardable) { |
38 return DiscardableMemoryProvider::GetInstance()->IsRegisteredForTest( | 70 return provider_->IsRegisteredForTest(discardable); |
39 discardable); | |
40 } | 71 } |
41 | 72 |
42 bool CanBePurged(const DiscardableMemory* discardable) { | 73 bool CanBePurged(const DiscardableMemory* discardable) { |
43 return DiscardableMemoryProvider::GetInstance()->CanBePurgedForTest( | 74 return provider_->CanBePurgedForTest(discardable); |
44 discardable); | |
45 } | 75 } |
46 | 76 |
47 size_t BytesAllocated() const { | 77 size_t BytesAllocated() const { |
48 return DiscardableMemoryProvider::GetInstance()-> | 78 return provider_->GetBytesAllocatedForTest(); |
49 GetBytesAllocatedForTest(); | |
50 } | 79 } |
51 | 80 |
52 void* Memory(const DiscardableMemory* discardable) const { | 81 void* Memory(const DiscardableMemory* discardable) const { |
53 return discardable->Memory(); | 82 return discardable->Memory(); |
54 } | 83 } |
55 | 84 |
56 void SetDiscardableMemoryLimit(size_t bytes) { | 85 void SetDiscardableMemoryLimit(size_t bytes) { |
57 DiscardableMemoryProvider::GetInstance()-> | 86 provider_->SetDiscardableMemoryLimit(bytes); |
58 SetDiscardableMemoryLimit(bytes); | |
59 } | 87 } |
60 | 88 |
61 void SetBytesToReclaimUnderModeratePressure(size_t bytes) { | 89 void SetBytesToReclaimUnderModeratePressure(size_t bytes) { |
62 DiscardableMemoryProvider::GetInstance()-> | 90 provider_->SetBytesToReclaimUnderModeratePressure(bytes); |
63 SetBytesToReclaimUnderModeratePressure(bytes); | 91 } |
| 92 |
| 93 scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) { |
| 94 scoped_ptr<TestDiscardableMemory> memory( |
| 95 new TestDiscardableMemory(provider_.get(), size)); |
| 96 if (memory->Lock() != DISCARDABLE_MEMORY_PURGED) |
| 97 return scoped_ptr<DiscardableMemory>(); |
| 98 return memory.PassAs<DiscardableMemory>(); |
64 } | 99 } |
65 | 100 |
66 private: | 101 private: |
67 MessageLoop message_loop_; | 102 MessageLoop message_loop_; |
68 scoped_ptr<DiscardableMemoryProvider> provider_; | 103 scoped_ptr<internal::DiscardableMemoryProvider> provider_; |
69 }; | 104 }; |
70 | 105 |
71 class DiscardableMemoryProviderTest | 106 class DiscardableMemoryProviderTest |
72 : public DiscardableMemoryProviderTestBase, | 107 : public DiscardableMemoryProviderTestBase, |
73 public testing::Test { | 108 public testing::Test { |
74 public: | 109 public: |
75 DiscardableMemoryProviderTest() {} | 110 DiscardableMemoryProviderTest() {} |
76 }; | 111 }; |
77 | 112 |
78 TEST_F(DiscardableMemoryProviderTest, CreateLockedMemory) { | 113 TEST_F(DiscardableMemoryProviderTest, CreateLockedMemory) { |
79 size_t size = 1024; | 114 size_t size = 1024; |
80 const scoped_ptr<DiscardableMemory> discardable( | 115 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
81 DiscardableMemory::CreateLockedMemory(size)); | |
82 EXPECT_TRUE(IsRegistered(discardable.get())); | 116 EXPECT_TRUE(IsRegistered(discardable.get())); |
83 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 117 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
84 EXPECT_EQ(1024u, BytesAllocated()); | 118 EXPECT_EQ(1024u, BytesAllocated()); |
85 EXPECT_FALSE(CanBePurged(discardable.get())); | 119 EXPECT_FALSE(CanBePurged(discardable.get())); |
86 } | 120 } |
87 | 121 |
88 TEST_F(DiscardableMemoryProviderTest, CreateLockedMemoryZeroSize) { | 122 TEST_F(DiscardableMemoryProviderTest, CreateLockedMemoryZeroSize) { |
89 size_t size = 0; | 123 size_t size = 0; |
90 const scoped_ptr<DiscardableMemory> discardable( | 124 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
91 DiscardableMemory::CreateLockedMemory(size)); | |
92 EXPECT_FALSE(discardable); | 125 EXPECT_FALSE(discardable); |
93 EXPECT_FALSE(IsRegistered(discardable.get())); | 126 EXPECT_FALSE(IsRegistered(discardable.get())); |
94 EXPECT_EQ(0u, BytesAllocated()); | 127 EXPECT_EQ(0u, BytesAllocated()); |
95 } | 128 } |
96 | 129 |
97 TEST_F(DiscardableMemoryProviderTest, LockAfterUnlock) { | 130 TEST_F(DiscardableMemoryProviderTest, LockAfterUnlock) { |
98 size_t size = 1024; | 131 size_t size = 1024; |
99 const scoped_ptr<DiscardableMemory> discardable( | 132 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
100 DiscardableMemory::CreateLockedMemory(size)); | |
101 EXPECT_TRUE(IsRegistered(discardable.get())); | 133 EXPECT_TRUE(IsRegistered(discardable.get())); |
102 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 134 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
103 EXPECT_EQ(1024u, BytesAllocated()); | 135 EXPECT_EQ(1024u, BytesAllocated()); |
104 EXPECT_FALSE(CanBePurged(discardable.get())); | 136 EXPECT_FALSE(CanBePurged(discardable.get())); |
105 | 137 |
106 // Now unlock so we can lock later. | 138 // Now unlock so we can lock later. |
107 discardable->Unlock(); | 139 discardable->Unlock(); |
108 EXPECT_TRUE(CanBePurged(discardable.get())); | 140 EXPECT_TRUE(CanBePurged(discardable.get())); |
109 | 141 |
110 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, discardable->Lock()); | 142 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, discardable->Lock()); |
111 EXPECT_FALSE(CanBePurged(discardable.get())); | 143 EXPECT_FALSE(CanBePurged(discardable.get())); |
112 } | 144 } |
113 | 145 |
114 TEST_F(DiscardableMemoryProviderTest, LockAfterPurge) { | 146 TEST_F(DiscardableMemoryProviderTest, LockAfterPurge) { |
115 size_t size = 1024; | 147 size_t size = 1024; |
116 const scoped_ptr<DiscardableMemory> discardable( | 148 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
117 DiscardableMemory::CreateLockedMemory(size)); | |
118 EXPECT_TRUE(IsRegistered(discardable.get())); | 149 EXPECT_TRUE(IsRegistered(discardable.get())); |
119 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 150 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
120 EXPECT_EQ(1024u, BytesAllocated()); | 151 EXPECT_EQ(1024u, BytesAllocated()); |
121 EXPECT_FALSE(CanBePurged(discardable.get())); | 152 EXPECT_FALSE(CanBePurged(discardable.get())); |
122 | 153 |
123 // Now unlock so we can lock later. | 154 // Now unlock so we can lock later. |
124 discardable->Unlock(); | 155 discardable->Unlock(); |
125 EXPECT_TRUE(CanBePurged(discardable.get())); | 156 EXPECT_TRUE(CanBePurged(discardable.get())); |
126 | 157 |
127 // Force the system to purge. | 158 // Force the system to purge. |
128 MemoryPressureListener::NotifyMemoryPressure( | 159 MemoryPressureListener::NotifyMemoryPressure( |
129 MemoryPressureListener::MEMORY_PRESSURE_CRITICAL); | 160 MemoryPressureListener::MEMORY_PRESSURE_CRITICAL); |
130 | 161 |
131 // Required because ObserverListThreadSafe notifies via PostTask. | 162 // Required because ObserverListThreadSafe notifies via PostTask. |
132 RunLoop().RunUntilIdle(); | 163 RunLoop().RunUntilIdle(); |
133 | 164 |
134 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, discardable->Lock()); | 165 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, discardable->Lock()); |
135 EXPECT_FALSE(CanBePurged(discardable.get())); | 166 EXPECT_FALSE(CanBePurged(discardable.get())); |
136 } | 167 } |
137 | 168 |
138 TEST_F(DiscardableMemoryProviderTest, LockAfterPurgeAndCannotReallocate) { | 169 TEST_F(DiscardableMemoryProviderTest, LockAfterPurgeAndCannotReallocate) { |
139 size_t size = 1024; | 170 size_t size = 1024; |
140 const scoped_ptr<DiscardableMemory> discardable( | 171 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
141 DiscardableMemory::CreateLockedMemory(size)); | |
142 EXPECT_TRUE(IsRegistered(discardable.get())); | 172 EXPECT_TRUE(IsRegistered(discardable.get())); |
143 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 173 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
144 EXPECT_EQ(1024u, BytesAllocated()); | 174 EXPECT_EQ(1024u, BytesAllocated()); |
145 EXPECT_FALSE(CanBePurged(discardable.get())); | 175 EXPECT_FALSE(CanBePurged(discardable.get())); |
146 | 176 |
147 // Now unlock so we can lock later. | 177 // Now unlock so we can lock later. |
148 discardable->Unlock(); | 178 discardable->Unlock(); |
149 EXPECT_TRUE(CanBePurged(discardable.get())); | 179 EXPECT_TRUE(CanBePurged(discardable.get())); |
150 | 180 |
151 // Set max allowed allocation to 1 byte. This will make cause the memory | 181 // Set max allowed allocation to 1 byte. This will make cause the memory |
152 // to be purged. | 182 // to be purged. |
153 SetDiscardableMemoryLimit(1); | 183 SetDiscardableMemoryLimit(1); |
154 | 184 |
155 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, discardable->Lock()); | 185 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, discardable->Lock()); |
156 EXPECT_FALSE(CanBePurged(discardable.get())); | 186 EXPECT_FALSE(CanBePurged(discardable.get())); |
157 } | 187 } |
158 | 188 |
159 TEST_F(DiscardableMemoryProviderTest, Overflow) { | 189 TEST_F(DiscardableMemoryProviderTest, Overflow) { |
160 { | 190 { |
161 size_t size = 1024; | 191 size_t size = 1024; |
162 const scoped_ptr<DiscardableMemory> discardable( | 192 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
163 DiscardableMemory::CreateLockedMemory(size)); | |
164 EXPECT_TRUE(IsRegistered(discardable.get())); | 193 EXPECT_TRUE(IsRegistered(discardable.get())); |
165 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 194 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
166 EXPECT_EQ(1024u, BytesAllocated()); | 195 EXPECT_EQ(1024u, BytesAllocated()); |
167 | 196 |
168 size_t massive_size = std::numeric_limits<size_t>::max(); | 197 size_t massive_size = std::numeric_limits<size_t>::max(); |
169 const scoped_ptr<DiscardableMemory> massive_discardable( | 198 const scoped_ptr<DiscardableMemory> massive_discardable( |
170 DiscardableMemory::CreateLockedMemory(massive_size)); | 199 CreateLockedMemory(massive_size)); |
171 EXPECT_FALSE(massive_discardable); | 200 EXPECT_FALSE(massive_discardable); |
172 EXPECT_EQ(1024u, BytesAllocated()); | 201 EXPECT_EQ(1024u, BytesAllocated()); |
173 } | 202 } |
174 EXPECT_EQ(0u, BytesAllocated()); | 203 EXPECT_EQ(0u, BytesAllocated()); |
175 } | 204 } |
176 | 205 |
177 class PermutationTestData { | 206 class PermutationTestData { |
178 public: | 207 public: |
179 PermutationTestData(unsigned d0, unsigned d1, unsigned d2) { | 208 PermutationTestData(unsigned d0, unsigned d1, unsigned d2) { |
180 ordering_[0] = d0; | 209 ordering_[0] = d0; |
(...skipping 10 matching lines...) Expand all Loading... |
191 class DiscardableMemoryProviderPermutationTest | 220 class DiscardableMemoryProviderPermutationTest |
192 : public DiscardableMemoryProviderTestBase, | 221 : public DiscardableMemoryProviderTestBase, |
193 public testing::TestWithParam<PermutationTestData> { | 222 public testing::TestWithParam<PermutationTestData> { |
194 public: | 223 public: |
195 DiscardableMemoryProviderPermutationTest() {} | 224 DiscardableMemoryProviderPermutationTest() {} |
196 | 225 |
197 protected: | 226 protected: |
198 // Use discardable memory in order specified by ordering parameter. | 227 // Use discardable memory in order specified by ordering parameter. |
199 void CreateAndUseDiscardableMemory() { | 228 void CreateAndUseDiscardableMemory() { |
200 for (int i = 0; i < 3; ++i) { | 229 for (int i = 0; i < 3; ++i) { |
201 discardables_[i] = DiscardableMemory::CreateLockedMemory(1024); | 230 discardables_[i] = CreateLockedMemory(1024); |
202 EXPECT_TRUE(discardables_[i]); | 231 EXPECT_TRUE(discardables_[i]); |
203 EXPECT_NE(static_cast<void*>(NULL), Memory(discardables_[i].get())); | 232 EXPECT_NE(static_cast<void*>(NULL), Memory(discardables_[i].get())); |
204 discardables_[i]->Unlock(); | 233 discardables_[i]->Unlock(); |
205 } | 234 } |
206 for (int i = 0; i < 3; ++i) { | 235 for (int i = 0; i < 3; ++i) { |
207 int index = GetParam().ordering()[i]; | 236 int index = GetParam().ordering()[i]; |
208 EXPECT_NE(DISCARDABLE_MEMORY_FAILED, discardables_[index]->Lock()); | 237 EXPECT_NE(DISCARDABLE_MEMORY_FAILED, discardables_[index]->Lock()); |
209 // Leave i == 0 locked. | 238 // Leave i == 0 locked. |
210 if (i > 0) | 239 if (i > 0) |
211 discardables_[index]->Unlock(); | 240 discardables_[index]->Unlock(); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 ::testing::Values(PermutationTestData(0, 1, 2), | 316 ::testing::Values(PermutationTestData(0, 1, 2), |
288 PermutationTestData(0, 2, 1), | 317 PermutationTestData(0, 2, 1), |
289 PermutationTestData(1, 0, 2), | 318 PermutationTestData(1, 0, 2), |
290 PermutationTestData(1, 2, 0), | 319 PermutationTestData(1, 2, 0), |
291 PermutationTestData(2, 0, 1), | 320 PermutationTestData(2, 0, 1), |
292 PermutationTestData(2, 1, 0))); | 321 PermutationTestData(2, 1, 0))); |
293 | 322 |
294 TEST_F(DiscardableMemoryProviderTest, NormalDestruction) { | 323 TEST_F(DiscardableMemoryProviderTest, NormalDestruction) { |
295 { | 324 { |
296 size_t size = 1024; | 325 size_t size = 1024; |
297 const scoped_ptr<DiscardableMemory> discardable( | 326 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
298 DiscardableMemory::CreateLockedMemory(size)); | |
299 EXPECT_TRUE(IsRegistered(discardable.get())); | 327 EXPECT_TRUE(IsRegistered(discardable.get())); |
300 EXPECT_EQ(1024u, BytesAllocated()); | 328 EXPECT_EQ(1024u, BytesAllocated()); |
301 } | 329 } |
302 EXPECT_EQ(0u, BytesAllocated()); | 330 EXPECT_EQ(0u, BytesAllocated()); |
303 } | 331 } |
304 | 332 |
305 TEST_F(DiscardableMemoryProviderTest, DestructionWhileLocked) { | 333 TEST_F(DiscardableMemoryProviderTest, DestructionWhileLocked) { |
306 { | 334 { |
307 size_t size = 1024; | 335 size_t size = 1024; |
308 const scoped_ptr<DiscardableMemory> discardable( | 336 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
309 DiscardableMemory::CreateLockedMemory(size)); | |
310 EXPECT_TRUE(IsRegistered(discardable.get())); | 337 EXPECT_TRUE(IsRegistered(discardable.get())); |
311 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 338 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
312 EXPECT_EQ(1024u, BytesAllocated()); | 339 EXPECT_EQ(1024u, BytesAllocated()); |
313 EXPECT_FALSE(CanBePurged(discardable.get())); | 340 EXPECT_FALSE(CanBePurged(discardable.get())); |
314 } | 341 } |
315 // Should have ignored the "locked" status and freed the discardable memory. | 342 // Should have ignored the "locked" status and freed the discardable memory. |
316 EXPECT_EQ(0u, BytesAllocated()); | 343 EXPECT_EQ(0u, BytesAllocated()); |
317 } | 344 } |
318 | 345 |
319 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) | 346 #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS) |
320 // Death tests are not supported with Android APKs. | 347 // Death tests are not supported with Android APKs. |
321 TEST_F(DiscardableMemoryProviderTest, UnlockedMemoryAccessCrashesInDebugMode) { | 348 TEST_F(DiscardableMemoryProviderTest, UnlockedMemoryAccessCrashesInDebugMode) { |
322 size_t size = 1024; | 349 size_t size = 1024; |
323 const scoped_ptr<DiscardableMemory> discardable( | 350 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
324 DiscardableMemory::CreateLockedMemory(size)); | |
325 EXPECT_TRUE(IsRegistered(discardable.get())); | 351 EXPECT_TRUE(IsRegistered(discardable.get())); |
326 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 352 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
327 EXPECT_EQ(1024u, BytesAllocated()); | 353 EXPECT_EQ(1024u, BytesAllocated()); |
328 EXPECT_FALSE(CanBePurged(discardable.get())); | 354 EXPECT_FALSE(CanBePurged(discardable.get())); |
329 discardable->Unlock(); | 355 discardable->Unlock(); |
330 EXPECT_TRUE(CanBePurged(discardable.get())); | 356 EXPECT_TRUE(CanBePurged(discardable.get())); |
331 // We *must* die if we are asked to vend a pointer to unlocked memory. | 357 // We *must* die if we are asked to vend a pointer to unlocked memory. |
332 EXPECT_DEATH(discardable->Memory(), ".*Check failed.*"); | 358 EXPECT_DEATH(discardable->Memory(), ".*Check failed.*"); |
333 } | 359 } |
334 #endif | 360 #endif |
335 | 361 |
336 class ThreadedDiscardableMemoryProviderTest | 362 class ThreadedDiscardableMemoryProviderTest |
337 : public DiscardableMemoryProviderTest { | 363 : public DiscardableMemoryProviderTest { |
338 public: | 364 public: |
339 ThreadedDiscardableMemoryProviderTest() | 365 ThreadedDiscardableMemoryProviderTest() |
340 : memory_usage_thread_("memory_usage_thread"), | 366 : memory_usage_thread_("memory_usage_thread"), |
341 thread_sync_(true, false) { | 367 thread_sync_(true, false) { |
342 } | 368 } |
343 | 369 |
344 virtual void SetUp() OVERRIDE { | 370 virtual void SetUp() OVERRIDE { |
345 memory_usage_thread_.Start(); | 371 memory_usage_thread_.Start(); |
346 } | 372 } |
347 | 373 |
348 virtual void TearDown() OVERRIDE { | 374 virtual void TearDown() OVERRIDE { |
349 memory_usage_thread_.Stop(); | 375 memory_usage_thread_.Stop(); |
350 } | 376 } |
351 | 377 |
352 void UseMemoryHelper() { | 378 void UseMemoryHelper() { |
353 size_t size = 1024; | 379 size_t size = 1024; |
354 const scoped_ptr<DiscardableMemory> discardable( | 380 const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size)); |
355 DiscardableMemory::CreateLockedMemory(size)); | |
356 EXPECT_TRUE(IsRegistered(discardable.get())); | 381 EXPECT_TRUE(IsRegistered(discardable.get())); |
357 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); | 382 EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get())); |
358 discardable->Unlock(); | 383 discardable->Unlock(); |
359 } | 384 } |
360 | 385 |
361 void SignalHelper() { | 386 void SignalHelper() { |
362 thread_sync_.Signal(); | 387 thread_sync_.Signal(); |
363 } | 388 } |
364 | 389 |
365 Thread memory_usage_thread_; | 390 Thread memory_usage_thread_; |
366 WaitableEvent thread_sync_; | 391 WaitableEvent thread_sync_; |
367 }; | 392 }; |
368 | 393 |
369 TEST_F(ThreadedDiscardableMemoryProviderTest, UseMemoryOnThread) { | 394 TEST_F(ThreadedDiscardableMemoryProviderTest, UseMemoryOnThread) { |
370 memory_usage_thread_.message_loop()->PostTask( | 395 memory_usage_thread_.message_loop()->PostTask( |
371 FROM_HERE, | 396 FROM_HERE, |
372 Bind(&ThreadedDiscardableMemoryProviderTest::UseMemoryHelper, | 397 Bind(&ThreadedDiscardableMemoryProviderTest::UseMemoryHelper, |
373 Unretained(this))); | 398 Unretained(this))); |
374 memory_usage_thread_.message_loop()->PostTask( | 399 memory_usage_thread_.message_loop()->PostTask( |
375 FROM_HERE, | 400 FROM_HERE, |
376 Bind(&ThreadedDiscardableMemoryProviderTest::SignalHelper, | 401 Bind(&ThreadedDiscardableMemoryProviderTest::SignalHelper, |
377 Unretained(this))); | 402 Unretained(this))); |
378 thread_sync_.Wait(); | 403 thread_sync_.Wait(); |
379 } | 404 } |
380 | 405 |
381 } // namespace base | 406 } // namespace base |
OLD | NEW |