1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "thermal_hidl_target_stress_test"
18
19 #include <android-base/logging.h>
20 #include <android/hardware/thermal/1.0/IThermal.h>
21 #include <android/hardware/thermal/1.0/types.h>
22
23 #include <gtest/gtest.h>
24 #include <chrono>
25 #include <condition_variable>
26 #include <mutex>
27
28 using ::android::hardware::hidl_vec;
29 using ::android::hardware::thermal::V1_0::CoolingDevice;
30 using ::android::hardware::thermal::V1_0::CpuUsage;
31 using ::android::hardware::thermal::V1_0::IThermal;
32 using ::android::hardware::thermal::V1_0::Temperature;
33 using ::android::hardware::thermal::V1_0::ThermalStatus;
34 using ::android::hardware::thermal::V1_0::ThermalStatusCode;
35 using ::android::sp;
36
37 #define NUMBER_LOOPS 100
38 #define TIMEOUT_PERIOD 1
39
40 class ThermalHidlStressTest : public ::testing::Test {
41 public:
SetUp()42 virtual void SetUp() override {
43 thermal_ = IThermal::getService();
44 ASSERT_NE(thermal_, nullptr);
45 count_ = 0;
46 }
47
48 /* Test code calls this function to wait for callback */
wait()49 inline std::cv_status wait() {
50 std::unique_lock<std::mutex> lock(mtx_);
51
52 std::cv_status status = std::cv_status::no_timeout;
53 auto now = std::chrono::system_clock::now();
54 while (count_ == 0) {
55 status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
56 if (status == std::cv_status::timeout) return status;
57 }
58 count_--;
59 return status;
60 }
61
62 protected:
63 sp<IThermal> thermal_;
64 std::mutex mtx_;
65 std::condition_variable cv_;
66 int count_;
67 };
68
69 /* Stress test for Thermal::getTemperatures(). */
TEST_F(ThermalHidlStressTest,stressTemperatures)70 TEST_F(ThermalHidlStressTest, stressTemperatures) {
71 for (int loops = 0; loops < NUMBER_LOOPS; loops++) {
72 thermal_->getTemperatures(
73 [&](ThermalStatus status, hidl_vec<Temperature> /* temperatures */) {
74 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
75 /* Inform the test about callback. */
76 std::unique_lock<std::mutex> lock(mtx_);
77 count_++;
78 cv_.notify_one();
79 });
80 EXPECT_EQ(std::cv_status::no_timeout, wait());
81 }
82 }
83
84 /* Stress test for Thermal::getCpuUsages(). */
TEST_F(ThermalHidlStressTest,stressCpuUsages)85 TEST_F(ThermalHidlStressTest, stressCpuUsages) {
86 for (int loops = 0; loops < NUMBER_LOOPS; loops++) {
87 thermal_->getCpuUsages(
88 [&](ThermalStatus status, hidl_vec<CpuUsage> /* cpuUsages */) {
89 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
90 /* Inform the test about callback. */
91 std::unique_lock<std::mutex> lock(mtx_);
92 count_++;
93 cv_.notify_one();
94 });
95 EXPECT_EQ(std::cv_status::no_timeout, wait());
96 }
97 }
98
99 /* Stress test for Thermal::getCoolingDevices(). */
TEST_F(ThermalHidlStressTest,stressCoolingDevices)100 TEST_F(ThermalHidlStressTest, stressCoolingDevices) {
101 for (int loops = 0; loops < NUMBER_LOOPS; loops++) {
102 thermal_->getCoolingDevices(
103 [&](ThermalStatus status, hidl_vec<CoolingDevice> /* coolingDevice */) {
104 EXPECT_EQ(ThermalStatusCode::SUCCESS, status.code);
105 /* Inform the test about callback. */
106 std::unique_lock<std::mutex> lock(mtx_);
107 count_++;
108 cv_.notify_one();
109 });
110 EXPECT_EQ(std::cv_status::no_timeout, wait());
111 }
112 }
113