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