| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/files/file.h" | 6 #include "base/files/file.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/test/multiprocess_test.h" | 10 #include "base/test/multiprocess_test.h" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 // Start a child process set to use the specified unlock action, and wait for | 146 // Start a child process set to use the specified unlock action, and wait for |
| 147 // it to lock the file. | 147 // it to lock the file. |
| 148 void StartChildAndSignalLock(const char* unlock_action) { | 148 void StartChildAndSignalLock(const char* unlock_action) { |
| 149 // Create a temporary dir and spin up a ChildLockExit subprocess against it. | 149 // Create a temporary dir and spin up a ChildLockExit subprocess against it. |
| 150 const FilePath temp_path = temp_dir_.GetPath(); | 150 const FilePath temp_path = temp_dir_.GetPath(); |
| 151 base::CommandLine child_command_line( | 151 base::CommandLine child_command_line( |
| 152 base::GetMultiProcessTestChildBaseCommandLine()); | 152 base::GetMultiProcessTestChildBaseCommandLine()); |
| 153 child_command_line.AppendSwitchPath(kTempDirFlag, temp_path); | 153 child_command_line.AppendSwitchPath(kTempDirFlag, temp_path); |
| 154 child_command_line.AppendSwitch(unlock_action); | 154 child_command_line.AppendSwitch(unlock_action); |
| 155 lock_child_ = | 155 |
| 156 base::SpawnMultiProcessTestChild(ChildMainString, child_command_line, | 156 spawn_child_ = base::SpawnMultiProcessTestChild( |
| 157 base::LaunchOptions()); | 157 ChildMainString, child_command_line, base::LaunchOptions()); |
| 158 ASSERT_TRUE(lock_child_.IsValid()); | 158 ASSERT_TRUE(spawn_child_.process.IsValid()); |
| 159 | 159 |
| 160 // Wait for the child to lock the file. | 160 // Wait for the child to lock the file. |
| 161 ASSERT_TRUE(WaitForEventOrTimeout(kSignalLockFileLocked)); | 161 ASSERT_TRUE(WaitForEventOrTimeout(kSignalLockFileLocked)); |
| 162 } | 162 } |
| 163 | 163 |
| 164 // Signal the child to exit cleanly. | 164 // Signal the child to exit cleanly. |
| 165 void ExitChildCleanly() { | 165 void ExitChildCleanly() { |
| 166 ASSERT_TRUE(SignalEvent(kSignalExit)); | 166 ASSERT_TRUE(SignalEvent(kSignalExit)); |
| 167 int rv = -1; | 167 int rv = -1; |
| 168 ASSERT_TRUE(WaitForMultiprocessTestChildExit( | 168 ASSERT_TRUE(WaitForMultiprocessTestChildExit( |
| 169 lock_child_, TestTimeouts::action_timeout(), &rv)); | 169 spawn_child_.process, TestTimeouts::action_timeout(), &rv)); |
| 170 ASSERT_EQ(0, rv); | 170 ASSERT_EQ(0, rv); |
| 171 } | 171 } |
| 172 | 172 |
| 173 base::ScopedTempDir temp_dir_; | 173 base::ScopedTempDir temp_dir_; |
| 174 base::File lock_file_; | 174 base::File lock_file_; |
| 175 base::Process lock_child_; | 175 base::SpawnChildResult spawn_child_; |
| 176 | 176 |
| 177 private: | 177 private: |
| 178 DISALLOW_COPY_AND_ASSIGN(FileLockingTest); | 178 DISALLOW_COPY_AND_ASSIGN(FileLockingTest); |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 // Test that locks are released by Unlock(). | 181 // Test that locks are released by Unlock(). |
| 182 TEST_F(FileLockingTest, LockAndUnlock) { | 182 TEST_F(FileLockingTest, LockAndUnlock) { |
| 183 StartChildAndSignalLock(kFileUnlock); | 183 StartChildAndSignalLock(kFileUnlock); |
| 184 | 184 |
| 185 ASSERT_NE(File::FILE_OK, lock_file_.Lock()); | 185 ASSERT_NE(File::FILE_OK, lock_file_.Lock()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 213 ASSERT_EQ(File::FILE_OK, lock_file_.Lock()); | 213 ASSERT_EQ(File::FILE_OK, lock_file_.Lock()); |
| 214 ASSERT_EQ(File::FILE_OK, lock_file_.Unlock()); | 214 ASSERT_EQ(File::FILE_OK, lock_file_.Unlock()); |
| 215 } | 215 } |
| 216 | 216 |
| 217 // Test that killing the process releases the lock. This should cover crashing. | 217 // Test that killing the process releases the lock. This should cover crashing. |
| 218 TEST_F(FileLockingTest, UnlockOnTerminate) { | 218 TEST_F(FileLockingTest, UnlockOnTerminate) { |
| 219 // The child will wait for an exit which never arrives. | 219 // The child will wait for an exit which never arrives. |
| 220 StartChildAndSignalLock(kExitUnlock); | 220 StartChildAndSignalLock(kExitUnlock); |
| 221 | 221 |
| 222 ASSERT_NE(File::FILE_OK, lock_file_.Lock()); | 222 ASSERT_NE(File::FILE_OK, lock_file_.Lock()); |
| 223 ASSERT_TRUE(TerminateMultiProcessTestChild(lock_child_, 0, true)); | 223 ASSERT_TRUE(TerminateMultiProcessTestChild(spawn_child_.process, 0, true)); |
| 224 ASSERT_EQ(File::FILE_OK, lock_file_.Lock()); | 224 ASSERT_EQ(File::FILE_OK, lock_file_.Lock()); |
| 225 ASSERT_EQ(File::FILE_OK, lock_file_.Unlock()); | 225 ASSERT_EQ(File::FILE_OK, lock_file_.Unlock()); |
| 226 } | 226 } |
| OLD | NEW |