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 "platform/time/include/time.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 namespace {
34 const int WAIT_CALLBACK_TIME_MS = 2000;
35 }
36
37 class AieClientSyncProcessShareMemoryFunctionTest : public testing::Test {};
38
39 /**
40 * Tests AieClientSyncProcess().
41 *
42 * isAsync The value of the input parameter AlgorithmInfo.isAsync of AieClientInit.
43 * isSyncProcessInputInfoNull Whether the input parameter InputInfo of AieClientSyncProcess is null or not.
44 * isSyncProcessSuccess Whether the expected result of AieClientSyncProcess is successful or not.
45 * isExpectedSyncProcessOutputInfoNotNull Whether the expected cb of AieClientSyncProcess is not null or not.
46 */
TestAieClientSyncProcess(bool isAsync,bool isSyncProcessInputInfoNull,bool isSyncProcessSuccess,bool isExpectedSyncProcessOutputInfoNotNull)47 static void TestAieClientSyncProcess(bool isAsync, bool isSyncProcessInputInfoNull, bool isSyncProcessSuccess,
48 bool isExpectedSyncProcessOutputInfoNotNull)
49 {
50 // Step 0: Defines variables.
51 ConfigInfo configInfo;
52 GetConfigInfo(configInfo);
53
54 ClientInfo clientInfo;
55 GetClientInfo(clientInfo);
56
57 AlgorithmInfo algorithmInfo;
58 int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
59 GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
60
61 ServiceDeadCb *cb = nullptr;
62 AIE_NEW(cb, ServiceDeadCb());
63
64 // Step 1: Invokes AieClientInit.
65 int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
66 EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
67 EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
68 EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
69 << ") is incorrect!";
70
71 // Step 2: Invokes AieClientPrepare.
72 bool isPrepareInputInfoNull = GetRandomBool();
73 DataInfo prepareInputInfo = GetDataInfo(isPrepareInputInfoNull, INPUT_INFO_PREPARE);
74 DataInfo prepareOutputInfo = GetDataInfo();
75
76 ClientCallback *callback = nullptr;
77 if (isAsync) {
78 AIE_NEW(callback, ClientCallback());
79 }
80
81 int prepareReturnCode = AieClientPrepare(clientInfo, algorithmInfo, prepareInputInfo, prepareOutputInfo, callback);
82 EXPECT_EQ(RETCODE_SUCCESS, prepareReturnCode) << "AieClientPrepare is failed!";
83 EXPECT_EQ(true, prepareOutputInfo.data != nullptr) << "Prepare outputInfo is incorrect!";
84
85 // Step 3: Invokes AieClientSyncProcess.
86 DataInfo syncProcessInputInfo = GetBigDataInfo(isSyncProcessInputInfoNull);
87 DataInfo syncProcessOutputInfo = GetDataInfo();
88 int syncProcessReturnCode = AieClientSyncProcess(clientInfo, algorithmInfo, syncProcessInputInfo,
89 syncProcessOutputInfo);
90 EXPECT_EQ(isSyncProcessSuccess, syncProcessReturnCode == RETCODE_SUCCESS) << "AieClientSyncProcess is failed!";
91 EXPECT_EQ(isExpectedSyncProcessOutputInfoNotNull, syncProcessOutputInfo.data != nullptr)
92 << "AieClientSyncProcess outputInfo is incorrect!";
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 // Step 8: Release data info.
111 FreeDataInfo(syncProcessInputInfo);
112 FreeDataInfo(syncProcessOutputInfo);
113 }
114
115 /**
116 * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientSyncProcessShareMemory_0100
117 * @tc.name : 01. algorithmInfo中isAsync=false,inputInfo不为空,调用AieClientSyncProcess返回成功_共享内存传大数据
118 * @tc.desc : [C- SOFTWARE -0200]
119 */
120 HWTEST_F(AieClientSyncProcessShareMemoryFunctionTest, testAieClientSyncProcessShareMemoryFunction0101,
121 Function | MediumTest | Level3)
122 {
123 HILOGI("[Test]testAieClientSyncProcessShareMemoryFunction0101.");
124 TestAieClientSyncProcess(false, false, true, true);
125 }