• 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 
18 #include "qos.h"
19 
20 
21 using namespace std;
22 using namespace testing::ext;
23 class QoSTestSuite : public testing::Test {
24 protected:
25     // Preset action of the test suite, which is executed before the first test case
SetUpTestCase(void)26     static void SetUpTestCase(void) {
27     }
28     // Test suite cleanup action, which is executed after the last test case
TearDownTestCase(void)29     static void TearDownTestCase(void) {
30     }
31     // Preset action of the test case
SetUp()32     virtual void SetUp()
33     {
34     }
35     // Cleanup action of the test case
TearDown()36     virtual void TearDown()
37     {
38     }
39 };
40 
41 /**
42  * @tc.name: SetThreadQoSNdkTest
43  * @tc.desc: Verify the Set QoSLevel function.
44  * @tc.type: FUNC
45  */
46 HWTEST_F(QoSTestSuite, SetThreadQoSNdkTest, Function | MediumTest | Level1)
47 {
48     int ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_BACKGROUND);
49     EXPECT_EQ(ret, 0);
50     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_UTILITY);
51     EXPECT_EQ(ret, 0);
52     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_DEFAULT);
53     EXPECT_EQ(ret, 0);
54     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INITIATED);
55     EXPECT_EQ(ret, 0);
56     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_DEADLINE_REQUEST);
57     EXPECT_EQ(ret, 0);
58     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INTERACTIVE);
59     EXPECT_EQ(ret, 0);
60     ret = OH_QoS_SetThreadQoS(QoS_Level(-1));
61     EXPECT_EQ(ret, -1);
62     ret = OH_QoS_SetThreadQoS(QoS_Level(6));
63     EXPECT_EQ(ret, -1);
64     ret = OH_QoS_SetThreadQoS(QoS_Level(1024));
65     EXPECT_EQ(ret, -1);
66 }
67 
68 /**
69  * @tc.name: ResetThreadQoSNdkTest
70  * @tc.desc: Verify the Reset QoSLevel function.
71  * @tc.type: FUNC
72  */
73 HWTEST_F(QoSTestSuite, ResetThreadQoSNdkTest, Function | MediumTest | Level1)
74 {
75     int ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_BACKGROUND);
76     EXPECT_EQ(ret, 0);
77     ret = OH_QoS_ResetThreadQoS();
78     EXPECT_EQ(ret, 0);
79 
80     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_UTILITY);
81     EXPECT_EQ(ret, 0);
82     ret =  OH_QoS_ResetThreadQoS();
83     EXPECT_EQ(ret, 0);
84 
85     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_DEFAULT);
86     EXPECT_EQ(ret, 0);
87     ret =  OH_QoS_ResetThreadQoS();
88     EXPECT_EQ(ret, 0);
89 
90     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INITIATED);
91     EXPECT_EQ(ret, 0);
92     ret =  OH_QoS_ResetThreadQoS();
93     EXPECT_EQ(ret, 0);
94 
95     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_DEADLINE_REQUEST);
96     EXPECT_EQ(ret, 0);
97     ret =  OH_QoS_ResetThreadQoS();
98     EXPECT_EQ(ret, 0);
99 
100     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INTERACTIVE);
101     EXPECT_EQ(ret, 0);
102     ret = OH_QoS_ResetThreadQoS();
103     EXPECT_EQ(ret, 0);
104 }
105 
106 /**
107  * @tc.name: GetThreadQoSNdkTest
108  * @tc.desc: Verify the Get QoSLevel function.
109  * @tc.type: FUNC
110  */
111 HWTEST_F(QoSTestSuite, GetThreadQoSNdkTest, Function | MediumTest | Level1)
112 {
113     int ret = OH_QoS_GetThreadQoS(nullptr);
114     EXPECT_EQ(ret, -1);
115 
116     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_BACKGROUND);
117     EXPECT_EQ(ret, 0);
118     enum QoS_Level level;
119     ret = OH_QoS_GetThreadQoS(&level);
120     EXPECT_EQ(ret, 0);
121     EXPECT_EQ(level, QoS_Level::QOS_BACKGROUND);
122 
123     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_UTILITY);
124     EXPECT_EQ(ret, 0);
125     ret = OH_QoS_GetThreadQoS(&level);
126     EXPECT_EQ(ret, 0);
127     EXPECT_EQ(level, QoS_Level::QOS_UTILITY);
128 
129     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_DEFAULT);
130     EXPECT_EQ(ret, 0);
131     ret = OH_QoS_GetThreadQoS(&level);
132     EXPECT_EQ(ret, 0);
133     EXPECT_EQ(level, QoS_Level::QOS_DEFAULT);
134 
135     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INITIATED);
136     EXPECT_EQ(ret, 0);
137     ret = OH_QoS_GetThreadQoS(&level);
138     EXPECT_EQ(ret, 0);
139     EXPECT_EQ(level, QoS_Level::QOS_USER_INITIATED);
140 
141     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_DEADLINE_REQUEST);
142     EXPECT_EQ(ret, 0);
143     ret = OH_QoS_GetThreadQoS(&level);
144     EXPECT_EQ(ret, 0);
145     EXPECT_EQ(level, QoS_Level::QOS_DEADLINE_REQUEST);
146 
147     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INTERACTIVE);
148     EXPECT_EQ(ret, 0);
149     ret = OH_QoS_GetThreadQoS(&level);
150     EXPECT_EQ(ret, 0);
151     EXPECT_EQ(level, QoS_Level::QOS_USER_INTERACTIVE);
152 
153     ret = OH_QoS_ResetThreadQoS();
154     EXPECT_EQ(ret, 0);
155     ret = OH_QoS_GetThreadQoS(&level);
156     EXPECT_EQ(ret, -1);
157 }
158 
159 /**
160  * @tc.name: ThreadQoSNdkTest
161  * @tc.desc: Double Test ThreadQoSNDKTest function.
162  * @tc.type: FUNC
163  */
164 HWTEST_F(QoSTestSuite, ThreadQoSNdkTest, Function | MediumTest | Level1)
165 {
166     int ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_BACKGROUND);
167     EXPECT_EQ(ret, 0);
168     ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_BACKGROUND);
169     EXPECT_EQ(ret, 0);
170 
171     enum QoS_Level level;
172     ret = OH_QoS_GetThreadQoS(&level);
173     EXPECT_EQ(ret, 0);
174     EXPECT_EQ(level, QoS_Level::QOS_BACKGROUND);
175     ret = OH_QoS_GetThreadQoS(&level);
176     EXPECT_EQ(ret, 0);
177     EXPECT_EQ(level, QoS_Level::QOS_BACKGROUND);
178 
179     ret = OH_QoS_ResetThreadQoS();
180     EXPECT_EQ(ret, 0);
181     ret = OH_QoS_ResetThreadQoS();
182     EXPECT_EQ(ret, -1);
183 }
184 
185 /*
186  * @tc.number:SUB_RESOURCESCHEDULE_QOS_0100
187  * @tc.name: GewuCreateSessionTest
188  * @tc.desc: Verify the GewuCreateSession function
189  * @tc.size: MediumTest
190  * @tc.type: Function
191  * @tc.level: Level 1
192  */
193 HWTEST_F(QoSTestSuite, GewuCreateSessionTest, Function | MediumTest | Level1)
194 {
195     const char *attributes = "{\"model\": \"/data/service/el1/public/gewu/Qwen2.5-0.5B-CPU-Q4\","
196         "\"eval_settings\": \"{\\\"backend\\\": \\\"cpu\\\", \\\"max_ctx\\\": \\\"8192\\\"}\"}";
197     OH_QoS_GewuCreateSessionResult ret = OH_QoS_GewuCreateSession(attributes);
198     if (ret.error == OH_QoS_GewuErrorCode::OH_QOS_GEWU_NOSYS)
199     {
200         GTEST_SKIP() << "Not support system, skip testCase";
201     }
202     else
203     {
204         EXPECT_EQ(ret.error, OH_QoS_GewuErrorCode::OH_QOS_GEWU_OK);
205 
206         OH_QoS_GewuDestroySession(ret.session);
207 
208         ret = OH_QoS_GewuCreateSession("");
209         EXPECT_EQ(ret.error, OH_QoS_GewuErrorCode::OH_QOS_GEWU_INVAL);
210     }
211 }
212 
MockCallback(void * context,const char * response)213 void MockCallback(void *context, const char *response)
214 {
215     printf("Mock callback called. Response: %s\n", response);
216 }
217 
218 /*
219  * @tc.number:SUB_RESOURCESCHEDULE_QOS_0200
220  * @tc.name: GewuSubmitRequestTest
221  * @tc.desc: Verify the GewuSubmitRequest function
222  * @tc.size: MediumTest
223  * @tc.type: Function
224  * @tc.level: Level 1
225  */
226 HWTEST_F(QoSTestSuite, GewuSubmitRequestTest, Function | MediumTest | Level1)
227 {
228     const char *attributes = "{\"model\": \"/data/service/el1/public/gewu/Qwen2.5-0.5B-CPU-Q4\","
229         "\"eval_settings\": \"{\\\"backend\\\": \\\"cpu\\\", \\\"max_ctx\\\": \\\"8192\\\"}\"}";
230     OH_QoS_GewuCreateSessionResult sessionResult = OH_QoS_GewuCreateSession(attributes);
231     const char *request = "{\
232         \"type\": \"ChatComplete\",\
233         \"messages\": [\
234             {\
235                 \"role\": \"developer\",\
236                 \"content\": \"Your are a helpful assistant.\"\
237             },\
238             {\
239                 \"role\": \"user\",\
240                 \"content\": \"What is OpenHarmony\"\
241             }\
242         ],\
243         \"stream\": true\
244     }";
245     void *fakeContext = reinterpret_cast<void *>(0x1234);
246     OH_QoS_GewuOnResponse cb = MockCallback;
247     OH_QoS_GewuSubmitRequestResult ret = OH_QoS_GewuSubmitRequest(sessionResult.session, request, cb, fakeContext);
248     if (ret.error == OH_QoS_GewuErrorCode::OH_QOS_GEWU_NOSYS)
249     {
250         GTEST_SKIP() << "Not support system, skip testCase";
251     }
252     else
253     {
254         EXPECT_EQ(ret.error, OH_QoS_GewuErrorCode::OH_QOS_GEWU_OK);
255 
256         OH_QoS_GewuDestroySession(sessionResult.session);
257 
258         ret = OH_QoS_GewuSubmitRequest(0, "", cb, fakeContext);
259         EXPECT_EQ(ret.error, OH_QoS_GewuErrorCode::OH_QOS_GEWU_INVAL);
260     }
261 }
262 
263 /*
264  * @tc.number:SUB_RESOURCESCHEDULE_QOS_0300
265  * @tc.name: GewuAbortRequestTest
266  * @tc.desc: Verify the GewuAbortRequest function
267  * @tc.size: MediumTest
268  * @tc.type: Function
269  * @tc.level: Level 1
270  */
271 HWTEST_F(QoSTestSuite, GewuAbortRequestTest, Function | MediumTest | Level1)
272 {
273     const char *attributes = "{\"model\": \"/data/service/el1/public/gewu/Qwen2.5-0.5B-CPU-Q4\","
274         "\"eval_settings\": \"{\\\"backend\\\": \\\"cpu\\\", \\\"max_ctx\\\": \\\"8192\\\"}\"}";
275     OH_QoS_GewuCreateSessionResult sessionResult = OH_QoS_GewuCreateSession(attributes);
276     OH_QoS_GewuErrorCode ret = OH_QoS_GewuAbortRequest(sessionResult.session, 0);
277     if (ret == OH_QoS_GewuErrorCode::OH_QOS_GEWU_NOSYS)
278     {
279         GTEST_SKIP() << "Not support system, skip testCase";
280     }
281     else
282     {
283         EXPECT_EQ(ret, OH_QoS_GewuErrorCode::OH_QOS_GEWU_OK);
284 
285         OH_QoS_GewuDestroySession(sessionResult.session);
286 
287         ret = OH_QoS_GewuAbortRequest(0, 0);
288         EXPECT_EQ(ret, OH_QoS_GewuErrorCode::OH_QOS_GEWU_INVAL);
289     }
290 }
291 
292 /*
293  * @tc.number:SUB_RESOURCESCHEDULE_QOS_0400
294  * @tc.name: GewuDestroySessionTest
295  * @tc.desc: Verify the GewuDestroySession function
296  * @tc.size: MediumTest
297  * @tc.type: Function
298  * @tc.level: Level 1
299  */
300 HWTEST_F(QoSTestSuite, GewuDestroySessionTest, Function | MediumTest | Level1)
301 {
302     const char *attributes = "{\"model\": \"/data/service/el1/public/gewu/Qwen2.5-0.5B-CPU-Q4\","
303         "\"eval_settings\": \"{\\\"backend\\\": \\\"cpu\\\", \\\"max_ctx\\\": \\\"8192\\\"}\"}";
304     OH_QoS_GewuCreateSessionResult sessionResult = OH_QoS_GewuCreateSession(attributes);
305     OH_QoS_GewuErrorCode ret = OH_QoS_GewuDestroySession(sessionResult.session);
306     if (ret == OH_QoS_GewuErrorCode::OH_QOS_GEWU_NOSYS)
307     {
308         GTEST_SKIP() << "Not support system, skip testCase";
309     }
310     else
311     {
312         EXPECT_EQ(ret, OH_QoS_GewuErrorCode::OH_QOS_GEWU_OK);
313 
314         ret = OH_QoS_GewuDestroySession(0);
315         EXPECT_EQ(ret, OH_QoS_GewuErrorCode::OH_QOS_GEWU_INVAL);
316     }
317 }
318 
319