Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "base/mac/mach_port_broker.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/synchronization/lock.h" | |
| 9 #include "base/test/multiprocess_test.h" | |
| 10 #include "base/test/test_timeouts.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "testing/multiprocess_func_list.h" | |
| 13 | |
| 14 namespace base { | |
| 15 | |
| 16 namespace { | |
| 17 const char kBootstrapPortName[] = "thisisatest"; | |
|
erikchen
2016/03/03 02:26:22
is it possible for tests from a single suite to be
Anand Mistry (off Chromium)
2016/03/03 02:48:55
Sharded test suites run in different processes, an
| |
| 18 } | |
| 19 | |
| 20 class MachPortBrokerTest : public testing::Test { | |
| 21 public: | |
| 22 MachPortBrokerTest() : broker_(kBootstrapPortName) {} | |
| 23 | |
| 24 // Helper function to acquire/release locks and call |PlaceholderForPid()|. | |
| 25 void AddPlaceholderForPid(base::ProcessHandle pid) { | |
| 26 base::AutoLock lock(broker_.GetLock()); | |
| 27 broker_.AddPlaceholderForPid(pid); | |
| 28 } | |
| 29 | |
| 30 // Helper function to acquire/release locks and call |FinalizePid()|. | |
| 31 void FinalizePid(base::ProcessHandle pid, | |
| 32 mach_port_t task_port) { | |
| 33 base::AutoLock lock(broker_.GetLock()); | |
| 34 broker_.FinalizePid(pid, task_port); | |
| 35 } | |
| 36 | |
| 37 protected: | |
| 38 MachPortBroker broker_; | |
| 39 }; | |
| 40 | |
| 41 TEST_F(MachPortBrokerTest, Locks) { | |
| 42 // Acquire and release the locks. Nothing bad should happen. | |
| 43 base::AutoLock lock(broker_.GetLock()); | |
| 44 } | |
| 45 | |
| 46 TEST_F(MachPortBrokerTest, AddPlaceholderAndFinalize) { | |
| 47 // Add a placeholder for PID 1. | |
| 48 AddPlaceholderForPid(1); | |
| 49 EXPECT_EQ(0u, broker_.TaskForPid(1)); | |
| 50 | |
| 51 // Finalize PID 1. | |
| 52 FinalizePid(1, 100u); | |
| 53 EXPECT_EQ(100u, broker_.TaskForPid(1)); | |
| 54 | |
| 55 // Should be no entry for PID 2. | |
| 56 EXPECT_EQ(0u, broker_.TaskForPid(2)); | |
| 57 } | |
| 58 | |
| 59 TEST_F(MachPortBrokerTest, FinalizeUnknownPid) { | |
| 60 // Finalizing an entry for an unknown pid should not add it to the map. | |
| 61 FinalizePid(1u, 100u); | |
| 62 EXPECT_EQ(0u, broker_.TaskForPid(1u)); | |
| 63 } | |
| 64 | |
| 65 MULTIPROCESS_TEST_MAIN(MachPortBrokerTestChild) { | |
| 66 CHECK(base::MachPortBroker::ChildSendTaskPortToParent(kBootstrapPortName)); | |
| 67 return 0; | |
| 68 } | |
| 69 | |
| 70 TEST_F(MachPortBrokerTest, ReceivePortFromChild) { | |
| 71 ASSERT_TRUE(broker_.Init()); | |
| 72 CommandLine command_line( | |
| 73 base::GetMultiProcessTestChildBaseCommandLine()); | |
| 74 broker_.GetLock().Acquire(); | |
| 75 base::Process test_child_process = base::SpawnMultiProcessTestChild( | |
| 76 "MachPortBrokerTestChild", command_line, LaunchOptions()); | |
| 77 broker_.AddPlaceholderForPid(test_child_process.Handle()); | |
| 78 broker_.GetLock().Release(); | |
| 79 | |
| 80 int rv = -1; | |
| 81 ASSERT_TRUE(test_child_process.WaitForExitWithTimeout( | |
| 82 TestTimeouts::action_timeout(), &rv)); | |
| 83 EXPECT_EQ(0, rv); | |
| 84 | |
| 85 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), | |
| 86 broker_.TaskForPid(test_child_process.Handle())); | |
| 87 } | |
| 88 | |
| 89 TEST_F(MachPortBrokerTest, ReceivePortFromChildWithoutAdding) { | |
| 90 ASSERT_TRUE(broker_.Init()); | |
| 91 CommandLine command_line( | |
| 92 base::GetMultiProcessTestChildBaseCommandLine()); | |
| 93 broker_.GetLock().Acquire(); | |
| 94 base::Process test_child_process = base::SpawnMultiProcessTestChild( | |
| 95 "MachPortBrokerTestChild", command_line, LaunchOptions()); | |
| 96 broker_.GetLock().Release(); | |
| 97 | |
| 98 int rv = -1; | |
| 99 ASSERT_TRUE(test_child_process.WaitForExitWithTimeout( | |
| 100 TestTimeouts::action_timeout(), &rv)); | |
| 101 EXPECT_EQ(0, rv); | |
| 102 | |
| 103 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), | |
| 104 broker_.TaskForPid(test_child_process.Handle())); | |
| 105 } | |
| 106 | |
| 107 } // namespace base | |
| OLD | NEW |