Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Unified Diff: remoting/host/process_stats_sender_unittest.cc

Issue 2775983003: [Chromoting] Retrieve process resource usage (ProcessStats) and its tests (Closed)
Patch Set: Resolve review comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a882deec5cb127fe1fe6104e4879c98f8d1c623e
--- /dev/null
+++ b/remoting/host/process_stats_sender_unittest.cc
@@ -0,0 +1,189 @@
+// 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 <utility>
+#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/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 FakeProcessStatsStub : public protocol::ProcessStatsStub {
+ public:
+ FakeProcessStatsStub() = default;
+ ~FakeProcessStatsStub() 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_;
+};
+
+class FakeProcessStatsAgent : public ProcessStatsAgent {
+ public:
+ FakeProcessStatsAgent() = default;
+ ~FakeProcessStatsAgent() override = default;
+
+ protocol::ProcessResourceUsage GetResourceUsage() override {
+ protocol::ProcessResourceUsage usage;
+ usage.set_process_name("FakeProcessStatsAgent");
+ usage.set_processor_usage(index_);
+ usage.set_working_set_size(index_);
+ usage.set_pagefile_size(index_);
+ index_++;
+ return usage;
+ }
+
+ // Checks the expected usage based on index.
+ static void AssertExpected(
+ const protocol::AggregatedProcessResourceUsage& usage,
+ size_t index) {
+ ASSERT_EQ(usage.processor_usage(), index);
+ ASSERT_EQ(usage.working_set_size(), index);
+ ASSERT_EQ(usage.pagefile_size(), index);
+ }
+
+ static void AssertExpected(
+ const protocol::ProcessResourceUsage& usage,
+ size_t index) {
+ ASSERT_EQ(usage.processor_usage(), index);
+ ASSERT_EQ(usage.working_set_size(), index);
+ ASSERT_EQ(usage.pagefile_size(), index);
+ }
+
+ size_t issued_times() const {
+ return index_;
+ }
+
+ private:
+ size_t index_ = 0;
+};
+
+} // namespace
+
+TEST(ProcessStatsSenderTest, ReportUsage) {
+ base::MessageLoop message_loop;
+ base::RunLoop run_loop;
+ FakeProcessStatsStub stub;
+ std::unique_ptr<ProcessStatsSender> stats;
+ // Owned by |stats|.
+ FakeProcessStatsAgent* agent = new FakeProcessStatsAgent();
+ message_loop.task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(
+ [](std::unique_ptr<ProcessStatsSender>* stats,
+ FakeProcessStatsStub* stub,
+ FakeProcessStatsAgent* agent) -> void {
+ stats->reset(new ProcessStatsSender(
+ stub, base::TimeDelta::FromMilliseconds(1)));
+ (*stats)->AddProcessStatsAgent(
+ std::unique_ptr<ProcessStatsAgent>(agent));
+ },
+ base::Unretained(&stats),
+ base::Unretained(&stub),
+ base::Unretained(agent)));
+ message_loop.task_runner()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind([](std::unique_ptr<ProcessStatsSender>* stats,
+ const FakeProcessStatsStub& stub,
+ const FakeProcessStatsAgent& agent) -> void {
+ ASSERT_EQ(stub.received().size(), agent.issued_times());
+ stats->reset();
+ },
+ base::Unretained(&stats),
+ base::ConstRef(stub),
+ base::ConstRef(*agent)),
+ base::TimeDelta::FromMilliseconds(100));
+ message_loop.task_runner()->PostDelayedTask(
+ FROM_HERE, run_loop.QuitClosure(),
+ base::TimeDelta::FromMilliseconds(101));
+ run_loop.Run();
+
+ ASSERT_GT(stub.received().size(), 0U);
+ for (size_t i = 0; i < stub.received().size(); i++) {
+ FakeProcessStatsAgent::AssertExpected(stub.received()[i], i);
+ }
+}
+
+TEST(ProcessStatsSenderTest, MergeUsage) {
+ base::MessageLoop message_loop;
+ base::RunLoop run_loop;
+ FakeProcessStatsStub stub;
+ std::unique_ptr<ProcessStatsSender> stats;
+ // Owned by |stats|.
+ FakeProcessStatsAgent* agent1 = new FakeProcessStatsAgent();
+ FakeProcessStatsAgent* agent2 = new FakeProcessStatsAgent();
+ message_loop.task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(
+ [](std::unique_ptr<ProcessStatsSender>* stats,
+ FakeProcessStatsStub* stub,
+ FakeProcessStatsAgent* agent1,
+ FakeProcessStatsAgent* agent2) -> void {
+ stats->reset(new ProcessStatsSender(
+ stub, base::TimeDelta::FromMilliseconds(1)));
+ (*stats)->AddProcessStatsAgent(
+ std::unique_ptr<ProcessStatsAgent>(agent1));
+ (*stats)->AddProcessStatsAgent(
+ std::unique_ptr<ProcessStatsAgent>(agent2));
+ },
+ base::Unretained(&stats),
+ base::Unretained(&stub),
+ base::Unretained(agent1),
+ base::Unretained(agent2)));
+ message_loop.task_runner()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind([](std::unique_ptr<ProcessStatsSender>* stats,
+ const FakeProcessStatsStub& stub,
+ const FakeProcessStatsAgent& agent1,
+ const FakeProcessStatsAgent& agent2) -> void {
+ ASSERT_EQ(stub.received().size(), agent1.issued_times());
+ ASSERT_EQ(stub.received().size(), agent2.issued_times());
+ stats->reset();
+ },
+ base::Unretained(&stats),
+ base::ConstRef(stub),
+ base::ConstRef(*agent1),
+ base::ConstRef(*agent2)),
+ base::TimeDelta::FromMilliseconds(100));
+ message_loop.task_runner()->PostDelayedTask(
+ FROM_HERE, run_loop.QuitClosure(),
+ base::TimeDelta::FromMilliseconds(101));
+ run_loop.Run();
+
+ ASSERT_GT(stub.received().size(), 0U);
+ for (size_t i = 0; i < stub.received().size(); i++) {
+ FakeProcessStatsAgent::AssertExpected(stub.received()[i], i * 2);
+ for (int j = 0; j < stub.received()[i].usages_size(); j++) {
+ FakeProcessStatsAgent::AssertExpected(
+ stub.received()[i].usages().Get(j), i);
+ }
+ }
+}
+
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698