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 "services/resource_coordinator/public/cpp/memory/memory_dump_manager_de legate_impl.h" | |
6 #include "base/bind.h" | |
7 #include "base/bind_helpers.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/trace_event/memory_dump_manager.h" | |
11 #include "base/trace_event/memory_dump_request_args.h" | |
12 #include "base/trace_event/trace_config.h" | |
13 #include "base/trace_event/trace_log.h" | |
14 #include "mojo/public/cpp/bindings/binding_set.h" | |
15 #include "services/resource_coordinator/public/cpp/memory/coordinator.h" | |
16 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume ntation.mojom.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using base::trace_event::MemoryDumpLevelOfDetail; | |
20 using base::trace_event::MemoryDumpManager; | |
21 using base::trace_event::MemoryDumpType; | |
22 | |
23 namespace memory_instrumentation { | |
24 | |
25 class MockCoordinator : public Coordinator, public mojom::Coordinator { | |
26 public: | |
27 void BindCoordinatorRequest(mojom::CoordinatorRequest request) override { | |
28 bindings_.AddBinding(this, std::move(request)); | |
29 } | |
30 | |
31 void RegisterProcessLocalDumpManager( | |
32 mojom::ProcessLocalDumpManagerPtr process_manager) override {} | |
33 | |
34 void RequestGlobalMemoryDump( | |
35 const base::trace_event::MemoryDumpRequestArgs& args, | |
36 const RequestGlobalMemoryDumpCallback& callback) override { | |
37 callback.Run(args.dump_guid, true); | |
38 } | |
39 | |
40 private: | |
41 mojo::BindingSet<mojom::Coordinator> bindings_; | |
42 }; | |
43 | |
44 class MemoryDumpManagerDelegateImplTest : public testing::Test { | |
45 public: | |
46 void SetUp() override { | |
47 message_loop_.reset(new base::MessageLoop()); | |
48 coordinator_.reset(new MockCoordinator()); | |
49 delegate_.reset(new MemoryDumpManagerDelegateImpl()); | |
50 mdm_.reset(new MemoryDumpManager()); | |
51 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | |
52 | |
53 // This will initialize |MemoryDumpManager|, too. | |
54 delegate_->InitializeWithCoordinator(coordinator_.get(), | |
55 base::ThreadTaskRunnerHandle::Get()); | |
56 | |
57 // Enable tracing. | |
58 std::string category_filter = "-*,"; | |
59 category_filter += MemoryDumpManager::kTraceCategory; | |
60 base::trace_event::TraceConfig trace_config(category_filter, ""); | |
61 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
62 trace_config, base::trace_event::TraceLog::RECORDING_MODE); | |
63 | |
64 // Reset the counters. | |
65 expected_callback_calls_ = 0; | |
66 dump_requests_received_by_coordinator_ = 0; | |
67 quit_closure_.Reset(); | |
68 } | |
69 | |
70 void TearDown() override { | |
71 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
72 MemoryDumpManager::SetInstanceForTesting(nullptr); | |
73 mdm_.reset(); | |
74 delegate_.reset(); | |
75 coordinator_.reset(); | |
76 message_loop_.reset(); | |
77 } | |
78 | |
79 void OnMemoryDump(int more_requests, uint64_t dump_guid, bool success) { | |
ssid
2017/02/23 20:21:50
OnGlobalMemoryDumpDone?
OnMemoryDump is overloaded
chiniforooshan
2017/02/24 15:51:03
Done.
| |
80 EXPECT_GT(expected_callback_calls_, 0); | |
81 EXPECT_FALSE(quit_closure_.is_null()); | |
82 | |
83 dump_requests_received_by_coordinator_ += success ? 1 : 0; | |
84 expected_callback_calls_--; | |
85 if (expected_callback_calls_ == 0) | |
86 quit_closure_.Run(); | |
87 | |
88 if (more_requests > 0) | |
89 SequentiallyRequestGlobalDumps(more_requests); | |
90 } | |
91 | |
92 void SequentiallyRequestGlobalDumps(int num_requests) { | |
93 MemoryDumpManager::GetInstance()->RequestGlobalDump( | |
94 MemoryDumpType::EXPLICITLY_TRIGGERED, MemoryDumpLevelOfDetail::LIGHT, | |
95 base::Bind(&MemoryDumpManagerDelegateImplTest::OnMemoryDump, | |
96 base::Unretained(this), num_requests - 1)); | |
97 } | |
98 | |
99 int expected_callback_calls_; | |
100 int dump_requests_received_by_coordinator_; | |
101 base::Closure quit_closure_; | |
102 | |
103 private: | |
104 std::unique_ptr<base::MessageLoop> message_loop_; | |
105 std::unique_ptr<MockCoordinator> coordinator_; | |
106 std::unique_ptr<MemoryDumpManagerDelegateImpl> delegate_; | |
107 std::unique_ptr<MemoryDumpManager> mdm_; | |
108 }; | |
109 | |
110 // Makes several global dump requests each after receiving the ACK for the | |
111 // previous one. There should be no throttling and all requests should be | |
112 // forwarded to the coordinator. | |
113 TEST_F(MemoryDumpManagerDelegateImplTest, NonOverlappingMemoryDumpRequests) { | |
114 base::RunLoop run_loop; | |
115 expected_callback_calls_ = 3; | |
116 quit_closure_ = run_loop.QuitClosure(); | |
117 SequentiallyRequestGlobalDumps(3); | |
118 run_loop.Run(); | |
119 EXPECT_EQ(3, dump_requests_received_by_coordinator_); | |
120 } | |
121 | |
122 // Makes several global dump requests without waiting for previous requests to | |
123 // finish. Only the first request should make it to the coordinator. The rest | |
124 // should be cancelled. | |
125 TEST_F(MemoryDumpManagerDelegateImplTest, OverlappingMemoryDumpRequests) { | |
126 base::RunLoop run_loop; | |
127 expected_callback_calls_ = 3; | |
128 quit_closure_ = run_loop.QuitClosure(); | |
129 SequentiallyRequestGlobalDumps(1); | |
130 SequentiallyRequestGlobalDumps(1); | |
131 SequentiallyRequestGlobalDumps(1); | |
132 run_loop.Run(); | |
133 EXPECT_EQ(1, dump_requests_received_by_coordinator_); | |
134 } | |
135 | |
136 } // namespace memory_instrumentation | |
OLD | NEW |