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

Side by Side Diff: base/memory/discardable_shared_memory_unittest.cc

Issue 1549003002: Switch to standard integer types in base/memory/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 12 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
« no previous file with comments | « base/memory/discardable_shared_memory.cc ('k') | base/memory/memory_pressure_listener.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/basictypes.h" 5 #include <stdint.h>
6
6 #include "base/memory/discardable_shared_memory.h" 7 #include "base/memory/discardable_shared_memory.h"
7 #include "base/process/process_metrics.h" 8 #include "base/process/process_metrics.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
10 namespace base { 11 namespace base {
11 namespace { 12 namespace {
12 13
13 class TestDiscardableSharedMemory : public DiscardableSharedMemory { 14 class TestDiscardableSharedMemory : public DiscardableSharedMemory {
14 public: 15 public:
15 TestDiscardableSharedMemory() {} 16 TestDiscardableSharedMemory() {}
16 17
17 explicit TestDiscardableSharedMemory(SharedMemoryHandle handle) 18 explicit TestDiscardableSharedMemory(SharedMemoryHandle handle)
18 : DiscardableSharedMemory(handle) {} 19 : DiscardableSharedMemory(handle) {}
19 20
20 void SetNow(Time now) { now_ = now; } 21 void SetNow(Time now) { now_ = now; }
21 22
22 private: 23 private:
23 // Overriden from DiscardableSharedMemory: 24 // Overriden from DiscardableSharedMemory:
24 Time Now() const override { return now_; } 25 Time Now() const override { return now_; }
25 26
26 Time now_; 27 Time now_;
27 }; 28 };
28 29
29 TEST(DiscardableSharedMemoryTest, CreateAndMap) { 30 TEST(DiscardableSharedMemoryTest, CreateAndMap) {
30 const uint32 kDataSize = 1024; 31 const uint32_t kDataSize = 1024;
31 32
32 TestDiscardableSharedMemory memory; 33 TestDiscardableSharedMemory memory;
33 bool rv = memory.CreateAndMap(kDataSize); 34 bool rv = memory.CreateAndMap(kDataSize);
34 ASSERT_TRUE(rv); 35 ASSERT_TRUE(rv);
35 EXPECT_GE(memory.mapped_size(), kDataSize); 36 EXPECT_GE(memory.mapped_size(), kDataSize);
36 EXPECT_TRUE(memory.IsMemoryLocked()); 37 EXPECT_TRUE(memory.IsMemoryLocked());
37 } 38 }
38 39
39 TEST(DiscardableSharedMemoryTest, CreateFromHandle) { 40 TEST(DiscardableSharedMemoryTest, CreateFromHandle) {
40 const uint32 kDataSize = 1024; 41 const uint32_t kDataSize = 1024;
41 42
42 TestDiscardableSharedMemory memory1; 43 TestDiscardableSharedMemory memory1;
43 bool rv = memory1.CreateAndMap(kDataSize); 44 bool rv = memory1.CreateAndMap(kDataSize);
44 ASSERT_TRUE(rv); 45 ASSERT_TRUE(rv);
45 46
46 SharedMemoryHandle shared_handle; 47 SharedMemoryHandle shared_handle;
47 ASSERT_TRUE( 48 ASSERT_TRUE(
48 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); 49 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
49 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle)); 50 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle));
50 51
51 TestDiscardableSharedMemory memory2(shared_handle); 52 TestDiscardableSharedMemory memory2(shared_handle);
52 rv = memory2.Map(kDataSize); 53 rv = memory2.Map(kDataSize);
53 ASSERT_TRUE(rv); 54 ASSERT_TRUE(rv);
54 EXPECT_TRUE(memory2.IsMemoryLocked()); 55 EXPECT_TRUE(memory2.IsMemoryLocked());
55 } 56 }
56 57
57 TEST(DiscardableSharedMemoryTest, LockAndUnlock) { 58 TEST(DiscardableSharedMemoryTest, LockAndUnlock) {
58 const uint32 kDataSize = 1024; 59 const uint32_t kDataSize = 1024;
59 60
60 TestDiscardableSharedMemory memory1; 61 TestDiscardableSharedMemory memory1;
61 bool rv = memory1.CreateAndMap(kDataSize); 62 bool rv = memory1.CreateAndMap(kDataSize);
62 ASSERT_TRUE(rv); 63 ASSERT_TRUE(rv);
63 64
64 // Memory is initially locked. Unlock it. 65 // Memory is initially locked. Unlock it.
65 memory1.SetNow(Time::FromDoubleT(1)); 66 memory1.SetNow(Time::FromDoubleT(1));
66 memory1.Unlock(0, 0); 67 memory1.Unlock(0, 0);
67 EXPECT_FALSE(memory1.IsMemoryLocked()); 68 EXPECT_FALSE(memory1.IsMemoryLocked());
68 69
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 rv = memory1.IsMemoryResident(); 103 rv = memory1.IsMemoryResident();
103 EXPECT_TRUE(rv); 104 EXPECT_TRUE(rv);
104 EXPECT_TRUE(memory1.IsMemoryLocked()); 105 EXPECT_TRUE(memory1.IsMemoryLocked());
105 106
106 // Unlock first instance. 107 // Unlock first instance.
107 memory1.SetNow(Time::FromDoubleT(4)); 108 memory1.SetNow(Time::FromDoubleT(4));
108 memory1.Unlock(0, 0); 109 memory1.Unlock(0, 0);
109 } 110 }
110 111
111 TEST(DiscardableSharedMemoryTest, Purge) { 112 TEST(DiscardableSharedMemoryTest, Purge) {
112 const uint32 kDataSize = 1024; 113 const uint32_t kDataSize = 1024;
113 114
114 TestDiscardableSharedMemory memory1; 115 TestDiscardableSharedMemory memory1;
115 bool rv = memory1.CreateAndMap(kDataSize); 116 bool rv = memory1.CreateAndMap(kDataSize);
116 ASSERT_TRUE(rv); 117 ASSERT_TRUE(rv);
117 118
118 SharedMemoryHandle shared_handle; 119 SharedMemoryHandle shared_handle;
119 ASSERT_TRUE( 120 ASSERT_TRUE(
120 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); 121 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
121 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle)); 122 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle));
122 123
(...skipping 21 matching lines...) Expand all
144 EXPECT_TRUE(rv); 145 EXPECT_TRUE(rv);
145 146
146 // Lock should fail as memory has been purged. 147 // Lock should fail as memory has been purged.
147 auto lock_rv = memory2.Lock(0, 0); 148 auto lock_rv = memory2.Lock(0, 0);
148 EXPECT_EQ(DiscardableSharedMemory::FAILED, lock_rv); 149 EXPECT_EQ(DiscardableSharedMemory::FAILED, lock_rv);
149 150
150 ASSERT_FALSE(memory2.IsMemoryResident()); 151 ASSERT_FALSE(memory2.IsMemoryResident());
151 } 152 }
152 153
153 TEST(DiscardableSharedMemoryTest, LastUsed) { 154 TEST(DiscardableSharedMemoryTest, LastUsed) {
154 const uint32 kDataSize = 1024; 155 const uint32_t kDataSize = 1024;
155 156
156 TestDiscardableSharedMemory memory1; 157 TestDiscardableSharedMemory memory1;
157 bool rv = memory1.CreateAndMap(kDataSize); 158 bool rv = memory1.CreateAndMap(kDataSize);
158 ASSERT_TRUE(rv); 159 ASSERT_TRUE(rv);
159 160
160 SharedMemoryHandle shared_handle; 161 SharedMemoryHandle shared_handle;
161 ASSERT_TRUE( 162 ASSERT_TRUE(
162 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); 163 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
163 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle)); 164 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle));
164 165
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 // The failed purge attempt should have updated usage time to the correct 213 // The failed purge attempt should have updated usage time to the correct
213 // value. 214 // value.
214 EXPECT_TRUE(memory1.last_known_usage().is_null()); 215 EXPECT_TRUE(memory1.last_known_usage().is_null());
215 216
216 // Purge should succeed now that usage time is correct. 217 // Purge should succeed now that usage time is correct.
217 rv = memory1.Purge(Time::FromDoubleT(7)); 218 rv = memory1.Purge(Time::FromDoubleT(7));
218 EXPECT_TRUE(rv); 219 EXPECT_TRUE(rv);
219 } 220 }
220 221
221 TEST(DiscardableSharedMemoryTest, LockShouldAlwaysFailAfterSuccessfulPurge) { 222 TEST(DiscardableSharedMemoryTest, LockShouldAlwaysFailAfterSuccessfulPurge) {
222 const uint32 kDataSize = 1024; 223 const uint32_t kDataSize = 1024;
223 224
224 TestDiscardableSharedMemory memory1; 225 TestDiscardableSharedMemory memory1;
225 bool rv = memory1.CreateAndMap(kDataSize); 226 bool rv = memory1.CreateAndMap(kDataSize);
226 ASSERT_TRUE(rv); 227 ASSERT_TRUE(rv);
227 228
228 SharedMemoryHandle shared_handle; 229 SharedMemoryHandle shared_handle;
229 ASSERT_TRUE( 230 ASSERT_TRUE(
230 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); 231 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
231 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle)); 232 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle));
232 233
233 TestDiscardableSharedMemory memory2(shared_handle); 234 TestDiscardableSharedMemory memory2(shared_handle);
234 rv = memory2.Map(kDataSize); 235 rv = memory2.Map(kDataSize);
235 ASSERT_TRUE(rv); 236 ASSERT_TRUE(rv);
236 237
237 memory2.SetNow(Time::FromDoubleT(1)); 238 memory2.SetNow(Time::FromDoubleT(1));
238 memory2.Unlock(0, 0); 239 memory2.Unlock(0, 0);
239 240
240 rv = memory2.Purge(Time::FromDoubleT(2)); 241 rv = memory2.Purge(Time::FromDoubleT(2));
241 EXPECT_TRUE(rv); 242 EXPECT_TRUE(rv);
242 243
243 // Lock should fail as memory has been purged. 244 // Lock should fail as memory has been purged.
244 auto lock_rv = memory2.Lock(0, 0); 245 auto lock_rv = memory2.Lock(0, 0);
245 EXPECT_EQ(DiscardableSharedMemory::FAILED, lock_rv); 246 EXPECT_EQ(DiscardableSharedMemory::FAILED, lock_rv);
246 } 247 }
247 248
248 TEST(DiscardableSharedMemoryTest, LockAndUnlockRange) { 249 TEST(DiscardableSharedMemoryTest, LockAndUnlockRange) {
249 const uint32 kDataSize = 32; 250 const uint32_t kDataSize = 32;
250 251
251 uint32 data_size_in_bytes = kDataSize * base::GetPageSize(); 252 uint32_t data_size_in_bytes = kDataSize * base::GetPageSize();
252 253
253 TestDiscardableSharedMemory memory1; 254 TestDiscardableSharedMemory memory1;
254 bool rv = memory1.CreateAndMap(data_size_in_bytes); 255 bool rv = memory1.CreateAndMap(data_size_in_bytes);
255 ASSERT_TRUE(rv); 256 ASSERT_TRUE(rv);
256 257
257 SharedMemoryHandle shared_handle; 258 SharedMemoryHandle shared_handle;
258 ASSERT_TRUE( 259 ASSERT_TRUE(
259 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); 260 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
260 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle)); 261 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle));
261 262
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 // The failed purge attempt should have updated usage time to the correct 301 // The failed purge attempt should have updated usage time to the correct
301 // value. 302 // value.
302 EXPECT_EQ(Time::FromDoubleT(8), memory1.last_known_usage()); 303 EXPECT_EQ(Time::FromDoubleT(8), memory1.last_known_usage());
303 304
304 // Purge should now succeed. 305 // Purge should now succeed.
305 rv = memory1.Purge(Time::FromDoubleT(10)); 306 rv = memory1.Purge(Time::FromDoubleT(10));
306 EXPECT_TRUE(rv); 307 EXPECT_TRUE(rv);
307 } 308 }
308 309
309 TEST(DiscardableSharedMemoryTest, MappedSize) { 310 TEST(DiscardableSharedMemoryTest, MappedSize) {
310 const uint32 kDataSize = 1024; 311 const uint32_t kDataSize = 1024;
311 312
312 TestDiscardableSharedMemory memory; 313 TestDiscardableSharedMemory memory;
313 bool rv = memory.CreateAndMap(kDataSize); 314 bool rv = memory.CreateAndMap(kDataSize);
314 ASSERT_TRUE(rv); 315 ASSERT_TRUE(rv);
315 316
316 EXPECT_LE(kDataSize, memory.mapped_size()); 317 EXPECT_LE(kDataSize, memory.mapped_size());
317 318
318 // Mapped size should be 0 after memory segment has been unmapped. 319 // Mapped size should be 0 after memory segment has been unmapped.
319 rv = memory.Unmap(); 320 rv = memory.Unmap();
320 EXPECT_TRUE(rv); 321 EXPECT_TRUE(rv);
321 EXPECT_EQ(0u, memory.mapped_size()); 322 EXPECT_EQ(0u, memory.mapped_size());
322 } 323 }
323 324
324 TEST(DiscardableSharedMemoryTest, Close) { 325 TEST(DiscardableSharedMemoryTest, Close) {
325 const uint32 kDataSize = 1024; 326 const uint32_t kDataSize = 1024;
326 327
327 TestDiscardableSharedMemory memory; 328 TestDiscardableSharedMemory memory;
328 bool rv = memory.CreateAndMap(kDataSize); 329 bool rv = memory.CreateAndMap(kDataSize);
329 ASSERT_TRUE(rv); 330 ASSERT_TRUE(rv);
330 331
331 // Mapped size should be unchanged after memory segment has been closed. 332 // Mapped size should be unchanged after memory segment has been closed.
332 memory.Close(); 333 memory.Close();
333 EXPECT_LE(kDataSize, memory.mapped_size()); 334 EXPECT_LE(kDataSize, memory.mapped_size());
334 335
335 // Memory is initially locked. Unlock it. 336 // Memory is initially locked. Unlock it.
336 memory.SetNow(Time::FromDoubleT(1)); 337 memory.SetNow(Time::FromDoubleT(1));
337 memory.Unlock(0, 0); 338 memory.Unlock(0, 0);
338 339
339 // Lock and unlock memory. 340 // Lock and unlock memory.
340 auto lock_rv = memory.Lock(0, 0); 341 auto lock_rv = memory.Lock(0, 0);
341 EXPECT_EQ(DiscardableSharedMemory::SUCCESS, lock_rv); 342 EXPECT_EQ(DiscardableSharedMemory::SUCCESS, lock_rv);
342 memory.SetNow(Time::FromDoubleT(2)); 343 memory.SetNow(Time::FromDoubleT(2));
343 memory.Unlock(0, 0); 344 memory.Unlock(0, 0);
344 } 345 }
345 346
346 // This test checks that zero-filled pages are returned after purging a segment 347 // This test checks that zero-filled pages are returned after purging a segment
347 // when DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE is 348 // when DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE is
348 // defined and MADV_REMOVE is supported. 349 // defined and MADV_REMOVE is supported.
349 #if defined(DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE) 350 #if defined(DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE)
350 TEST(DiscardableSharedMemoryTest, ZeroFilledPagesAfterPurge) { 351 TEST(DiscardableSharedMemoryTest, ZeroFilledPagesAfterPurge) {
351 const uint32 kDataSize = 1024; 352 const uint32_t kDataSize = 1024;
352 353
353 TestDiscardableSharedMemory memory1; 354 TestDiscardableSharedMemory memory1;
354 bool rv = memory1.CreateAndMap(kDataSize); 355 bool rv = memory1.CreateAndMap(kDataSize);
355 ASSERT_TRUE(rv); 356 ASSERT_TRUE(rv);
356 357
357 SharedMemoryHandle shared_handle; 358 SharedMemoryHandle shared_handle;
358 ASSERT_TRUE( 359 ASSERT_TRUE(
359 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); 360 memory1.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
360 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle)); 361 ASSERT_TRUE(SharedMemory::IsHandleValid(shared_handle));
361 362
(...skipping 10 matching lines...) Expand all
372 EXPECT_FALSE(memory1.IsMemoryLocked()); 373 EXPECT_FALSE(memory1.IsMemoryLocked());
373 374
374 // Memory is unlocked, but our usage timestamp is incorrect. 375 // Memory is unlocked, but our usage timestamp is incorrect.
375 rv = memory1.Purge(Time::FromDoubleT(2)); 376 rv = memory1.Purge(Time::FromDoubleT(2));
376 EXPECT_FALSE(rv); 377 EXPECT_FALSE(rv);
377 rv = memory1.Purge(Time::FromDoubleT(3)); 378 rv = memory1.Purge(Time::FromDoubleT(3));
378 EXPECT_TRUE(rv); 379 EXPECT_TRUE(rv);
379 380
380 // Check that reading memory after it has been purged is returning 381 // Check that reading memory after it has been purged is returning
381 // zero-filled pages. 382 // zero-filled pages.
382 uint8 expected_data[kDataSize] = {}; 383 uint8_t expected_data[kDataSize] = {};
383 EXPECT_EQ(memcmp(memory2.memory(), expected_data, kDataSize), 0); 384 EXPECT_EQ(memcmp(memory2.memory(), expected_data, kDataSize), 0);
384 } 385 }
385 #endif 386 #endif
386 387
387 } // namespace 388 } // namespace
388 } // namespace base 389 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/discardable_shared_memory.cc ('k') | base/memory/memory_pressure_listener.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698