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

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, 8 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 }
35
36 const std::vector<protocol::AggregatedProcessResourceUsage>& received()
37 const {
38 return received_;
39 }
40
41 void Clear() { received_.clear(); }
42
43 private:
44 std::vector<protocol::AggregatedProcessResourceUsage> received_;
45 };
46
47 class FakeProcessStatsAgent : public ProcessStatsAgent {
48 public:
49 FakeProcessStatsAgent() = default;
50 ~FakeProcessStatsAgent() override = default;
51
52 protocol::ProcessResourceUsage GetResourceUsage() override {
53 protocol::ProcessResourceUsage usage;
54 usage.set_process_name("FakeProcessStatsAgent");
55 usage.set_processor_usage(index_);
56 usage.set_working_set_size(index_);
57 usage.set_pagefile_size(index_);
58 index_++;
59 return usage;
60 }
61
62 // Checks the expected usage based on index.
63 static void AssertExpected(
64 const protocol::AggregatedProcessResourceUsage& usage,
65 size_t index) {
66 ASSERT_EQ(usage.processor_usage(), index);
67 ASSERT_EQ(usage.working_set_size(), index);
68 ASSERT_EQ(usage.pagefile_size(), index);
69 }
70
71 static void AssertExpected(
72 const protocol::ProcessResourceUsage& usage,
73 size_t index) {
74 ASSERT_EQ(usage.processor_usage(), index);
75 ASSERT_EQ(usage.working_set_size(), index);
76 ASSERT_EQ(usage.pagefile_size(), index);
77 }
78
79 size_t issued_times() const {
80 return index_;
81 }
82
83 private:
84 size_t index_ = 0;
85 };
86
87 } // namespace
88
89 TEST(ProcessStatsSenderTest, ReportUsage) {
90 base::MessageLoop message_loop;
91 base::RunLoop run_loop;
92 FakeProcessStatsStub stub;
93 std::unique_ptr<ProcessStatsSender> stats;
94 // Owned by |stats|.
95 FakeProcessStatsAgent* agent = new FakeProcessStatsAgent();
96 message_loop.task_runner()->PostTask(
97 FROM_HERE,
98 base::Bind(
99 [](std::unique_ptr<ProcessStatsSender>* stats,
100 FakeProcessStatsStub* stub,
101 FakeProcessStatsAgent* agent) -> void {
102 stats->reset(new ProcessStatsSender(
103 stub, base::TimeDelta::FromMilliseconds(1)));
104 (*stats)->AddProcessStatsAgent(
105 std::unique_ptr<ProcessStatsAgent>(agent));
106 },
107 base::Unretained(&stats),
108 base::Unretained(&stub),
109 base::Unretained(agent)));
110 message_loop.task_runner()->PostDelayedTask(
111 FROM_HERE,
112 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats,
113 const FakeProcessStatsStub& stub,
114 const FakeProcessStatsAgent& agent) -> void {
115 ASSERT_EQ(stub.received().size(), agent.issued_times());
116 stats->reset();
117 },
118 base::Unretained(&stats),
119 base::ConstRef(stub),
120 base::ConstRef(*agent)),
121 base::TimeDelta::FromMilliseconds(100));
122 message_loop.task_runner()->PostDelayedTask(
123 FROM_HERE, run_loop.QuitClosure(),
124 base::TimeDelta::FromMilliseconds(101));
125 run_loop.Run();
126
127 ASSERT_GT(stub.received().size(), 0U);
128 for (size_t i = 0; i < stub.received().size(); i++) {
129 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i);
130 }
131 }
132
133 TEST(ProcessStatsSenderTest, MergeUsage) {
134 base::MessageLoop message_loop;
135 base::RunLoop run_loop;
136 FakeProcessStatsStub stub;
137 std::unique_ptr<ProcessStatsSender> stats;
138 // Owned by |stats|.
139 FakeProcessStatsAgent* agent1 = new FakeProcessStatsAgent();
140 FakeProcessStatsAgent* agent2 = new FakeProcessStatsAgent();
141 message_loop.task_runner()->PostTask(
142 FROM_HERE,
143 base::Bind(
144 [](std::unique_ptr<ProcessStatsSender>* stats,
145 FakeProcessStatsStub* stub,
146 FakeProcessStatsAgent* agent1,
147 FakeProcessStatsAgent* agent2) -> void {
148 stats->reset(new ProcessStatsSender(
149 stub, base::TimeDelta::FromMilliseconds(1)));
150 (*stats)->AddProcessStatsAgent(
151 std::unique_ptr<ProcessStatsAgent>(agent1));
152 (*stats)->AddProcessStatsAgent(
153 std::unique_ptr<ProcessStatsAgent>(agent2));
154 },
155 base::Unretained(&stats),
156 base::Unretained(&stub),
157 base::Unretained(agent1),
158 base::Unretained(agent2)));
159 message_loop.task_runner()->PostDelayedTask(
160 FROM_HERE,
161 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats,
162 const FakeProcessStatsStub& stub,
163 const FakeProcessStatsAgent& agent1,
164 const FakeProcessStatsAgent& agent2) -> void {
165 ASSERT_EQ(stub.received().size(), agent1.issued_times());
166 ASSERT_EQ(stub.received().size(), agent2.issued_times());
167 stats->reset();
168 },
169 base::Unretained(&stats),
170 base::ConstRef(stub),
171 base::ConstRef(*agent1),
172 base::ConstRef(*agent2)),
173 base::TimeDelta::FromMilliseconds(100));
174 message_loop.task_runner()->PostDelayedTask(
175 FROM_HERE, run_loop.QuitClosure(),
176 base::TimeDelta::FromMilliseconds(101));
177 run_loop.Run();
178
179 ASSERT_GT(stub.received().size(), 0U);
180 for (size_t i = 0; i < stub.received().size(); i++) {
181 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i * 2);
182 for (int j = 0; j < stub.received()[i].usages_size(); j++) {
183 FakeProcessStatsAgent::AssertExpected(
184 stub.received()[i].usages().Get(j), i);
185 }
186 }
187 }
188
189 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698