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

Side by Side Diff: services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl_unittest.cc

Issue 2819413002: [memory-infra] Remove MemoryDumpManagerDelegate (Closed)
Patch Set: rebase. Created 3 years, 7 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
(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 mdm_.reset(new MemoryDumpManager());
50 MemoryDumpManager::SetInstanceForTesting(mdm_.get());
51 MemoryDumpManagerDelegateImpl::Config config(coordinator_.get());
52 auto delegate = base::MakeUnique<MemoryDumpManagerDelegateImpl>(config);
53 delegate->SetAsNonCoordinatorForTesting();
54 mdm_->Initialize(std::move(delegate));
55
56 // Enable tracing.
57 std::string category_filter = "-*,";
58 category_filter += MemoryDumpManager::kTraceCategory;
59 base::trace_event::TraceConfig trace_config(category_filter, "");
60 base::trace_event::TraceLog::GetInstance()->SetEnabled(
61 trace_config, base::trace_event::TraceLog::RECORDING_MODE);
62
63 // Reset the counters.
64 expected_callback_calls_ = 0;
65 dump_requests_received_by_coordinator_ = 0;
66 quit_closure_.Reset();
67 }
68
69 void TearDown() override {
70 base::trace_event::TraceLog::GetInstance()->SetDisabled();
71 MemoryDumpManager::SetInstanceForTesting(nullptr);
72 mdm_.reset();
73 coordinator_.reset();
74 message_loop_.reset();
75 }
76
77 void OnGlobalMemoryDumpDone(int more_requests,
78 uint64_t dump_guid,
79 bool success) {
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::OnGlobalMemoryDumpDone,
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<MemoryDumpManager> mdm_;
107 };
108
109 // Makes several global dump requests each after receiving the ACK for the
110 // previous one. There should be no throttling and all requests should be
111 // forwarded to the coordinator.
112 TEST_F(MemoryDumpManagerDelegateImplTest, NonOverlappingMemoryDumpRequests) {
113 base::RunLoop run_loop;
114 expected_callback_calls_ = 3;
115 quit_closure_ = run_loop.QuitClosure();
116 SequentiallyRequestGlobalDumps(3);
117 run_loop.Run();
118 EXPECT_EQ(3, dump_requests_received_by_coordinator_);
119 }
120
121 // Makes several global dump requests without waiting for previous requests to
122 // finish. Only the first request should make it to the coordinator. The rest
123 // should be cancelled.
124 TEST_F(MemoryDumpManagerDelegateImplTest, OverlappingMemoryDumpRequests) {
125 base::RunLoop run_loop;
126 expected_callback_calls_ = 3;
127 quit_closure_ = run_loop.QuitClosure();
128 SequentiallyRequestGlobalDumps(1);
129 SequentiallyRequestGlobalDumps(1);
130 SequentiallyRequestGlobalDumps(1);
131 run_loop.Run();
132 EXPECT_EQ(1, dump_requests_received_by_coordinator_);
133 }
134
135 } // namespace memory_instrumentation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698