Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1029)

Side by Side Diff: services/resource_coordinator/memory/coordinator/coordinator_impl_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698