Chromium Code Reviews| 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/host/process_stats_sender.h" | |
| 6 | |
| 7 #include <stdint.h> | |
|
joedow
2017/04/24 17:16:09
nit: #include <cstdint> instead of old C style hea
Hzj_jie
2017/04/25 00:17:54
It looks like stdint.h is preferred in Chromium: i
joedow
2017/04/25 15:57:28
It is one of the newly allowed C++11 features (htt
Hzj_jie
2017/04/25 22:19:19
I believe it's an allowed feature, but a standard
| |
| 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/proto/process_stats.pb.h" | |
| 18 #include "remoting/protocol/process_stats_stub.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace remoting { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 class MockProcessStatsStub : public protocol::ProcessStatsStub { | |
|
joedow
2017/04/24 17:16:09
Rename MockProcessStatsStub to FakeProcessStatsStu
Hzj_jie
2017/04/25 00:17:54
Oh, you have made two similar comments in my two c
joedow
2017/04/25 15:57:28
They have different meanings when it comes to test
Hzj_jie
2017/04/25 22:19:19
Thank you for the explanation, very clear.
| |
| 26 public: | |
| 27 MockProcessStatsStub() = default; | |
| 28 ~MockProcessStatsStub() override = default; | |
| 29 | |
| 30 void OnProcessStats( | |
| 31 const protocol::AggregatedProcessResourceUsage& usage) override { | |
| 32 received_.push_back(usage); | |
| 33 } | |
| 34 | |
| 35 const std::vector<protocol::AggregatedProcessResourceUsage>& received() | |
| 36 const { | |
| 37 return received_; | |
| 38 } | |
| 39 | |
| 40 void Clear() { received_.clear(); } | |
| 41 | |
| 42 private: | |
| 43 std::vector<protocol::AggregatedProcessResourceUsage> received_; | |
| 44 }; | |
| 45 | |
| 46 class MockProcessStatsAgent : public ProcessStatsAgent { | |
|
joedow
2017/04/24 17:16:09
Rename to FakeProcessStatsAgent or TestProcessStat
Hzj_jie
2017/04/25 00:17:54
Done.
| |
| 47 public: | |
| 48 MockProcessStatsAgent() = default; | |
| 49 ~MockProcessStatsAgent() override = default; | |
| 50 | |
| 51 protocol::ProcessResourceUsage GetResourceUsage() override { | |
| 52 protocol::ProcessResourceUsage usage; | |
| 53 usage.set_process_name("MockProcessStatsAgent"); | |
| 54 usage.set_processor_usage(index_); | |
| 55 usage.set_working_set_size(index_); | |
| 56 usage.set_pagefile_size(index_); | |
| 57 index_++; | |
| 58 return usage; | |
| 59 } | |
| 60 | |
| 61 // Asserts the input |usage| contains data generated by the |index|-th (starts | |
| 62 // from 0) GetResourceUsage(). | |
|
joedow
2017/04/24 17:16:09
I'm a little confused by this term '|index|-th', m
Hzj_jie
2017/04/25 00:17:54
Done.
| |
| 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 size_t issued_times() const { | |
| 72 return index_; | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 size_t index_ = 0; | |
| 77 }; | |
| 78 | |
| 79 } // namespace | |
| 80 | |
| 81 TEST(ProcessStatsSenderTest, ReportUsage) { | |
| 82 base::MessageLoop message_loop; | |
| 83 base::RunLoop run_loop; | |
| 84 MockProcessStatsStub stub; | |
| 85 std::unique_ptr<ProcessStatsSender> stats; | |
| 86 // Owned by |stats|. | |
| 87 MockProcessStatsAgent* agent = new MockProcessStatsAgent(); | |
| 88 message_loop.task_runner()->PostTask( | |
| 89 FROM_HERE, | |
| 90 base::Bind( | |
| 91 [](std::unique_ptr<ProcessStatsSender>* stats, | |
| 92 MockProcessStatsStub* stub, | |
| 93 MockProcessStatsAgent* agent) -> void { | |
| 94 stats->reset(new ProcessStatsSender( | |
| 95 stub, base::TimeDelta::FromMilliseconds(1))); | |
| 96 (*stats)->AddProcessStatsAgent( | |
| 97 std::unique_ptr<ProcessStatsAgent>(agent)); | |
| 98 }, | |
| 99 base::Unretained(&stats), | |
| 100 base::Unretained(&stub), | |
| 101 base::Unretained(agent))); | |
| 102 message_loop.task_runner()->PostDelayedTask( | |
| 103 FROM_HERE, | |
| 104 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats, | |
| 105 const MockProcessStatsStub& stub, | |
| 106 const MockProcessStatsAgent& agent) -> void { | |
| 107 ASSERT_EQ(stub.received().size(), agent.issued_times()); | |
| 108 stats->reset(); | |
| 109 }, | |
| 110 base::Unretained(&stats), | |
| 111 base::ConstRef(stub), | |
| 112 base::ConstRef(*agent)), | |
| 113 base::TimeDelta::FromMilliseconds(100)); | |
|
joedow
2017/04/24 17:16:09
You should avoid relying on intervals (i.e. 100ms)
Hzj_jie
2017/04/25 00:17:54
It's a little bit hard to do so. Indeed this test
joedow
2017/04/25 15:57:28
It might spend 100ms if it was running on a machin
Hzj_jie
2017/04/25 22:19:19
I agree. A simple choice is to remove the ASSERT_G
| |
| 114 message_loop.task_runner()->PostDelayedTask( | |
| 115 FROM_HERE, run_loop.QuitClosure(), | |
| 116 base::TimeDelta::FromMilliseconds(101)); | |
| 117 run_loop.Run(); | |
| 118 | |
| 119 ASSERT_GT(stub.received().size(), 0U); | |
|
joedow
2017/04/24 17:16:09
Can you assert a specific value (rather than a gen
Hzj_jie
2017/04/25 00:17:54
See my previous comment.
| |
| 120 for (size_t i = 0; i < stub.received().size(); i++) { | |
| 121 MockProcessStatsAgent::AssertExpected(stub.received()[i], i); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 TEST(ProcessStatsSenderTest, MergeUsage) { | |
| 126 base::MessageLoop message_loop; | |
| 127 base::RunLoop run_loop; | |
| 128 MockProcessStatsStub stub; | |
| 129 std::unique_ptr<ProcessStatsSender> stats; | |
| 130 // Owned by |stats|. | |
| 131 MockProcessStatsAgent* agent1 = new MockProcessStatsAgent(); | |
| 132 MockProcessStatsAgent* agent2 = new MockProcessStatsAgent(); | |
| 133 message_loop.task_runner()->PostTask( | |
| 134 FROM_HERE, | |
| 135 base::Bind( | |
| 136 [](std::unique_ptr<ProcessStatsSender>* stats, | |
| 137 MockProcessStatsStub* stub, | |
| 138 MockProcessStatsAgent* agent1, | |
| 139 MockProcessStatsAgent* agent2) -> void { | |
| 140 stats->reset(new ProcessStatsSender( | |
| 141 stub, base::TimeDelta::FromMilliseconds(1))); | |
| 142 (*stats)->AddProcessStatsAgent( | |
| 143 std::unique_ptr<ProcessStatsAgent>(agent1)); | |
| 144 (*stats)->AddProcessStatsAgent( | |
| 145 std::unique_ptr<ProcessStatsAgent>(agent2)); | |
| 146 }, | |
| 147 base::Unretained(&stats), | |
| 148 base::Unretained(&stub), | |
| 149 base::Unretained(agent1), | |
| 150 base::Unretained(agent2))); | |
| 151 message_loop.task_runner()->PostDelayedTask( | |
| 152 FROM_HERE, | |
| 153 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats, | |
| 154 const MockProcessStatsStub& stub, | |
| 155 const MockProcessStatsAgent& agent1, | |
| 156 const MockProcessStatsAgent& agent2) -> void { | |
| 157 ASSERT_EQ(stub.received().size(), agent1.issued_times()); | |
| 158 ASSERT_EQ(stub.received().size(), agent2.issued_times()); | |
| 159 stats->reset(); | |
| 160 }, | |
| 161 base::Unretained(&stats), | |
| 162 base::ConstRef(stub), | |
| 163 base::ConstRef(*agent1), | |
| 164 base::ConstRef(*agent2)), | |
| 165 base::TimeDelta::FromMilliseconds(100)); | |
| 166 message_loop.task_runner()->PostDelayedTask( | |
| 167 FROM_HERE, run_loop.QuitClosure(), | |
| 168 base::TimeDelta::FromMilliseconds(101)); | |
| 169 run_loop.Run(); | |
| 170 | |
| 171 ASSERT_GT(stub.received().size(), 0U); | |
| 172 for (size_t i = 0; i < stub.received().size(); i++) { | |
| 173 MockProcessStatsAgent::AssertExpected(stub.received()[i], i * 2); | |
|
joedow
2017/04/24 17:16:09
IIUC you are verifying the aggregate, but can you
Hzj_jie
2017/04/25 00:17:54
I think it's covered by ReportUsage test already.
joedow
2017/04/25 15:57:28
Thanks for adding it. It isn't necessarily covere
Hzj_jie
2017/04/25 22:19:19
Oh, reasonable.
| |
| 174 } | |
| 175 } | |
| 176 | |
| 177 } // namespace remoting | |
| OLD | NEW |