| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "content/browser/mach_broker_mac.h" | 5 #include "content/browser/mach_broker_mac.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/test/multiprocess_test.h" | 10 #include "base/test/multiprocess_test.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 } | 35 } |
| 36 | 36 |
| 37 void InvalidateChildProcessId(int child_process_id) { | 37 void InvalidateChildProcessId(int child_process_id) { |
| 38 broker_.InvalidateChildProcessId(child_process_id); | 38 broker_.InvalidateChildProcessId(child_process_id); |
| 39 } | 39 } |
| 40 | 40 |
| 41 int GetChildProcessCount(int child_process_id) { | 41 int GetChildProcessCount(int child_process_id) { |
| 42 return broker_.child_process_id_map_.count(child_process_id); | 42 return broker_.child_process_id_map_.count(child_process_id); |
| 43 } | 43 } |
| 44 | 44 |
| 45 base::Process LaunchTestChild(const std::string& function, | 45 base::SpawnChildResult LaunchTestChild(const std::string& function, |
| 46 int child_process_id) { | 46 int child_process_id) { |
| 47 base::AutoLock lock(broker_.GetLock()); | 47 base::AutoLock lock(broker_.GetLock()); |
| 48 base::Process test_child_process = base::SpawnMultiProcessTestChild( | 48 base::SpawnChildResult spawn_child = base::SpawnMultiProcessTestChild( |
| 49 function, base::GetMultiProcessTestChildBaseCommandLine(), | 49 function, base::GetMultiProcessTestChildBaseCommandLine(), |
| 50 base::LaunchOptions()); | 50 base::LaunchOptions()); |
| 51 broker_.AddPlaceholderForPid(test_child_process.Handle(), child_process_id); | 51 broker_.AddPlaceholderForPid(spawn_child.process.Handle(), |
| 52 return test_child_process; | 52 child_process_id); |
| 53 return spawn_child; |
| 53 } | 54 } |
| 54 | 55 |
| 55 void WaitForChildExit(base::Process& process) { | 56 void WaitForChildExit(base::Process& process) { |
| 56 int rv = -1; | 57 int rv = -1; |
| 57 ASSERT_TRUE(process.WaitForExitWithTimeout( | 58 ASSERT_TRUE(process.WaitForExitWithTimeout( |
| 58 TestTimeouts::action_timeout(), &rv)); | 59 TestTimeouts::action_timeout(), &rv)); |
| 59 EXPECT_EQ(0, rv); | 60 EXPECT_EQ(0, rv); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void WaitForTaskPort() { | 63 void WaitForTaskPort() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 84 TEST_F(MachBrokerTest, Locks) { | 85 TEST_F(MachBrokerTest, Locks) { |
| 85 // Acquire and release the locks. Nothing bad should happen. | 86 // Acquire and release the locks. Nothing bad should happen. |
| 86 base::AutoLock lock(broker_.GetLock()); | 87 base::AutoLock lock(broker_.GetLock()); |
| 87 } | 88 } |
| 88 | 89 |
| 89 TEST_F(MachBrokerTest, AddChildProcess) { | 90 TEST_F(MachBrokerTest, AddChildProcess) { |
| 90 { | 91 { |
| 91 base::AutoLock lock(broker_.GetLock()); | 92 base::AutoLock lock(broker_.GetLock()); |
| 92 broker_.EnsureRunning(); | 93 broker_.EnsureRunning(); |
| 93 } | 94 } |
| 94 base::Process child_process = LaunchTestChild("MachBrokerTestChild", 7); | 95 base::SpawnChildResult spawn_child = |
| 96 LaunchTestChild("MachBrokerTestChild", 7); |
| 95 WaitForTaskPort(); | 97 WaitForTaskPort(); |
| 96 EXPECT_EQ(child_process.Handle(), received_process_); | 98 EXPECT_EQ(spawn_child.process.Handle(), received_process_); |
| 97 WaitForChildExit(child_process); | 99 WaitForChildExit(spawn_child.process); |
| 98 | 100 |
| 99 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), | 101 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), |
| 100 broker_.TaskForPid(child_process.Handle())); | 102 broker_.TaskForPid(spawn_child.process.Handle())); |
| 101 EXPECT_EQ(1, GetChildProcessCount(7)); | 103 EXPECT_EQ(1, GetChildProcessCount(7)); |
| 102 | 104 |
| 103 // Should be no entry for any other PID. | 105 // Should be no entry for any other PID. |
| 104 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), | 106 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), |
| 105 broker_.TaskForPid(child_process.Handle() + 1)); | 107 broker_.TaskForPid(spawn_child.process.Handle() + 1)); |
| 106 | 108 |
| 107 InvalidateChildProcessId(7); | 109 InvalidateChildProcessId(7); |
| 108 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), | 110 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), |
| 109 broker_.TaskForPid(child_process.Handle())); | 111 broker_.TaskForPid(spawn_child.process.Handle())); |
| 110 EXPECT_EQ(0, GetChildProcessCount(7)); | 112 EXPECT_EQ(0, GetChildProcessCount(7)); |
| 111 } | 113 } |
| 112 | 114 |
| 113 } // namespace content | 115 } // namespace content |
| OLD | NEW |