| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <sys/file.h> | 7 #include <sys/file.h> |
| 8 | 8 |
| 9 #include "chrome/browser/process_singleton.h" | 9 #include "chrome/browser/process_singleton.h" |
| 10 | 10 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 | 63 |
| 64 ScopedTempDir temp_dir_; | 64 ScopedTempDir temp_dir_; |
| 65 FilePath lock_path_; | 65 FilePath lock_path_; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Test that the base case doesn't blow up. | 68 // Test that the base case doesn't blow up. |
| 69 TEST_F(ProcessSingletonMacTest, Basic) { | 69 TEST_F(ProcessSingletonMacTest, Basic) { |
| 70 ProcessSingleton ps(temp_dir_.path()); | 70 ProcessSingleton ps(temp_dir_.path()); |
| 71 EXPECT_FALSE(IsLocked()); | 71 EXPECT_FALSE(IsLocked()); |
| 72 EXPECT_TRUE(ps.Create()); | 72 EXPECT_TRUE(ps.Create(ProcessSingleton::NotificationCallback())); |
| 73 EXPECT_TRUE(IsLocked()); | 73 EXPECT_TRUE(IsLocked()); |
| 74 ps.Cleanup(); | 74 ps.Cleanup(); |
| 75 EXPECT_FALSE(IsLocked()); | 75 EXPECT_FALSE(IsLocked()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // The destructor should release the lock. | 78 // The destructor should release the lock. |
| 79 TEST_F(ProcessSingletonMacTest, DestructorReleases) { | 79 TEST_F(ProcessSingletonMacTest, DestructorReleases) { |
| 80 EXPECT_FALSE(IsLocked()); | 80 EXPECT_FALSE(IsLocked()); |
| 81 { | 81 { |
| 82 ProcessSingleton ps(temp_dir_.path()); | 82 ProcessSingleton ps(temp_dir_.path()); |
| 83 EXPECT_TRUE(ps.Create()); | 83 EXPECT_TRUE(ps.Create(ProcessSingleton::NotificationCallback())); |
| 84 EXPECT_TRUE(IsLocked()); | 84 EXPECT_TRUE(IsLocked()); |
| 85 } | 85 } |
| 86 EXPECT_FALSE(IsLocked()); | 86 EXPECT_FALSE(IsLocked()); |
| 87 } | 87 } |
| 88 | 88 |
| 89 // Multiple singletons should interlock appropriately. | 89 // Multiple singletons should interlock appropriately. |
| 90 TEST_F(ProcessSingletonMacTest, Interlock) { | 90 TEST_F(ProcessSingletonMacTest, Interlock) { |
| 91 ProcessSingleton ps1(temp_dir_.path()); | 91 ProcessSingleton ps1(temp_dir_.path()); |
| 92 ProcessSingleton ps2(temp_dir_.path()); | 92 ProcessSingleton ps2(temp_dir_.path()); |
| 93 | 93 |
| 94 // Windows and Linux use a command-line flag to suppress this, but | 94 // Windows and Linux use a command-line flag to suppress this, but |
| 95 // it is on a sub-process so the scope is contained. Rather than | 95 // it is on a sub-process so the scope is contained. Rather than |
| 96 // add additional API to process_singleton.h in an #ifdef, just tell | 96 // add additional API to process_singleton.h in an #ifdef, just tell |
| 97 // the reader what to expect and move on. | 97 // the reader what to expect and move on. |
| 98 LOG(ERROR) << "Expect two failures to obtain the lock."; | 98 LOG(ERROR) << "Expect two failures to obtain the lock."; |
| 99 | 99 |
| 100 // When |ps1| has the lock, |ps2| cannot get it. | 100 // When |ps1| has the lock, |ps2| cannot get it. |
| 101 EXPECT_FALSE(IsLocked()); | 101 EXPECT_FALSE(IsLocked()); |
| 102 EXPECT_TRUE(ps1.Create()); | 102 EXPECT_TRUE(ps1.Create(ProcessSingleton::NotificationCallback())); |
| 103 EXPECT_TRUE(IsLocked()); | 103 EXPECT_TRUE(IsLocked()); |
| 104 EXPECT_FALSE(ps2.Create()); | 104 EXPECT_FALSE(ps2.Create(ProcessSingleton::NotificationCallback())); |
| 105 ps1.Cleanup(); | 105 ps1.Cleanup(); |
| 106 | 106 |
| 107 // And when |ps2| has the lock, |ps1| cannot get it. | 107 // And when |ps2| has the lock, |ps1| cannot get it. |
| 108 EXPECT_FALSE(IsLocked()); | 108 EXPECT_FALSE(IsLocked()); |
| 109 EXPECT_TRUE(ps2.Create()); | 109 EXPECT_TRUE(ps2.Create(ProcessSingleton::NotificationCallback())); |
| 110 EXPECT_TRUE(IsLocked()); | 110 EXPECT_TRUE(IsLocked()); |
| 111 EXPECT_FALSE(ps1.Create()); | 111 EXPECT_FALSE(ps1.Create(ProcessSingleton::NotificationCallback())); |
| 112 ps2.Cleanup(); | 112 ps2.Cleanup(); |
| 113 EXPECT_FALSE(IsLocked()); | 113 EXPECT_FALSE(IsLocked()); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Like |Interlock| test, but via |NotifyOtherProcessOrCreate()|. | 116 // Like |Interlock| test, but via |NotifyOtherProcessOrCreate()|. |
| 117 TEST_F(ProcessSingletonMacTest, NotifyOtherProcessOrCreate) { | 117 TEST_F(ProcessSingletonMacTest, NotifyOtherProcessOrCreate) { |
| 118 ProcessSingleton ps1(temp_dir_.path()); | 118 ProcessSingleton ps1(temp_dir_.path()); |
| 119 ProcessSingleton ps2(temp_dir_.path()); | 119 ProcessSingleton ps2(temp_dir_.path()); |
| 120 | 120 |
| 121 // Windows and Linux use a command-line flag to suppress this, but | 121 // Windows and Linux use a command-line flag to suppress this, but |
| 122 // it is on a sub-process so the scope is contained. Rather than | 122 // it is on a sub-process so the scope is contained. Rather than |
| 123 // add additional API to process_singleton.h in an #ifdef, just tell | 123 // add additional API to process_singleton.h in an #ifdef, just tell |
| 124 // the reader what to expect and move on. | 124 // the reader what to expect and move on. |
| 125 LOG(ERROR) << "Expect two failures to obtain the lock."; | 125 LOG(ERROR) << "Expect two failures to obtain the lock."; |
| 126 | 126 |
| 127 // When |ps1| has the lock, |ps2| cannot get it. | 127 // When |ps1| has the lock, |ps2| cannot get it. |
| 128 EXPECT_FALSE(IsLocked()); | 128 EXPECT_FALSE(IsLocked()); |
| 129 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, ps1.NotifyOtherProcessOrCreate()); | 129 EXPECT_EQ( |
| 130 ProcessSingleton::PROCESS_NONE, |
| 131 ps1.NotifyOtherProcessOrCreate(ProcessSingleton::NotificationCallback())); |
| 130 EXPECT_TRUE(IsLocked()); | 132 EXPECT_TRUE(IsLocked()); |
| 131 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, ps2.NotifyOtherProcessOrCreate()); | 133 EXPECT_EQ( |
| 134 ProcessSingleton::PROFILE_IN_USE, |
| 135 ps2.NotifyOtherProcessOrCreate(ProcessSingleton::NotificationCallback())); |
| 132 ps1.Cleanup(); | 136 ps1.Cleanup(); |
| 133 | 137 |
| 134 // And when |ps2| has the lock, |ps1| cannot get it. | 138 // And when |ps2| has the lock, |ps1| cannot get it. |
| 135 EXPECT_FALSE(IsLocked()); | 139 EXPECT_FALSE(IsLocked()); |
| 136 EXPECT_EQ(ProcessSingleton::PROCESS_NONE, ps2.NotifyOtherProcessOrCreate()); | 140 EXPECT_EQ( |
| 141 ProcessSingleton::PROCESS_NONE, |
| 142 ps2.NotifyOtherProcessOrCreate(ProcessSingleton::NotificationCallback())); |
| 137 EXPECT_TRUE(IsLocked()); | 143 EXPECT_TRUE(IsLocked()); |
| 138 EXPECT_EQ(ProcessSingleton::PROFILE_IN_USE, ps1.NotifyOtherProcessOrCreate()); | 144 EXPECT_EQ( |
| 145 ProcessSingleton::PROFILE_IN_USE, |
| 146 ps1.NotifyOtherProcessOrCreate(ProcessSingleton::NotificationCallback())); |
| 139 ps2.Cleanup(); | 147 ps2.Cleanup(); |
| 140 EXPECT_FALSE(IsLocked()); | 148 EXPECT_FALSE(IsLocked()); |
| 141 } | 149 } |
| 142 | 150 |
| 143 // TODO(shess): Test that the lock is released when the process dies. | 151 // TODO(shess): Test that the lock is released when the process dies. |
| 144 // DEATH_TEST? I don't know. If the code to communicate between | 152 // DEATH_TEST? I don't know. If the code to communicate between |
| 145 // browser processes is ever written, this all would need to be tested | 153 // browser processes is ever written, this all would need to be tested |
| 146 // more like the other platforms, in which case it would be easy. | 154 // more like the other platforms, in which case it would be easy. |
| 147 | 155 |
| 148 } // namespace | 156 } // namespace |
| OLD | NEW |