OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/basictypes.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 } | 253 } |
254 | 254 |
255 memory1.Close(); | 255 memory1.Close(); |
256 memory2.Close(); | 256 memory2.Close(); |
257 | 257 |
258 rv = memory1.Delete(test_name); | 258 rv = memory1.Delete(test_name); |
259 EXPECT_TRUE(rv); | 259 EXPECT_TRUE(rv); |
260 } | 260 } |
261 #endif | 261 #endif |
262 | 262 |
| 263 // Check that memory is still mapped after its closed. |
| 264 TEST(SharedMemoryTest, CloseNoUnmap) { |
| 265 const size_t kDataSize = 4096; |
| 266 |
| 267 SharedMemory memory; |
| 268 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); |
| 269 char* ptr = static_cast<char*>(memory.memory()); |
| 270 ASSERT_NE(ptr, static_cast<void*>(NULL)); |
| 271 memset(ptr, 'G', kDataSize); |
| 272 |
| 273 memory.Close(); |
| 274 |
| 275 EXPECT_EQ(ptr, memory.memory()); |
| 276 EXPECT_EQ(SharedMemory::NULLHandle(), memory.handle()); |
| 277 |
| 278 for (size_t i = 0; i < kDataSize; i++) { |
| 279 EXPECT_EQ('G', ptr[i]); |
| 280 } |
| 281 |
| 282 memory.Unmap(); |
| 283 EXPECT_EQ(nullptr, memory.memory()); |
| 284 } |
| 285 |
263 // Create a set of N threads to each open a shared memory segment and write to | 286 // Create a set of N threads to each open a shared memory segment and write to |
264 // it. Verify that they are always reading/writing consistent data. | 287 // it. Verify that they are always reading/writing consistent data. |
265 TEST(SharedMemoryTest, MultipleThreads) { | 288 TEST(SharedMemoryTest, MultipleThreads) { |
266 MultipleThreadMain::CleanUp(); | 289 MultipleThreadMain::CleanUp(); |
267 // On POSIX we have a problem when 2 threads try to create the shmem | 290 // On POSIX we have a problem when 2 threads try to create the shmem |
268 // (a file) at exactly the same time, since create both creates the | 291 // (a file) at exactly the same time, since create both creates the |
269 // file and zerofills it. We solve the problem for this unit test | 292 // file and zerofills it. We solve the problem for this unit test |
270 // (make it not flaky) by starting with 1 thread, then | 293 // (make it not flaky) by starting with 1 thread, then |
271 // intentionally don't clean up its shmem before running with | 294 // intentionally don't clean up its shmem before running with |
272 // kNumThreads. | 295 // kNumThreads. |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 SharedMemoryProcessTest::CleanUp(); | 714 SharedMemoryProcessTest::CleanUp(); |
692 } | 715 } |
693 | 716 |
694 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 717 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
695 return SharedMemoryProcessTest::TaskTestMain(); | 718 return SharedMemoryProcessTest::TaskTestMain(); |
696 } | 719 } |
697 | 720 |
698 #endif // !OS_IOS | 721 #endif // !OS_IOS |
699 | 722 |
700 } // namespace base | 723 } // namespace base |
OLD | NEW |