1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "adaptivecpu/CpuLoadReaderProcStat.h"
20 #include "mocks.h"
21
22 #define LOG_TAG "powerhal-adaptivecpu"
23
24 using testing::_;
25 using testing::ByMove;
26 using testing::Return;
27
28 namespace aidl {
29 namespace google {
30 namespace hardware {
31 namespace power {
32 namespace impl {
33 namespace pixel {
34
TEST(CpuLoadReaderProcStatTest,GetRecentCpuLoads)35 TEST(CpuLoadReaderProcStatTest, GetRecentCpuLoads) {
36 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
37 EXPECT_CALL(*filesystem, ReadFileStream("/proc/stat", _))
38 .Times(2)
39 .WillOnce([](auto _path __attribute__((unused)), auto result) {
40 std::stringstream ss;
41 ss << "bad line\n";
42 ss << "cpu1 100 0 0 50 0 0 0 0 0 0\n";
43 ss << "cpu2 200 0 0 50 0 0 0 0 0 0\n";
44 *result = std::make_unique<std::istringstream>(ss.str());
45 return true;
46 })
47 .WillOnce([](auto _path __attribute__((unused)), auto result) {
48 std::stringstream ss;
49 ss << "bad line\n";
50 ss << "cpu1 200 0 0 150 0 0 0 0 0 0\n";
51 ss << "cpu2 500 0 0 150 0 0 0 0 0 0\n";
52 *result = std::make_unique<std::istringstream>(ss.str());
53 return true;
54 });
55
56 CpuLoadReaderProcStat reader(std::move(filesystem));
57 reader.Init();
58
59 std::array<double, NUM_CPU_CORES> actualPercentages;
60 ASSERT_TRUE(reader.GetRecentCpuLoads(&actualPercentages));
61 std::array<double, NUM_CPU_CORES> expectedPercentages({0, 0.5, 0.25, 0, 0, 0, 0, 0});
62 ASSERT_EQ(actualPercentages, expectedPercentages);
63 }
64
TEST(CpuLoadReaderProcStatTest,GetRecentCpuLoads_failsWithMissingValues)65 TEST(CpuLoadReaderProcStatTest, GetRecentCpuLoads_failsWithMissingValues) {
66 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
67 EXPECT_CALL(*filesystem, ReadFileStream("/proc/stat", _))
68 .Times(2)
69 .WillOnce([](auto _path __attribute__((unused)), auto result) {
70 std::stringstream ss;
71 ss << "bad line\n";
72 ss << "cpu1 100 0 0 50 0 0 0\n";
73 ss << "cpu2 200 0 0 50 0 0 0\n";
74 *result = std::make_unique<std::istringstream>(ss.str());
75 return true;
76 })
77 .WillOnce([](auto _path __attribute__((unused)), auto result) {
78 std::stringstream ss;
79 ss << "bad line\n";
80 ss << "cpu1 200 0 0 150 0 0 0\n";
81 ss << "cpu2 500 0 0 150 0 0 0\n";
82 *result = std::make_unique<std::istringstream>(ss.str());
83 return true;
84 });
85
86 CpuLoadReaderProcStat reader(std::move(filesystem));
87 reader.Init();
88 std::array<double, NUM_CPU_CORES> actualPercentages;
89 ASSERT_FALSE(reader.GetRecentCpuLoads(&actualPercentages));
90 }
91
TEST(CpuLoadReaderProcStatTest,GetRecentCpuLoads_failsWithEmptyFile)92 TEST(CpuLoadReaderProcStatTest, GetRecentCpuLoads_failsWithEmptyFile) {
93 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
94 EXPECT_CALL(*filesystem, ReadFileStream("/proc/stat", _))
95 .Times(2)
96 .WillRepeatedly([](auto _path __attribute__((unused)), auto result) {
97 *result = std::make_unique<std::istringstream>("");
98 return true;
99 });
100
101 CpuLoadReaderProcStat reader(std::move(filesystem));
102 reader.Init();
103 std::array<double, NUM_CPU_CORES> actualPercentages;
104 ASSERT_FALSE(reader.GetRecentCpuLoads(&actualPercentages));
105 }
106
TEST(CpuLoadReaderProcStatTest,GetRecentCpuLoads_failsWithDifferentCpus)107 TEST(CpuLoadReaderProcStatTest, GetRecentCpuLoads_failsWithDifferentCpus) {
108 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
109 EXPECT_CALL(*filesystem, ReadFileStream("/proc/stat", _))
110 .Times(2)
111 .WillOnce([](auto _path __attribute__((unused)), auto result) {
112 std::stringstream ss;
113 ss << "bad line\n";
114 ss << "cpu1 100 0 0 50 0 0 0 0 0 0\n";
115 ss << "cpu2 200 0 0 50 0 0 0 0 0 0\n";
116 *result = std::make_unique<std::istringstream>(ss.str());
117 return true;
118 })
119 .WillOnce([](auto _path __attribute__((unused)), auto result) {
120 std::stringstream ss;
121 ss << "bad line\n";
122 ss << "cpu1 200 0 0 150 0 0 0 0 0 0\n";
123 ss << "cpu3 500 0 0 150 0 0 0 0 0 0\n";
124 *result = std::make_unique<std::istringstream>(ss.str());
125 return true;
126 });
127
128 CpuLoadReaderProcStat reader(std::move(filesystem));
129 reader.Init();
130 std::array<double, NUM_CPU_CORES> actualPercentages;
131 ASSERT_FALSE(reader.GetRecentCpuLoads(&actualPercentages));
132 }
133
134 } // namespace pixel
135 } // namespace impl
136 } // namespace power
137 } // namespace hardware
138 } // namespace google
139 } // namespace aidl
140