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

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: Fix up linux not compiling 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 "build/build_config.h"
6
7 #if defined(OS_LINUX)
8 #include <sys/un.h>
9 #endif // defined(OS_LINUX)
10
11 #include "base/basictypes.h"
12 #include "base/scoped_ptr.h"
13 #include "base/stringprintf.h"
14 #include "base/test/multiprocess_test.h"
15 #include "chrome/common/multi_process_lock.h"
16 #include "testing/multiprocess_func_list.h"
17
18 static char kMutexName[] = "shared_mutex_unittest " __DATE__ " " __TIME__;
Mark Mentovai 2010/11/15 23:42:55 This is no good. Multiple invocations of the same
dmac 2010/11/15 23:55:24 How do you suggest adding the randomness you seek?
Mark Mentovai 2010/11/16 04:05:50 Can you pass a cookie to the processes on the comm
dmac 2010/11/16 17:54:17 Done.
19
20 class MultiProcessLockTest : public base::MultiProcessTest {
21 };
22
23 TEST_F(MultiProcessLockTest, BasicCreationTest) {
24 // Test basic creation/destruction with no lock taken
25 scoped_ptr<MultiProcessLock> scoped(MultiProcessLock::Create(kMutexName));
26 scoped.reset(NULL);
27 }
28
29 TEST_F(MultiProcessLockTest, LongNameTest) {
30 // Linux has a max path name of 108 characters.
31 // http://lxr.linux.no/linux+v2.6.36/include/linux/un.h
32 // This is enforced on all platforms.
33 std::string name("This is a name that is longer than one hundred and eight "
34 "characters to make sure that we fail appropriately on linux when we "
35 "have a path that is to long for linux to handle " __DATE__ " " __TIME__);
Mark Mentovai 2010/11/15 23:42:55 And these guys are just gratuitous. Take them out.
dmac 2010/11/16 17:54:17 Done.
36 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(name));
37 EXPECT_FALSE(test_lock->TryLock());
38 }
39
40 TEST_F(MultiProcessLockTest, SimpleLock) {
41 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
42 EXPECT_TRUE(test_lock->TryLock());
43 base::ProcessHandle handle = SpawnChild("MultiProcessLockTryFailMain", false);
44 ASSERT_TRUE(handle);
45 int exit_code = 0;
46 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
47 EXPECT_EQ(exit_code, 0);
48 test_lock->Unlock();
49 handle = SpawnChild("MultiProcessLockTrySucceedMain", false);
50 ASSERT_TRUE(handle);
51 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
52 EXPECT_EQ(exit_code, 0);
53 }
54
55 TEST_F(MultiProcessLockTest, RecursiveLock) {
56 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
57 EXPECT_TRUE(test_lock->TryLock());
58
59 // Will cause LOG in debug, but will complete.
60 EXPECT_TRUE(test_lock->TryLock());
61 test_lock->Unlock();
62
63 // Will cause LOG in debug, but will complete.
64 test_lock->Unlock();
65 test_lock.reset();
66 }
67
68 TEST_F(MultiProcessLockTest, LockScope) {
69 // Check to see that lock is released when it goes out of scope.
Mark Mentovai 2010/11/15 23:42:55 This doesn’t really check with {scoping}, which is
dmac 2010/11/15 23:55:24 Why isn't the scoped_ptr with a reset not giving u
Mark Mentovai 2010/11/16 04:05:50 It simulates it, but I was hoping for a check that
dmac 2010/11/16 17:54:17 Done.
70 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
71 EXPECT_TRUE(test_lock->TryLock());
72 test_lock.reset(MultiProcessLock::Create(kMutexName));
73 EXPECT_TRUE(test_lock->TryLock());
Mark Mentovai 2010/11/15 23:42:55 Also, this will still be true if the lock is still
dmac 2010/11/15 23:55:24 Good point. I'll look into this one...
dmac 2010/11/16 17:54:17 Done.
74 test_lock.reset();
75 }
76
77 #if defined(OS_LINUX)
78 TEST_F(MultiProcessLockTest, MaxNameLength) {
Mark Mentovai 2010/11/15 23:42:55 Ah, I see what you’ve done. I think the COMPILE_A
dmac 2010/11/15 23:55:24 Ah... missed the assert. Went looking for it, and
dmac 2010/11/16 17:54:17 Done.
79 // +1 for terminator, +1 for 0 in position 0 that makes it an
80 // abstract named socket.
81 // If this test fails it is because sockaddr_un.sun_path size has been
82 // redefined and MULTI_PROCESS_LOCK_NAME_MAX_LEN can change accordingly.
83 // Only tested on Linux because it is the only platform that has a limit.
84 struct sockaddr_un addr;
85 EXPECT_EQ(sizeof(addr.sun_path),
86 MultiProcessLock::MULTI_PROCESS_LOCK_NAME_MAX_LEN + 2);
87 }
88 #endif // defined(OS_LINUX)
89
90 MULTIPROCESS_TEST_MAIN(MultiProcessLockTryFailMain) {
91 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
92 EXPECT_FALSE(test_lock->TryLock());
93 return 0;
94 }
95
96
Mark Mentovai 2010/11/15 23:42:55 Extra blank line.
dmac 2010/11/16 17:54:17 Done.
97 MULTIPROCESS_TEST_MAIN(MultiProcessLockTrySucceedMain) {
98 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(kMutexName));
99 EXPECT_TRUE(test_lock->TryLock());
100 return 0;
101 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698