OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/edk/test/mojo_test_base.h" | 5 #include "mojo/edk/test/mojo_test_base.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "mojo/edk/embedder/embedder.h" | 9 #include "mojo/edk/embedder/embedder.h" |
10 #include "mojo/edk/system/handle_signals_state.h" | 10 #include "mojo/edk/system/handle_signals_state.h" |
11 #include "mojo/public/c/system/buffer.h" | 11 #include "mojo/public/c/system/buffer.h" |
12 #include "mojo/public/c/system/data_pipe.h" | 12 #include "mojo/public/c/system/data_pipe.h" |
13 #include "mojo/public/c/system/functions.h" | 13 #include "mojo/public/c/system/functions.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
17 #include "base/mac/mach_port_broker.h" | |
18 #endif | |
19 | |
16 namespace mojo { | 20 namespace mojo { |
17 namespace edk { | 21 namespace edk { |
18 namespace test { | 22 namespace test { |
19 | 23 |
24 | |
25 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
26 namespace { | |
27 base::MachPortBroker* g_mach_broker = nullptr; | |
28 } | |
29 #endif | |
30 | |
20 MojoTestBase::MojoTestBase() { | 31 MojoTestBase::MojoTestBase() { |
32 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
33 if (!g_mach_broker) { | |
34 g_mach_broker = new base::MachPortBroker("mojo_test"); | |
35 CHECK(g_mach_broker->Init()); | |
36 SetMachPortProviderIfNeeded(g_mach_broker); | |
37 } | |
38 #endif | |
21 } | 39 } |
22 | 40 |
23 MojoTestBase::~MojoTestBase() {} | 41 MojoTestBase::~MojoTestBase() {} |
24 | 42 |
25 MojoTestBase::ClientController& MojoTestBase::StartClient( | 43 MojoTestBase::ClientController& MojoTestBase::StartClient( |
26 const std::string& client_name) { | 44 const std::string& client_name) { |
27 clients_.push_back( | 45 clients_.push_back( |
28 make_scoped_ptr(new ClientController(client_name, this))); | 46 make_scoped_ptr(new ClientController(client_name, this))); |
29 return *clients_.back(); | 47 return *clients_.back(); |
30 } | 48 } |
31 | 49 |
32 MojoTestBase::ClientController::ClientController(const std::string& client_name, | 50 MojoTestBase::ClientController::ClientController(const std::string& client_name, |
33 MojoTestBase* test) | 51 MojoTestBase* test) |
34 : test_(test) | 52 : test_(test) { |
35 #if !defined(OS_IOS) | 53 #if !defined(OS_IOS) |
36 , | 54 #if defined(OS_MACOSX) |
37 pipe_(helper_.StartChild(client_name)) | 55 base::AutoLock lock(g_mach_broker->GetLock()); |
Ken Rockot(use gerrit already)
2016/03/09 08:34:31
nit: please document why you have to acquire this
Anand Mistry (off Chromium)
2016/03/11 07:44:18
Done.
| |
38 #endif | 56 #endif |
39 { | 57 pipe_ = helper_.StartChild(client_name); |
58 #if defined(OS_MACOSX) | |
59 g_mach_broker->AddPlaceholderForPid(helper_.test_child().Handle()); | |
60 #endif | |
61 #endif | |
40 } | 62 } |
41 | 63 |
42 MojoTestBase::ClientController::~ClientController() { | 64 MojoTestBase::ClientController::~ClientController() { |
43 CHECK(was_shutdown_) | 65 CHECK(was_shutdown_) |
44 << "Test clients should be waited on explicitly with WaitForShutdown()."; | 66 << "Test clients should be waited on explicitly with WaitForShutdown()."; |
45 } | 67 } |
46 | 68 |
47 int MojoTestBase::ClientController::WaitForShutdown() { | 69 int MojoTestBase::ClientController::WaitForShutdown() { |
48 was_shutdown_ = true; | 70 was_shutdown_ = true; |
49 #if !defined(OS_IOS) | 71 #if !defined(OS_IOS) |
50 return helper_.WaitForChildShutdown(); | 72 int retval = helper_.WaitForChildShutdown(); |
73 #if defined(OS_MACOSX) | |
74 base::AutoLock lock(g_mach_broker->GetLock()); | |
75 g_mach_broker->InvalidatePid(helper_.test_child().Handle()); | |
76 #endif | |
77 return retval; | |
51 #else | 78 #else |
52 NOTREACHED(); | 79 NOTREACHED(); |
53 return 1; | 80 return 1; |
54 #endif | 81 #endif |
55 } | 82 } |
56 | 83 |
57 // static | 84 // static |
58 void MojoTestBase::CloseHandle(MojoHandle h) { | 85 void MojoTestBase::CloseHandle(MojoHandle h) { |
59 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h)); | 86 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h)); |
60 } | 87 } |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), | 287 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), |
261 MOJO_RESULT_OK); | 288 MOJO_RESULT_OK); |
262 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); | 289 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); |
263 | 290 |
264 return std::string(buffer.data(), buffer.size()); | 291 return std::string(buffer.data(), buffer.size()); |
265 } | 292 } |
266 | 293 |
267 } // namespace test | 294 } // namespace test |
268 } // namespace edk | 295 } // namespace edk |
269 } // namespace mojo | 296 } // namespace mojo |
OLD | NEW |