OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/process_stats_sender.h" | 5 #include "remoting/host/process_stats_sender.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "remoting/host/process_stats_agent.h" | 17 #include "remoting/host/process_stats_agent.h" |
18 #include "remoting/proto/process_stats.pb.h" | 18 #include "remoting/proto/process_stats.pb.h" |
19 #include "remoting/protocol/process_stats_stub.h" | 19 #include "remoting/protocol/fake_process_stats_stub.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 namespace remoting { | 22 namespace remoting { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 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 { | 26 class FakeProcessStatsAgent : public ProcessStatsAgent { |
61 public: | 27 public: |
62 FakeProcessStatsAgent() = default; | 28 FakeProcessStatsAgent() = default; |
63 ~FakeProcessStatsAgent() override = default; | 29 ~FakeProcessStatsAgent() override = default; |
64 | 30 |
65 protocol::ProcessResourceUsage GetResourceUsage() override { | 31 protocol::ProcessResourceUsage GetResourceUsage() override { |
66 protocol::ProcessResourceUsage usage; | 32 protocol::ProcessResourceUsage usage; |
67 usage.set_process_name("FakeProcessStatsAgent"); | 33 usage.set_process_name("FakeProcessStatsAgent"); |
68 usage.set_processor_usage(index_); | 34 usage.set_processor_usage(index_); |
69 usage.set_working_set_size(index_); | 35 usage.set_working_set_size(index_); |
(...skipping 22 matching lines...) Expand all Loading... |
92 | 58 |
93 private: | 59 private: |
94 size_t index_ = 0; | 60 size_t index_ = 0; |
95 }; | 61 }; |
96 | 62 |
97 } // namespace | 63 } // namespace |
98 | 64 |
99 TEST(ProcessStatsSenderTest, ReportUsage) { | 65 TEST(ProcessStatsSenderTest, ReportUsage) { |
100 base::MessageLoop message_loop; | 66 base::MessageLoop message_loop; |
101 base::RunLoop run_loop; | 67 base::RunLoop run_loop; |
102 FakeProcessStatsStub stub; | 68 protocol::FakeProcessStatsStub stub; |
103 std::unique_ptr<ProcessStatsSender> stats; | 69 std::unique_ptr<ProcessStatsSender> stats; |
104 FakeProcessStatsAgent agent; | 70 FakeProcessStatsAgent agent; |
105 | 71 |
106 stub.set_expected_usage_count(10); | 72 stub.set_expected_usage_count(10); |
107 stub.set_quit_closure(base::Bind( | 73 stub.set_quit_closure(base::Bind( |
108 [](std::unique_ptr<ProcessStatsSender>* stats, | 74 [](std::unique_ptr<ProcessStatsSender>* stats, |
109 const FakeProcessStatsStub& stub, const FakeProcessStatsAgent& agent, | 75 const protocol::FakeProcessStatsStub& stub, |
| 76 const FakeProcessStatsAgent& agent, |
110 base::RunLoop* run_loop) -> void { | 77 base::RunLoop* run_loop) -> void { |
111 ASSERT_EQ(stub.received().size(), agent.issued_times()); | 78 ASSERT_EQ(stub.received().size(), agent.issued_times()); |
112 stats->reset(); | 79 stats->reset(); |
113 run_loop->Quit(); | 80 run_loop->Quit(); |
114 }, | 81 }, |
115 base::Unretained(&stats), base::ConstRef(stub), base::ConstRef(agent), | 82 base::Unretained(&stats), base::ConstRef(stub), base::ConstRef(agent), |
116 base::Unretained(&run_loop))); | 83 base::Unretained(&run_loop))); |
117 message_loop.task_runner()->PostTask( | 84 message_loop.task_runner()->PostTask( |
118 FROM_HERE, | 85 FROM_HERE, |
119 base::Bind( | 86 base::Bind( |
120 [](std::unique_ptr<ProcessStatsSender>* stats, | 87 [](std::unique_ptr<ProcessStatsSender>* stats, |
121 FakeProcessStatsStub* stub, FakeProcessStatsAgent* agent) -> void { | 88 protocol::FakeProcessStatsStub* stub, |
| 89 FakeProcessStatsAgent* agent) -> void { |
122 stats->reset(new ProcessStatsSender( | 90 stats->reset(new ProcessStatsSender( |
123 stub, base::TimeDelta::FromMilliseconds(1), { agent })); | 91 stub, base::TimeDelta::FromMilliseconds(1), |
| 92 { agent, nullptr })); |
124 }, | 93 }, |
125 base::Unretained(&stats), base::Unretained(&stub), | 94 base::Unretained(&stats), base::Unretained(&stub), |
126 base::Unretained(&agent))); | 95 base::Unretained(&agent))); |
127 run_loop.Run(); | 96 run_loop.Run(); |
128 | 97 |
129 ASSERT_EQ(stub.received().size(), 10U); | 98 ASSERT_EQ(stub.received().size(), 10U); |
130 for (size_t i = 0; i < stub.received().size(); i++) { | 99 for (size_t i = 0; i < stub.received().size(); i++) { |
131 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i); | 100 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i); |
132 } | 101 } |
133 } | 102 } |
134 | 103 |
135 TEST(ProcessStatsSenderTest, MergeUsage) { | 104 TEST(ProcessStatsSenderTest, MergeUsage) { |
136 base::MessageLoop message_loop; | 105 base::MessageLoop message_loop; |
137 base::RunLoop run_loop; | 106 base::RunLoop run_loop; |
138 FakeProcessStatsStub stub; | 107 protocol::FakeProcessStatsStub stub; |
139 std::unique_ptr<ProcessStatsSender> stats; | 108 std::unique_ptr<ProcessStatsSender> stats; |
140 // Owned by |stats|. | 109 // Owned by |stats|. |
141 FakeProcessStatsAgent agent1; | 110 FakeProcessStatsAgent agent1; |
142 FakeProcessStatsAgent agent2; | 111 FakeProcessStatsAgent agent2; |
143 | 112 |
144 stub.set_expected_usage_count(10); | 113 stub.set_expected_usage_count(10); |
145 stub.set_quit_closure(base::Bind( | 114 stub.set_quit_closure(base::Bind( |
146 [](std::unique_ptr<ProcessStatsSender>* stats, | 115 [](std::unique_ptr<ProcessStatsSender>* stats, |
147 const FakeProcessStatsStub& stub, const FakeProcessStatsAgent& agent1, | 116 const protocol::FakeProcessStatsStub& stub, |
148 const FakeProcessStatsAgent& agent2, base::RunLoop* run_loop) -> void { | 117 const FakeProcessStatsAgent& agent1, |
| 118 const FakeProcessStatsAgent& agent2, |
| 119 base::RunLoop* run_loop) -> void { |
149 ASSERT_EQ(stub.received().size(), agent1.issued_times()); | 120 ASSERT_EQ(stub.received().size(), agent1.issued_times()); |
150 ASSERT_EQ(stub.received().size(), agent2.issued_times()); | 121 ASSERT_EQ(stub.received().size(), agent2.issued_times()); |
151 stats->reset(); | 122 stats->reset(); |
152 run_loop->Quit(); | 123 run_loop->Quit(); |
153 }, | 124 }, |
154 base::Unretained(&stats), base::ConstRef(stub), base::ConstRef(agent1), | 125 base::Unretained(&stats), base::ConstRef(stub), base::ConstRef(agent1), |
155 base::ConstRef(agent2), base::Unretained(&run_loop))); | 126 base::ConstRef(agent2), base::Unretained(&run_loop))); |
156 message_loop.task_runner()->PostTask( | 127 message_loop.task_runner()->PostTask( |
157 FROM_HERE, | 128 FROM_HERE, |
158 base::Bind( | 129 base::Bind( |
159 [](std::unique_ptr<ProcessStatsSender>* stats, | 130 [](std::unique_ptr<ProcessStatsSender>* stats, |
160 FakeProcessStatsStub* stub, FakeProcessStatsAgent* agent1, | 131 protocol::FakeProcessStatsStub* stub, |
| 132 FakeProcessStatsAgent* agent1, |
161 FakeProcessStatsAgent* agent2) -> void { | 133 FakeProcessStatsAgent* agent2) -> void { |
162 stats->reset(new ProcessStatsSender( | 134 stats->reset(new ProcessStatsSender( |
163 stub, base::TimeDelta::FromMilliseconds(1), | 135 stub, base::TimeDelta::FromMilliseconds(1), |
164 { agent1, agent2 } )); | 136 { agent1, nullptr, agent2, nullptr, nullptr } )); |
165 }, | 137 }, |
166 base::Unretained(&stats), base::Unretained(&stub), | 138 base::Unretained(&stats), base::Unretained(&stub), |
167 base::Unretained(&agent1), base::Unretained(&agent2))); | 139 base::Unretained(&agent1), base::Unretained(&agent2))); |
168 run_loop.Run(); | 140 run_loop.Run(); |
169 | 141 |
170 ASSERT_EQ(stub.received().size(), 10U); | 142 ASSERT_EQ(stub.received().size(), 10U); |
171 for (size_t i = 0; i < stub.received().size(); i++) { | 143 for (size_t i = 0; i < stub.received().size(); i++) { |
172 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i * 2); | 144 FakeProcessStatsAgent::AssertExpected(stub.received()[i], i * 2); |
173 for (int j = 0; j < stub.received()[i].usages_size(); j++) { | 145 for (int j = 0; j < stub.received()[i].usages_size(); j++) { |
174 FakeProcessStatsAgent::AssertExpected( | 146 FakeProcessStatsAgent::AssertExpected( |
175 stub.received()[i].usages().Get(j), i); | 147 stub.received()[i].usages().Get(j), i); |
176 } | 148 } |
177 } | 149 } |
178 } | 150 } |
179 | 151 |
180 } // namespace remoting | 152 } // namespace remoting |
OLD | NEW |