Index: remoting/host/process_stats_sender_unittest.cc |
diff --git a/remoting/host/process_stats_sender_unittest.cc b/remoting/host/process_stats_sender_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6c6b0bdff505eba2a58408e45e164e51f1aaca1b |
--- /dev/null |
+++ b/remoting/host/process_stats_sender_unittest.cc |
@@ -0,0 +1,143 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/host/process_stats_sender.h" |
+ |
+#include <stdint.h> |
+ |
+#include <vector> |
+ |
+#include "base/bind.h" |
+#include "base/location.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/time/time.h" |
+#include "remoting/host/current_process_stats_agent.h" |
+#include "remoting/host/forward_process_stats_agent.h" |
+#include "remoting/proto/process_stats.pb.h" |
+#include "remoting/protocol/process_stats_stub.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace remoting { |
+ |
+namespace { |
+ |
+class MockProcessStatsStub : public protocol::ProcessStatsStub { |
+ public: |
+ MockProcessStatsStub() = default; |
+ ~MockProcessStatsStub() override = default; |
+ |
+ void OnProcessStats( |
+ const protocol::AggregatedProcessResourceUsage& usage) override { |
+ received_.push_back(usage); |
+ } |
+ |
+ const std::vector<protocol::AggregatedProcessResourceUsage>& received() |
+ const { |
+ return received_; |
+ } |
+ |
+ void Clear() { received_.clear(); } |
+ |
+ private: |
+ std::vector<protocol::AggregatedProcessResourceUsage> received_; |
+}; |
+ |
+} // namespace |
+ |
+namespace host { |
+ |
+TEST(ProcessStatsSenderTest, ReportUsage) { |
+ base::MessageLoop message_loop; |
+ base::RunLoop run_loop; |
+ MockProcessStatsStub stub; |
+ std::unique_ptr<ProcessStatsSender> stats; |
+ message_loop.task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind( |
+ [](std::unique_ptr<ProcessStatsSender>* stats, |
+ MockProcessStatsStub* stub) -> void { |
+ stats->reset( |
+ new ProcessStatsSender(stub, base::TimeDelta::FromSeconds(1))); |
+ (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( |
+ new CurrentProcessStatsAgent("current"))); |
+ }, |
+ base::Unretained(&stats), base::Unretained(&stub))); |
+ message_loop.task_runner()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind([](std::unique_ptr<ProcessStatsSender>* stats) |
+ -> void { stats->reset(); }, |
+ base::Unretained(&stats)), |
+ base::TimeDelta::FromSecondsD(2.1)); |
+ message_loop.task_runner()->PostDelayedTask( |
+ FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSecondsD(2.2)); |
+ run_loop.Run(); |
+ |
+ ASSERT_EQ(2U, stub.received().size()); |
+} |
+ |
+TEST(ProcessStatsSenderTest, MergeUsage) { |
+ base::MessageLoop message_loop; |
+ base::RunLoop run_loop; |
+ MockProcessStatsStub stub; |
+ std::unique_ptr<ProcessStatsSender> stats; |
+ ForwardProcessStatsAgent* forwarder = nullptr; |
+ // Owned by |stats|. |
+ forwarder = new ForwardProcessStatsAgent(); |
+ message_loop.task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind( |
+ [](std::unique_ptr<ProcessStatsSender>* stats, |
+ MockProcessStatsStub* stub, |
+ ForwardProcessStatsAgent* forwarder) -> void { |
+ stats->reset( |
+ new ProcessStatsSender(stub, base::TimeDelta::FromSeconds(1))); |
+ (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( |
+ new CurrentProcessStatsAgent("current"))); |
+ (*stats)->AddProcessStatsAgent( |
+ std::unique_ptr<ProcessStatsAgent>(forwarder)); |
+ }, |
+ base::Unretained(&stats), base::Unretained(&stub), |
+ base::Unretained(forwarder))); |
+ message_loop.task_runner()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind( |
+ [](std::unique_ptr<ProcessStatsSender>* stats, |
+ ForwardProcessStatsAgent* forwarder) -> void { |
+ protocol::ProcessResourceUsage fake_usage; |
+ fake_usage.set_process_name("forward"); |
+ fake_usage.set_processor_usage(10000000); |
+ fake_usage.set_working_set_size(SIZE_MAX >> 1); |
+ fake_usage.set_pagefile_size(SIZE_MAX >> 1); |
+ forwarder->OnProcessStats(fake_usage); |
+ }, |
+ base::Unretained(&stats), base::Unretained(forwarder)), |
+ base::TimeDelta::FromSecondsD(1.1)); |
+ message_loop.task_runner()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind([](std::unique_ptr<ProcessStatsSender>* stats) |
+ -> void { stats->reset(); }, |
+ base::Unretained(&stats)), |
+ base::TimeDelta::FromSecondsD(3.1)); |
+ message_loop.task_runner()->PostDelayedTask( |
+ FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSecondsD(3.2)); |
+ run_loop.Run(); |
+ |
+ ASSERT_EQ(3U, stub.received().size()); |
+ ASSERT_EQ(stub.received()[0].usages_size(), 0); |
+ ASSERT_LT(stub.received()[0].processor_usage(), 10000000); |
+ ASSERT_LT(stub.received()[0].working_set_size(), SIZE_MAX >> 1); |
+ ASSERT_LT(stub.received()[0].pagefile_size(), SIZE_MAX >> 1); |
+ ASSERT_EQ(stub.received()[1].usages_size(), 2); |
+ ASSERT_GE(stub.received()[1].processor_usage(), 10000000); |
+ ASSERT_GE(stub.received()[1].working_set_size(), SIZE_MAX >> 1); |
+ ASSERT_GE(stub.received()[1].pagefile_size(), SIZE_MAX >> 1); |
+ ASSERT_EQ(stub.received()[2].usages_size(), 2); |
+ ASSERT_GE(stub.received()[2].processor_usage(), 10000000); |
+ ASSERT_GE(stub.received()[2].working_set_size(), SIZE_MAX >> 1); |
+ ASSERT_GE(stub.received()[2].pagefile_size(), SIZE_MAX >> 1); |
+} |
+ |
+} // namespace host |
+} // namespace remoting |