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