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 #include <mediautils/ThreadSnapshot.h> 18 #include <mediautils/TidWrapper.h> 19 20 #define LOG_TAG "media_threadsnapshot_tests" 21 22 #include <gtest/gtest.h> 23 #include <utils/Log.h> 24 25 #include <chrono> 26 #include <thread> 27 28 using namespace android; 29 using namespace android::mediautils; 30 31 // Disables false-positives from base::Split() 32 // 33 // See mismatched sanitized libraries here: 34 // https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow __asan_default_options()35extern "C" const char* __asan_default_options() { 36 return "detect_container_overflow=0"; 37 } 38 TEST(media_threadsnapshot_tests,basic)39TEST(media_threadsnapshot_tests, basic) { 40 using namespace std::chrono_literals; 41 42 ThreadSnapshot threadSnapshot(getThreadIdWrapper()); 43 44 threadSnapshot.onBegin(); 45 46 std::string snapshot1 = threadSnapshot.toString(); 47 48 std::this_thread::sleep_for(100ms); 49 50 threadSnapshot.onEnd(); 51 52 std::string snapshot2 = threadSnapshot.toString(); 53 54 // Either we can't get a snapshot, or they must be different when taken when thread is running. 55 if (snapshot1.empty()) { 56 ASSERT_TRUE(snapshot2.empty()); 57 } else { 58 ASSERT_FALSE(snapshot2.empty()); 59 ASSERT_NE(snapshot1, snapshot2); 60 } 61 } 62