Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 1 | #include "pid/ec/pid.hpp" |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 2 | #include "pid/zone.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 3 | #include "sensors/manager.hpp" |
| 4 | #include "test/controller_mock.hpp" |
| 5 | #include "test/helpers.hpp" |
| 6 | #include "test/sensor_mock.hpp" |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 7 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 8 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 9 | |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <cstring> |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 16 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 17 | namespace pid_control |
| 18 | { |
| 19 | namespace |
| 20 | { |
| 21 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | using ::testing::_; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 23 | using ::testing::IsNull; |
| 24 | using ::testing::Return; |
| 25 | using ::testing::StrEq; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 26 | |
| 27 | static std::string modeInterface = "xyz.openbmc_project.Control.Mode"; |
| 28 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 29 | namespace |
| 30 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 31 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 32 | TEST(PidZoneConstructorTest, BoringConstructorTest) |
| 33 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 34 | // Build a PID Zone. |
| 35 | |
| 36 | sdbusplus::SdBusMock sdbus_mock_passive, sdbus_mock_host, sdbus_mock_mode; |
| 37 | auto bus_mock_passive = sdbusplus::get_mocked_new(&sdbus_mock_passive); |
| 38 | auto bus_mock_host = sdbusplus::get_mocked_new(&sdbus_mock_host); |
| 39 | auto bus_mock_mode = sdbusplus::get_mocked_new(&sdbus_mock_mode); |
| 40 | |
| 41 | EXPECT_CALL(sdbus_mock_host, |
| 42 | sd_bus_add_object_manager( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 43 | IsNull(), _, StrEq("/xyz/openbmc_project/extsensors"))) |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 44 | .WillOnce(Return(0)); |
| 45 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 46 | SensorManager m(bus_mock_passive, bus_mock_host); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 47 | |
| 48 | bool defer = true; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 49 | const char* objPath = "/path/"; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 50 | int64_t zone = 1; |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 51 | double minThermalOutput = 1000.0; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 52 | double failSafePercent = 0.75; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 53 | conf::CycleTime cycleTime; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 54 | |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 55 | double d; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 56 | std::vector<std::string> properties; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 57 | SetupDbusObject(&sdbus_mock_mode, defer, objPath, modeInterface, properties, |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 58 | &d); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 59 | |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 60 | DbusPidZone p(zone, minThermalOutput, failSafePercent, cycleTime, m, |
| 61 | bus_mock_mode, objPath, defer); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 62 | // Success. |
| 63 | } |
| 64 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | } // namespace |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 66 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 67 | class PidZoneTest : public ::testing::Test |
| 68 | { |
| 69 | protected: |
| 70 | PidZoneTest() : |
| 71 | property_index(), properties(), sdbus_mock_passive(), sdbus_mock_host(), |
| 72 | sdbus_mock_mode() |
| 73 | { |
| 74 | EXPECT_CALL(sdbus_mock_host, |
| 75 | sd_bus_add_object_manager( |
| 76 | IsNull(), _, StrEq("/xyz/openbmc_project/extsensors"))) |
| 77 | .WillOnce(Return(0)); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 78 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 79 | auto bus_mock_passive = sdbusplus::get_mocked_new(&sdbus_mock_passive); |
| 80 | auto bus_mock_host = sdbusplus::get_mocked_new(&sdbus_mock_host); |
| 81 | auto bus_mock_mode = sdbusplus::get_mocked_new(&sdbus_mock_mode); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 82 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 83 | // Compiler weirdly not happy about just instantiating mgr(...); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 84 | SensorManager m(bus_mock_passive, bus_mock_host); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 85 | mgr = std::move(m); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 86 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 87 | SetupDbusObject(&sdbus_mock_mode, defer, objPath, modeInterface, |
| 88 | properties, &property_index); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 89 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 90 | zone = std::make_unique<DbusPidZone>(zoneId, minThermalOutput, |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 91 | failSafePercent, cycleTime, mgr, |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 92 | bus_mock_mode, objPath, defer); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 93 | } |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 94 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 95 | // unused |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 96 | double property_index; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 97 | std::vector<std::string> properties; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 98 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 99 | sdbusplus::SdBusMock sdbus_mock_passive; |
| 100 | sdbusplus::SdBusMock sdbus_mock_host; |
| 101 | sdbusplus::SdBusMock sdbus_mock_mode; |
| 102 | int64_t zoneId = 1; |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 103 | double minThermalOutput = 1000.0; |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 104 | double failSafePercent = 0.75; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 105 | bool defer = true; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 106 | const char* objPath = "/path/"; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 107 | SensorManager mgr; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 108 | conf::CycleTime cycleTime; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 109 | |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 110 | std::unique_ptr<DbusPidZone> zone; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 113 | TEST_F(PidZoneTest, GetZoneId_ReturnsExpected) |
| 114 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 115 | // Verifies the zoneId returned is what we expect. |
| 116 | |
Patrick Venture | 0bbeaf8 | 2018-10-30 18:50:31 -0700 | [diff] [blame] | 117 | EXPECT_EQ(zoneId, zone->getZoneID()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 120 | TEST_F(PidZoneTest, GetAndSetManualModeTest_BehavesAsExpected) |
| 121 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 122 | // Verifies that the zone starts in manual mode. Verifies that one can set |
| 123 | // the mode. |
| 124 | EXPECT_FALSE(zone->getManualMode()); |
| 125 | |
| 126 | zone->setManualMode(true); |
| 127 | EXPECT_TRUE(zone->getManualMode()); |
| 128 | } |
| 129 | |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 130 | TEST_F(PidZoneTest, SetManualMode_RedundantWritesEnabledOnceAfterManualMode) |
| 131 | { |
| 132 | // Tests adding a fan PID controller to the zone, and verifies it's |
| 133 | // touched during processing. |
| 134 | |
| 135 | std::unique_ptr<PIDController> tpid = |
| 136 | std::make_unique<ControllerMock>("fan1", zone.get()); |
| 137 | ControllerMock* tmock = reinterpret_cast<ControllerMock*>(tpid.get()); |
| 138 | |
| 139 | // Access the internal pid configuration to clear it out (unrelated to the |
| 140 | // test). |
| 141 | ec::pid_info_t* info = tpid->getPIDInfo(); |
| 142 | std::memset(info, 0x00, sizeof(ec::pid_info_t)); |
| 143 | |
| 144 | zone->addFanPID(std::move(tpid)); |
| 145 | |
| 146 | EXPECT_CALL(*tmock, setptProc()).WillOnce(Return(10.0)); |
| 147 | EXPECT_CALL(*tmock, inputProc()).WillOnce(Return(11.0)); |
| 148 | EXPECT_CALL(*tmock, outputProc(_)); |
| 149 | |
| 150 | // while zone is in auto mode redundant writes should be disabled |
| 151 | EXPECT_FALSE(zone->getRedundantWrite()); |
| 152 | |
| 153 | // but switching from manual to auto enables a single redundant write |
| 154 | zone->setManualMode(true); |
| 155 | zone->setManualMode(false); |
| 156 | EXPECT_TRUE(zone->getRedundantWrite()); |
| 157 | |
| 158 | // after one iteration of a pid loop redundant write should be cleared |
| 159 | zone->processFans(); |
| 160 | EXPECT_FALSE(zone->getRedundantWrite()); |
| 161 | } |
| 162 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 163 | TEST_F(PidZoneTest, RpmSetPoints_AddMaxClear_BehaveAsExpected) |
| 164 | { |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 165 | // Tests addSetPoint, clearSetPoints, determineMaxSetPointRequest |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 166 | // and getMinThermalSetPoint. |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 167 | |
| 168 | // At least one value must be above the minimum thermal setpoint used in |
| 169 | // the constructor otherwise it'll choose that value |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 170 | std::vector<double> values = {100, 200, 300, 400, 500, 5000}; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 171 | for (auto v : values) |
| 172 | { |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 173 | zone->addSetPoint(v, ""); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // This will pull the maximum RPM setpoint request. |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 177 | zone->determineMaxSetPointRequest(); |
| 178 | EXPECT_EQ(5000, zone->getMaxSetPointRequest()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 179 | |
| 180 | // Clear the values, so it'll choose the minimum thermal setpoint. |
Patrick Venture | 9bbf333 | 2019-07-16 10:50:37 -0700 | [diff] [blame] | 181 | zone->clearSetPoints(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 182 | |
| 183 | // 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] | 184 | zone->determineMaxSetPointRequest(); |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 185 | EXPECT_EQ(zone->getMinThermalSetPoint(), zone->getMaxSetPointRequest()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 188 | TEST_F(PidZoneTest, RpmSetPoints_AddBelowMinimum_BehavesAsExpected) |
| 189 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 190 | // Tests adding several RPM setpoints, however, they're all lower than the |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 191 | // configured minimal thermal setpoint RPM value. |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 192 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 193 | std::vector<double> values = {100, 200, 300, 400, 500}; |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 194 | for (auto v : values) |
| 195 | { |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 196 | zone->addSetPoint(v, ""); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // This will pull the maximum RPM setpoint request. |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 200 | zone->determineMaxSetPointRequest(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 201 | |
| 202 | // Verifies the value returned in the minimal thermal rpm set point. |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 203 | EXPECT_EQ(zone->getMinThermalSetPoint(), zone->getMaxSetPointRequest()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 206 | TEST_F(PidZoneTest, GetFailSafePercent_ReturnsExpected) |
| 207 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 208 | // Verify the value used to create the object is stored. |
| 209 | EXPECT_EQ(failSafePercent, zone->getFailSafePercent()); |
| 210 | } |
| 211 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 212 | TEST_F(PidZoneTest, ThermalInputs_FailsafeToValid_ReadsSensors) |
| 213 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 214 | // This test will add a couple thermal inputs, and verify that the zone |
| 215 | // initializes into failsafe mode, and will read each sensor. |
| 216 | |
| 217 | std::string name1 = "temp1"; |
| 218 | int64_t timeout = 1; |
| 219 | |
| 220 | std::unique_ptr<Sensor> sensor1 = |
| 221 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 222 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 223 | |
| 224 | std::string name2 = "temp2"; |
| 225 | std::unique_ptr<Sensor> sensor2 = |
| 226 | std::make_unique<SensorMock>(name2, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 227 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 228 | |
| 229 | std::string type = "unchecked"; |
| 230 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 231 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 232 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 233 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 234 | |
| 235 | // Now that the sensors exist, add them to the zone. |
| 236 | zone->addThermalInput(name1); |
| 237 | zone->addThermalInput(name2); |
| 238 | |
| 239 | // Initialize Zone |
| 240 | zone->initializeCache(); |
| 241 | |
| 242 | // Verify now in failsafe mode. |
| 243 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 244 | |
| 245 | ReadReturn r1; |
| 246 | r1.value = 10.0; |
| 247 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 248 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 249 | |
| 250 | ReadReturn r2; |
| 251 | r2.value = 11.0; |
| 252 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 253 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 254 | |
| 255 | // Read the sensors, this will put the values into the cache. |
| 256 | zone->updateSensors(); |
| 257 | |
| 258 | // We should no longer be in failsafe mode. |
| 259 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 260 | |
| 261 | EXPECT_EQ(r1.value, zone->getCachedValue(name1)); |
| 262 | EXPECT_EQ(r2.value, zone->getCachedValue(name2)); |
| 263 | } |
| 264 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 265 | TEST_F(PidZoneTest, FanInputTest_VerifiesFanValuesCached) |
| 266 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 267 | // This will add a couple fan inputs, and verify the values are cached. |
| 268 | |
| 269 | std::string name1 = "fan1"; |
| 270 | int64_t timeout = 2; |
| 271 | |
| 272 | std::unique_ptr<Sensor> sensor1 = |
| 273 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 274 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 275 | |
| 276 | std::string name2 = "fan2"; |
| 277 | std::unique_ptr<Sensor> sensor2 = |
| 278 | std::make_unique<SensorMock>(name2, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 279 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 280 | |
| 281 | std::string type = "unchecked"; |
| 282 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 283 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 284 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 285 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 286 | |
| 287 | // Now that the sensors exist, add them to the zone. |
| 288 | zone->addFanInput(name1); |
| 289 | zone->addFanInput(name2); |
| 290 | |
| 291 | // Initialize Zone |
| 292 | zone->initializeCache(); |
| 293 | |
| 294 | ReadReturn r1; |
| 295 | r1.value = 10.0; |
| 296 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 297 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 298 | |
| 299 | ReadReturn r2; |
| 300 | r2.value = 11.0; |
| 301 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 302 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 303 | |
| 304 | // Method under test will read through each fan sensor for the zone and |
| 305 | // cache the values. |
| 306 | zone->updateFanTelemetry(); |
| 307 | |
| 308 | EXPECT_EQ(r1.value, zone->getCachedValue(name1)); |
| 309 | EXPECT_EQ(r2.value, zone->getCachedValue(name2)); |
| 310 | } |
| 311 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 312 | TEST_F(PidZoneTest, ThermalInput_ValueTimeoutEntersFailSafeMode) |
| 313 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 314 | // On the second updateSensors call, the updated timestamp will be beyond |
| 315 | // the timeout limit. |
| 316 | |
| 317 | int64_t timeout = 1; |
| 318 | |
| 319 | std::string name1 = "temp1"; |
| 320 | std::unique_ptr<Sensor> sensor1 = |
| 321 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 322 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 323 | |
| 324 | std::string name2 = "temp2"; |
| 325 | std::unique_ptr<Sensor> sensor2 = |
| 326 | std::make_unique<SensorMock>(name2, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 327 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 328 | |
| 329 | std::string type = "unchecked"; |
| 330 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 331 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 332 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 333 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 334 | |
| 335 | zone->addThermalInput(name1); |
| 336 | zone->addThermalInput(name2); |
| 337 | |
| 338 | // Initialize Zone |
| 339 | zone->initializeCache(); |
| 340 | |
| 341 | // Verify now in failsafe mode. |
| 342 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 343 | |
| 344 | ReadReturn r1; |
| 345 | r1.value = 10.0; |
| 346 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 347 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 348 | |
| 349 | ReadReturn r2; |
| 350 | r2.value = 11.0; |
| 351 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 352 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 353 | |
| 354 | zone->updateSensors(); |
| 355 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 356 | |
| 357 | // Ok, so we're not in failsafe mode, so let's set updated to the past. |
| 358 | // sensor1 will have an updated field older than its timeout value, but |
| 359 | // sensor2 will be fine. :D |
| 360 | r1.updated -= std::chrono::seconds(3); |
| 361 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 362 | |
| 363 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 364 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 365 | |
| 366 | // Method under test will read each sensor. One sensor's value is older |
| 367 | // than the timeout for that sensor and this triggers failsafe mode. |
| 368 | zone->updateSensors(); |
| 369 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 370 | } |
| 371 | |
Will Liang | ded0ab5 | 2019-05-15 17:10:06 +0800 | [diff] [blame] | 372 | TEST_F(PidZoneTest, FanInputTest_FailsafeToValid_ReadsSensors) |
| 373 | { |
| 374 | // This will add a couple fan inputs, and verify the values are cached. |
| 375 | |
| 376 | std::string name1 = "fan1"; |
| 377 | int64_t timeout = 2; |
| 378 | |
| 379 | std::unique_ptr<Sensor> sensor1 = |
| 380 | std::make_unique<SensorMock>(name1, timeout); |
| 381 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
| 382 | |
| 383 | std::string name2 = "fan2"; |
| 384 | std::unique_ptr<Sensor> sensor2 = |
| 385 | std::make_unique<SensorMock>(name2, timeout); |
| 386 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
| 387 | |
| 388 | std::string type = "unchecked"; |
| 389 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 390 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 391 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 392 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 393 | |
| 394 | // Now that the sensors exist, add them to the zone. |
| 395 | zone->addFanInput(name1); |
| 396 | zone->addFanInput(name2); |
| 397 | |
| 398 | // Initialize Zone |
| 399 | zone->initializeCache(); |
| 400 | |
| 401 | // Verify now in failsafe mode. |
| 402 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 403 | |
| 404 | ReadReturn r1; |
| 405 | r1.value = 10.0; |
| 406 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 407 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 408 | |
| 409 | ReadReturn r2; |
| 410 | r2.value = 11.0; |
| 411 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 412 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 413 | |
| 414 | // Method under test will read through each fan sensor for the zone and |
| 415 | // cache the values. |
| 416 | zone->updateFanTelemetry(); |
| 417 | |
| 418 | // We should no longer be in failsafe mode. |
| 419 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 420 | |
| 421 | EXPECT_EQ(r1.value, zone->getCachedValue(name1)); |
| 422 | EXPECT_EQ(r2.value, zone->getCachedValue(name2)); |
| 423 | } |
| 424 | |
| 425 | TEST_F(PidZoneTest, FanInputTest_ValueTimeoutEntersFailSafeMode) |
| 426 | { |
| 427 | // This will add a couple fan inputs, and verify the values are cached. |
| 428 | |
| 429 | std::string name1 = "fan1"; |
| 430 | int64_t timeout = 2; |
| 431 | |
| 432 | std::unique_ptr<Sensor> sensor1 = |
| 433 | std::make_unique<SensorMock>(name1, timeout); |
| 434 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
| 435 | |
| 436 | std::string name2 = "fan2"; |
| 437 | std::unique_ptr<Sensor> sensor2 = |
| 438 | std::make_unique<SensorMock>(name2, timeout); |
| 439 | SensorMock* sensor_ptr2 = reinterpret_cast<SensorMock*>(sensor2.get()); |
| 440 | |
| 441 | std::string type = "unchecked"; |
| 442 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 443 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 444 | mgr.addSensor(type, name2, std::move(sensor2)); |
| 445 | EXPECT_EQ(mgr.getSensor(name2), sensor_ptr2); |
| 446 | |
| 447 | // Now that the sensors exist, add them to the zone. |
| 448 | zone->addFanInput(name1); |
| 449 | zone->addFanInput(name2); |
| 450 | |
| 451 | // Initialize Zone |
| 452 | zone->initializeCache(); |
| 453 | |
| 454 | // Verify now in failsafe mode. |
| 455 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 456 | |
| 457 | ReadReturn r1; |
| 458 | r1.value = 10.0; |
| 459 | r1.updated = std::chrono::high_resolution_clock::now(); |
| 460 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 461 | |
| 462 | ReadReturn r2; |
| 463 | r2.value = 11.0; |
| 464 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 465 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 466 | |
| 467 | // Method under test will read through each fan sensor for the zone and |
| 468 | // cache the values. |
| 469 | zone->updateFanTelemetry(); |
| 470 | |
| 471 | // We should no longer be in failsafe mode. |
| 472 | EXPECT_FALSE(zone->getFailSafeMode()); |
| 473 | |
| 474 | r1.updated -= std::chrono::seconds(3); |
| 475 | r2.updated = std::chrono::high_resolution_clock::now(); |
| 476 | |
| 477 | EXPECT_CALL(*sensor_ptr1, read()).WillOnce(Return(r1)); |
| 478 | EXPECT_CALL(*sensor_ptr2, read()).WillOnce(Return(r2)); |
| 479 | |
| 480 | zone->updateFanTelemetry(); |
| 481 | EXPECT_TRUE(zone->getFailSafeMode()); |
| 482 | } |
| 483 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 484 | TEST_F(PidZoneTest, GetSensorTest_ReturnsExpected) |
| 485 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 486 | // One can grab a sensor from the manager through the zone. |
| 487 | |
| 488 | int64_t timeout = 1; |
| 489 | |
| 490 | std::string name1 = "temp1"; |
| 491 | std::unique_ptr<Sensor> sensor1 = |
| 492 | std::make_unique<SensorMock>(name1, timeout); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 493 | SensorMock* sensor_ptr1 = reinterpret_cast<SensorMock*>(sensor1.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 494 | |
| 495 | std::string type = "unchecked"; |
| 496 | mgr.addSensor(type, name1, std::move(sensor1)); |
| 497 | EXPECT_EQ(mgr.getSensor(name1), sensor_ptr1); |
| 498 | |
| 499 | zone->addThermalInput(name1); |
| 500 | |
| 501 | // Verify method under test returns the pointer we expect. |
| 502 | EXPECT_EQ(mgr.getSensor(name1), zone->getSensor(name1)); |
| 503 | } |
| 504 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 505 | TEST_F(PidZoneTest, AddThermalPIDTest_VerifiesThermalPIDsProcessed) |
| 506 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 507 | // Tests adding a thermal PID controller to the zone, and verifies it's |
| 508 | // touched during processing. |
| 509 | |
| 510 | std::unique_ptr<PIDController> tpid = |
| 511 | std::make_unique<ControllerMock>("thermal1", zone.get()); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 512 | ControllerMock* tmock = reinterpret_cast<ControllerMock*>(tpid.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 513 | |
| 514 | // Access the internal pid configuration to clear it out (unrelated to the |
| 515 | // test). |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 516 | ec::pid_info_t* info = tpid->getPIDInfo(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 517 | std::memset(info, 0x00, sizeof(ec::pid_info_t)); |
| 518 | |
| 519 | zone->addThermalPID(std::move(tpid)); |
| 520 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 521 | EXPECT_CALL(*tmock, setptProc()).WillOnce(Return(10.0)); |
| 522 | EXPECT_CALL(*tmock, inputProc()).WillOnce(Return(11.0)); |
| 523 | EXPECT_CALL(*tmock, outputProc(_)); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 524 | |
| 525 | // Method under test will, for each thermal PID, call setpt, input, and |
| 526 | // output. |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 527 | zone->processThermals(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 530 | TEST_F(PidZoneTest, AddFanPIDTest_VerifiesFanPIDsProcessed) |
| 531 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 532 | // Tests adding a fan PID controller to the zone, and verifies it's |
| 533 | // touched during processing. |
| 534 | |
| 535 | std::unique_ptr<PIDController> tpid = |
| 536 | std::make_unique<ControllerMock>("fan1", zone.get()); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 537 | ControllerMock* tmock = reinterpret_cast<ControllerMock*>(tpid.get()); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 538 | |
| 539 | // Access the internal pid configuration to clear it out (unrelated to the |
| 540 | // test). |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 541 | ec::pid_info_t* info = tpid->getPIDInfo(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 542 | std::memset(info, 0x00, sizeof(ec::pid_info_t)); |
| 543 | |
| 544 | zone->addFanPID(std::move(tpid)); |
| 545 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 546 | EXPECT_CALL(*tmock, setptProc()).WillOnce(Return(10.0)); |
| 547 | EXPECT_CALL(*tmock, inputProc()).WillOnce(Return(11.0)); |
| 548 | EXPECT_CALL(*tmock, outputProc(_)); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 549 | |
| 550 | // 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] | 551 | zone->processFans(); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 554 | TEST_F(PidZoneTest, ManualModeDbusTest_VerifySetManualBehavesAsExpected) |
| 555 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 556 | // The manual(bool) method is inherited from the dbus mode interface. |
| 557 | |
| 558 | // Verifies that someone doesn't remove the internal call to the dbus |
| 559 | // object from which we're inheriting. |
| 560 | EXPECT_CALL(sdbus_mock_mode, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 561 | sd_bus_emit_properties_changed_strv( |
| 562 | IsNull(), StrEq(objPath), StrEq(modeInterface), NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 563 | .WillOnce(Invoke( |
| 564 | [&]([[maybe_unused]] sd_bus* bus, [[maybe_unused]] const char* path, |
| 565 | [[maybe_unused]] const char* interface, const char** names) { |
| 566 | EXPECT_STREQ("Manual", names[0]); |
| 567 | return 0; |
| 568 | })); |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 569 | |
| 570 | // Method under test will set the manual mode to true and broadcast this |
| 571 | // change on dbus. |
| 572 | zone->manual(true); |
| 573 | EXPECT_TRUE(zone->getManualMode()); |
| 574 | } |
| 575 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 576 | TEST_F(PidZoneTest, FailsafeDbusTest_VerifiesReturnsExpected) |
| 577 | { |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 578 | // This property is implemented by us as read-only, such that trying to |
| 579 | // write to it will have no effect. |
| 580 | EXPECT_EQ(zone->failSafe(), zone->getFailSafeMode()); |
| 581 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 582 | |
| 583 | } // namespace |
| 584 | } // namespace pid_control |