• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "protocol/struct_definition/aie_info_define.h"
22 #include "utils/aie_client_callback.h"
23 #include "utils/aie_client_common.h"
24 #include "utils/aie_client_const.h"
25 #include "utils/log/aie_log.h"
26 #include "utils/service_dead_cb.h"
27 #include "utils/utils.h"
28 
29 using namespace ::testing;
30 using namespace testing::ext;
31 using namespace OHOS::AI;
32 
33 class AieClientReleaseFunctionTest : public testing::Test {};
34 
35 /**
36  * Tests AieClientRelease.
37  * Invoke sequence: AieClientInit-AieClientPrepare-AieClientSyncProcess/AieClientAsyncProcess-AieClientRelease.
38  *
39  * isAsync  The value of the input parameter AlgorithmInfo.isAsync of AieClientInit.
40  * isPerformProcess Whether to invoke AieClientSyncProcess/AieClientAsyncProcess or not.
41  */
TestAieClientRelease(bool isAsync,bool isPerformProcess)42 static void TestAieClientRelease(bool isAsync, bool isPerformProcess)
43 {
44     // Step 0: Defines variables.
45     ConfigInfo configInfo;
46     GetConfigInfo(configInfo);
47 
48     ClientInfo clientInfo;
49     GetClientInfo(clientInfo);
50 
51     AlgorithmInfo algorithmInfo;
52     int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
53     GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
54 
55     ServiceDeadCb *cb = nullptr;
56     AIE_NEW(cb, ServiceDeadCb());
57 
58     // Step 1: Invokes AieClientInit.
59     int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
60     EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
61     EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
62     EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
63                                               << ") is incorrect!";
64 
65     // Step 2: Invokes AieClientPrepare.
66     bool isPrepareInputInfoNull = GetRandomBool();
67     DataInfo prepareInputInfo = GetDataInfo(isPrepareInputInfoNull, INPUT_INFO_PREPARE);
68     DataInfo prepareOutputInfo = GetDataInfo();
69 
70     IClientCb *callback = nullptr;
71     if (isAsync) {
72         AIE_NEW(callback, ClientCallback());
73     }
74     int prepareReturnCode = AieClientPrepare(clientInfo, algorithmInfo, prepareInputInfo, prepareOutputInfo, callback);
75     EXPECT_EQ(RETCODE_SUCCESS, prepareReturnCode) << "AieClientPrepare is failed!";
76     EXPECT_EQ(true, prepareOutputInfo.data != nullptr) << "Prepare outputInfo is incorrect!";
77 
78     // Step 3: Invokes AieClientSyncProcess/AieClientAsyncProcess.
79     bool isProcessInputInfoNull = GetRandomBool();
80     if (isPerformProcess) {
81         if (isAsync) {
82             DataInfo asyncProcessInputInfo = GetDataInfo(isProcessInputInfoNull, INPUT_INFO_ASYNC_PROCESS);
83             int asyncProcessReturnCode = AieClientAsyncProcess(clientInfo, algorithmInfo, asyncProcessInputInfo);
84             EXPECT_EQ(RETCODE_SUCCESS, asyncProcessReturnCode) << "AieClientAsyncProcess is failed!";
85         } else {
86             DataInfo syncProcessInputInfo = GetDataInfo(isProcessInputInfoNull, INPUT_INFO_SYNC_PROCESS);
87             DataInfo syncProcessOutputInfo = GetDataInfo();
88             int syncProcessReturnCode = AieClientSyncProcess(clientInfo, algorithmInfo, syncProcessInputInfo,
89                                                              syncProcessOutputInfo);
90             EXPECT_EQ(0, syncProcessReturnCode) << "AieClientSyncProcess is failed!";
91             EXPECT_EQ(true, syncProcessOutputInfo.data != nullptr) << "AieClientSyncProcess outputInfo is "
92                                                                       "incorrect!";
93         }
94     }
95 
96     // Step 4: Invokes AieClientRelease.
97     bool isReleaseInputInfoNull = GetRandomBool();
98     DataInfo releaseInputInfo = GetDataInfo(isReleaseInputInfoNull, INPUT_INFO_RELEASE);
99     int releaseReturnCode = AieClientRelease(clientInfo, algorithmInfo, releaseInputInfo);
100     EXPECT_EQ(RETCODE_SUCCESS, releaseReturnCode) << "AieClientRelease is failed!";
101 
102     // Step 5: Invoke AieClientDestroy().
103     int destroyReturnCode = AieClientDestroy(clientInfo);
104     EXPECT_EQ(RETCODE_SUCCESS, destroyReturnCode) << "AieClientDestroy is failed!";
105 
106     // Step 6: Deletes callback.
107     AIE_DELETE(cb);
108     if (isAsync) {
109         AIE_DELETE(callback);
110     }
111 }
112 
113 /**
114  * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientRelease_0100
115  * @tc.name   : 01. algorithmInfo中isAsync=false,不执行算法,调用AieClientRelease返回成功
116  * @tc.desc   : [C- SOFTWARE -0200]
117  */
118 HWTEST_F(AieClientReleaseFunctionTest, testAieClientReleaseFunction001, Function | MediumTest | Level2)
119 {
120     HILOGI("[Test]testAieClientReleaseFunction001.");
121     TestAieClientRelease(false, false);
122 }
123 
124 /**
125  * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientRelease_0200
126  * @tc.name   : 02. algorithmInfo中isAsync=true,不执行算法,调用AieClientRelease返回成功
127  * @tc.desc   : [C- SOFTWARE -0200]
128  */
129 HWTEST_F(AieClientReleaseFunctionTest, testAieClientReleaseFunction002, Function | MediumTest | Level3)
130 {
131     HILOGI("[Test]testAieClientReleaseFunction002.");
132     TestAieClientRelease(true, false);
133 }
134 
135 /**
136  * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientRelease_0300
137  * @tc.name   : 03. 同步执行算法之后,调用AieClientRelease返回成功
138  * @tc.desc   : [C- SOFTWARE -0200]
139  */
140 HWTEST_F(AieClientReleaseFunctionTest, testAieClientReleaseFunction003, Function | MediumTest | Level3)
141 {
142     HILOGI("[Test]testAieClientReleaseFunction003.");
143     TestAieClientRelease(false, true);
144 }
145 
146 /**
147  * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientRelease_0400
148  * @tc.name   : 04. 异步执行算法之后,调用AieClientRelease返回成功
149  * @tc.desc   : [C- SOFTWARE -0200]
150  */
151 HWTEST_F(AieClientReleaseFunctionTest, testAieClientReleaseFunction004, Function | MediumTest | Level3)
152 {
153     HILOGI("[Test]testAieClientReleaseFunction004.");
154     TestAieClientRelease(true, true);
155 }