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

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: Update the test case 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 if (received_.size() >= static_cast<size_t>(expected_usage_received_) &&
joedow 2017/04/26 16:44:06 Would this ever be a problem if you expected 2 usa
Hzj_jie 2017/04/26 20:47:19 No, I do not think it's possible, since we push_ba
35 !quit_closure_.is_null()) {
36 quit_closure_.Run();
joedow 2017/04/26 16:44:06 Do you want to immediately run this closure? You
Hzj_jie 2017/04/26 20:47:19 I think running it immediately should be a better
joedow 2017/04/26 23:48:01 This callback is only used for testing so I don't
Hzj_jie 2017/04/27 02:21:33 Sorry, I have not followed. IMO, if the quit_closu
37 }
38 }
39
40 const std::vector<protocol::AggregatedProcessResourceUsage>& received()
41 const {
42 return received_;
43 }
44
45 void SetQuitWhen(int expected_usage_received, base::Closure quit_closure) {
joedow 2017/04/26 16:44:06 The names here are a little confusing. Instead of
Hzj_jie 2017/04/26 20:47:20 Done. But no matter how many agents, the stub rece
46 expected_usage_received_ = expected_usage_received;
joedow 2017/04/26 16:44:06 nit: rename expected_usage_received_ to expected_u
Hzj_jie 2017/04/26 20:47:19 Done.
47 quit_closure_ = quit_closure;
48 }
49
50 private:
51 std::vector<protocol::AggregatedProcessResourceUsage> received_;
52 int expected_usage_received_ = 0;
joedow 2017/04/26 16:44:06 If this member were a size_t, could you remove the
Hzj_jie 2017/04/26 20:47:20 I believe the code style discourages the usage of
joedow 2017/04/26 23:48:01 Coding style discourages it if your intention is t
Hzj_jie 2017/04/27 02:21:33 Done.
53 base::Closure quit_closure_;
54 };
55
56 class FakeProcessStatsAgent : public ProcessStatsAgent {
57 public:
58 FakeProcessStatsAgent() = default;
59 ~FakeProcessStatsAgent() override = default;
60
61 protocol::ProcessResourceUsage GetResourceUsage() override {
62 protocol::ProcessResourceUsage usage;
63 usage.set_process_name("FakeProcessStatsAgent");
64 usage.set_processor_usage(index_);
65 usage.set_working_set_size(index_);
66 usage.set_pagefile_size(index_);
67 index_++;
68 return usage;
69 }
70
71 // Checks the expected usage based on index.
72 static void AssertExpected(
73 const protocol::AggregatedProcessResourceUsage& usage,
74 size_t index) {
75 ASSERT_EQ(usage.processor_usage(), index);
76 ASSERT_EQ(usage.working_set_size(), index);
77 ASSERT_EQ(usage.pagefile_size(), index);
78 }
79
80 static void AssertExpected(
81 const protocol::ProcessResourceUsage& usage,
82 size_t index) {
83 ASSERT_EQ(usage.processor_usage(), index);
84 ASSERT_EQ(usage.working_set_size(), index);
85 ASSERT_EQ(usage.pagefile_size(), index);
86 }
87
88 size_t issued_times() const {
89 return index_;
90 }
91
92 private:
93 size_t index_ = 0;
94 };
95
96 } // namespace
97
98 TEST(ProcessStatsSenderTest, ReportUsage) {
99 base::MessageLoop message_loop;
100 base::RunLoop run_loop;
101 FakeProcessStatsStub stub;
102 std::unique_ptr<ProcessStatsSender> stats;
103 // Owned by |stats|.
104 FakeProcessStatsAgent* agent = new FakeProcessStatsAgent();
105
106 stub.SetQuitWhen(10,
107 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats,
108 const FakeProcessStatsStub& stub,
109 const FakeProcessStatsAgent& agent,
110 base::RunLoop* run_loop) -> void {
111 ASSERT_EQ(stub.received().size(), agent.issued_times());
112 stats->reset();
113 run_loop->Quit();
114 },
115 base::Unretained(&stats),
116 base::ConstRef(stub),
117 base::ConstRef(*agent),
118 base::Unretained(&run_loop)));
119 message_loop.task_runner()->PostTask(
120 FROM_HERE,
121 base::Bind(
122 [](std::unique_ptr<ProcessStatsSender>* stats,
123 FakeProcessStatsStub* stub,
124 FakeProcessStatsAgent* agent) -> void {
125 stats->reset(new ProcessStatsSender(
126 stub, base::TimeDelta::FromMilliseconds(1)));
127 (*stats)->AddProcessStatsAgent(
128 std::unique_ptr<ProcessStatsAgent>(agent));
129 },
130 base::Unretained(&stats),
131 base::Unretained(&stub),
132 base::Unretained(agent)));
133 run_loop.Run();
134
135 ASSERT_GT(stub.received().size(), 0U);
joedow 2017/04/26 16:44:06 You can use a specific comparison now (since you a
Hzj_jie 2017/04/26 20:47:19 Sorry, I simply forgot to update it.
136 for (size_t i = 0; i < stub.received().size(); i++) {
137 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i);
138 }
139 }
140
141 TEST(ProcessStatsSenderTest, MergeUsage) {
142 base::MessageLoop message_loop;
143 base::RunLoop run_loop;
144 FakeProcessStatsStub stub;
145 std::unique_ptr<ProcessStatsSender> stats;
146 // Owned by |stats|.
147 FakeProcessStatsAgent* agent1 = new FakeProcessStatsAgent();
148 FakeProcessStatsAgent* agent2 = new FakeProcessStatsAgent();
149
150 stub.SetQuitWhen(10,
151 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats,
152 const FakeProcessStatsStub& stub,
153 const FakeProcessStatsAgent& agent1,
154 const FakeProcessStatsAgent& agent2,
155 base::RunLoop* run_loop) -> void {
156 ASSERT_EQ(stub.received().size(), agent1.issued_times());
157 ASSERT_EQ(stub.received().size(), agent2.issued_times());
158 stats->reset();
159 run_loop->Quit();
160 },
161 base::Unretained(&stats),
162 base::ConstRef(stub),
163 base::ConstRef(*agent1),
164 base::ConstRef(*agent2),
165 base::Unretained(&run_loop)));
166 message_loop.task_runner()->PostTask(
167 FROM_HERE,
168 base::Bind(
169 [](std::unique_ptr<ProcessStatsSender>* stats,
170 FakeProcessStatsStub* stub,
171 FakeProcessStatsAgent* agent1,
172 FakeProcessStatsAgent* agent2) -> void {
173 stats->reset(new ProcessStatsSender(
174 stub, base::TimeDelta::FromMilliseconds(1)));
175 (*stats)->AddProcessStatsAgent(
176 std::unique_ptr<ProcessStatsAgent>(agent1));
177 (*stats)->AddProcessStatsAgent(
178 std::unique_ptr<ProcessStatsAgent>(agent2));
179 },
180 base::Unretained(&stats),
181 base::Unretained(&stub),
182 base::Unretained(agent1),
183 base::Unretained(agent2)));
184 run_loop.Run();
185
186 ASSERT_GT(stub.received().size(), 0U);
187 for (size_t i = 0; i < stub.received().size(); i++) {
188 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i * 2);
189 for (int j = 0; j < stub.received()[i].usages_size(); j++) {
190 FakeProcessStatsAgent::AssertExpected(
191 stub.received()[i].usages().Get(j), i);
192 }
193 }
194 }
195
196 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698