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

Side by Side Diff: content/browser/mach_broker_mac_unittest.cc

Issue 1755973002: Move non-content specific parts of content::MachBroker into base::MachPortBroker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test changes. Created 4 years, 9 months 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
OLDNEW
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/synchronization/lock.h" 8 #include "base/synchronization/lock.h"
9 #include "base/test/multiprocess_test.h"
10 #include "base/test/test_timeouts.h"
8 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/multiprocess_func_list.h"
9 13
10 namespace content { 14 namespace content {
11 15
12 class MachBrokerTest : public testing::Test { 16 class MachBrokerTest : public testing::Test {
13 public: 17 public:
14 // Helper function to acquire/release locks and call |PlaceholderForPid()|. 18 // Helper function to acquire/release locks and call |PlaceholderForPid()|.
15 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) { 19 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) {
16 base::AutoLock lock(broker_.GetLock()); 20 base::AutoLock lock(broker_.GetLock());
17 broker_.AddPlaceholderForPid(pid, child_process_id); 21 broker_.AddPlaceholderForPid(pid, child_process_id);
18 } 22 }
19 23
20 void InvalidateChildProcessId(int child_process_id) { 24 void InvalidateChildProcessId(int child_process_id) {
21 broker_.InvalidateChildProcessId(child_process_id); 25 broker_.InvalidateChildProcessId(child_process_id);
22 } 26 }
23 27
24 int GetChildProcessCount(int child_process_id) { 28 int GetChildProcessCount(int child_process_id) {
25 return broker_.child_process_id_map_.count(child_process_id); 29 return broker_.child_process_id_map_.count(child_process_id);
26 } 30 }
27 31
28 // Helper function to acquire/release locks and call |FinalizePid()|. 32 base::Process LaunchTestChild(const std::string& function,
29 void FinalizePid(base::ProcessHandle pid, 33 int child_process_id) {
30 mach_port_t task_port) {
31 base::AutoLock lock(broker_.GetLock()); 34 base::AutoLock lock(broker_.GetLock());
32 broker_.FinalizePid(pid, task_port); 35 base::Process test_child_process = base::SpawnMultiProcessTestChild(
36 function, base::GetMultiProcessTestChildBaseCommandLine(),
37 LaunchOptions());
38 broker_.AddPlaceholderForPid(test_child_process.Handle, child_process_id);
39 return test_child_process;
40 }
41
42 void WaitForChildExit(base::Process& process) {
43 int rv = -1;
44 ASSERT_TRUE(process.WaitForExitWithTimeout(
45 TestTimeouts::action_timeout(), &rv));
46 EXPECT_EQ(0, rv);
33 } 47 }
34 48
35 protected: 49 protected:
36 MachBroker broker_; 50 MachBroker broker_;
37 }; 51 };
38 52
53 MULTIPROCESS_TEST_MAIN(MachBrokerTestChild) {
54 CHECK(MachBroker::ChildSendTaskPortToParent());
55 return 0;
56 }
57
39 TEST_F(MachBrokerTest, Locks) { 58 TEST_F(MachBrokerTest, Locks) {
40 // Acquire and release the locks. Nothing bad should happen. 59 // Acquire and release the locks. Nothing bad should happen.
41 base::AutoLock lock(broker_.GetLock()); 60 base::AutoLock lock(broker_.GetLock());
42 } 61 }
43 62
44 TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) { 63 TEST_F(MachBrokerTest, AddChildProcess) {
45 // Add a placeholder for PID 1. 64 broker_.EnsureRunning();
46 AddPlaceholderForPid(1, 1); 65 base::Process child_process = LaunchTestChild("MachBrokerTestChild", 7);
47 EXPECT_EQ(0u, broker_.TaskForPid(1)); 66 WaitForChildExit(child_process);
48 67
49 // Finalize PID 1. 68 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL),
50 FinalizePid(1, 100u); 69 broker_.TaskForPid(child_process.Handle()));
51 EXPECT_EQ(100u, broker_.TaskForPid(1)); 70 EXPECT_EQ(1, GetChildProcessCount(7));
52 71
53 // Should be no entry for PID 2. 72 // Should be no entry for any other PID.
54 EXPECT_EQ(0u, broker_.TaskForPid(2)); 73 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
55 } 74 broker_.TaskForPid(child_process.Handle() + 1));
56 75
57 TEST_F(MachBrokerTest, InvalidateChildProcessId) { 76 InvalidateChildProcessId(7);
58 // Add a placeholder for PID 1 and child process id 50. 77 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
59 AddPlaceholderForPid(1, 50); 78 broker_.TaskForPid(child_process.Handle()));
60 FinalizePid(1, 100u); 79 EXPECT_EQ(0, GetChildProcessCount(7));
61
62 EXPECT_EQ(100u, broker_.TaskForPid(1));
63 InvalidateChildProcessId(50);
64 EXPECT_EQ(0u, broker_.TaskForPid(1));
65 }
66
67 TEST_F(MachBrokerTest, ValidateChildProcessIdMap) {
68 // Add a placeholder for PID 1 and child process id 50.
69 AddPlaceholderForPid(1, 50);
70 FinalizePid(1, 100u);
71
72 EXPECT_EQ(1, GetChildProcessCount(50));
73 InvalidateChildProcessId(50);
74 EXPECT_EQ(0, GetChildProcessCount(50));
75 }
76
77 TEST_F(MachBrokerTest, FinalizeUnknownPid) {
78 // Finalizing an entry for an unknown pid should not add it to the map.
79 FinalizePid(1u, 100u);
80 EXPECT_EQ(0u, broker_.TaskForPid(1u));
81 } 80 }
82 81
83 } // namespace content 82 } // namespace content
OLDNEW
« base/mac/mach_port_broker_unittest.cc ('K') | « content/browser/mach_broker_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698