| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "services/resource_coordinator/memory/coordinator/coordinator_impl.h" | 5 #include "services/resource_coordinator/memory/coordinator/coordinator_impl.h" |
| 6 #include "base/bind.h" | |
| 7 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 8 #include "base/callback_forward.h" | 7 #include "base/callback_forward.h" |
| 9 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 10 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 12 #include "base/trace_event/memory_dump_request_args.h" | 11 #include "base/trace_event/memory_dump_request_args.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "mojo/public/cpp/bindings/interface_request.h" | 13 #include "mojo/public/cpp/bindings/interface_request.h" |
| 15 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume
ntation.mojom.h" | 14 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume
ntation.mojom.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 namespace memory_instrumentation { | 17 namespace memory_instrumentation { |
| 19 | 18 |
| 20 class CoordinatorImplTest : public testing::Test { | 19 class CoordinatorImplTest : public testing::Test { |
| 21 public: | 20 public: |
| 22 CoordinatorImplTest() {} | 21 CoordinatorImplTest() {} |
| 23 void SetUp() override { | 22 void SetUp() override { |
| 24 dump_response_args_ = {0U, false}; | 23 dump_response_args_ = {static_cast<uint64_t>(-1), false}; |
| 25 coordinator_.reset(new CoordinatorImpl()); | |
| 26 coordinator_->InitializeForTest(base::ThreadTaskRunnerHandle::Get()); | |
| 27 } | 24 } |
| 28 | 25 |
| 29 void TearDown() override { coordinator_.reset(); } | |
| 30 | |
| 31 void RegisterProcessLocalDumpManager( | 26 void RegisterProcessLocalDumpManager( |
| 32 mojom::ProcessLocalDumpManagerPtr process_manager) { | 27 mojom::ProcessLocalDumpManagerPtr process_manager) { |
| 33 base::ThreadTaskRunnerHandle::Get()->PostTask( | 28 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 34 FROM_HERE, | 29 FROM_HERE, base::Bind(&CoordinatorImpl::RegisterProcessLocalDumpManager, |
| 35 base::Bind(&CoordinatorImpl::RegisterProcessLocalDumpManager, | 30 base::Unretained(&coordinator_), |
| 36 base::Unretained(coordinator_.get()), | 31 base::Passed(&process_manager))); |
| 37 base::Passed(&process_manager))); | |
| 38 } | 32 } |
| 39 | 33 |
| 40 void RequestGlobalMemoryDump(base::trace_event::MemoryDumpRequestArgs args, | 34 void RequestGlobalMemoryDump(base::trace_event::MemoryDumpRequestArgs args, |
| 41 base::Closure closure) { | 35 base::Closure closure) { |
| 42 base::ThreadTaskRunnerHandle::Get()->PostTask( | 36 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 43 FROM_HERE, | 37 FROM_HERE, |
| 44 base::Bind(&CoordinatorImpl::RequestGlobalMemoryDump, | 38 base::Bind(&CoordinatorImpl::RequestGlobalMemoryDump, |
| 45 base::Unretained(coordinator_.get()), args, | 39 base::Unretained(&coordinator_), args, |
| 46 base::Bind(&CoordinatorImplTest::OnGlobalMemoryDumpResponse, | 40 base::Bind(&CoordinatorImplTest::OnGlobalMemoryDumpResponse, |
| 47 base::Unretained(this), closure))); | 41 base::Unretained(this), closure))); |
| 48 } | 42 } |
| 49 | 43 |
| 50 void OnGlobalMemoryDumpResponse(base::Closure closure, | 44 void OnGlobalMemoryDumpResponse(base::Closure closure, |
| 51 uint64_t dump_guid, | 45 uint64_t dump_guid, |
| 52 bool success) { | 46 bool success) { |
| 53 dump_response_args_ = {dump_guid, success}; | 47 dump_response_args_ = {dump_guid, success}; |
| 54 closure.Run(); | 48 closure.Run(); |
| 55 } | 49 } |
| 56 | 50 |
| 57 protected: | 51 protected: |
| 58 struct DumpResponseArgs { | 52 struct DumpResponseArgs { |
| 59 uint64_t dump_guid; | 53 uint64_t dump_guid; |
| 60 bool success; | 54 bool success; |
| 61 }; | 55 }; |
| 62 | 56 |
| 63 DumpResponseArgs dump_response_args_; | 57 DumpResponseArgs dump_response_args_; |
| 64 | 58 |
| 65 private: | 59 private: |
| 66 std::unique_ptr<CoordinatorImpl> coordinator_; | 60 CoordinatorImpl coordinator_; |
| 67 base::MessageLoop message_loop_; | 61 base::MessageLoop message_loop_; |
| 68 }; | 62 }; |
| 69 | 63 |
| 70 class MockDumpManager : public mojom::ProcessLocalDumpManager { | 64 class MockDumpManager : mojom::ProcessLocalDumpManager { |
| 71 public: | 65 public: |
| 72 MockDumpManager(CoordinatorImplTest* test_coordinator, int expected_calls) | 66 MockDumpManager(CoordinatorImplTest* test_coordinator, int expected_calls) |
| 73 : binding_(this), expected_calls_(expected_calls) { | 67 : binding_(this), expected_calls_(expected_calls) { |
| 74 // Register to the coordinator. | 68 // Register to the coordinator. |
| 75 mojom::ProcessLocalDumpManagerPtr process_manager; | 69 mojom::ProcessLocalDumpManagerPtr process_manager; |
| 76 binding_.Bind(mojo::MakeRequest(&process_manager)); | 70 binding_.Bind(mojo::MakeRequest(&process_manager)); |
| 77 test_coordinator->RegisterProcessLocalDumpManager( | 71 test_coordinator->RegisterProcessLocalDumpManager( |
| 78 std::move(process_manager)); | 72 std::move(process_manager)); |
| 79 } | 73 } |
| 80 | 74 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 92 int expected_calls_; | 86 int expected_calls_; |
| 93 }; | 87 }; |
| 94 | 88 |
| 95 TEST_F(CoordinatorImplTest, NoProcessLocalManagers) { | 89 TEST_F(CoordinatorImplTest, NoProcessLocalManagers) { |
| 96 base::RunLoop run_loop; | 90 base::RunLoop run_loop; |
| 97 base::trace_event::MemoryDumpRequestArgs args = { | 91 base::trace_event::MemoryDumpRequestArgs args = { |
| 98 1234, base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED, | 92 1234, base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 99 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 93 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
| 100 RequestGlobalMemoryDump(args, run_loop.QuitClosure()); | 94 RequestGlobalMemoryDump(args, run_loop.QuitClosure()); |
| 101 run_loop.Run(); | 95 run_loop.Run(); |
| 102 EXPECT_EQ(1234U, dump_response_args_.dump_guid); | 96 EXPECT_EQ(static_cast<uint64_t>(1234), dump_response_args_.dump_guid); |
| 103 EXPECT_TRUE(dump_response_args_.success); | 97 EXPECT_TRUE(dump_response_args_.success); |
| 104 } | 98 } |
| 105 | 99 |
| 106 TEST_F(CoordinatorImplTest, SeveralProcessLocalManagers) { | 100 TEST_F(CoordinatorImplTest, SeveralProcessLocalManagers) { |
| 107 base::RunLoop run_loop; | 101 base::RunLoop run_loop; |
| 108 | 102 |
| 109 MockDumpManager dump_manager_1(this, 1); | 103 MockDumpManager dump_manager_1(this, 1); |
| 110 MockDumpManager dump_manager_2(this, 1); | 104 MockDumpManager dump_manager_2(this, 1); |
| 111 base::trace_event::MemoryDumpRequestArgs args = { | 105 base::trace_event::MemoryDumpRequestArgs args = { |
| 112 2345, base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED, | 106 2345, base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 113 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 107 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
| 114 RequestGlobalMemoryDump(args, run_loop.QuitClosure()); | 108 RequestGlobalMemoryDump(args, run_loop.QuitClosure()); |
| 115 | 109 |
| 116 run_loop.Run(); | 110 run_loop.Run(); |
| 117 | 111 |
| 118 EXPECT_EQ(2345U, dump_response_args_.dump_guid); | 112 EXPECT_EQ(static_cast<uint64_t>(2345), dump_response_args_.dump_guid); |
| 119 EXPECT_TRUE(dump_response_args_.success); | 113 EXPECT_TRUE(dump_response_args_.success); |
| 120 } | 114 } |
| 121 | 115 |
| 122 TEST_F(CoordinatorImplTest, FaultyProcessLocalManager) { | 116 TEST_F(CoordinatorImplTest, FaultyProcessLocalManager) { |
| 123 base::RunLoop run_loop; | 117 base::RunLoop run_loop; |
| 124 | 118 |
| 125 MockDumpManager dump_manager_1(this, 1); | 119 MockDumpManager dump_manager_1(this, 1); |
| 126 std::unique_ptr<MockDumpManager> dump_manager_2(new MockDumpManager(this, 0)); | 120 std::unique_ptr<MockDumpManager> dump_manager_2(new MockDumpManager(this, 0)); |
| 127 base::trace_event::MemoryDumpRequestArgs args = { | 121 base::trace_event::MemoryDumpRequestArgs args = { |
| 128 3456, base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED, | 122 3456, base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 129 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 123 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
| 130 RequestGlobalMemoryDump(args, run_loop.QuitClosure()); | 124 RequestGlobalMemoryDump(args, run_loop.QuitClosure()); |
| 131 // One of the process-local managers dies after a global dump is requested and | 125 // One of the process-local managers dies after a global dump is requested and |
| 132 // before it receives the corresponding process dump request. The coordinator | 126 // before it receives the corresponding process dump request. The coordinator |
| 133 // should detect that one of its clients is disconnected and claim the global | 127 // should detect that one of its clients is disconnected and claim the global |
| 134 // dump attempt has failed. | 128 // dump attempt has failed. |
| 135 base::ThreadTaskRunnerHandle::Get()->PostTask( | 129 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 136 FROM_HERE, base::Bind([](std::unique_ptr<MockDumpManager> dm) {}, | 130 FROM_HERE, base::Bind([](std::unique_ptr<MockDumpManager> dm) {}, |
| 137 base::Passed(&dump_manager_2))); | 131 base::Passed(&dump_manager_2))); |
| 138 | 132 |
| 139 run_loop.Run(); | 133 run_loop.Run(); |
| 140 | 134 |
| 141 EXPECT_EQ(3456U, dump_response_args_.dump_guid); | 135 EXPECT_EQ(static_cast<uint64_t>(3456), dump_response_args_.dump_guid); |
| 142 EXPECT_FALSE(dump_response_args_.success); | 136 EXPECT_FALSE(dump_response_args_.success); |
| 143 } | 137 } |
| 144 } // namespace memory_instrumentation | 138 } // namespace memory_instrumentation |
| OLD | NEW |