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 HitraceAsyncWriteTimeoutTest : 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 HitraceAsyncWriteTimeoutTest::SetUp()
43 {
44 CloseTrace();
45 }
46
47 namespace {
48 /**
49 * @tc.name: AsyncWriteTimeoutTest001
50 * @tc.desc: Test DumpTraceAsync func, execute without 5 seconds timeout
51 * @tc.type: FUNC
52 */
53 HWTEST_F(HitraceAsyncWriteTimeoutTest, AsyncWriteTimeoutTest001, TestSize.Level2)
54 {
55 const std::vector<std::string> tagGroups = {"scene_performance"};
56 ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
57
__anona86718210202(TraceRetInfo traceInfo) 58 std::function<void(TraceRetInfo)> func = [](TraceRetInfo traceInfo) {
59 off_t totalFileSz = 0;
60 for (auto& files : traceInfo.outputFiles) {
61 totalFileSz += GetFileSize(files);
62 GTEST_LOG_(INFO) << "output: " << files << " file size : " << GetFileSize(files);
63 }
64 EXPECT_EQ(totalFileSz, traceInfo.fileSize);
65 };
66 auto ret = DumpTraceAsync(0, 0, INT64_MAX, func);
67 EXPECT_EQ(ret.errorCode, TraceErrorCode::SUCCESS);
68 GTEST_LOG_(INFO) << "interface return file size : " << ret.fileSize;
69 for (auto file : ret.outputFiles) {
70 GTEST_LOG_(INFO) << "interface return file : " << file;
71 }
72 ret = DumpTraceAsync(0, 0, INT64_MAX, func);
73 EXPECT_EQ(ret.errorCode, TraceErrorCode::SUCCESS);
74 GTEST_LOG_(INFO) << "interface return file size : " << ret.fileSize;
75 for (auto file : ret.outputFiles) {
76 GTEST_LOG_(INFO) << "interface return file : " << file;
77 }
78 sleep(15); // 15 : wait 15 seconds to avoid crash in SIGPIPE
79 // Close trace after async dump
80 ASSERT_EQ(CloseTrace(), TraceErrorCode::SUCCESS);
81 }
82 } // namespace
83 } // namespace Hitrace
84 } // namespace HiviewDFX
85 } // namespace OHOS