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

Side by Side Diff: chrome/common/multi_process_lock_unittest.cc

Issue 4721001: Add multi_process_lock to chrome/common (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: forced the over unlock case Created 10 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/scoped_ptr.h"
7 #include "base/test/multiprocess_test.h"
8 #include "chrome/common/multi_process_lock.h"
9 #include "testing/multiprocess_func_list.h"
10
11 static const char kMutexName[] = "shared_mutex_unittest";
12
13 class MultiProcessLockTest : public base::MultiProcessTest {
14 };
15
16 TEST_F(MultiProcessLockTest, BasicCreationTest) {
17 // Test basic creation/destruction with no lock taken
18 scoped_ptr<MultiProcessLock> scoped(MultiProcessLock::Create(kMutexName));
19 scoped.reset(NULL);
20 }
21
22 TEST_F(MultiProcessLockTest, LongNameTest) {
23 const char kLongName[] = "This is a name that is longer than one hundred and "
24 "four characters to make sure that we fail appropriately on linux when "
25 "we have a path that is to long for linux to handle.";
26 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kLongName));
27 #if defined(OS_LINUX)
28 EXPECT_FALSE(test_lock->TryLock());
29 #else // defined(OS_LINUX)
30 EXPECT_TRUE(test_lock->TryLock());
31 #endif // defined(OS_LINUX)
32 }
33
34 TEST_F(MultiProcessLockTest, SimpleLock) {
35 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
36 EXPECT_TRUE(test_lock->TryLock());
37 base::ProcessHandle handle = SpawnChild("MultiProcessLockTryFailMain", false);
38 ASSERT_TRUE(handle);
39 int exit_code = 0;
40 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
41 EXPECT_EQ(exit_code, 0);
42 test_lock->Unlock();
43 handle = SpawnChild("MultiProcessLockTrySucceedMain", false);
44 ASSERT_TRUE(handle);
45 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
46 EXPECT_EQ(exit_code, 0);
47 }
48
49 TEST_F(MultiProcessLockTest, RecursiveLock) {
50 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
51 EXPECT_TRUE(test_lock->TryLock());
52 EXPECT_TRUE(test_lock->TryLock());
53 test_lock->Unlock();
54 base::ProcessHandle handle = SpawnChild("MultiProcessLockTryFailMain", false);
55 ASSERT_TRUE(handle);
56 int exit_code = 0;
57 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
58 EXPECT_EQ(exit_code, 0);
59 EXPECT_TRUE(test_lock->TryLock());
60 test_lock->Unlock();
61 handle = SpawnChild("MultiProcessLockTryFailMain", false);
62 ASSERT_TRUE(handle);
63 exit_code = 0;
64 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
65 EXPECT_EQ(exit_code, 0); test_lock->Unlock();
66 handle = SpawnChild("MultiProcessLockTrySucceedMain", false);
67 ASSERT_TRUE(handle);
68 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
69 EXPECT_EQ(exit_code, 0);
70
71 // Will cause LOG in debug, but will complete.
72 test_lock->Unlock();
73
74 test_lock.reset();
75 }
76
77 MULTIPROCESS_TEST_MAIN(MultiProcessLockTryFailMain) {
78 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
79 EXPECT_FALSE(test_lock->TryLock());
80 return 0;
81 }
82
83
84 MULTIPROCESS_TEST_MAIN(MultiProcessLockTrySucceedMain) {
85 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
86 EXPECT_TRUE(test_lock->TryLock());
87 return 0;
88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698