Andy Hung | 5c6d68a | 2022-03-09 21:54:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "timecheck_tests" |
| 18 | |
| 19 | #include <mediautils/TimeCheck.h> |
| 20 | |
| 21 | #include <atomic> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <utils/Log.h> |
| 24 | |
| 25 | using namespace android::mediautils; |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 26 | using namespace std::chrono_literals; |
| 27 | |
| 28 | namespace { |
Andy Hung | 5c6d68a | 2022-03-09 21:54:59 -0800 | [diff] [blame] | 29 | |
| 30 | TEST(timecheck_tests, success) { |
| 31 | bool timeoutRegistered = false; |
| 32 | float elapsedMsRegistered = 0.f; |
| 33 | bool event = false; |
| 34 | |
| 35 | { |
| 36 | TimeCheck timeCheck("success", |
| 37 | [&event, &timeoutRegistered, &elapsedMsRegistered] |
| 38 | (bool timeout, float elapsedMs) { |
| 39 | timeoutRegistered = timeout; |
| 40 | elapsedMsRegistered = elapsedMs; |
| 41 | event = true; |
| 42 | }, 1000 /* msec */, false /* crash */); |
| 43 | } |
| 44 | ASSERT_TRUE(event); |
| 45 | ASSERT_FALSE(timeoutRegistered); |
| 46 | ASSERT_GT(elapsedMsRegistered, 0.f); |
| 47 | } |
| 48 | |
| 49 | TEST(timecheck_tests, timeout) { |
| 50 | bool timeoutRegistered = false; |
| 51 | float elapsedMsRegistered = 0.f; |
| 52 | std::atomic_bool event = false; // seq-cst implies acquire-release |
| 53 | |
| 54 | { |
| 55 | TimeCheck timeCheck("timeout", |
| 56 | [&event, &timeoutRegistered, &elapsedMsRegistered] |
| 57 | (bool timeout, float elapsedMs) { |
| 58 | timeoutRegistered = timeout; |
| 59 | elapsedMsRegistered = elapsedMs; |
| 60 | event = true; // store-release, must be last. |
| 61 | }, 1 /* msec */, false /* crash */); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 62 | std::this_thread::sleep_for(100ms); |
Andy Hung | 5c6d68a | 2022-03-09 21:54:59 -0800 | [diff] [blame] | 63 | } |
| 64 | ASSERT_TRUE(event); // load-acquire, must be first. |
| 65 | ASSERT_TRUE(timeoutRegistered); // only called once on failure, not on dealloc. |
| 66 | ASSERT_GT(elapsedMsRegistered, 0.f); |
| 67 | } |
| 68 | |
| 69 | // Note: We do not test TimeCheck crash because TimeCheck is multithreaded and the |
| 70 | // EXPECT_EXIT() signal catching is imperfect due to the gtest fork. |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 71 | |
| 72 | } // namespace |