1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <string>
18
19 #include "hitrace_dump.h"
20 #include "trace_file_utils.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace Hitrace {
25 using namespace testing::ext;
26 class HitraceAsyncReadTimeoutTest : public testing::Test {
27 public:
SetUpTestCase(void)28 static void SetUpTestCase(void)
29 {
30 system("service_control stop hiview");
31 }
32
TearDownTestCase(void)33 static void TearDownTestCase(void)
34 {
35 system("service_control start hiview");
36 }
37
38 void SetUp();
TearDown()39 void TearDown() {}
40 };
41
SetUp()42 void HitraceAsyncReadTimeoutTest::SetUp()
43 {
44 CloseTrace();
45 }
46
47 namespace {
48 /**
49 * @tc.name: AsyncReadTimeoutTest001
50 * @tc.desc: Test DumpTraceAsync func
51 * @tc.type: FUNC
52 */
53 HWTEST_F(HitraceAsyncReadTimeoutTest, AsyncReadTimeoutTest001, TestSize.Level2)
54 {
55 const std::vector<std::string> tagGroups = {"scene_performance"};
56 ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
__anoncb60fa520202(TraceRetInfo traceInfo) 57 std::function<void(TraceRetInfo)> func = [](TraceRetInfo traceInfo) {
58 EXPECT_EQ(traceInfo.errorCode, TraceErrorCode::TRACE_TASK_DUMP_TIMEOUT);
59 };
60 auto ret = DumpTraceAsync(0, 0, INT64_MAX, func);
61 EXPECT_EQ(ret.errorCode, TraceErrorCode::TRACE_TASK_DUMP_TIMEOUT);
62 ASSERT_EQ(CloseTrace(), TraceErrorCode::SUCCESS);
63 }
64
65 /**
66 * @tc.name: AsyncReadTimeoutTest002
67 * @tc.desc: Test DumpTraceAsync func, call twice.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(HitraceAsyncReadTimeoutTest, AsyncReadTimeoutTest002, TestSize.Level2)
71 {
72 const std::vector<std::string> tagGroups = {"scene_performance"};
73 ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
__anoncb60fa520302(TraceRetInfo traceInfo) 74 std::function<void(TraceRetInfo)> func = [](TraceRetInfo traceInfo) {
75 EXPECT_EQ(traceInfo.errorCode, TraceErrorCode::TRACE_TASK_DUMP_TIMEOUT);
76 };
77 auto ret = DumpTraceAsync(0, 0, INT64_MAX, func);
78 EXPECT_EQ(ret.errorCode, TraceErrorCode::TRACE_TASK_DUMP_TIMEOUT);
79 ret = DumpTraceAsync(0, 0, INT64_MAX, func);
80 EXPECT_EQ(ret.errorCode, TraceErrorCode::TRACE_TASK_DUMP_TIMEOUT);
81 ASSERT_EQ(CloseTrace(), TraceErrorCode::SUCCESS);
82 }
83 } // namespace
84 } // namespace Hitrace
85 } // namespace HiviewDFX
86 } // namespace OHOS