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> |
| 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/host/current_process_stats_agent.h" |
| 17 #include "remoting/host/forward_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 MockProcessStatsStub : public protocol::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 namespace host { |
| 50 |
| 51 TEST(ProcessStatsSenderTest, ReportUsage) { |
| 52 base::MessageLoop message_loop; |
| 53 base::RunLoop run_loop; |
| 54 MockProcessStatsStub stub; |
| 55 std::unique_ptr<ProcessStatsSender> stats; |
| 56 message_loop.task_runner()->PostTask( |
| 57 FROM_HERE, |
| 58 base::Bind( |
| 59 [](std::unique_ptr<ProcessStatsSender>* stats, |
| 60 MockProcessStatsStub* stub) -> void { |
| 61 stats->reset( |
| 62 new ProcessStatsSender(stub, base::TimeDelta::FromSeconds(1))); |
| 63 (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( |
| 64 new CurrentProcessStatsAgent("current"))); |
| 65 }, |
| 66 base::Unretained(&stats), base::Unretained(&stub))); |
| 67 message_loop.task_runner()->PostDelayedTask( |
| 68 FROM_HERE, |
| 69 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats) |
| 70 -> void { stats->reset(); }, |
| 71 base::Unretained(&stats)), |
| 72 base::TimeDelta::FromSecondsD(2.1)); |
| 73 message_loop.task_runner()->PostDelayedTask( |
| 74 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSecondsD(2.2)); |
| 75 run_loop.Run(); |
| 76 |
| 77 ASSERT_EQ(2U, stub.received().size()); |
| 78 } |
| 79 |
| 80 TEST(ProcessStatsSenderTest, MergeUsage) { |
| 81 base::MessageLoop message_loop; |
| 82 base::RunLoop run_loop; |
| 83 MockProcessStatsStub stub; |
| 84 std::unique_ptr<ProcessStatsSender> stats; |
| 85 ForwardProcessStatsAgent* forwarder = nullptr; |
| 86 // Owned by |stats|. |
| 87 forwarder = new ForwardProcessStatsAgent(); |
| 88 message_loop.task_runner()->PostTask( |
| 89 FROM_HERE, |
| 90 base::Bind( |
| 91 [](std::unique_ptr<ProcessStatsSender>* stats, |
| 92 MockProcessStatsStub* stub, |
| 93 ForwardProcessStatsAgent* forwarder) -> void { |
| 94 stats->reset( |
| 95 new ProcessStatsSender(stub, base::TimeDelta::FromSeconds(1))); |
| 96 (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( |
| 97 new CurrentProcessStatsAgent("current"))); |
| 98 (*stats)->AddProcessStatsAgent( |
| 99 std::unique_ptr<ProcessStatsAgent>(forwarder)); |
| 100 }, |
| 101 base::Unretained(&stats), base::Unretained(&stub), |
| 102 base::Unretained(forwarder))); |
| 103 message_loop.task_runner()->PostDelayedTask( |
| 104 FROM_HERE, |
| 105 base::Bind( |
| 106 [](std::unique_ptr<ProcessStatsSender>* stats, |
| 107 ForwardProcessStatsAgent* forwarder) -> void { |
| 108 protocol::ProcessResourceUsage fake_usage; |
| 109 fake_usage.set_process_name("forward"); |
| 110 fake_usage.set_processor_usage(10000000); |
| 111 fake_usage.set_working_set_size(SIZE_MAX >> 1); |
| 112 fake_usage.set_pagefile_size(SIZE_MAX >> 1); |
| 113 forwarder->OnProcessStats(fake_usage); |
| 114 }, |
| 115 base::Unretained(&stats), base::Unretained(forwarder)), |
| 116 base::TimeDelta::FromSecondsD(1.1)); |
| 117 message_loop.task_runner()->PostDelayedTask( |
| 118 FROM_HERE, |
| 119 base::Bind([](std::unique_ptr<ProcessStatsSender>* stats) |
| 120 -> void { stats->reset(); }, |
| 121 base::Unretained(&stats)), |
| 122 base::TimeDelta::FromSecondsD(3.1)); |
| 123 message_loop.task_runner()->PostDelayedTask( |
| 124 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSecondsD(3.2)); |
| 125 run_loop.Run(); |
| 126 |
| 127 ASSERT_EQ(3U, stub.received().size()); |
| 128 ASSERT_EQ(stub.received()[0].usages_size(), 0); |
| 129 ASSERT_LT(stub.received()[0].processor_usage(), 10000000); |
| 130 ASSERT_LT(stub.received()[0].working_set_size(), SIZE_MAX >> 1); |
| 131 ASSERT_LT(stub.received()[0].pagefile_size(), SIZE_MAX >> 1); |
| 132 ASSERT_EQ(stub.received()[1].usages_size(), 2); |
| 133 ASSERT_GE(stub.received()[1].processor_usage(), 10000000); |
| 134 ASSERT_GE(stub.received()[1].working_set_size(), SIZE_MAX >> 1); |
| 135 ASSERT_GE(stub.received()[1].pagefile_size(), SIZE_MAX >> 1); |
| 136 ASSERT_EQ(stub.received()[2].usages_size(), 2); |
| 137 ASSERT_GE(stub.received()[2].processor_usage(), 10000000); |
| 138 ASSERT_GE(stub.received()[2].working_set_size(), SIZE_MAX >> 1); |
| 139 ASSERT_GE(stub.received()[2].pagefile_size(), SIZE_MAX >> 1); |
| 140 } |
| 141 |
| 142 } // namespace host |
| 143 } // namespace remoting |
OLD | NEW |