Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 1 | #include "../fallback.hpp" |
| 2 | |
| 3 | #include "../psensor.hpp" |
| 4 | |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 5 | #include <iostream> |
| 6 | #include <string> |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 7 | |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 9 | |
| 10 | int pstate = -1; |
| 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace fan |
| 15 | { |
| 16 | namespace presence |
| 17 | { |
Mike Capps | a081956 | 2022-06-13 10:17:10 -0400 | [diff] [blame] | 18 | void setPresence(const Fan&, bool newState) |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 19 | { |
| 20 | if (newState) |
| 21 | { |
| 22 | pstate = 1; |
| 23 | } |
| 24 | else |
| 25 | { |
| 26 | pstate = 0; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | class TestSensor : public PresenceSensor |
| 31 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 32 | public: |
| 33 | bool start() override |
| 34 | { |
| 35 | ++started; |
| 36 | return _present; |
| 37 | } |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 38 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 39 | void stop() override |
| 40 | { |
| 41 | ++stopped; |
| 42 | } |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 43 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 44 | bool present() override |
| 45 | { |
| 46 | return _present; |
| 47 | } |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 48 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 49 | void fail() override |
| 50 | { |
| 51 | ++failed; |
| 52 | } |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 53 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 54 | bool _present = false; |
| 55 | size_t started = 0; |
| 56 | size_t stopped = 0; |
| 57 | size_t failed = 0; |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } // namespace presence |
| 61 | } // namespace fan |
| 62 | } // namespace phosphor |
| 63 | |
| 64 | using namespace phosphor::fan::presence; |
| 65 | |
| 66 | TEST(FallbackTest, TestOne) |
| 67 | { |
| 68 | // Validate a single sensor. |
| 69 | // Validate on->off->on. |
| 70 | pstate = -1; |
Mike Capps | a081956 | 2022-06-13 10:17:10 -0400 | [diff] [blame] | 71 | Fan fan{"/path", "name", 0}; |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 72 | TestSensor ts; |
| 73 | ts._present = true; |
| 74 | std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts}; |
| 75 | Fallback f{fan, sensors}; |
| 76 | |
| 77 | f.monitor(); |
| 78 | ASSERT_EQ(pstate, 1); |
| 79 | ASSERT_EQ(ts.failed, 0); |
| 80 | ASSERT_EQ(ts.stopped, 0); |
| 81 | ASSERT_EQ(ts.started, 1); |
| 82 | |
| 83 | f.stateChanged(false); |
| 84 | ASSERT_EQ(pstate, 0); |
| 85 | ASSERT_EQ(ts.failed, 0); |
| 86 | ASSERT_EQ(ts.stopped, 0); |
| 87 | ASSERT_EQ(ts.started, 1); |
| 88 | |
| 89 | f.stateChanged(true); |
| 90 | ASSERT_EQ(pstate, 1); |
| 91 | ASSERT_EQ(ts.failed, 0); |
| 92 | ASSERT_EQ(ts.stopped, 0); |
| 93 | ASSERT_EQ(ts.started, 1); |
| 94 | } |
| 95 | |
| 96 | TEST(FallbackTest, TestTwoA) |
| 97 | { |
| 98 | // Validate two sensors. |
| 99 | // Validate both sensors on->off->on |
| 100 | |
| 101 | pstate = -1; |
Mike Capps | a081956 | 2022-06-13 10:17:10 -0400 | [diff] [blame] | 102 | Fan fan{"/path", "name", 0}; |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 103 | TestSensor ts1, ts2; |
| 104 | ts1._present = true; |
| 105 | ts2._present = true; |
| 106 | |
| 107 | std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; |
| 108 | Fallback f{fan, sensors}; |
| 109 | |
| 110 | f.monitor(); |
| 111 | ASSERT_EQ(pstate, 1); |
| 112 | ASSERT_EQ(ts1.failed, 0); |
| 113 | ASSERT_EQ(ts1.stopped, 0); |
| 114 | ASSERT_EQ(ts1.started, 1); |
| 115 | ASSERT_EQ(ts2.failed, 0); |
| 116 | ASSERT_EQ(ts2.stopped, 0); |
| 117 | ASSERT_EQ(ts2.started, 0); |
| 118 | |
| 119 | ts1._present = false; |
| 120 | ts2._present = false; |
| 121 | f.stateChanged(false); |
| 122 | ASSERT_EQ(pstate, 0); |
| 123 | ASSERT_EQ(ts1.failed, 0); |
| 124 | ASSERT_EQ(ts1.stopped, 0); |
| 125 | ASSERT_EQ(ts1.started, 1); |
| 126 | ASSERT_EQ(ts2.failed, 0); |
| 127 | ASSERT_EQ(ts2.stopped, 0); |
| 128 | ASSERT_EQ(ts2.started, 0); |
| 129 | |
| 130 | ts1._present = true; |
| 131 | ts2._present = true; |
| 132 | f.stateChanged(true); |
| 133 | ASSERT_EQ(pstate, 1); |
| 134 | ASSERT_EQ(ts1.failed, 0); |
| 135 | ASSERT_EQ(ts1.stopped, 0); |
| 136 | ASSERT_EQ(ts1.started, 1); |
| 137 | ASSERT_EQ(ts2.failed, 0); |
| 138 | ASSERT_EQ(ts2.stopped, 0); |
| 139 | ASSERT_EQ(ts2.started, 0); |
| 140 | } |
| 141 | |
Brad Bishop | 7cd75d7 | 2017-07-13 01:42:26 -0400 | [diff] [blame] | 142 | TEST(FallbackTest, TestTwoB) |
| 143 | { |
| 144 | // Validate two sensors. |
| 145 | // Validate first sensor on->off. |
| 146 | |
| 147 | pstate = -1; |
| 148 | Fan fan{"/path", "name"}; |
| 149 | TestSensor ts1, ts2; |
| 150 | ts1._present = true; |
| 151 | ts2._present = true; |
| 152 | |
| 153 | std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; |
| 154 | Fallback f{fan, sensors}; |
| 155 | |
| 156 | f.monitor(); |
| 157 | ASSERT_EQ(pstate, 1); |
| 158 | ts1._present = false; |
| 159 | f.stateChanged(false); |
| 160 | ASSERT_EQ(pstate, 1); |
| 161 | ASSERT_EQ(ts1.failed, 1); |
| 162 | ASSERT_EQ(ts1.stopped, 1); |
| 163 | ASSERT_EQ(ts1.started, 1); |
| 164 | ASSERT_EQ(ts2.failed, 0); |
| 165 | ASSERT_EQ(ts2.stopped, 0); |
| 166 | ASSERT_EQ(ts2.started, 1); |
| 167 | |
| 168 | // Flip the state of both sensors. |
| 169 | ts1._present = true; |
| 170 | ts2._present = false; |
| 171 | f.stateChanged(false); |
| 172 | ASSERT_EQ(pstate, 0); |
| 173 | ASSERT_EQ(ts1.failed, 1); |
| 174 | ASSERT_EQ(ts1.stopped, 1); |
| 175 | ASSERT_EQ(ts1.started, 1); |
| 176 | ASSERT_EQ(ts2.failed, 0); |
| 177 | ASSERT_EQ(ts2.stopped, 0); |
| 178 | ASSERT_EQ(ts2.started, 1); |
| 179 | } |
| 180 | |
| 181 | TEST(FallbackTest, TestTwoC) |
| 182 | { |
| 183 | // Validate two sensors. |
| 184 | // Validate first in bad state. |
| 185 | |
| 186 | pstate = -1; |
| 187 | Fan fan{"/path", "name"}; |
| 188 | TestSensor ts1, ts2; |
| 189 | ts1._present = false; |
| 190 | ts2._present = true; |
| 191 | |
| 192 | std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; |
| 193 | Fallback f{fan, sensors}; |
| 194 | |
| 195 | f.monitor(); |
| 196 | ASSERT_EQ(pstate, 1); |
| 197 | ASSERT_EQ(ts1.failed, 1); |
| 198 | ASSERT_EQ(ts1.stopped, 0); |
| 199 | ASSERT_EQ(ts1.started, 0); |
| 200 | ASSERT_EQ(ts2.failed, 0); |
| 201 | ASSERT_EQ(ts2.stopped, 0); |
| 202 | ASSERT_EQ(ts2.started, 1); |
| 203 | |
| 204 | f.stateChanged(false); |
| 205 | ASSERT_EQ(pstate, 0); |
| 206 | ASSERT_EQ(ts1.failed, 1); |
| 207 | ASSERT_EQ(ts1.stopped, 0); |
| 208 | ASSERT_EQ(ts1.started, 0); |
| 209 | ASSERT_EQ(ts2.failed, 0); |
| 210 | ASSERT_EQ(ts2.stopped, 0); |
| 211 | ASSERT_EQ(ts2.started, 1); |
| 212 | } |
| 213 | |
| 214 | TEST(FallbackTest, TestTwoD) |
| 215 | { |
| 216 | // Validate two sensors. |
| 217 | // Validate both in bad state. |
| 218 | |
| 219 | pstate = -1; |
| 220 | Fan fan{"/path", "name"}; |
| 221 | TestSensor ts1, ts2; |
| 222 | ts1._present = false; |
| 223 | ts2._present = false; |
| 224 | |
| 225 | std::vector<std::reference_wrapper<PresenceSensor>> sensors{ts1, ts2}; |
| 226 | Fallback f{fan, sensors}; |
| 227 | |
| 228 | f.monitor(); |
| 229 | ASSERT_EQ(pstate, 0); |
| 230 | ASSERT_EQ(ts1.failed, 0); |
| 231 | ASSERT_EQ(ts1.stopped, 0); |
| 232 | ASSERT_EQ(ts1.started, 1); |
| 233 | ASSERT_EQ(ts2.failed, 0); |
| 234 | ASSERT_EQ(ts2.stopped, 0); |
| 235 | ASSERT_EQ(ts2.started, 0); |
| 236 | } |