1 /*
2 * Copyright (c) 2021 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 <cstring>
17 #include <ctime>
18 #include <unistd.h>
19
20 #include "gtest/gtest.h"
21
22 #include "client_executor/include/i_aie_client.inl"
23 #include "kits/ai_retcode.h"
24 #include "platform/time/include/time.h"
25 #include "protocol/struct_definition/aie_info_define.h"
26 #include "service_dead_cb.h"
27 #include "utils/log/aie_log.h"
28
29 using namespace OHOS::AI;
30 using namespace testing::ext;
31
32 namespace {
33 const char * const INPUT_CHARACTER = "inputData";
34 const char * const CONFIG_DESCRIPTION = "config information";
35 const long long CLIENT_INFO_VERSION = 1;
36 const int CLIENT_ID = -1;
37 const int SESSION_ID = -1;
38 const long long ALGORITHM_INFO_CLIENT_VERSION = 2;
39 const int ALGORITHM_SYNC_TYPE = 0;
40 const int ALGORITHM_ASYNC_TYPE = 1;
41 const long long ALGORITHM_VERSION = 1;
42 const int OPERATE_ID = 2;
43 const int REQUEST_ID = 3;
44 const time_t TIME_TEST = 20;
45 }
46
47 class AieClientReliabilityTest : public testing::Test {
48 public:
49 // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()50 static void SetUpTestCase() {};
51
52 // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()53 static void TearDownTestCase() {};
54
55 // SetUp:Execute before each test case
SetUp()56 void SetUp() {};
57
58 // TearDown:Execute after each test case
TearDown()59 void TearDown() {};
60 };
61
62 class ClientCallback : public IClientCb {
63 public:
64 ClientCallback() = default;
65 ~ClientCallback() override = default;
OnResult(const DataInfo & result,int resultCode,int requestId)66 void OnResult(const DataInfo &result, int resultCode, int requestId) override
67 {
68 HILOGI("[Test]TestAlgorithmInfo002 OnResult resultCode[%d], requestId[%d], resultData[%s], resultLength[%d].",
69 resultCode, requestId, result.data, result.length);
70 }
71 };
FreeDataInfo(DataInfo & dataInfo)72 static void FreeDataInfo(DataInfo &dataInfo)
73 {
74 if (dataInfo.data == nullptr || dataInfo.length <= 0) {
75 return;
76 }
77 free(dataInfo.data);
78 dataInfo.data = nullptr;
79 dataInfo.length = 0;
80 }
81
82 /**
83 * @tc.name: AieClientSyncReliabilityTest001
84 * @tc.desc: Test AieClient sync reliability
85 * @tc.type: RELI
86 * @tc.require: AR000F77MI
87 */
88 HWTEST_F(AieClientReliabilityTest, AieClientSyncReliabilityTest001, TestSize.Level0)
89 {
90 HILOGI("[Test]TestSyncAieClientReliability.");
91
92 time_t asyncStart = GetCurTimeSec();
93 while (true) {
94 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
95
96 const char *str = INPUT_CHARACTER;
97 char *inputData = const_cast<char*>(str);
98 int len = strlen(str) + 1;
99
100 ClientInfo clientInfo = {
101 .clientVersion = CLIENT_INFO_VERSION,
102 .clientId = CLIENT_ID,
103 .sessionId = SESSION_ID,
104 .serverUid = INVALID_UID,
105 .clientUid = INVALID_UID,
106 .extendLen = len,
107 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
108 };
109
110 AlgorithmInfo algoInfo = {
111 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
112 .isAsync = false,
113 .algorithmType = ALGORITHM_SYNC_TYPE,
114 .algorithmVersion = ALGORITHM_VERSION,
115 .isCloud = true,
116 .operateId = OPERATE_ID,
117 .requestId = REQUEST_ID,
118 .extendLen = len,
119 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
120 };
121
122 DataInfo inputInfo = {
123 .data = reinterpret_cast<unsigned char*>(inputData),
124 .length = len,
125 };
126
127 ServiceDeadCb cb;
128 int resultCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
129 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
130
131 DataInfo outputInfo;
132
133 resultCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, nullptr);
134 FreeDataInfo(outputInfo);
135 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
136
137 resultCode = AieClientSyncProcess(clientInfo, algoInfo, inputInfo, outputInfo);
138 FreeDataInfo(outputInfo);
139 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
140
141 resultCode = AieClientRelease(clientInfo, algoInfo, inputInfo);
142 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
143
144 resultCode = AieClientDestroy(clientInfo);
145 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
146
147 time_t currentTime = GetCurTimeSec();
148 if ((currentTime - asyncStart) >= TIME_TEST) {
149 break;
150 }
151 }
152 }
153
154 /**
155 * @tc.name: AieClientAsyncReliabilityTest001
156 * @tc.desc: Test AieClient async reliability
157 * @tc.type: RELI
158 * @tc.require: AR000F77MI
159 */
160 HWTEST_F(AieClientReliabilityTest, AieClientAsyncReliabilityTest001, TestSize.Level0)
161 {
162 HILOGI("[Test]TestAsyncAieClientReliability.");
163
164 time_t asyncStart = GetCurTimeSec();
165 while (true) {
166 ConfigInfo configInfo {.description = CONFIG_DESCRIPTION};
167
168 const char *str = INPUT_CHARACTER;
169 char *inputData = const_cast<char*>(str);
170 int len = strlen(str) + 1;
171
172 ClientInfo clientInfo = {
173 .clientVersion = CLIENT_INFO_VERSION,
174 .clientId = CLIENT_ID,
175 .sessionId = SESSION_ID,
176 .serverUid = INVALID_UID,
177 .clientUid = INVALID_UID,
178 .extendLen = len,
179 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
180 };
181
182 AlgorithmInfo algoInfo = {
183 .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
184 .isAsync = true,
185 .algorithmType = ALGORITHM_ASYNC_TYPE,
186 .algorithmVersion = ALGORITHM_VERSION,
187 .isCloud = true,
188 .operateId = OPERATE_ID,
189 .requestId = REQUEST_ID,
190 .extendLen = len,
191 .extendMsg = reinterpret_cast<unsigned char*>(inputData),
192 };
193
194 DataInfo inputInfo = {
195 .data = reinterpret_cast<unsigned char*>(inputData),
196 .length = len,
197 };
198
199 ServiceDeadCb cb;
200 int resultCode = AieClientInit(configInfo, clientInfo, algoInfo, &cb);
201 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
202
203 ClientCallback callback;
204
205 DataInfo outputInfo;
206 resultCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, &callback);
207 FreeDataInfo(outputInfo);
208 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
209
210 resultCode = AieClientAsyncProcess(clientInfo, algoInfo, inputInfo);
211 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
212 StepSleepMs(500);
213
214 resultCode = AieClientRelease(clientInfo, algoInfo, inputInfo);
215 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
216
217 resultCode = AieClientDestroy(clientInfo);
218 ASSERT_EQ(resultCode, RETCODE_SUCCESS);
219
220 time_t currentTime = GetCurTimeSec();
221 if ((currentTime - asyncStart) >= TIME_TEST) {
222 break;
223 }
224 }
225 }
226