• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020-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  * Description : testcases for distributedschedule_lite subsystem
16  * Create      : 2020/05/16
17  */
18 
19 #include <stdlib.h>
20 #include "cmsis_os.h"
21 #include "securec.h"
22 #include "cmsis_os2.h"
23 #include "hctest.h"
24 #include "samgr_lite.h"
25 
26 #define OPER_INTERVAL 200  // 200ms
27 #define TARGET_NUM 2
28 
29 static const char *GetName(Service *service);
30 static BOOL Initialize(Service *service, Identity identity);
31 static BOOL MessageHandle(Service *service, Request *msg);
32 static TaskConfig GetTaskConfig(Service *service);
33 
34 static const char *FEATURE_GetName(Feature *feature);
35 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity);
36 static void FEATURE_OnStop(Feature *feature, Identity identity);
37 static BOOL FEATURE_OnMessage(Feature *feature, Request *request);
38 
39 static void *g_threadID11;  // for the first service threadID
40 static void *g_threadID12;  // for the first feature threadID
41 static void *g_threadID21;  // for the second service threadID
42 static void *g_threadID22;  // for the second feature threadID
43 
44 typedef struct DemoApi {
45     INHERIT_IUNKNOWN;
46     BOOL(*FeatureApi001)
47     (IUnknown *iUnknown, char *para1);
48     int32 (*SendRequestProxyF)(const Identity *identity, const Request *request, Handler handler);
49 } DemoApi;
50 
51 typedef struct DemoFeature {
52     INHERIT_FEATURE;
53     INHERIT_IUNKNOWNENTRY(DemoApi);
54     Identity identity;
55     int featureCalledCount;
56 } DemoFeature;
57 
58 typedef struct DefaultFeatureApi {
59     INHERIT_IUNKNOWN;
60     BOOL(*DefaultApi001)
61     (IUnknown *iUnknown, char *para1);
62     int32 (*SendRequestProxyDF)(const Identity *identity, const Request *request, Handler handler);
63 } DefaultFeatureApi;
64 
65 typedef struct DemoService {
66     INHERIT_SERVICE;
67     INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
68     Identity identity;
69     int serviceCalledCount;
70 } DemoService;
71 
DefaultApi001(IUnknown * iUnknown,char * para1)72 static BOOL DefaultApi001(IUnknown *iUnknown, char *para1)
73 {
74     (void)iUnknown;
75     (void)para1;
76     return TRUE;
77 }
SendRequestProxyDF(const Identity * identity,const Request * request,Handler handler)78 static int32 SendRequestProxyDF(const Identity *identity, const Request *request, Handler handler)
79 {
80     return SAMGR_SendRequest(identity, request, handler);
81 }
82 
FeatureApi001(IUnknown * iUnknown,char * para1)83 static BOOL FeatureApi001(IUnknown *iUnknown, char *para1)
84 {
85     (void)iUnknown;
86     (void)para1;
87     return TRUE;
88 }
SendRequestProxyF(const Identity * identity,const Request * request,Handler handler)89 static int32 SendRequestProxyF(const Identity *identity, const Request *request, Handler handler)
90 {
91     return SAMGR_SendRequest(identity, request, handler);
92 }
93 
94 static DemoService g_service[] = {
95     {
96         .GetName = GetName,
97         .Initialize = Initialize,
98         .MessageHandle = MessageHandle,
99         .GetTaskConfig = GetTaskConfig,
100         .ref = 1,
101         .iUnknown = {
102             DEFAULT_IUNKNOWN_IMPL,
103             .DefaultApi001 = DefaultApi001,
104             .SendRequestProxyDF = SendRequestProxyDF,
105         },
106         .serviceCalledCount = 0,
107     },
108     {
109         .GetName = GetName,
110         .Initialize = Initialize,
111         .MessageHandle = MessageHandle,
112         .GetTaskConfig = GetTaskConfig,
113         .ref = 1,
114         .iUnknown = {
115             DEFAULT_IUNKNOWN_IMPL,
116             .DefaultApi001 = DefaultApi001,
117             .SendRequestProxyDF = SendRequestProxyDF,
118         },
119         .serviceCalledCount = 0,
120     }
121 };
122 
GetName(Service * service)123 static const char *GetName(Service *service)
124 {
125     if (service == (Service *)&g_service[0]) {
126         return "SharedTask01";
127     } else {
128         return "SharedTask02";
129     }
130 }
131 
Initialize(Service * service,Identity identity)132 static BOOL Initialize(Service *service, Identity identity)
133 {
134     DemoService *demoService = (DemoService *)service;
135     demoService->identity = identity;
136 
137     return TRUE;
138 }
139 
MessageHandle(Service * service,Request * msg)140 static BOOL MessageHandle(Service *service, Request *msg)
141 {
142     (void)msg;
143     DemoService *demoService = (DemoService *)service;
144     demoService->serviceCalledCount++;
145     if (service == (Service *)&g_service[0]) {
146         g_threadID11 =  osThreadGetId();
147     } else {
148         g_threadID21 =  osThreadGetId();
149     }
150     return TRUE;
151 }
152 
GetTaskConfig(Service * service)153 static TaskConfig GetTaskConfig(Service *service)
154 {
155     TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 1600, 20, SHARED_TASK};
156     if (service == (Service *)&g_service[0]) {
157         config.priority = PRI_NORMAL;
158     } else {
159         config.priority = PRI_NORMAL + 1;
160     }
161     return config;
162 }
163 
164 static DemoFeature g_createFeature[] = {
165     {
166         .GetName = FEATURE_GetName,
167         .OnInitialize = FEATURE_OnInitialize,
168         .OnStop = FEATURE_OnStop,
169         .OnMessage = FEATURE_OnMessage,
170         .ref = 1,
171         .iUnknown = {
172             DEFAULT_IUNKNOWN_IMPL,
173             .FeatureApi001 = FeatureApi001,
174             .SendRequestProxyF = SendRequestProxyF,
175         },
176         .identity = {-1, -1, NULL},
177         .featureCalledCount = 0,
178     },
179     {
180         .GetName = FEATURE_GetName,
181         .OnInitialize = FEATURE_OnInitialize,
182         .OnStop = FEATURE_OnStop,
183         .OnMessage = FEATURE_OnMessage,
184         .ref = 1,
185         .iUnknown = {
186             DEFAULT_IUNKNOWN_IMPL,
187             .FeatureApi001 = FeatureApi001,
188             .SendRequestProxyF = SendRequestProxyF,
189         },
190         .identity = {-1, -1, NULL},
191         .featureCalledCount = 0,
192     },
193 };
194 
FEATURE_GetName(Feature * feature)195 static const char *FEATURE_GetName(Feature *feature)
196 {
197     (void)feature;
198     return "featureName501";
199 }
200 
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)201 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
202 {
203     (void)parent;
204     DemoFeature *demoFeature = (DemoFeature *)feature;
205     demoFeature->identity = identity;
206 }
207 
FEATURE_OnStop(Feature * feature,Identity identity)208 static void FEATURE_OnStop(Feature *feature, Identity identity)
209 {
210     (void)feature;
211     (void)identity;
212 }
213 
FEATURE_OnMessage(Feature * feature,Request * request)214 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
215 {
216     DemoFeature *demoFeature = (DemoFeature *)feature;
217     demoFeature->featureCalledCount++;
218 
219     printf("[hctest]FEATURE_OnMessage request msgId is %d \n", request->msgId);
220     if (feature == (Feature *)&g_createFeature[0]) {
221         g_threadID12 =  osThreadGetId();
222     } else {
223         g_threadID22 =  osThreadGetId();
224     }
225     return TRUE;
226 }
227 
ServiceInit(void)228 static void ServiceInit(void)
229 {
230     for (int i = 0; i < TARGET_NUM; i++) {
231         SAMGR_GetInstance()->RegisterService((Service *)&g_service[i]);
232     }
233 }
234 SYS_SERVICE_INIT(ServiceInit);
235 
FeatureInit(void)236 static void FeatureInit(void)
237 {
238     for (int i = 0; i < TARGET_NUM; i++) {
239         SAMGR_GetInstance()->RegisterDefaultFeatureApi(g_service[i].GetName((Service *)&g_service[i]),
240                                                        GET_IUNKNOWN(g_service[i]));
241 
242         SAMGR_GetInstance()->RegisterFeature(g_service[i].GetName((Service *)&g_service[i]),
243                                              (Feature *)&g_createFeature[i]);
244         SAMGR_GetInstance()->RegisterFeatureApi(g_service[i].GetName((Service *)&g_service[i]), "featureName501",
245                                                 GET_IUNKNOWN(g_createFeature[i]));
246     }
247 }
248 SYS_FEATURE_INIT(FeatureInit);
249 
GetIUnknown(const char * serviceName,const char * featureName)250 static DemoApi *GetIUnknown(const char *serviceName, const char *featureName)
251 {
252     DemoApi *demoApi = NULL;
253     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(serviceName, featureName);
254     if (iUnknown == NULL) {
255         return NULL;
256     }
257     int result = iUnknown->QueryInterface(iUnknown, 0, (void **)&demoApi);
258     if (result == 0 && demoApi != NULL) {
259         return demoApi;
260     } else {
261         return NULL;
262     }
263 }
264 
GetDefaultIUnknown(const char * serviceName)265 static DefaultFeatureApi *GetDefaultIUnknown(const char *serviceName)
266 {
267     DefaultFeatureApi *defaultApi = NULL;
268     IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(serviceName);
269     if (iUnknown == NULL) {
270         return NULL;
271     }
272     int result = iUnknown->QueryInterface(iUnknown, 0, (void **)&defaultApi);
273     if (result == 0 && defaultApi != NULL) {
274         return defaultApi;
275     } else {
276         return NULL;
277     }
278 }
279 
280 LITE_TEST_SUIT(distributedschedule, taskpool, ShareTaskFuncTestSuite);
281 
ShareTaskFuncTestSuiteSetUp(void)282 static BOOL ShareTaskFuncTestSuiteSetUp(void)
283 {
284     osDelay(OPER_INTERVAL);
285     return TRUE;
286 }
287 
ShareTaskFuncTestSuiteTearDown(void)288 static BOOL ShareTaskFuncTestSuiteTearDown(void)
289 {
290     return TRUE;
291 }
292 
293 /**
294  * @tc.number    : DMSLite_SAMGR_Taskpool_SharedTask_0010
295  * @tc.name      : Service share taskpool by the same priority, the first task function is ok
296  * @tc.desc      : [C- SOFTWARE -0200]
297  */
298 LITE_TEST_CASE(ShareTaskFuncTestSuite, testSharedTask0010, Function | MediumTest | Level2)
299 {
300     DemoApi *demoApi = GetIUnknown("SharedTask01", "featureName501");
301     if (demoApi == NULL) {
302         TEST_FAIL();
303     }
304     BOOL result = demoApi->FeatureApi001((IUnknown *)demoApi, "xxxx");
305     TEST_ASSERT_EQUAL_INT(result, TRUE);
306 
307     g_createFeature[0].featureCalledCount = 0;
308     Request request = {.msgId = 0, .msgValue = 0};
309     char *body = "I wanna async call good result!";
310     request.len = (int16)(strlen(body) + 1);
311     request.data = malloc(request.len);
312     if (request.data == NULL) {
313         TEST_FAIL();
314     }
315     strcpy_s(request.data, request.len, body);
316     int32 result2 = demoApi->SendRequestProxyF(&(g_createFeature[0].identity), &request, NULL);
317     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
318     osDelay(OPER_INTERVAL);
319     TEST_ASSERT_EQUAL_INT(g_createFeature[0].featureCalledCount == 1, TRUE);
320 
321     // *************************** //
322     DefaultFeatureApi *defaultApi = GetDefaultIUnknown("SharedTask01");
323     if (defaultApi == NULL) {
324         TEST_FAIL();
325     }
326     result = defaultApi->DefaultApi001((IUnknown *)defaultApi, "yyyy");
327     TEST_ASSERT_EQUAL_INT(result, TRUE);
328 
329     g_service[0].serviceCalledCount = 0;
330     Request request2 = {.msgId = 0, .msgValue = 0};
331     char *body2 = "I want to call defaultFeature!";
332     request2.len = (int16)(strlen(body2) + 1);
333     request2.data = malloc(request2.len);
334     if (request2.data == NULL) {
335         TEST_FAIL();
336     }
337     strcpy_s(request2.data, request2.len, body2);
338     result2 = defaultApi->SendRequestProxyDF(&(g_service[0].identity), &request2, NULL);
339     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
340     osDelay(OPER_INTERVAL);
341     TEST_ASSERT_EQUAL_INT(g_service[0].serviceCalledCount == 1, TRUE);
342 };
343 
344 /**
345  * @tc.number    : DMSLite_SAMGR_Taskpool_SharedTask_0020
346  * @tc.name      : Service share taskpool by the same priority, the second task function is ok
347  * @tc.desc      : [C- SOFTWARE -0200]
348  */
349 LITE_TEST_CASE(ShareTaskFuncTestSuite, testSharedTask0020, Function | MediumTest | Level2)
350 {
351     DemoApi *demoApi = GetIUnknown("SharedTask02", "featureName501");
352     if (demoApi == NULL) {
353         TEST_FAIL();
354     }
355     BOOL result = demoApi->FeatureApi001((IUnknown *)demoApi, "xxxx");
356     TEST_ASSERT_EQUAL_INT(result, TRUE);
357 
358     g_createFeature[1].featureCalledCount = 0;
359     Request request = {.msgId = 0, .msgValue = 0};
360     char *body = "I wanna async call good result!";
361     request.len = (int16)(strlen(body) + 1);
362     request.data = malloc(request.len);
363     if (request.data == NULL) {
364         TEST_FAIL();
365     }
366     strcpy_s(request.data, request.len, body);
367     int32 result2 = demoApi->SendRequestProxyF(&(g_createFeature[1].identity), &request, NULL);
368     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
369     osDelay(OPER_INTERVAL);
370     TEST_ASSERT_EQUAL_INT(g_createFeature[1].featureCalledCount == 1, TRUE);
371 
372     // *************************** //
373     DefaultFeatureApi *defaultApi = GetDefaultIUnknown("SharedTask02");
374     if (defaultApi == NULL) {
375         TEST_FAIL();
376     }
377     result = defaultApi->DefaultApi001((IUnknown *)defaultApi, "yyyy");
378     TEST_ASSERT_EQUAL_INT(result, TRUE);
379 
380     g_service[1].serviceCalledCount = 0;
381     Request request2 = {.msgId = 0, .msgValue = 0};
382     char *body2 = "I want to call defaultFeature!";
383     request2.len = (int16)(strlen(body2) + 1);
384     request2.data = malloc(request2.len);
385     if (request2.data == NULL) {
386         TEST_FAIL();
387     }
388     strcpy_s(request2.data, request2.len, body2);
389     result2 = defaultApi->SendRequestProxyDF(&(g_service[1].identity), &request2, NULL);
390     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
391     osDelay(OPER_INTERVAL);
392     TEST_ASSERT_EQUAL_INT(g_service[1].serviceCalledCount == 1, TRUE);
393 };
394 
395 /**
396  * @tc.number    : DMSLite_SAMGR_Taskpool_SharedTask_0030
397  * @tc.name      : Service share taskpool by the same priority, the thread id of the services is the same
398  * @tc.desc      : [C- SOFTWARE -0200]
399  */
400 LITE_TEST_CASE(ShareTaskFuncTestSuite, testSharedTask0030, Function | MediumTest | Level2)
401 {
402     DefaultFeatureApi *defaultApi = GetDefaultIUnknown("SharedTask01");
403     if (defaultApi == NULL) {
404         TEST_FAIL();
405     }
406     Request request = {.msgId = 0, .msgValue = 0};
407     char *body = "I want to call defaultFeature!";
408     request.len = (int16)(strlen(body) + 1);
409     request.data = malloc(request.len);
410     if (request.data == NULL) {
411         TEST_FAIL();
412     }
413     strcpy_s(request.data, request.len, body);
414     defaultApi->SendRequestProxyDF(&(g_service[0].identity), &request, NULL);
415 
416     DefaultFeatureApi *defaultApi2 = GetDefaultIUnknown("SharedTask01");
417     if (defaultApi2 == NULL) {
418         TEST_FAIL();
419     }
420     Request request2 = {.msgId = 0, .msgValue = 0};
421     request2.len = (int16)(strlen(body) + 1);
422     request2.data = malloc(request2.len);
423     if (request2.data == NULL) {
424         TEST_FAIL();
425     }
426     strcpy_s(request2.data, request2.len, body);
427     defaultApi2->SendRequestProxyDF(&(g_service[1].identity), &request2, NULL);
428 
429     osDelay(OPER_INTERVAL);
430     TEST_ASSERT_EQUAL_INT(g_threadID11 == g_threadID21, TRUE);
431 };
432 
433 /**
434  * @tc.number    : DMSLite_SAMGR_Taskpool_SharedTask_0040
435  * @tc.name      : Service share taskpool by the same priority, the thread id of the features is the same
436  * @tc.desc      : [C- SOFTWARE -0200]
437  */
438 LITE_TEST_CASE(ShareTaskFuncTestSuite, testSharedTask0040, Function | MediumTest | Level2)
439 {
440     DemoApi *demoApi = GetIUnknown("SharedTask01", "featureName501");
441     if (demoApi == NULL) {
442         TEST_FAIL();
443     }
444     Request request1 = {.msgId = 1, .msgValue = 0};
445     char *body = "I wanna async call good result!";
446     request1.len = (int16)(strlen(body) + 1);
447     request1.data = malloc(request1.len);
448     if (request1.data == NULL) {
449         TEST_FAIL();
450     }
451     strcpy_s(request1.data, request1.len, body);
452     demoApi->SendRequestProxyF(&(g_createFeature[0].identity), &request1, NULL);
453 
454     DemoApi *demoApi2 = GetIUnknown("SharedTask02", "featureName501");
455     if (demoApi2 == NULL) {
456         TEST_FAIL();
457     }
458     Request request2 = {.msgId = 1 + 1, .msgValue = 0};
459     request2.len = (int16)(strlen(body) + 1);
460     request2.data = malloc(request2.len);
461     if (request2.data == NULL) {
462         TEST_FAIL();
463     }
464     strcpy_s(request2.data, request2.len, body);
465     demoApi2->SendRequestProxyF(&(g_createFeature[1].identity), &request2, NULL);
466 
467     osDelay(OPER_INTERVAL);
468     TEST_ASSERT_EQUAL_INT(g_threadID12 == g_threadID22, TRUE);
469 };
470 RUN_TEST_SUITE(ShareTaskFuncTestSuite);