Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 1 | #include "pid/ec/logging.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 2 | #include "pid/ec/pid.hpp" |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 3 | #include "pid/zone.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 4 | #include "sensors/manager.hpp" |
| 5 | #include "test/controller_mock.hpp" |
| 6 | #include "test/helpers.hpp" |
| 7 | #include "test/sensor_mock.hpp" |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 8 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 9 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 10 | |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 11 | #include <chrono> |
| 12 | #include <cstring> |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 17 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 18 | namespace pid_control |
| 19 | { |
| 20 | namespace |
| 21 | { |
| 22 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 23 | using ::testing::_; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 24 | using ::testing::IsNull; |
| 25 | using ::testing::Return; |
| 26 | using ::testing::StrEq; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 27 | |
| 28 | static std::string modeInterface = "xyz.openbmc_project.Control.Mode"; |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 29 | static std::string debugZoneInterface = "xyz.openbmc_project.Debug.Pid.Zone"; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 30 | static std::string enableInterface = "xyz.openbmc_project.Object.Enable"; |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 31 | static std::string debugThermalPowerInterface = |
| 32 | "xyz.openbmc_project.Debug.Pid.ThermalPower"; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 33 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 34 | namespace |
| 35 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 36 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 37 | TEST(PidZoneConstructorTest, BoringConstructorTest) |
| 38 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 39 | // Build a PID Zone. |
| 40 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 41 | sdbusplus::SdBusMock sdbus_mock_passive, sdbus_mock_host, sdbus_mock_mode, |
| 42 | sdbus_mock_enable; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 43 | auto bus_mock_passive = sdbusplus::get_mocked_new(&sdbus_mock_passive); |
| 44 | auto bus_mock_host = sdbusplus::get_mocked_new(&sdbus_mock_host); |
| 45 | auto bus_mock_mode = sdbusplus::get_mocked_new(&sdbus_mock_mode); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 46 | auto bus_mock_enable = sdbusplus::get_mocked_new(&sdbus_mock_enable); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 47 | |
| 48 | EXPECT_CALL(sdbus_mock_host, |
| 49 | sd_bus_add_object_manager( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 50 | IsNull(), _, StrEq("/xyz/openbmc_project/extsensors"))) |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 51 | .WillOnce(Return(0)); |
| 52 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 53 | SensorManager m(bus_mock_passive, bus_mock_host); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 54 | |
| 55 | bool defer = true; |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 56 | bool accSetPoint = false; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 57 | const char* objPath = "/path/"; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 58 | int64_t zone = 1; |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 59 | double minThermalOutput = 1000.0; |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 60 | double failSafePercent = 0; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 61 | conf::CycleTime cycleTime; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 62 | |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 63 | double d; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 64 | std::vector<std::string> properties; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | SetupDbusObject(&sdbus_mock_mode, defer, objPath, modeInterface, properties, |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 66 | &d); |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 67 | SetupDbusObject(&sdbus_mock_mode, defer, objPath, debugZoneInterface, |
| 68 | properties, &d); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 69 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 70 | std::string sensorname = "temp1"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 71 | std::string pidsensorpath = |
| 72 | "/xyz/openbmc_project/settings/fanctrl/zone1/" + sensorname; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 73 | |
| 74 | double de; |
| 75 | std::vector<std::string> propertiesenable; |
| 76 | SetupDbusObject(&sdbus_mock_enable, defer, pidsensorpath.c_str(), |
| 77 | enableInterface, propertiesenable, &de); |
| 78 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 79 | DbusPidZone p(zone, minThermalOutput, failSafePercent, cycleTime, m, |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 80 | bus_mock_mode, objPath, defer, accSetPoint); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 81 | // Success. |
| 82 | } |
| 83 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 84 | } // namespace |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 85 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 86 | class PidZoneTest : public ::testing::Test |
| 87 | { |
| 88 | protected: |
| 89 | PidZoneTest() : |
| 90 | property_index(), properties(), sdbus_mock_passive(), sdbus_mock_host(), |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 91 | sdbus_mock_mode(), sdbus_mock_enable() |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 92 | { |
| 93 | EXPECT_CALL(sdbus_mock_host, |
| 94 | sd_bus_add_object_manager( |
| 95 | IsNull(), _, StrEq("/xyz/openbmc_project/extsensors"))) |
| 96 | .WillOnce(Return(0)); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 97 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 98 | auto bus_mock_passive = sdbusplus::get_mocked_new(&sdbus_mock_passive); |
| 99 | auto bus_mock_host = sdbusplus::get_mocked_new(&sdbus_mock_host); |
| 100 | auto bus_mock_mode = sdbusplus::get_mocked_new(&sdbus_mock_mode); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 101 | auto bus_mock_enable = sdbusplus::get_mocked_new(&sdbus_mock_enable); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 102 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 103 | // Compiler weirdly not happy about just instantiating mgr(...); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 104 | SensorManager m(bus_mock_passive, bus_mock_host); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 105 | mgr = std::move(m); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 106 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 107 | SetupDbusObject(&sdbus_mock_mode, defer, objPath, modeInterface, |
| 108 | properties, &property_index); |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 109 | SetupDbusObject(&sdbus_mock_mode, defer, objPath, debugZoneInterface, |
| 110 | properties, &property_index); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 111 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 112 | SetupDbusObject(&sdbus_mock_enable, defer, pidsensorpath.c_str(), |
| 113 | enableInterface, propertiesenable, |
| 114 | &propertyenable_index); |
| 115 | |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 116 | zone = std::make_unique<DbusPidZone>( |
| 117 | zoneId, minThermalOutput, failSafePercent, cycleTime, mgr, |
| 118 | bus_mock_mode, objPath, defer, accSetPoint); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 119 | } |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 120 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 121 | // unused |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 122 | double property_index; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 123 | std::vector<std::string> properties; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 124 | double propertyenable_index; |
| 125 | std::vector<std::string> propertiesenable; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 126 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 127 | sdbusplus::SdBusMock sdbus_mock_passive; |
| 128 | sdbusplus::SdBusMock sdbus_mock_host; |
| 129 | sdbusplus::SdBusMock sdbus_mock_mode; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 130 | sdbusplus::SdBusMock sdbus_mock_enable; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 131 | int64_t zoneId = 1; |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 132 | double minThermalOutput = 1000.0; |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 133 | double failSafePercent = 0; |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 134 | double setpoint = 50.0; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 135 | bool defer = true; |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 136 | bool accSetPoint = false; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 137 | const char* objPath = "/path/"; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 138 | SensorManager mgr; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 139 | conf::CycleTime cycleTime; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 140 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 141 | std::string sensorname = "temp1"; |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 142 | std::string sensorType = "temp"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 143 | std::string pidsensorpath = |
| 144 | "/xyz/openbmc_project/settings/fanctrl/zone1/" + sensorname; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 145 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 146 | std::unique_ptr<DbusPidZone> zone; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 149 | TEST_F(PidZoneTest, GetZoneId_ReturnsExpected) |
| 150 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 151 | // Verifies the zoneId returned is what we expect. |
| 152 | |
Patrick Venture | 0bbeaf8 | 2018-10-30 18:50:31 -0700 | [diff] [blame] | 153 | EXPECT_EQ(zoneId, zone->getZoneID()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 156 | TEST_F(PidZoneTest, GetAndSetManualModeTest_BehavesAsExpected) |
| 157 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 158 | // Verifies that the zone starts in manual mode. Verifies that one can set |
| 159 | // the mode. |
| 160 | EXPECT_FALSE(zone->getManualMode()); |
| 161 | |
| 162 | zone->setManualMode(true); |
| 163 | EXPECT_TRUE(zone->getManualMode()); |
| 164 | } |
| 165 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 166 | TEST_F(PidZoneTest, AddPidControlProcessGetAndSetEnableTest_BehavesAsExpected) |
| 167 | { |
| 168 | // Verifies that the zone starts in enable mode. Verifies that one can set |
| 169 | // enable the mode. |
| 170 | auto bus_mock_enable = sdbusplus::get_mocked_new(&sdbus_mock_enable); |
| 171 | |
| 172 | EXPECT_CALL(sdbus_mock_mode, sd_bus_emit_properties_changed_strv( |
| 173 | IsNull(), StrEq(pidsensorpath.c_str()), |
| 174 | StrEq(enableInterface), NotNull())) |
| 175 | .Times(::testing::AnyNumber()) |
| 176 | .WillOnce(Invoke( |
| 177 | [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path, |
| 178 | [[maybe_unused]] const char* interface, const char** names) { |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 179 | EXPECT_STREQ("Enable", names[0]); |
| 180 | return 0; |
| 181 | })); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 182 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 183 | zone->addPidControlProcess(sensorname, sensorType, setpoint, |
| 184 | bus_mock_enable, pidsensorpath.c_str(), defer); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 185 | EXPECT_TRUE(zone->isPidProcessEnabled(sensorname)); |
| 186 | } |
| 187 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 188 | TEST_F(PidZoneTest, SetManualMode_RedundantWritesEnabledOnceAfterManualMode) |
| 189 | { |
| 190 | // Tests adding a fan PID controller to the zone, and verifies it's |
| 191 | // touched during processing. |
| 192 | |
| 193 | std::unique_ptr<PIDController> tpid = |
| 194 | std::make_unique<ControllerMock>("fan1", zone.get()); |
| 195 | ControllerMock* tmock = reinterpret_cast<ControllerMock*>(tpid.get()); |
| 196 | |
| 197 | // Access the internal pid configuration to clear it out (unrelated to the |
| 198 | // test). |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame^] | 199 | [[maybe_unused]] ec::pid_info_t* info = tpid->getPIDInfo(); |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 200 | |
| 201 | zone->addFanPID(std::move(tpid)); |
| 202 | |
| 203 | EXPECT_CALL(*tmock, setptProc()).WillOnce(Return(10.0)); |
| 204 | EXPECT_CALL(*tmock, inputProc()).WillOnce(Return(11.0)); |
| 205 | EXPECT_CALL(*tmock, outputProc(_)); |
| 206 | |
| 207 | // while zone is in auto mode redundant writes should be disabled |
| 208 | EXPECT_FALSE(zone->getRedundantWrite()); |
| 209 | |
| 210 | // but switching from manual to auto enables a single redundant write |
| 211 | zone->setManualMode(true); |
| 212 | zone->setManualMode(false); |
| 213 | EXPECT_TRUE(zone->getRedundantWrite()); |
| 214 | |
| 215 | // after one iteration of a pid loop redundant write should be cleared |
| 216 | zone->processFans(); |
| 217 | EXPECT_FALSE(zone->getRedundantWrite()); |
| 218 | } |
| 219 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 220 | TEST_F(PidZoneTest, RpmSetPoints_AddMaxClear_BehaveAsExpected) |
| 221 | { |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 222 | // Tests addSetPoint, clearSetPoints, determineMaxSetPointRequest |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 223 | // and getMinThermalSetPoint. |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 224 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 225 | // Need to add pid control process for the zone that can enable |
| 226 | // the process and add the set point. |
| 227 | auto bus_mock_enable = sdbusplus::get_mocked_new(&sdbus_mock_enable); |
| 228 | |
| 229 | EXPECT_CALL(sdbus_mock_mode, sd_bus_emit_properties_changed_strv( |
| 230 | IsNull(), StrEq(pidsensorpath.c_str()), |
| 231 | StrEq(enableInterface), NotNull())) |
| 232 | .Times(::testing::AnyNumber()) |
| 233 | .WillOnce(Invoke( |
| 234 | [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path, |
| 235 | [[maybe_unused]] const char* interface, const char** names) { |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 236 | EXPECT_STREQ("Enable", names[0]); |
| 237 | return 0; |
| 238 | })); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 239 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 240 | zone->addPidControlProcess(sensorname, sensorType, setpoint, |
| 241 | bus_mock_enable, pidsensorpath.c_str(), defer); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 242 | |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 243 | // At least one value must be above the minimum thermal setpoint used in |
| 244 | // the constructor otherwise it'll choose that value |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 245 | std::vector<double> values = {100, 200, 300, 400, 500, 5000}; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 246 | |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 247 | for (auto v : values) |
| 248 | { |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 249 | zone->addSetPoint(v, sensorname); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // This will pull the maximum RPM setpoint request. |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 253 | zone->determineMaxSetPointRequest(); |
| 254 | EXPECT_EQ(5000, zone->getMaxSetPointRequest()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 255 | |
| 256 | // Clear the values, so it'll choose the minimum thermal setpoint. |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 257 | zone->clearSetPoints(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 258 | |
| 259 | // This will go through the RPM set point values and grab the maximum. |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 260 | zone->determineMaxSetPointRequest(); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 261 | EXPECT_EQ(zone->getMinThermalSetPoint(), zone->getMaxSetPointRequest()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 264 | TEST_F(PidZoneTest, RpmSetPoints_AddBelowMinimum_BehavesAsExpected) |
| 265 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 266 | // Tests adding several RPM setpoints, however, they're all lower than the |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 267 | // configured minimal thermal setpoint RPM value. |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 268 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 269 | // Need to add pid control process for the zone that can enable |
| 270 | // the process and add the set point. |
| 271 | auto bus_mock_enable = sdbusplus::get_mocked_new(&sdbus_mock_enable); |
| 272 | |
| 273 | EXPECT_CALL(sdbus_mock_mode, sd_bus_emit_properties_changed_strv( |
| 274 | IsNull(), StrEq(pidsensorpath.c_str()), |
| 275 | StrEq(enableInterface), NotNull())) |
| 276 | .Times(::testing::AnyNumber()) |
| 277 | .WillOnce(Invoke( |
| 278 | [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path, |
| 279 | [[maybe_unused]] const char* interface, const char** names) { |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 280 | EXPECT_STREQ("Enable", names[0]); |
| 281 | return 0; |
| 282 | })); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 283 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 284 | zone->addPidControlProcess(sensorname, sensorType, setpoint, |
| 285 | bus_mock_enable, pidsensorpath.c_str(), defer); |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 286 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 287 | std::vector<double> values = {100, 200, 300, 400, 500}; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 288 | |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 289 | for (auto v : values) |
| 290 | { |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 291 | zone->addSetPoint(v, sensorname); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // This will pull the maximum RPM setpoint request. |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 295 | zone->determineMaxSetPointRequest(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 296 | |
| 297 | // Verifies the value returned in the minimal thermal rpm set point. |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 298 | EXPECT_EQ(zone->getMinThermalSetPoint(), zone->getMaxSetPointRequest()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 301 | TEST_F(PidZoneTest, GetFailSafePercent_ReturnsExpected) |
| 302 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 303 | // Verify the value used to create the object is stored. |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 304 | // when the final failsafe percent is zero , it indicate |
| 305 | // no failsafe percent is configured  , set it to 100% as |
| 306 | // the default setting. |
| 307 | |
| 308 | std::vector<double> values = {0, 0, 0}; |
| 309 | int64_t defaultPercent = 100; |
| 310 | |
| 311 | zone->addPidFailSafePercent("temp1", values[0]); |
| 312 | zone->addPidFailSafePercent("temp2", values[1]); |
| 313 | zone->addPidFailSafePercent("temp3", values[2]); |
| 314 | |
| 315 | zone->initPidFailSafePercent(); |
| 316 | |
| 317 | EXPECT_EQ(defaultPercent, zone->getFailSafePercent()); |
| 318 | } |
| 319 | |
| 320 | TEST_F(PidZoneTest, GetFailSafePercent_VerifyReturnsExpected) |
| 321 | { |
| 322 | // Tests adding PID controller with FailSafePercent to the zone, |
| 323 | // and verifies it's returned as expected. |
| 324 | |
| 325 | std::vector<double> values = {60, 80, 70}; |
| 326 | double max_value = 0; |
| 327 | |
| 328 | for (const auto& value : values) |
| 329 | { |
| 330 | max_value = std::max(max_value, value); |
| 331 | } |
| 332 | |
| 333 | zone->addPidFailSafePercent("temp1", values[0]); |
| 334 | zone->addPidFailSafePercent("temp2", values[1]); |
| 335 | zone->addPidFailSafePercent("temp3", values[2]); |
| 336 | |
| 337 | zone->initPidFailSafePercent(); |
| 338 | |
| 339 | EXPECT_EQ(max_value, zone->getFailSafePercent()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 342 | TEST_F(PidZoneTest, ThermalInputs_FailsafeToValid_ReadsSensors) |
| 343 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 344 | // This test will add a couple thermal inputs, and verify that the zone |
| 345 | // initializes into failsafe mode, and will read each sensor. |
| 346 | |
| 347 | std::string name1 = "temp1"; |
| 348 | int64_t timeout = 1; |
| 349 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 350 | std::unique_ptr<Sensor> sensor1 = |
| 351 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 352 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 353 | |
| 354 | std::string name2 = "temp2"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 355 | std::unique_ptr<Sensor> sensor2 = |
| 356 | std::make_unique<SensorMock>(name2, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 357 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 358 | |
| 359 | std::string type = "unchecked"; |
| 360 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 361 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 362 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 363 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 364 | |
| 365 | // Now that the sensors exist, add them to the zone. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 366 | zone->addThermalInput(name1, false); |
| 367 | zone->addThermalInput(name2, false); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 368 | |
| 369 | // Initialize Zone |
| 370 | zone->initializeCache(); |
| 371 | |
| 372 | // Verify now in failsafe mode. |
| 373 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 374 | |
| 375 | ReadReturn r1; |
| 376 | r1.value = 10.0; |
| 377 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 378 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 379 | |
| 380 | ReadReturn r2; |
| 381 | r2.value = 11.0; |
| 382 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 383 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 384 | |
| 385 | // Read the sensors, this will put the values into the cache. |
| 386 | zone->updateSensors(); |
| 387 | |
| 388 | // We should no longer be in failsafe mode. |
| 389 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 390 | |
| 391 | EXPECT_EQ(r1.value, zone->getCachedValue(name1)); |
| 392 | EXPECT_EQ(r2.value, zone->getCachedValue(name2)); |
| 393 | } |
| 394 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 395 | TEST_F(PidZoneTest, FanInputTest_VerifiesFanValuesCached) |
| 396 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 397 | // This will add a couple fan inputs, and verify the values are cached. |
| 398 | |
| 399 | std::string name1 = "fan1"; |
| 400 | int64_t timeout = 2; |
| 401 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 402 | std::unique_ptr<Sensor> sensor1 = |
| 403 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 404 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 405 | |
| 406 | std::string name2 = "fan2"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 407 | std::unique_ptr<Sensor> sensor2 = |
| 408 | std::make_unique<SensorMock>(name2, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 409 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 410 | |
| 411 | std::string type = "unchecked"; |
| 412 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 413 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 414 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 415 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 416 | |
| 417 | // Now that the sensors exist, add them to the zone. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 418 | zone->addFanInput(name1, false); |
| 419 | zone->addFanInput(name2, false); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 420 | |
| 421 | // Initialize Zone |
| 422 | zone->initializeCache(); |
| 423 | |
| 424 | ReadReturn r1; |
| 425 | r1.value = 10.0; |
| 426 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 427 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 428 | |
| 429 | ReadReturn r2; |
| 430 | r2.value = 11.0; |
| 431 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 432 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 433 | |
| 434 | // Method under test will read through each fan sensor for the zone and |
| 435 | // cache the values. |
| 436 | zone->updateFanTelemetry(); |
| 437 | |
| 438 | EXPECT_EQ(r1.value, zone->getCachedValue(name1)); |
| 439 | EXPECT_EQ(r2.value, zone->getCachedValue(name2)); |
| 440 | } |
| 441 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 442 | TEST_F(PidZoneTest, ThermalInput_ValueTimeoutEntersFailSafeMode) |
| 443 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 444 | // On the second updateSensors call, the updated timestamp will be beyond |
| 445 | // the timeout limit. |
| 446 | |
| 447 | int64_t timeout = 1; |
| 448 | |
| 449 | std::string name1 = "temp1"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 450 | std::unique_ptr<Sensor> sensor1 = |
| 451 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 452 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 453 | |
| 454 | std::string name2 = "temp2"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 455 | std::unique_ptr<Sensor> sensor2 = |
| 456 | std::make_unique<SensorMock>(name2, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 457 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 458 | |
| 459 | std::string type = "unchecked"; |
| 460 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 461 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 462 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 463 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 464 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 465 | zone->addThermalInput(name1, false); |
| 466 | zone->addThermalInput(name2, false); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 467 | |
| 468 | // Initialize Zone |
| 469 | zone->initializeCache(); |
| 470 | |
| 471 | // Verify now in failsafe mode. |
| 472 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 473 | |
| 474 | ReadReturn r1; |
| 475 | r1.value = 10.0; |
| 476 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 477 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 478 | |
| 479 | ReadReturn r2; |
| 480 | r2.value = 11.0; |
| 481 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 482 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 483 | |
| 484 | zone->updateSensors(); |
| 485 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 486 | |
| 487 | // Ok, so we're not in failsafe mode, so let's set updated to the past. |
| 488 | // sensor1 will have an updated field older than its timeout value, but |
| 489 | // sensor2 will be fine. :D |
| 490 | r1.updated -= std::chrono::seconds(3); |
| 491 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 492 | |
| 493 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 494 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 495 | |
| 496 | // Method under test will read each sensor. One sensor's value is older |
| 497 | // than the timeout for that sensor and this triggers failsafe mode. |
| 498 | zone->updateSensors(); |
| 499 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 500 | } |
| 501 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 502 | TEST_F(PidZoneTest, ThermalInput_MissingIsAcceptableNoFailSafe) |
| 503 | { |
| 504 | // This is similar to the above test, but because missingIsAcceptable |
| 505 | // is set for sensor1, the zone should not enter failsafe mode when |
| 506 | // only sensor1 goes missing. |
| 507 | // However, sensor2 going missing should still trigger failsafe mode. |
| 508 | |
| 509 | int64_t timeout = 1; |
| 510 | |
| 511 | std::string name1 = "temp1"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 512 | std::unique_ptr<Sensor> sensor1 = |
| 513 | std::make_unique<SensorMock>(name1, timeout); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 514 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
| 515 | |
| 516 | std::string name2 = "temp2"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 517 | std::unique_ptr<Sensor> sensor2 = |
| 518 | std::make_unique<SensorMock>(name2, timeout); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 519 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
| 520 | |
| 521 | std::string type = "unchecked"; |
| 522 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 523 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 524 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 525 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 526 | |
| 527 | // Only sensor1 has MissingIsAcceptable enabled for it |
| 528 | zone->addThermalInput(name1, true); |
| 529 | zone->addThermalInput(name2, false); |
| 530 | |
| 531 | // Initialize Zone |
| 532 | zone->initializeCache(); |
| 533 | |
| 534 | // As sensors are not initialized, zone should be in failsafe mode |
| 535 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 536 | |
| 537 | // r1 not populated here, intentionally, to simulate a sensor that |
| 538 | // is not available yet, perhaps takes a long time to start up. |
| 539 | ReadReturn r1; |
| 540 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 541 | |
| 542 | ReadReturn r2; |
| 543 | r2.value = 11.0; |
| 544 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 545 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 546 | |
| 547 | zone->updateSensors(); |
| 548 | |
| 549 | // Only sensor2 has been initialized here. Failsafe should be false, |
| 550 | // because sensor1 MissingIsAcceptable so it is OK for it to go missing. |
| 551 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 552 | |
| 553 | r1.value = 10.0; |
| 554 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 555 | |
| 556 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 557 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 558 | zone->updateSensors(); |
| 559 | |
| 560 | // Both sensors are now properly initialized |
| 561 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 562 | |
| 563 | // Ok, so we're not in failsafe mode, so let's set updated to the past. |
| 564 | // sensor1 will have an updated field older than its timeout value, but |
| 565 | // sensor2 will be fine. :D |
| 566 | r1.updated -= std::chrono::seconds(3); |
| 567 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 568 | |
| 569 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 570 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 571 | zone->updateSensors(); |
| 572 | |
| 573 | // MissingIsAcceptable is true for sensor1, so the zone should not be |
| 574 | // thrown into failsafe mode. |
| 575 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 576 | |
| 577 | // Do the same thing, but for the opposite sensors: r1 is good, |
| 578 | // but r2 is set to some time in the past. |
| 579 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 580 | r2.updated -= std::chrono::seconds(3); |
| 581 | |
| 582 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 583 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 584 | zone->updateSensors(); |
| 585 | |
| 586 | // Now, the zone should be in failsafe mode, because sensor2 does not |
| 587 | // have MissingIsAcceptable set true, it is still subject to failsafe. |
| 588 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 589 | |
| 590 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 591 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 592 | |
| 593 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 594 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 595 | zone->updateSensors(); |
| 596 | |
| 597 | // The failsafe mode should cease, as both sensors are good again. |
| 598 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 599 | } |
| 600 | |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 601 | TEST_F(PidZoneTest, FanInputTest_FailsafeToValid_ReadsSensors) |
| 602 | { |
| 603 | // This will add a couple fan inputs, and verify the values are cached. |
| 604 | |
| 605 | std::string name1 = "fan1"; |
| 606 | int64_t timeout = 2; |
| 607 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 608 | std::unique_ptr<Sensor> sensor1 = |
| 609 | std::make_unique<SensorMock>(name1, timeout); |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 610 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
| 611 | |
| 612 | std::string name2 = "fan2"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 613 | std::unique_ptr<Sensor> sensor2 = |
| 614 | std::make_unique<SensorMock>(name2, timeout); |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 615 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
| 616 | |
| 617 | std::string type = "unchecked"; |
| 618 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 619 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 620 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 621 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 622 | |
| 623 | // Now that the sensors exist, add them to the zone. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 624 | zone->addFanInput(name1, false); |
| 625 | zone->addFanInput(name2, false); |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 626 | |
| 627 | // Initialize Zone |
| 628 | zone->initializeCache(); |
| 629 | |
| 630 | // Verify now in failsafe mode. |
| 631 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 632 | |
| 633 | ReadReturn r1; |
| 634 | r1.value = 10.0; |
| 635 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 636 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 637 | |
| 638 | ReadReturn r2; |
| 639 | r2.value = 11.0; |
| 640 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 641 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 642 | |
| 643 | // Method under test will read through each fan sensor for the zone and |
| 644 | // cache the values. |
| 645 | zone->updateFanTelemetry(); |
| 646 | |
| 647 | // We should no longer be in failsafe mode. |
| 648 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 649 | |
| 650 | EXPECT_EQ(r1.value, zone->getCachedValue(name1)); |
| 651 | EXPECT_EQ(r2.value, zone->getCachedValue(name2)); |
| 652 | } |
| 653 | |
| 654 | TEST_F(PidZoneTest, FanInputTest_ValueTimeoutEntersFailSafeMode) |
| 655 | { |
| 656 | // This will add a couple fan inputs, and verify the values are cached. |
| 657 | |
| 658 | std::string name1 = "fan1"; |
| 659 | int64_t timeout = 2; |
| 660 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 661 | std::unique_ptr<Sensor> sensor1 = |
| 662 | std::make_unique<SensorMock>(name1, timeout); |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 663 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
| 664 | |
| 665 | std::string name2 = "fan2"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 666 | std::unique_ptr<Sensor> sensor2 = |
| 667 | std::make_unique<SensorMock>(name2, timeout); |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 668 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
| 669 | |
| 670 | std::string type = "unchecked"; |
| 671 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 672 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 673 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 674 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 675 | |
| 676 | // Now that the sensors exist, add them to the zone. |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 677 | zone->addFanInput(name1, false); |
| 678 | zone->addFanInput(name2, false); |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 679 | |
| 680 | // Initialize Zone |
| 681 | zone->initializeCache(); |
| 682 | |
| 683 | // Verify now in failsafe mode. |
| 684 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 685 | |
| 686 | ReadReturn r1; |
| 687 | r1.value = 10.0; |
| 688 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 689 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 690 | |
| 691 | ReadReturn r2; |
| 692 | r2.value = 11.0; |
| 693 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 694 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 695 | |
| 696 | // Method under test will read through each fan sensor for the zone and |
| 697 | // cache the values. |
| 698 | zone->updateFanTelemetry(); |
| 699 | |
| 700 | // We should no longer be in failsafe mode. |
| 701 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 702 | |
| 703 | r1.updated -= std::chrono::seconds(3); |
| 704 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 705 | |
| 706 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 707 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 708 | |
| 709 | zone->updateFanTelemetry(); |
| 710 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 711 | } |
| 712 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 713 | TEST_F(PidZoneTest, GetSensorTest_ReturnsExpected) |
| 714 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 715 | // One can grab a sensor from the manager through the zone. |
| 716 | |
| 717 | int64_t timeout = 1; |
| 718 | |
| 719 | std::string name1 = "temp1"; |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 720 | std::unique_ptr<Sensor> sensor1 = |
| 721 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 722 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 723 | |
| 724 | std::string type = "unchecked"; |
| 725 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 726 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 727 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 728 | zone->addThermalInput(name1, false); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 729 | |
| 730 | // Verify method under test returns the pointer we expect. |
| 731 | EXPECT_EQ(mgr.getSensor(name1), zone->getSensor(name1)); |
| 732 | } |
| 733 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 734 | TEST_F(PidZoneTest, AddThermalPIDTest_VerifiesThermalPIDsProcessed) |
| 735 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 736 | // Tests adding a thermal PID controller to the zone, and verifies it's |
| 737 | // touched during processing. |
| 738 | |
| 739 | std::unique_ptr<PIDController> tpid = |
| 740 | std::make_unique<ControllerMock>("thermal1", zone.get()); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 741 | ControllerMock* tmock = reinterpret_cast<ControllerMock*>(tpid.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 742 | |
| 743 | // Access the internal pid configuration to clear it out (unrelated to the |
| 744 | // test). |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame^] | 745 | [[maybe_unused]] ec::pid_info_t* info = tpid->getPIDInfo(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 746 | |
| 747 | zone->addThermalPID(std::move(tpid)); |
| 748 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 749 | EXPECT_CALL(*tmock, setptProc()).WillOnce(Return(10.0)); |
| 750 | EXPECT_CALL(*tmock, inputProc()).WillOnce(Return(11.0)); |
| 751 | EXPECT_CALL(*tmock, outputProc(_)); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 752 | |
| 753 | // Method under test will, for each thermal PID, call setpt, input, and |
| 754 | // output. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 755 | zone->processThermals(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 758 | TEST_F(PidZoneTest, AddFanPIDTest_VerifiesFanPIDsProcessed) |
| 759 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 760 | // Tests adding a fan PID controller to the zone, and verifies it's |
| 761 | // touched during processing. |
| 762 | |
| 763 | std::unique_ptr<PIDController> tpid = |
| 764 | std::make_unique<ControllerMock>("fan1", zone.get()); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 765 | ControllerMock* tmock = reinterpret_cast<ControllerMock*>(tpid.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 766 | |
| 767 | // Access the internal pid configuration to clear it out (unrelated to the |
| 768 | // test). |
Harvey Wu | 1b3b730 | 2024-10-04 16:49:46 +0800 | [diff] [blame^] | 769 | [[maybe_unused]] ec::pid_info_t* info = tpid->getPIDInfo(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 770 | |
| 771 | zone->addFanPID(std::move(tpid)); |
| 772 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 773 | EXPECT_CALL(*tmock, setptProc()).WillOnce(Return(10.0)); |
| 774 | EXPECT_CALL(*tmock, inputProc()).WillOnce(Return(11.0)); |
| 775 | EXPECT_CALL(*tmock, outputProc(_)); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 776 | |
| 777 | // Method under test will, for each fan PID, call setpt, input, and output. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 778 | zone->processFans(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 781 | TEST_F(PidZoneTest, ManualModeDbusTest_VerifySetManualBehavesAsExpected) |
| 782 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 783 | // The manual(bool) method is inherited from the dbus mode interface. |
| 784 | |
| 785 | // Verifies that someone doesn't remove the internal call to the dbus |
| 786 | // object from which we're inheriting. |
| 787 | EXPECT_CALL(sdbus_mock_mode, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 788 | sd_bus_emit_properties_changed_strv( |
| 789 | IsNull(), StrEq(objPath), StrEq(modeInterface), NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 790 | .WillOnce(Invoke( |
| 791 | [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path, |
| 792 | [[maybe_unused]] const char* interface, const char** names) { |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 793 | EXPECT_STREQ("Manual", names[0]); |
| 794 | return 0; |
| 795 | })); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 796 | |
| 797 | // Method under test will set the manual mode to true and broadcast this |
| 798 | // change on dbus. |
| 799 | zone->manual(true); |
| 800 | EXPECT_TRUE(zone->getManualMode()); |
| 801 | } |
| 802 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 803 | TEST_F(PidZoneTest, FailsafeDbusTest_VerifiesReturnsExpected) |
| 804 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 805 | // This property is implemented by us as read-only, such that trying to |
| 806 | // write to it will have no effect. |
| 807 | EXPECT_EQ(zone->failSafe(), zone->getFailSafeMode()); |
| 808 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 809 | |
| 810 | } // namespace |
| 811 | } // namespace pid_control |