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

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: thread_checker_ should be correctly constructed 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
« no previous file with comments | « remoting/host/process_stats_sender.cc ('k') | remoting/host/process_stats_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(), expected_usage_count_);
35 DCHECK(!quit_closure_.is_null());
36 if (received_.size() == 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(size_t expected_usage_count) {
51 expected_usage_count_ = expected_usage_count;
52 }
53
54 private:
55 std::vector<protocol::AggregatedProcessResourceUsage> received_;
56 size_t 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 FakeProcessStatsAgent agent;
105
106 stub.set_expected_usage_count(10);
107 stub.set_quit_closure(base::Bind(
108 [](std::unique_ptr<ProcessStatsSender>* stats,
109 const FakeProcessStatsStub& stub, 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), base::ConstRef(stub), base::ConstRef(agent),
116 base::Unretained(&run_loop)));
117 message_loop.task_runner()->PostTask(
118 FROM_HERE,
119 base::Bind(
120 [](std::unique_ptr<ProcessStatsSender>* stats,
121 FakeProcessStatsStub* stub, FakeProcessStatsAgent* agent) -> void {
122 stats->reset(new ProcessStatsSender(
123 stub, base::TimeDelta::FromMilliseconds(1), { agent }));
124 },
125 base::Unretained(&stats), base::Unretained(&stub),
126 base::Unretained(&agent)));
127 run_loop.Run();
128
129 ASSERT_EQ(stub.received().size(), 10U);
130 for (size_t i = 0; i < stub.received().size(); i++) {
131 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i);
132 }
133 }
134
135 TEST(ProcessStatsSenderTest, MergeUsage) {
136 base::MessageLoop message_loop;
137 base::RunLoop run_loop;
138 FakeProcessStatsStub stub;
139 std::unique_ptr<ProcessStatsSender> stats;
140 // Owned by |stats|.
141 FakeProcessStatsAgent agent1;
142 FakeProcessStatsAgent agent2;
143
144 stub.set_expected_usage_count(10);
145 stub.set_quit_closure(base::Bind(
146 [](std::unique_ptr<ProcessStatsSender>* stats,
147 const FakeProcessStatsStub& stub, const FakeProcessStatsAgent& agent1,
148 const FakeProcessStatsAgent& agent2, base::RunLoop* run_loop) -> void {
149 ASSERT_EQ(stub.received().size(), agent1.issued_times());
150 ASSERT_EQ(stub.received().size(), agent2.issued_times());
151 stats->reset();
152 run_loop->Quit();
153 },
154 base::Unretained(&stats), base::ConstRef(stub), base::ConstRef(agent1),
155 base::ConstRef(agent2), base::Unretained(&run_loop)));
156 message_loop.task_runner()->PostTask(
157 FROM_HERE,
158 base::Bind(
159 [](std::unique_ptr<ProcessStatsSender>* stats,
160 FakeProcessStatsStub* stub, FakeProcessStatsAgent* agent1,
161 FakeProcessStatsAgent* agent2) -> void {
162 stats->reset(new ProcessStatsSender(
163 stub, base::TimeDelta::FromMilliseconds(1),
164 { agent1, agent2 } ));
165 },
166 base::Unretained(&stats), base::Unretained(&stub),
167 base::Unretained(&agent1), base::Unretained(&agent2)));
168 run_loop.Run();
169
170 ASSERT_EQ(stub.received().size(), 10U);
171 for (size_t i = 0; i < stub.received().size(); i++) {
172 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i * 2);
173 for (int j = 0; j < stub.received()[i].usages_size(); j++) {
174 FakeProcessStatsAgent::AssertExpected(
175 stub.received()[i].usages().Get(j), i);
176 }
177 }
178 }
179
180 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/process_stats_sender.cc ('k') | remoting/host/process_stats_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698