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