blob: 9833dc9210beec5dbd436c69516f56f5ec947862 [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;
26
27TEST(timecheck_tests, success) {
28 bool timeoutRegistered = false;
29 float elapsedMsRegistered = 0.f;
30 bool event = false;
31
32 {
33 TimeCheck timeCheck("success",
34 [&event, &timeoutRegistered, &elapsedMsRegistered]
35 (bool timeout, float elapsedMs) {
36 timeoutRegistered = timeout;
37 elapsedMsRegistered = elapsedMs;
38 event = true;
39 }, 1000 /* msec */, false /* crash */);
40 }
41 ASSERT_TRUE(event);
42 ASSERT_FALSE(timeoutRegistered);
43 ASSERT_GT(elapsedMsRegistered, 0.f);
44}
45
46TEST(timecheck_tests, timeout) {
47 bool timeoutRegistered = false;
48 float elapsedMsRegistered = 0.f;
49 std::atomic_bool event = false; // seq-cst implies acquire-release
50
51 {
52 TimeCheck timeCheck("timeout",
53 [&event, &timeoutRegistered, &elapsedMsRegistered]
54 (bool timeout, float elapsedMs) {
55 timeoutRegistered = timeout;
56 elapsedMsRegistered = elapsedMs;
57 event = true; // store-release, must be last.
58 }, 1 /* msec */, false /* crash */);
59 usleep(100 * 1000 /* usec */); // extra time as callback called by different thread.
60 }
61 ASSERT_TRUE(event); // load-acquire, must be first.
62 ASSERT_TRUE(timeoutRegistered); // only called once on failure, not on dealloc.
63 ASSERT_GT(elapsedMsRegistered, 0.f);
64}
65
66// Note: We do not test TimeCheck crash because TimeCheck is multithreaded and the
67// EXPECT_EXIT() signal catching is imperfect due to the gtest fork.