1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "fan_fault_detect_test.h"
17
18 #include <atomic>
19 #include <condition_variable>
20 #include <mutex>
21
22 #include "hisysevent.h"
23 #include "hisysevent_listener.h"
24 #include "hisysevent_manager.h"
25 #include "hisysevent_record.h"
26 #include "thermal_log.h"
27
28 using namespace testing::ext;
29 using namespace OHOS::HiviewDFX;
30 using namespace OHOS::PowerMgr;
31
32 namespace {
33 const std::string DOMAIN = "THERMAL";
34 const std::string EVENT = "FAN_FAULT";
35 const std::string TAG = "ID";
36 const std::string FAN = "fan";
37 const std::string GPU = "gpu";
38 const std::string SOC = "soc";
39 const int32_t FAN_SLOW_THERHOLD = 500;
40 const int32_t FAN_FAST_THERHOLD = 1500;
41 const int32_t TEMP_HIGH_THERHOLD = 50000;
42 const int32_t TEMP_LOW_THERHOLD = 30000;
43 const int32_t FAN_SLOW_SPEED = 400;
44 const int32_t FAN_FAST_SPEED = 1600;
45 const int32_t TEMP_HIGH = 60000;
46 const int32_t TEMP_LOW = 20000;
47 const int64_t TIME_OUT = 2;
48 std::mutex g_mutex;
49 std::condition_variable g_callbackCV;
50 std::atomic_bool g_eventTriggered = false;
51
52 class Watcher : public HiSysEventListener {
53 public:
Watcher(std::function<void (std::shared_ptr<HiSysEventRecord>)> func)54 explicit Watcher(std::function<void(std::shared_ptr<HiSysEventRecord>)> func) : func_(func) {}
~Watcher()55 virtual ~Watcher() {}
56
OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)57 void OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent) final
58 {
59 if (sysEvent == nullptr || func_ == nullptr) {
60 return;
61 }
62 func_(sysEvent);
63 }
64
OnServiceDied()65 void OnServiceDied() final {}
66
67 private:
68 std::function<void(std::shared_ptr<HiSysEventRecord>)> func_;
69 };
70 } // namespace
71
SetUpTestCase()72 void FanFaultDetectTest::SetUpTestCase() {}
73
TearDownTestCase()74 void FanFaultDetectTest::TearDownTestCase() {}
75
SetUp()76 void FanFaultDetectTest::SetUp() {}
77
TearDown()78 void FanFaultDetectTest::TearDown() {}
79
InitFanFaultInfoMap(const std::shared_ptr<FanFaultDetect> & fanFaultDetect)80 void FanFaultDetectTest::InitFanFaultInfoMap(const std::shared_ptr<FanFaultDetect>& fanFaultDetect)
81 {
82 FanSensorInfo fanSlowSensorInfo;
83 fanSlowSensorInfo.insert(std::make_pair(FAN, FAN_SLOW_THERHOLD));
84 fanSlowSensorInfo.insert(std::make_pair(SOC, TEMP_HIGH_THERHOLD));
85 fanSlowSensorInfo.insert(std::make_pair(GPU, TEMP_HIGH_THERHOLD));
86
87 FanSensorInfo fanFastSensorInfo;
88 fanFastSensorInfo.insert(std::make_pair(FAN, FAN_FAST_THERHOLD));
89 fanFastSensorInfo.insert(std::make_pair(SOC, TEMP_LOW_THERHOLD));
90 fanFastSensorInfo.insert(std::make_pair(GPU, TEMP_LOW_THERHOLD));
91
92 FanFaultInfoMap fanFaultInfoMap;
93 fanFaultInfoMap.insert(std::make_pair(FAN_FAULT_TOO_SLOW, fanSlowSensorInfo));
94 fanFaultInfoMap.insert(std::make_pair(FAN_FAULT_TOO_FAST, fanFastSensorInfo));
95
96 fanFaultDetect->SetFaultInfoMap(fanFaultInfoMap);
97 }
98
GetFaultId(int64_t & faultId,const FanSensorInfo & report)99 void FanFaultDetectTest::GetFaultId(int64_t& faultId, const FanSensorInfo& report)
100 {
101 std::shared_ptr<FanFaultDetect> fanFaultDetect = std::make_shared<FanFaultDetect>();
102 EXPECT_NE(fanFaultDetect, nullptr);
103 InitFanFaultInfoMap(fanFaultDetect);
104
105 auto watcher = std::make_shared<Watcher>([&faultId] (std::shared_ptr<HiSysEventRecord> sysEvent) {
106 if (sysEvent == nullptr) {
107 return;
108 }
109 sysEvent->GetParamValue(TAG, faultId);
110 g_eventTriggered = true;
111 g_callbackCV.notify_one();
112 });
113
114 OHOS::HiviewDFX::ListenerRule listenerRule(DOMAIN, EVENT, OHOS::HiviewDFX::RuleType::WHOLE_WORD);
115 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
116 sysRules.emplace_back(listenerRule);
117 auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
118 EXPECT_TRUE(ret == SUCCESS);
119
120 fanFaultDetect->OnFanSensorInfoChanged(report);
121 std::unique_lock<std::mutex> lock(g_mutex);
122 g_callbackCV.wait_for(lock, std::chrono::seconds(TIME_OUT), [] {
123 return g_eventTriggered.load();
124 });
125 g_eventTriggered = false;
126
127 ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher);
128 EXPECT_TRUE(ret == SUCCESS);
129 }
130
131 namespace {
132 #if EVENT_FAN
133 /**
134 * @tc.name: FanFaultDetectTest001
135 * @tc.desc: test class FanFaultDetectTest function
136 * @tc.type: FUNC
137 */
138 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest001, TestSize.Level0)
139 {
140 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest001 start");
141 int64_t faultId = FAN_FAULT_OK;
142 FanSensorInfo report;
143 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
144 report.insert(std::make_pair(SOC, TEMP_HIGH));
145 report.insert(std::make_pair(GPU, TEMP_HIGH));
146 GetFaultId(faultId, report);
147 EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW);
148 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest001 end");
149 }
150
151 /**
152 * @tc.name: FanFaultDetectTest002
153 * @tc.desc: test class FanFaultDetectTest function
154 * @tc.type: FUNC
155 */
156 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest002, TestSize.Level0)
157 {
158 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest002 start");
159 int64_t faultId = FAN_FAULT_OK;
160 FanSensorInfo report;
161 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
162 report.insert(std::make_pair(SOC, TEMP_HIGH));
163 report.insert(std::make_pair(GPU, TEMP_LOW));
164 GetFaultId(faultId, report);
165 EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW);
166 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest002 end");
167 }
168
169 /**
170 * @tc.name: FanFaultDetectTest003
171 * @tc.desc: test class FanFaultDetectTest function
172 * @tc.type: FUNC
173 */
174 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest003, TestSize.Level0)
175 {
176 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest003 start");
177 int64_t faultId = FAN_FAULT_OK;
178 FanSensorInfo report;
179 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
180 report.insert(std::make_pair(SOC, TEMP_LOW));
181 report.insert(std::make_pair(GPU, TEMP_HIGH));
182 GetFaultId(faultId, report);
183 EXPECT_TRUE(faultId == FAN_FAULT_TOO_SLOW);
184 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest003 end");
185 }
186
187 /**
188 * @tc.name: FanFaultDetectTest004
189 * @tc.desc: test class FanFaultDetectTest function
190 * @tc.type: FUNC
191 */
192 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest004, TestSize.Level0)
193 {
194 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest004 start");
195 int64_t faultId = FAN_FAULT_OK;
196 FanSensorInfo report;
197 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
198 report.insert(std::make_pair(SOC, TEMP_LOW));
199 report.insert(std::make_pair(GPU, TEMP_LOW));
200 GetFaultId(faultId, report);
201 EXPECT_TRUE(faultId == FAN_FAULT_TOO_FAST);
202 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest004 end");
203 }
204 #endif
205
206 /**
207 * @tc.name: FanFaultDetectTest005
208 * @tc.desc: test class FanFaultDetectTest function
209 * @tc.type: FUNC
210 */
211 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest005, TestSize.Level0)
212 {
213 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest005 start");
214 int64_t faultId = FAN_FAULT_OK;
215 FanSensorInfo report;
216 report.insert(std::make_pair(FAN, FAN_SLOW_SPEED));
217 report.insert(std::make_pair(SOC, TEMP_LOW));
218 report.insert(std::make_pair(GPU, TEMP_LOW));
219 GetFaultId(faultId, report);
220 EXPECT_TRUE(faultId == FAN_FAULT_OK);
221 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest005 end");
222 }
223
224 /**
225 * @tc.name: FanFaultDetectTest006
226 * @tc.desc: test class FanFaultDetectTest function
227 * @tc.type: FUNC
228 */
229 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest006, TestSize.Level0)
230 {
231 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest006 start");
232 int64_t faultId = FAN_FAULT_OK;
233 FanSensorInfo report;
234 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
235 report.insert(std::make_pair(SOC, TEMP_HIGH));
236 report.insert(std::make_pair(GPU, TEMP_LOW));
237 GetFaultId(faultId, report);
238 EXPECT_TRUE(faultId == FAN_FAULT_OK);
239 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest006 end");
240 }
241
242 /**
243 * @tc.name: FanFaultDetectTest007
244 * @tc.desc: test class FanFaultDetectTest function
245 * @tc.type: FUNC
246 */
247 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest007, TestSize.Level0)
248 {
249 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest007 start");
250 int64_t faultId = FAN_FAULT_OK;
251 FanSensorInfo report;
252 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
253 report.insert(std::make_pair(SOC, TEMP_LOW));
254 report.insert(std::make_pair(GPU, TEMP_HIGH));
255 GetFaultId(faultId, report);
256 EXPECT_TRUE(faultId == FAN_FAULT_OK);
257 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest007 end");
258 }
259
260 /**
261 * @tc.name: FanFaultDetectTest008
262 * @tc.desc: test class FanFaultDetectTest function
263 * @tc.type: FUNC
264 */
265 HWTEST_F(FanFaultDetectTest, FanFaultDetectTest008, TestSize.Level0)
266 {
267 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest008 start");
268 int64_t faultId = FAN_FAULT_OK;
269 FanSensorInfo report;
270 report.insert(std::make_pair(FAN, FAN_FAST_SPEED));
271 report.insert(std::make_pair(SOC, TEMP_HIGH));
272 report.insert(std::make_pair(GPU, TEMP_HIGH));
273 GetFaultId(faultId, report);
274 EXPECT_TRUE(faultId == FAN_FAULT_OK);
275 THERMAL_HILOGD(LABEL_TEST, "FanFaultDetectTest008 end");
276 }
277 } // namespace
278