Chromium Code Reviews| Index: remoting/base/process_stats_unittest.cc |
| diff --git a/remoting/base/process_stats_unittest.cc b/remoting/base/process_stats_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c3aedaa938e0a9cb46d5f635e6e341e986c9c258 |
| --- /dev/null |
| +++ b/remoting/base/process_stats_unittest.cc |
| @@ -0,0 +1,141 @@ |
| +// 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/base/process_stats.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/base/current_process_stats_agent.h" |
| +#include "remoting/base/forward_process_stats_agent.h" |
| +#include "remoting/base/process_stats_stub.h" |
| +#include "remoting/proto/process_stats.pb.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace remoting { |
| + |
| +namespace { |
| + |
| +class MockProcessStatsStub : public 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 |
| + |
| +TEST(ProcessStatsTest, ReportUsage) { |
| + base::MessageLoop message_loop; |
| + base::RunLoop run_loop; |
| + MockProcessStatsStub stub; |
| + std::unique_ptr<ProcessStats> stats; |
| + message_loop.task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + [](std::unique_ptr<ProcessStats>* stats, |
| + MockProcessStatsStub* stub) -> void { |
| + stats->reset( |
| + new ProcessStats(stub, base::TimeDelta::FromSeconds(1))); |
| + (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( |
| + new CurrentProcessStatsAgent("current"))); |
| + (*stats)->Start(); |
| + }, |
| + base::Unretained(&stats), base::Unretained(&stub))); |
| + message_loop.task_runner()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind( |
| + [](std::unique_ptr<ProcessStats>* 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(ProcessStatsTest, MergeUsage) { |
| + base::MessageLoop message_loop; |
| + base::RunLoop run_loop; |
| + MockProcessStatsStub stub; |
| + std::unique_ptr<ProcessStats> stats; |
| + ForwardProcessStatsAgent* forwarder = nullptr; |
| + // Owned by |stats|. |
| + forwarder = new ForwardProcessStatsAgent(); |
| + message_loop.task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + [](std::unique_ptr<ProcessStats>* stats, MockProcessStatsStub* stub, |
| + ForwardProcessStatsAgent* forwarder) -> void { |
| + stats->reset( |
| + new ProcessStats(stub, base::TimeDelta::FromSeconds(1))); |
| + (*stats)->AddProcessStatsAgent(std::unique_ptr<ProcessStatsAgent>( |
| + new CurrentProcessStatsAgent("current"))); |
| + (*stats)->AddProcessStatsAgent( |
| + std::unique_ptr<ProcessStatsAgent>(forwarder)); |
| + (*stats)->Start(); |
| + }, |
| + base::Unretained(&stats), base::Unretained(&stub), |
| + base::Unretained(forwarder))); |
| + message_loop.task_runner()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind( |
| + [](std::unique_ptr<ProcessStats>* 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<ProcessStats>* 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)); |
|
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.
|
| + 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 remoting |