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

Side by Side Diff: remoting/host/process_stats_sender_unittest.cc

Issue 2775983003: [Chromoting] Retrieve process resource usage (ProcessStats) and its tests (Closed)
Patch Set: Resolve review comments 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 "remoting/host/process_stats_sender.h"
6
7 #include <stdint.h>
8
9 #include <utility>
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/location.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/time/time.h"
17 #include "remoting/host/process_stats_agent.h"
18 #include "remoting/proto/process_stats.pb.h"
19 #include "remoting/protocol/process_stats_stub.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace remoting {
23
24 namespace {
25
26 class FakeProcessStatsStub : public protocol::ProcessStatsStub {
27 public:
28 FakeProcessStatsStub() = default;
29 ~FakeProcessStatsStub() override = default;
30
31 void OnProcessStats(
32 const protocol::AggregatedProcessResourceUsage& usage) override {
33 received_.push_back(usage);
34 DCHECK_LE(received_.size(), static_cast<size_t>(expected_usage_count_));
35 DCHECK(!quit_closure_.is_null());
36 if (received_.size() == static_cast<size_t>(expected_usage_count_)) {
37 quit_closure_.Run();
38 }
39 }
40
41 const std::vector<protocol::AggregatedProcessResourceUsage>& received()
42 const {
43 return received_;
44 }
45
46 void set_quit_closure(base::Closure quit_closure) {
47 quit_closure_ = quit_closure;
48 }
49
50 void set_expected_usage_count(int expected_usage_count) {
51 expected_usage_count_ = expected_usage_count;
52 }
53
54 private:
55 std::vector<protocol::AggregatedProcessResourceUsage> received_;
56 int expected_usage_count_ = 0;
57 base::Closure quit_closure_;
58 };
59
60 class FakeProcessStatsAgent : public ProcessStatsAgent {
61 public:
62 FakeProcessStatsAgent() = default;
63 ~FakeProcessStatsAgent() override = default;
64
65 protocol::ProcessResourceUsage GetResourceUsage() override {
66 protocol::ProcessResourceUsage usage;
67 usage.set_process_name("FakeProcessStatsAgent");
68 usage.set_processor_usage(index_);
69 usage.set_working_set_size(index_);
70 usage.set_pagefile_size(index_);
71 index_++;
72 return usage;
73 }
74
75 // Checks the expected usage based on index.
76 static void AssertExpected(
77 const protocol::AggregatedProcessResourceUsage& usage,
78 size_t index) {
79 ASSERT_EQ(usage.processor_usage(), index);
80 ASSERT_EQ(usage.working_set_size(), index);
81 ASSERT_EQ(usage.pagefile_size(), index);
82 }
83
84 static void AssertExpected(const protocol::ProcessResourceUsage& usage,
85 size_t index) {
86 ASSERT_EQ(usage.processor_usage(), index);
87 ASSERT_EQ(usage.working_set_size(), index);
88 ASSERT_EQ(usage.pagefile_size(), index);
89 }
90
91 size_t issued_times() const { return index_; }
92
93 private:
94 size_t index_ = 0;
95 };
96
97 } // namespace
98
99 TEST(ProcessStatsSenderTest, ReportUsage) {
100 base::MessageLoop message_loop;
101 base::RunLoop run_loop;
102 FakeProcessStatsStub stub;
103 std::unique_ptr<ProcessStatsSender> stats;
104 // Owned by |stats|.
105 FakeProcessStatsAgent* agent = new FakeProcessStatsAgent();
106
107 stub.set_expected_usage_count(10);
108 stub.set_quit_closure(base::Bind(
109 [](std::unique_ptr<ProcessStatsSender>* stats,
110 const FakeProcessStatsStub& stub, const FakeProcessStatsAgent& agent,
111 base::RunLoop* run_loop) -> void {
112 ASSERT_EQ(stub.received().size(), agent.issued_times());
113 stats->reset();
114 run_loop->Quit();
115 },
116 base::Unretained(&stats), base::ConstRef(stub), base::ConstRef(*agent),
117 base::Unretained(&run_loop)));
118 message_loop.task_runner()->PostTask(
119 FROM_HERE,
120 base::Bind(
121 [](std::unique_ptr<ProcessStatsSender>* stats,
122 FakeProcessStatsStub* stub, FakeProcessStatsAgent* agent) -> void {
123 stats->reset(new ProcessStatsSender(
124 stub, base::TimeDelta::FromMilliseconds(1)));
125 (*stats)->AddProcessStatsAgent(
126 std::unique_ptr<ProcessStatsAgent>(agent));
127 },
128 base::Unretained(&stats), base::Unretained(&stub),
129 base::Unretained(agent)));
130 run_loop.Run();
131
132 ASSERT_EQ(stub.received().size(), 10U);
133 for (size_t i = 0; i < stub.received().size(); i++) {
134 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i);
135 }
136 }
137
138 TEST(ProcessStatsSenderTest, MergeUsage) {
139 base::MessageLoop message_loop;
140 base::RunLoop run_loop;
141 FakeProcessStatsStub stub;
142 std::unique_ptr<ProcessStatsSender> stats;
143 // Owned by |stats|.
144 FakeProcessStatsAgent* agent1 = new FakeProcessStatsAgent();
145 FakeProcessStatsAgent* agent2 = new FakeProcessStatsAgent();
146
147 stub.set_expected_usage_count(10);
148 stub.set_quit_closure(base::Bind(
149 [](std::unique_ptr<ProcessStatsSender>* stats,
150 const FakeProcessStatsStub& stub, const FakeProcessStatsAgent& agent1,
151 const FakeProcessStatsAgent& agent2, base::RunLoop* run_loop) -> void {
152 ASSERT_EQ(stub.received().size(), agent1.issued_times());
153 ASSERT_EQ(stub.received().size(), agent2.issued_times());
154 stats->reset();
155 run_loop->Quit();
156 },
157 base::Unretained(&stats), base::ConstRef(stub), base::ConstRef(*agent1),
158 base::ConstRef(*agent2), base::Unretained(&run_loop)));
159 message_loop.task_runner()->PostTask(
160 FROM_HERE,
161 base::Bind(
162 [](std::unique_ptr<ProcessStatsSender>* stats,
163 FakeProcessStatsStub* stub, FakeProcessStatsAgent* agent1,
164 FakeProcessStatsAgent* agent2) -> void {
165 stats->reset(new ProcessStatsSender(
166 stub, base::TimeDelta::FromMilliseconds(1)));
167 (*stats)->AddProcessStatsAgent(
168 std::unique_ptr<ProcessStatsAgent>(agent1));
169 (*stats)->AddProcessStatsAgent(
170 std::unique_ptr<ProcessStatsAgent>(agent2));
171 },
172 base::Unretained(&stats), base::Unretained(&stub),
173 base::Unretained(agent1), base::Unretained(agent2)));
174 run_loop.Run();
175
176 ASSERT_EQ(stub.received().size(), 10U);
177 for (size_t i = 0; i < stub.received().size(); i++) {
178 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i * 2);
179 for (int j = 0; j < stub.received()[i].usages_size(); j++) {
180 FakeProcessStatsAgent::AssertExpected(stub.received()[i].usages().Get(j),
181 i);
182 }
183 }
184 }
185
186 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698