Tri Vo | 1817750 | 2018-10-20 16:11:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #include <hardware_legacy/power.h> |
| 18 | |
| 19 | #include <string> |
| 20 | #include <thread> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | // Stress test acquiring/releasing WakeLocks. |
| 28 | TEST(LibpowerTest, WakeLockStressTest) { |
| 29 | // numThreads threads will acquire/release numLocks locks each. |
| 30 | constexpr int numThreads = 20; |
| 31 | constexpr int numLocks = 10000; |
| 32 | std::vector<std::thread> tds; |
| 33 | |
| 34 | for (int i = 0; i < numThreads; i++) { |
| 35 | tds.emplace_back([i] { |
| 36 | for (int j = 0; j < numLocks; j++) { |
| 37 | // We want ids to be unique. |
| 38 | std::string id = std::to_string(i) + "/" + std::to_string(j); |
| 39 | ASSERT_EQ(acquire_wake_lock(PARTIAL_WAKE_LOCK, id.c_str()), 0); |
| 40 | ASSERT_EQ(release_wake_lock(id.c_str()), 0); |
| 41 | } |
| 42 | }); |
| 43 | } |
| 44 | for (auto& td : tds) { |
| 45 | td.join(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | } // namespace android |