1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/system_cpu/cpu_probe.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/system_cpu/cpu_probe.h"
#include "components/system_cpu/cpu_sample.h"
#include "components/system_cpu/pressure_test_support.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace system_cpu {
class CpuProbeTest : public testing::Test {
public:
CpuProbeTest() : cpu_probe_(std::make_unique<FakeCpuProbe>()) {}
void WaitForUpdate() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::RunLoop run_loop;
cpu_probe_->RequestSample(
base::BindOnce(&CpuProbeTest::CollectorCallback, base::Unretained(this))
.Then(run_loop.QuitClosure()));
run_loop.Run();
}
void CollectorCallback(std::optional<CpuSample> sample) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (sample.has_value()) {
samples_.push_back(sample->cpu_utilization);
}
}
void RequestRepeatedUpdates(base::TimeDelta interval) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
timer_.Start(
FROM_HERE, interval, base::BindLambdaForTesting([&] {
cpu_probe_->RequestSample(base::BindOnce(
&CpuProbeTest::CollectorCallback, base::Unretained(this)));
}));
}
protected:
SEQUENCE_CHECKER(sequence_checker_);
base::test::TaskEnvironment task_environment_;
// This member is a std::unique_ptr instead of a plain CpuProbe
// so it can be replaced inside tests.
std::unique_ptr<CpuProbe> cpu_probe_;
// The samples reported by the callback.
std::vector<double> samples_ GUARDED_BY_CONTEXT(sequence_checker_);
// Timer used to request samples repeatedly.
base::RepeatingTimer timer_ GUARDED_BY_CONTEXT(sequence_checker_);
};
using CpuProbeDeathTest = CpuProbeTest;
TEST_F(CpuProbeTest, RequestSample) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
static_cast<FakeCpuProbe*>(cpu_probe_.get())->SetLastSample(CpuSample{0.9});
cpu_probe_->StartSampling();
WaitForUpdate();
EXPECT_THAT(samples_, testing::ElementsAre(0.9));
}
TEST_F(CpuProbeTest, RepeatedSamples) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::vector<CpuSample> samples = {
// Value right after construction.
CpuSample{0.6},
// Value after first Update(), should be discarded.
CpuSample{0.9},
// Value after second Update(), should be reported.
CpuSample{0.4},
};
base::RunLoop run_loop;
cpu_probe_ = std::make_unique<StreamingCpuProbe>(std::move(samples),
run_loop.QuitClosure());
cpu_probe_->StartSampling();
RequestRepeatedUpdates(base::Milliseconds(1));
run_loop.Run();
EXPECT_THAT(samples_, testing::ElementsAre(0.4));
}
TEST_F(CpuProbeTest, DestroyWhileSampling) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
static_cast<FakeCpuProbe*>(cpu_probe_.get())->SetLastSample(CpuSample{0.9});
cpu_probe_->StartSampling();
// Test passes as long as it doesn't crash.
base::RunLoop run_loop;
cpu_probe_->RequestSample(base::BindOnce(
[](base::ScopedClosureRunner quit_closure_runner,
std::optional<CpuSample>) {
// `quit_closure_runner` will run the bound `quit_closure` when the
// callback is destroyed. The callback function shouldn't actually
// execute because the CpuProbe is destroyed before the RunLoop
// starts, so there's no object to receive it.
FAIL();
},
base::ScopedClosureRunner(run_loop.QuitClosure())));
cpu_probe_.reset();
run_loop.Run();
}
// TODO(crbug.com/41485857): Fix test flakily timing out and re-enable.
TEST_F(CpuProbeDeathTest, DISABLED_CpuUtilizationTooLarge) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::vector<CpuSample> samples = {
// Value right after construction.
CpuSample{0.6},
// Value after first Update(), should be discarded.
CpuSample{0.9},
// Crash expected.
CpuSample{1.1},
};
base::RunLoop run_loop;
cpu_probe_ = std::make_unique<StreamingCpuProbe>(std::move(samples),
run_loop.QuitClosure());
cpu_probe_->StartSampling();
RequestRepeatedUpdates(base::Milliseconds(1));
EXPECT_CHECK_DEATH_WITH(run_loop.Run(), "cpu_utilization <= 1.0");
}
// TODO(crbug.com/41485857): Fix test flakily timing out and re-enable.
TEST_F(CpuProbeDeathTest, DISABLED_CpuUtilizationTooSmall) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::vector<CpuSample> samples = {
// Value right after construction.
CpuSample{0.6},
// Value after first Update(), should be discarded.
CpuSample{0.9},
// Crash expected.
CpuSample{-0.5},
};
base::RunLoop run_loop;
cpu_probe_ = std::make_unique<StreamingCpuProbe>(std::move(samples),
run_loop.QuitClosure());
cpu_probe_->StartSampling();
RequestRepeatedUpdates(base::Milliseconds(1));
EXPECT_CHECK_DEATH_WITH(run_loop.Run(), "cpu_utilization >= 0.0");
}
// TODO(crbug.com/41485857): Fix test flakily timing out and re-enable.
TEST_F(CpuProbeDeathTest, DISABLED_RequestSampleWithoutStartSampling) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
static_cast<FakeCpuProbe*>(cpu_probe_.get())->SetLastSample(CpuSample{0.9});
EXPECT_CHECK_DEATH(WaitForUpdate());
}
} // namespace system_cpu
|