blob: bd91efa1ba158648db48c567aa1e1e204d0d907e [file] [log] [blame]
Andy Hung5c6d68a2022-03-09 21:54:59 -08001/*
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
25using namespace android::mediautils;
Andy Hunga2a1ac32022-03-18 16:12:11 -070026using namespace std::chrono_literals;
27
28namespace {
Andy Hung5c6d68a2022-03-09 21:54:59 -080029TEST(timecheck_tests, success) {
30 bool timeoutRegistered = false;
31 float elapsedMsRegistered = 0.f;
32 bool event = false;
33
34 {
35 TimeCheck timeCheck("success",
36 [&event, &timeoutRegistered, &elapsedMsRegistered]
37 (bool timeout, float elapsedMs) {
38 timeoutRegistered = timeout;
39 elapsedMsRegistered = elapsedMs;
40 event = true;
Andy Hungf8ab0932022-06-13 19:49:43 -070041 }, 1000ms /* timeoutDuration */, {} /* secondChanceDuration */, false /* crash */);
Andy Hung5c6d68a2022-03-09 21:54:59 -080042 }
43 ASSERT_TRUE(event);
44 ASSERT_FALSE(timeoutRegistered);
45 ASSERT_GT(elapsedMsRegistered, 0.f);
46}
47
48TEST(timecheck_tests, timeout) {
49 bool timeoutRegistered = false;
50 float elapsedMsRegistered = 0.f;
51 std::atomic_bool event = false; // seq-cst implies acquire-release
52
53 {
54 TimeCheck timeCheck("timeout",
55 [&event, &timeoutRegistered, &elapsedMsRegistered]
56 (bool timeout, float elapsedMs) {
57 timeoutRegistered = timeout;
58 elapsedMsRegistered = elapsedMs;
59 event = true; // store-release, must be last.
Andy Hungf8ab0932022-06-13 19:49:43 -070060 }, 1ms /* timeoutDuration */, {} /* secondChanceDuration */, false /* crash */);
Andy Hunga2a1ac32022-03-18 16:12:11 -070061 std::this_thread::sleep_for(100ms);
Andy Hung5c6d68a2022-03-09 21:54:59 -080062 }
63 ASSERT_TRUE(event); // load-acquire, must be first.
64 ASSERT_TRUE(timeoutRegistered); // only called once on failure, not on dealloc.
65 ASSERT_GT(elapsedMsRegistered, 0.f);
66}
67
68// Note: We do not test TimeCheck crash because TimeCheck is multithreaded and the
69// EXPECT_EXIT() signal catching is imperfect due to the gtest fork.
Andy Hunga2a1ac32022-03-18 16:12:11 -070070
Atneya Naircce14202022-09-05 20:17:50 -070071// Note, the following test is to manually verify the correct thread is aborted.
72// Due to difficulties with gtest and EXPECT_EXIT, this is difficult to verify
73// automatically. TODO(b/246446561) Attempt to use EXPECT_EXIT
74
75#if 0
76void threadFunction() {
77 bool timeoutRegistered = false;
78 float elapsedMsRegistered = 0.f;
79 std::atomic_bool event = false; // seq-cst implies acquire-release
80 {
81 TimeCheck timeCheck("timeout",
82 [&event, &timeoutRegistered, &elapsedMsRegistered]
83 (bool timeout, float elapsedMs) {
84 timeoutRegistered = timeout;
85 elapsedMsRegistered = elapsedMs;
86 event = true; // store-release, must be last.
87 }, 1ms /* timeoutDuration */, {} /* secondChanceDuration */, true /* crash */);
88 std::this_thread::sleep_for(100ms);
89 ADD_FAILURE();
90 }
91}
92
93TEST(timecheck_tests, death) {
94 std::thread mthread{threadFunction};
95 mthread.join();
96}
97#endif
98
Andy Hungf8ab0932022-06-13 19:49:43 -070099} // namespace
Atneya Naircce14202022-09-05 20:17:50 -0700100