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