OLD | NEW |
---|---|
(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/base/process_stats.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/location.h" | |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/run_loop.h" | |
15 #include "base/time/time.h" | |
16 #include "remoting/base/current_process_stats_agent.h" | |
17 #include "remoting/base/forward_process_stats_agent.h" | |
18 #include "remoting/base/process_stats_stub.h" | |
19 #include "remoting/proto/process_stats.pb.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace remoting { | |
23 | |
24 namespace { | |
25 | |
26 class MockProcessStatsStub : public ProcessStatsStub { | |
27 public: | |
28 MockProcessStatsStub() = default; | |
29 ~MockProcessStatsStub() 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 } // namespace | |
48 | |
49 TEST(ProcessStatsTest, ReportUsage) { | |
50 base::MessageLoop message_loop; | |
51 base::RunLoop run_loop; | |
52 MockProcessStatsStub stub; | |
53 std::unique_ptr<ProcessStats> stats; | |
54 message_loop.task_runner()->PostTask( | |
55 FROM_HERE, | |
56 base::Bind( | |
57 [](std::unique_ptr<ProcessStats>* stats, | |
58 MockProcessStatsStub* stub) -> void { | |
59 stats->reset( | |
60 new ProcessStats(stub, base::TimeDelta::FromSeconds(1))); | |
61 (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( | |
62 new CurrentProcessStatsAgent("current"))); | |
63 (*stats)->Start(); | |
64 }, | |
65 base::Unretained(&stats), base::Unretained(&stub))); | |
66 message_loop.task_runner()->PostDelayedTask( | |
67 FROM_HERE, | |
68 base::Bind( | |
69 [](std::unique_ptr<ProcessStats>* stats) -> void { stats->reset(); }, | |
70 base::Unretained(&stats)), | |
71 base::TimeDelta::FromSecondsD(2.1)); | |
72 message_loop.task_runner()->PostDelayedTask( | |
73 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSecondsD(2.2)); | |
74 run_loop.Run(); | |
75 | |
76 ASSERT_EQ(2U, stub.received().size()); | |
77 } | |
78 | |
79 TEST(ProcessStatsTest, MergeUsage) { | |
80 base::MessageLoop message_loop; | |
81 base::RunLoop run_loop; | |
82 MockProcessStatsStub stub; | |
83 std::unique_ptr<ProcessStats> stats; | |
84 ForwardProcessStatsAgent* forwarder = nullptr; | |
85 // Owned by |stats|. | |
86 forwarder = new ForwardProcessStatsAgent(); | |
87 message_loop.task_runner()->PostTask( | |
88 FROM_HERE, | |
89 base::Bind( | |
90 [](std::unique_ptr<ProcessStats>* stats, MockProcessStatsStub* stub, | |
91 ForwardProcessStatsAgent* forwarder) -> void { | |
92 stats->reset( | |
93 new ProcessStats(stub, base::TimeDelta::FromSeconds(1))); | |
94 (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( | |
95 new CurrentProcessStatsAgent("current"))); | |
96 (*stats)->AddProcessStatsAgent( | |
97 std::unique_ptr<ProcessStatsAgent>(forwarder)); | |
98 (*stats)->Start(); | |
99 }, | |
100 base::Unretained(&stats), base::Unretained(&stub), | |
101 base::Unretained(forwarder))); | |
102 message_loop.task_runner()->PostDelayedTask( | |
103 FROM_HERE, | |
104 base::Bind( | |
105 [](std::unique_ptr<ProcessStats>* stats, | |
106 ForwardProcessStatsAgent* forwarder) -> void { | |
107 protocol::ProcessResourceUsage fake_usage; | |
108 fake_usage.set_process_name("forward"); | |
109 fake_usage.set_processor_usage(10000000); | |
110 fake_usage.set_working_set_size(SIZE_MAX >> 1); | |
111 fake_usage.set_pagefile_size(SIZE_MAX >> 1); | |
112 forwarder->OnProcessStats(fake_usage); | |
113 }, | |
114 base::Unretained(&stats), base::Unretained(forwarder)), | |
115 base::TimeDelta::FromSecondsD(1.1)); | |
116 message_loop.task_runner()->PostDelayedTask( | |
117 FROM_HERE, | |
118 base::Bind( | |
119 [](std::unique_ptr<ProcessStats>* stats) -> void { stats->reset(); }, | |
120 base::Unretained(&stats)), | |
121 base::TimeDelta::FromSecondsD(3.1)); | |
122 message_loop.task_runner()->PostDelayedTask( | |
123 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSecondsD(3.2)); | |
Sergey Ulanov
2017/04/06 05:55:50
Looks like this test may be flaky if the timer tas
Hzj_jie
2017/04/06 19:41:05
Done.
| |
124 run_loop.Run(); | |
125 | |
126 ASSERT_EQ(3U, stub.received().size()); | |
127 ASSERT_EQ(stub.received()[0].usages_size(), 0); | |
128 ASSERT_LT(stub.received()[0].processor_usage(), 10000000); | |
129 ASSERT_LT(stub.received()[0].working_set_size(), SIZE_MAX >> 1); | |
130 ASSERT_LT(stub.received()[0].pagefile_size(), SIZE_MAX >> 1); | |
131 ASSERT_EQ(stub.received()[1].usages_size(), 2); | |
132 ASSERT_GE(stub.received()[1].processor_usage(), 10000000); | |
133 ASSERT_GE(stub.received()[1].working_set_size(), SIZE_MAX >> 1); | |
134 ASSERT_GE(stub.received()[1].pagefile_size(), SIZE_MAX >> 1); | |
135 ASSERT_EQ(stub.received()[2].usages_size(), 2); | |
136 ASSERT_GE(stub.received()[2].processor_usage(), 10000000); | |
137 ASSERT_GE(stub.received()[2].working_set_size(), SIZE_MAX >> 1); | |
138 ASSERT_GE(stub.received()[2].pagefile_size(), SIZE_MAX >> 1); | |
139 } | |
140 | |
141 } // namespace remoting | |
OLD | NEW |