1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 #include <fcntl.h>
16 #include <fstream>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <string>
20 #include <thread>
21 #include <unistd.h>
22
23 #include "result_transporter.h"
24 #include "trace_plugin_config.pb.h"
25 #include "trace_plugin_result.pb.h"
26
27 using namespace testing::ext;
28
29 namespace {
30 constexpr int WAIT_MS = 1000;
31 using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer;
32 using ConstVoidPtr = std::unique_ptr<const void>::pointer;
33 class ResultTransporterTest : public ::testing::Test {
34 protected:
35 std::string name_;
36
SetUp()37 void SetUp() override
38 {
39 name_ = "Transporter";
40 }
41
TearDown()42 void TearDown() override {}
43 };
44
WriteFunc(WriterStructPtr writer,ConstVoidPtr data,size_t size)45 long WriteFunc(WriterStructPtr writer, ConstVoidPtr data, size_t size)
46 {
47 if (writer == nullptr || data == nullptr || size <= 0) {
48 return -1;
49 }
50
51 return 0;
52 }
53
FlushFunc(WriterStructPtr writer)54 bool FlushFunc(WriterStructPtr writer)
55 {
56 if (writer == nullptr) {
57 return false;
58 }
59 return true;
60 }
61
62 /*
63 * @tc.name: IsFlushTime
64 * @tc.desc: test ResultTransporter::IsFlushTime.
65 * @tc.type: FUNC
66 */
67 HWTEST_F(ResultTransporterTest, IsFlushTimeInvalidParam, TestSize.Level1)
68 {
69 OHOS::Profiler::Plugins::ResultTransporter reporter("", nullptr);
70 reporter.SetFlushInterval(WAIT_MS);
71 reporter.IsFlushTime();
72 usleep(WAIT_MS);
73 EXPECT_FALSE(reporter.IsFlushTime());
74 }
75
76 /*
77 * @tc.name: IsFlushTime
78 * @tc.desc: test ResultTransporter::IsFlushTime.
79 * @tc.type: FUNC
80 */
81 HWTEST_F(ResultTransporterTest, IsFlushTimeTrue, TestSize.Level1)
82 {
83 WriterStruct writer = {WriteFunc, FlushFunc};
84 OHOS::Profiler::Plugins::ResultTransporter reporter(name_, static_cast<WriterStructPtr>(&writer));
85 reporter.SetFlushInterval(1);
86 EXPECT_TRUE(reporter.IsFlushTime());
87 }
88
89 /*
90 * @tc.name: IsFlushTime
91 * @tc.desc: test ResultTransporter::IsFlushTime.
92 * @tc.type: FUNC
93 */
94 HWTEST_F(ResultTransporterTest, IsFlushTimeFalse, TestSize.Level1)
95 {
96 WriterStruct writer = {WriteFunc, FlushFunc};
97 OHOS::Profiler::Plugins::ResultTransporter reporter(name_, static_cast<WriterStructPtr>(&writer));
98 reporter.SetFlushInterval(WAIT_MS);
99 reporter.IsFlushTime();
100 usleep(WAIT_MS);
101 EXPECT_FALSE(reporter.IsFlushTime());
102 }
103
104 /*
105 * @tc.name: Write
106 * @tc.desc: test ResultTransporter::Submit.
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ResultTransporterTest, WriteInvalidParam, TestSize.Level1)
110 {
111 OHOS::Profiler::Plugins::ResultTransporter reporter("", nullptr);
112 auto packet = std::make_unique<TracePluginResult>();
113 ASSERT_TRUE(reporter.Submit(std::move(packet)));
114 }
115
116 /*
117 * @tc.name: Write
118 * @tc.desc: test ResultTransporter::Submit.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(ResultTransporterTest, FlushInvalidParam, TestSize.Level1)
122 {
123 WriterStruct flush = {WriteFunc, nullptr};
124 OHOS::Profiler::Plugins::ResultTransporter reporter(name_, static_cast<WriterStructPtr>(&flush));
125 auto packet = std::make_unique<TracePluginResult>();
126 reporter.SetFlushInterval(0);
127 ASSERT_TRUE(reporter.IsFlushTime());
128 reporter.SetFlushThreshold(0);
129 EXPECT_TRUE(reporter.Submit(std::move(packet)));
130 }
131
132 /*
133 * @tc.name: Write
134 * @tc.desc: test ResultTransporter::Submit.
135 * @tc.type: FUNCquit
136 */
137 HWTEST_F(ResultTransporterTest, WriteFalse, TestSize.Level1)
138 {
139 WriterStruct writer = {WriteFunc, FlushFunc};
140 OHOS::Profiler::Plugins::ResultTransporter reporter(name_, static_cast<WriterStructPtr>(&writer));
141 auto packet = std::make_unique<TracePluginResult>();
142 reporter.SetFlushInterval(0);
143 ASSERT_TRUE(reporter.IsFlushTime());
144 reporter.SetFlushThreshold(0);
145 EXPECT_TRUE(reporter.Submit(std::move(packet)));
146 }
147 } // namespace