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 "gtest/gtest.h"
17 #include <cstring>
18 #include <unistd.h>
19
20 #include "client_executor/include/i_aie_client.inl"
21 #include "communication_adapter/include/sa_async_handler.h"
22 #include "communication_adapter/include/sa_client.h"
23 #include "platform/time/include/time.h"
24 #include "protocol/struct_definition/aie_info_define.h"
25 #include "server_executor/include/server_executor.h"
26 #include "utils/aie_client_callback.h"
27 #include "utils/aie_client_common.h"
28 #include "utils/aie_client_const.h"
29 #include "utils/aie_macros.h"
30 #include "utils/log/aie_log.h"
31 #include "utils/service_dead_cb.h"
32 #include "utils/utils.h"
33
34 using namespace ::testing;
35 using namespace testing::ext;
36 using namespace OHOS::AI;
37
38 namespace {
39 const int WAIT_CALLBACK_TIME_MS = 2000;
40 }
41
42 class AieClientAsyncProcessFunctionTest : public testing::Test {};
43
44 /**
45 * Tests AieClientAsyncProcess().
46 *
47 * isAsync The value of the input parameter AlgorithmInfo.isAsync of AieClientInit.
48 * isAsyncProcessInputInfoNull Whether the input parameter InputInfo of AieClientAsyncProcess is null or not.
49 * isAsyncProcessSuccess Whether the expected result of AieClientAsyncProcess is successful or not.
50 * isExpectedAsyncProcessCbNotNull Whether the expected cb of AieClientAsyncProcess is not null or not.
51 */
TestAieClientAsyncProcess(bool isAsync,bool isAsyncProcessInputInfoNull,bool isAsyncProcessSuccess,bool isExpectedAsyncProcessCbNotNull)52 static void TestAieClientAsyncProcess(bool isAsync, bool isAsyncProcessInputInfoNull, bool isAsyncProcessSuccess,
53 bool isExpectedAsyncProcessCbNotNull)
54 {
55 // Step 0: Defines variables.
56 ConfigInfo configInfo;
57 GetConfigInfo(configInfo);
58
59 ClientInfo clientInfo;
60 GetClientInfo(clientInfo);
61
62 AlgorithmInfo algorithmInfo;
63 int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
64 GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
65
66 ServiceDeadCb *cb = nullptr;
67 AIE_NEW(cb, ServiceDeadCb());
68
69 // Step 1: Invokes AieClientInit.
70 int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
71 EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
72 EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
73 EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
74 << ") is incorrect!";
75
76 // Step 2: Invokes AieClientPrepare.
77 bool isPrepareInputInfoNull = GetRandomBool();
78 DataInfo prepareInputInfo = GetDataInfo(isPrepareInputInfoNull, INPUT_INFO_PREPARE);
79 DataInfo prepareOutputInfo = GetDataInfo();
80
81 ClientCallback *callback = nullptr;
82 if (isAsync) {
83 AIE_NEW(callback, ClientCallback());
84 }
85 int prepareReturnCode = AieClientPrepare(clientInfo, algorithmInfo, prepareInputInfo, prepareOutputInfo, callback);
86 EXPECT_EQ(RETCODE_SUCCESS, prepareReturnCode) << "AieClientPrepare is failed!";
87 EXPECT_EQ(true, prepareOutputInfo.data != nullptr) << "Prepare outputInfo is incorrect!";
88
89 // Step 3: Invokes AieClientAsyncProcess.
90 DataInfo asyncProcessInputInfo = GetDataInfo(isAsyncProcessInputInfoNull, INPUT_INFO_ASYNC_PROCESS);
91 int asyncProcessReturnCode = AieClientAsyncProcess(clientInfo, algorithmInfo, asyncProcessInputInfo);
92 EXPECT_EQ(isAsyncProcessSuccess, asyncProcessReturnCode == 0) << "AieClientAsyncProcess is failed!";
93
94 // Step 4: Sleeps.
95 StepSleepMs(WAIT_CALLBACK_TIME_MS);
96
97 // Step 5: Invokes AieClientRelease.
98 DataInfo releaseInputInfo = GetDataInfo(false, INPUT_INFO_RELEASE);
99 int releaseReturnCode = AieClientRelease(clientInfo, algorithmInfo, releaseInputInfo);
100 EXPECT_EQ(RETCODE_SUCCESS, releaseReturnCode) << "AieClientRelease is failed!";
101
102 // Step 6: Invokes AieClientDestroy.
103 int destroyReturnCode = AieClientDestroy(clientInfo);
104 EXPECT_EQ(RETCODE_SUCCESS, destroyReturnCode) << "AieClientDestroy is failed!";
105
106 // Step 7: Deletes callback.
107 AIE_DELETE(cb);
108 AIE_DELETE(callback);
109 }
110
111 /**
112 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientAsyncProcess_01_0100
113 * @tc.name : 01. algorithmInfo中isAsync=true,inputInfo不为空,调用AieClientAsyncProcess返回成功
114 * @tc.desc : [C- SOFTWARE -0200]
115 */
116 HWTEST_F(AieClientAsyncProcessFunctionTest, testAieClientAsyncProcessFunction0101, Function | MediumTest | Level2)
117 {
118 HILOGI("[Test]testAieClientAsyncProcessFunction0101.");
119 TestAieClientAsyncProcess(true, false, true, true);
120 }
121
122 /**
123 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientAsyncProcess_01_0200
124 * @tc.name : 02. algorithmInfo中isAsync=true,inputInfo为空,调用AieClientAsyncProcess返回成功
125 * @tc.desc : [C- SOFTWARE -0200]
126 */
127 HWTEST_F(AieClientAsyncProcessFunctionTest, testAieClientAsyncProcessFunction0102, Function | MediumTest | Level3)
128 {
129 HILOGI("[Test]testAieClientAsyncProcessFunction0102.");
130 TestAieClientAsyncProcess(true, true, true, true);
131 }
132
133 /**
134 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientAsyncProcess_02_0100
135 * @tc.name : 01. algorithmInfo中isAsync=false,调用AieClientAsyncProcess返回失败
136 * @tc.desc : [C- SOFTWARE -0200]
137 */
138 HWTEST_F(AieClientAsyncProcessFunctionTest, testAieClientAsyncProcessFunction0201, Function | MediumTest | Level3)
139 {
140 HILOGI("[Test]testAieClientAsyncProcessFunction0201.");
141 bool isAsyncProcessInputInfoNull = GetRandomBool();
142 TestAieClientAsyncProcess(false, isAsyncProcessInputInfoNull, false, false);
143 }
144