1 /*
2 * Copyright (c) 2022-2023 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 "gtest/gtest.h"
18 #include "softbus_adapter_thread.h"
19 #include "softbus_errcode.h"
20
21 using namespace std;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 static SoftBusCond g_cond;
26 static SoftBusMutex g_mutex;
27 const int32_t DELAY_TIME = 1000;
28
29 class SoftbusThreadTest : public testing::Test {
30 protected:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 };
36
SetUpTestCase(void)37 void SoftbusThreadTest::SetUpTestCase(void)
38 {
39 }
40
TearDownTestCase(void)41 void SoftbusThreadTest::TearDownTestCase(void)
42 {
43 }
44
SetUp()45 void SoftbusThreadTest::SetUp()
46 {
47 }
48
TearDown()49 void SoftbusThreadTest::TearDown()
50 {
51 }
52
SoftBusThreadTask(void * arg)53 static void *SoftBusThreadTask(void *arg)
54 {
55 printf("----------%s--------\n", __FUNCTION__);
56 SoftBusSleepMs(DELAY_TIME);
57 return static_cast<void *>(const_cast<char *>("SoftBusThreadTask"));
58 }
59
ThreadSelfTest(void * arg)60 static void *ThreadSelfTest(void *arg)
61 {
62 printf("----------%s--------\n", __FUNCTION__);
63 SoftBusThread thread = SoftBusThreadGetSelf();
64 EXPECT_TRUE(thread != 0);
65 SoftBusSleepMs(DELAY_TIME);
66 return nullptr;
67 }
68
ThreadWaitTest(void * arg)69 static void *ThreadWaitTest(void *arg)
70 {
71 printf("----------%s--------\n", __FUNCTION__);
72 int32_t ret = SoftBusCondWait(&g_cond, &g_mutex, NULL);
73 EXPECT_EQ(SOFTBUS_OK, ret);
74 SoftBusSleepMs(DELAY_TIME);
75 return nullptr;
76 }
77
ThreadSignalTest(void * arg)78 static void *ThreadSignalTest(void *arg)
79 {
80 printf("----------%s--------\n", __FUNCTION__);
81 SoftBusSleepMs(DELAY_TIME);
82 int32_t ret = SoftBusCondSignal(&g_cond);
83 EXPECT_EQ(SOFTBUS_OK, ret);
84 return nullptr;
85 }
86
87 /*
88 * @tc.name: SoftbusMutexAttrInitTest001
89 * @tc.desc: mutexAttr is nullptr
90 * @tc.type: FUNC
91 * @tc.require: 1
92 */
93 HWTEST_F(SoftbusThreadTest, SoftbusMutexAttrInitTest001, TestSize.Level0)
94 {
95 int32_t ret = SoftBusMutexAttrInit(nullptr);
96 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
97 }
98
99 /*
100 * @tc.name: SoftbusMutexAttrInitTest002
101 * @tc.desc: mutexAttr is valid
102 * @tc.type: FUNC
103 * @tc.require: 1
104 */
105 HWTEST_F(SoftbusThreadTest, SoftbusMutexAttrInitTest002, TestSize.Level0)
106 {
107 SoftBusMutexAttr mutexAttr;
108 int32_t ret = SoftBusMutexAttrInit(&mutexAttr);
109 EXPECT_EQ(SOFTBUS_OK, ret);
110 EXPECT_EQ(SOFTBUS_MUTEX_NORMAL, mutexAttr.type);
111 }
112
113 /*
114 * @tc.name: SoftBusMutexInitTest001
115 * @tc.desc: mutexAttr is nullptr
116 * @tc.type: FUNC
117 * @tc.require: 1
118 */
119 HWTEST_F(SoftbusThreadTest, SoftBusMutexInitTest001, TestSize.Level0)
120 {
121 SoftBusMutex mutex = 0;
122 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
123 EXPECT_EQ(SOFTBUS_OK, ret);
124 }
125
126 /*
127 * @tc.name: SoftBusMutexInitTest002
128 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
129 * @tc.type: FUNC
130 * @tc.require: 1
131 */
132 HWTEST_F(SoftbusThreadTest, SoftBusMutexInitTest002, TestSize.Level0)
133 {
134 SoftBusMutex mutex = 0;
135 SoftBusMutexAttr mutexAttr = {
136 .type = SOFTBUS_MUTEX_NORMAL,
137 };
138 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
139 EXPECT_EQ(SOFTBUS_OK, ret);
140 }
141
142 /*
143 * @tc.name: SoftBusMutexInitTest003
144 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_RECURSIVE
145 * @tc.type: FUNC
146 * @tc.require: 1
147 */
148 HWTEST_F(SoftbusThreadTest, SoftBusMutexInitTest003, TestSize.Level0)
149 {
150 SoftBusMutex mutex = 0;
151 SoftBusMutexAttr mutexAttr = {
152 .type = SOFTBUS_MUTEX_RECURSIVE,
153 };
154 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
155 EXPECT_EQ(SOFTBUS_OK, ret);
156 }
157
158 /*
159 * @tc.name: SoftBusMutexLockTest001
160 * @tc.desc: mutex is nullptr
161 * @tc.type: FUNC
162 * @tc.require: 1
163 */
164 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest001, TestSize.Level0)
165 {
166 int32_t ret = SoftBusMutexLock(nullptr);
167 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
168 }
169
170 /*
171 * @tc.name: SoftBusMutexLockTest002
172 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
173 * @tc.type: FUNC
174 * @tc.require: 1
175 */
176 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest002, TestSize.Level0)
177 {
178 SoftBusMutex mutex = 0;
179 SoftBusMutexAttr mutexAttr = {
180 .type = SOFTBUS_MUTEX_NORMAL,
181 };
182 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
183 EXPECT_EQ(SOFTBUS_OK, ret);
184 ret = SoftBusMutexLock(&mutex);
185 EXPECT_EQ(SOFTBUS_OK, ret);
186 }
187
188 /*
189 * @tc.name: SoftBusMutexLockTest003
190 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_RECURSIVE
191 * @tc.type: FUNC
192 * @tc.require: 1
193 */
194 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest003, TestSize.Level0)
195 {
196 SoftBusMutex mutex = 0;
197 SoftBusMutexAttr mutexAttr = {
198 .type = SOFTBUS_MUTEX_RECURSIVE,
199 };
200 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
201 EXPECT_EQ(SOFTBUS_OK, ret);
202 ret = SoftBusMutexLock(&mutex);
203 EXPECT_EQ(SOFTBUS_OK, ret);
204 }
205
206 /*
207 * @tc.name: SoftBusMutexLockTest004
208 * @tc.desc: mutex is default
209 * @tc.type: FUNC
210 * @tc.require: 1
211 */
212 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest004, TestSize.Level0)
213 {
214 SoftBusMutex mutex = 0;
215 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
216 ret = SoftBusMutexLock(&mutex);
217 EXPECT_EQ(SOFTBUS_OK, ret);
218 }
219
220 /*
221 * @tc.name: SoftBusMutexLockTest005
222 * @tc.desc: mutex value is 0
223 * @tc.type: FUNC
224 * @tc.require: 1
225 */
226 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockTest005, TestSize.Level0)
227 {
228 SoftBusMutex mutex = 0;
229 int32_t ret = SoftBusMutexLock(&mutex);
230 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
231 }
232
233 /*
234 * @tc.name: SoftBusMutexUnlockTest001
235 * @tc.desc: mutex is nullptr
236 * @tc.type: FUNC
237 * @tc.require: 1
238 */
239 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest001, TestSize.Level0)
240 {
241 int32_t ret = SoftBusMutexUnlock(nullptr);
242 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
243 }
244
245 /*
246 * @tc.name: SoftBusMutexUnlockTest002
247 * @tc.desc: mutex value is 0
248 * @tc.type: FUNC
249 * @tc.require: 1
250 */
251 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest002, TestSize.Level0)
252 {
253 SoftBusMutex mutex = 0;
254 int32_t ret = SoftBusMutexUnlock(&mutex);
255 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
256 }
257
258 /*
259 * @tc.name: SoftBusMutexUnlockTest003
260 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
261 * @tc.type: FUNC
262 * @tc.require: 1
263 */
264 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest003, TestSize.Level0)
265 {
266 SoftBusMutex mutex = 0;
267 SoftBusMutexAttr mutexAttr = {
268 .type = SOFTBUS_MUTEX_NORMAL,
269 };
270 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
271 EXPECT_EQ(SOFTBUS_OK, ret);
272 ret = SoftBusMutexLock(&mutex);
273 EXPECT_EQ(SOFTBUS_OK, ret);
274 ret = SoftBusMutexUnlock(&mutex);
275 EXPECT_EQ(SOFTBUS_OK, ret);
276 }
277
278 /*
279 * @tc.name: SoftBusMutexUnlockTest004
280 * @tc.desc: mutexAttr type is SOFTBUS_MUTEX_NORMAL
281 * @tc.type: FUNC
282 * @tc.require: 1
283 */
284 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest004, TestSize.Level0)
285 {
286 SoftBusMutex mutex = 0;
287 SoftBusMutexAttr mutexAttr = {
288 .type = SOFTBUS_MUTEX_RECURSIVE,
289 };
290 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
291 EXPECT_EQ(SOFTBUS_OK, ret);
292 ret = SoftBusMutexLock(&mutex);
293 EXPECT_EQ(SOFTBUS_OK, ret);
294 ret = SoftBusMutexUnlock(&mutex);
295 EXPECT_EQ(SOFTBUS_OK, ret);
296 }
297
298 /*
299 * @tc.name: SoftBusMutexUnlockTest005
300 * @tc.desc: mutex value is default
301 * @tc.type: FUNC
302 * @tc.require: 1
303 */
304 HWTEST_F(SoftbusThreadTest, SoftBusMutexUnlockTest005, TestSize.Level0)
305 {
306 SoftBusMutex mutex = 0;
307 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
308 EXPECT_EQ(SOFTBUS_OK, ret);
309 ret = SoftBusMutexLock(&mutex);
310 EXPECT_EQ(SOFTBUS_OK, ret);
311 ret = SoftBusMutexUnlock(&mutex);
312 EXPECT_EQ(SOFTBUS_OK, ret);
313 }
314
315 /*
316 * @tc.name: SoftBusMutexDestroyTest001
317 * @tc.desc: mutex is nullptr
318 * @tc.type: FUNC
319 * @tc.require: 1
320 */
321 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest001, TestSize.Level0)
322 {
323 int32_t ret = SoftBusMutexDestroy(nullptr);
324 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
325 }
326
327 /*
328 * @tc.name: SoftBusMutexDestroyTest002
329 * @tc.desc: mutex value is 0
330 * @tc.type: FUNC
331 * @tc.require: 1
332 */
333 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest002, TestSize.Level0)
334 {
335 SoftBusMutex mutex = 0;
336 int32_t ret = SoftBusMutexDestroy(&mutex);
337 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
338 }
339
340 /*
341 * @tc.name: SoftBusMutexDestroyTest003
342 * @tc.desc: mutexAttr is nullptr
343 * @tc.type: FUNC
344 * @tc.require: 1
345 */
346 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest003, TestSize.Level0)
347 {
348 SoftBusMutex mutex = 0;
349 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
350 EXPECT_EQ(SOFTBUS_OK, ret);
351
352 ret = SoftBusMutexDestroy(&mutex);
353 EXPECT_EQ(SOFTBUS_OK, ret);
354 }
355
356 /*
357 * @tc.name: SoftBusMutexDestroyTest004
358 * @tc.desc: mutexAttr is SOFTBUS_MUTEX_NORMAL
359 * @tc.type: FUNC
360 * @tc.require: 1
361 */
362 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest004, TestSize.Level0)
363 {
364 SoftBusMutex mutex = 0;
365 SoftBusMutexAttr mutexAttr = {
366 .type = SOFTBUS_MUTEX_NORMAL,
367 };
368 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
369 EXPECT_EQ(SOFTBUS_OK, ret);
370
371 ret = SoftBusMutexDestroy(&mutex);
372 EXPECT_EQ(SOFTBUS_OK, ret);
373 }
374
375 /*
376 * @tc.name: SoftBusMutexDestroyTest005
377 * @tc.desc: mutexAttr is SOFTBUS_MUTEX_RECURSIVE
378 * @tc.type: FUNC
379 * @tc.require: 1
380 */
381 HWTEST_F(SoftbusThreadTest, SoftBusMutexDestroyTest005, TestSize.Level0)
382 {
383 SoftBusMutex mutex = 0;
384 SoftBusMutexAttr mutexAttr = {
385 .type = SOFTBUS_MUTEX_RECURSIVE,
386 };
387 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
388 EXPECT_EQ(SOFTBUS_OK, ret);
389
390 ret = SoftBusMutexDestroy(&mutex);
391 EXPECT_EQ(SOFTBUS_OK, ret);
392 }
393
394 /*
395 * @tc.name: SoftBusMutexLockGuardTest001
396 * @tc.desc: should call SoftBusMutexUnlock automatically when leave bracket scope
397 * @tc.type: FUNC
398 * @tc.require:
399 */
400 HWTEST_F(SoftbusThreadTest, SoftBusMutexLockGuardTest001, TestSize.Level0)
401 {
402 SoftBusMutex mutex = 0;
403 int32_t ret = SoftBusMutexInit(&mutex, nullptr);
404 EXPECT_EQ(SOFTBUS_OK, ret);
405 {
406 ret = SoftBusMutexLock(&mutex);
407 EXPECT_EQ(SOFTBUS_OK, ret);
408 SOFTBUS_LOCK_GUARD(mutex);
409 }
410 {
411 ret = SoftBusMutexLock(&mutex);
412 EXPECT_EQ(SOFTBUS_OK, ret);
413 SOFTBUS_LOCK_GUARD(mutex);
414 }
415 ret = SoftBusMutexDestroy(&mutex);
416 EXPECT_EQ(SOFTBUS_OK, ret);
417 }
418
419 /*
420 * @tc.name: SoftBusThreadAttrInitTest001
421 * @tc.desc: threadAttr is nullptr
422 * @tc.type: FUNC
423 * @tc.require: 1
424 */
425 HWTEST_F(SoftbusThreadTest, SoftBusThreadAttrInitTest001, TestSize.Level0)
426 {
427 int32_t ret = SoftBusThreadAttrInit(nullptr);
428 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
429 }
430
431 /*
432 * @tc.name: SoftBusThreadAttrInitTest002
433 * @tc.desc: threadAttr is valid
434 * @tc.type: FUNC
435 * @tc.require: 1
436 */
437 HWTEST_F(SoftbusThreadTest, SoftBusThreadAttrInitTest002, TestSize.Level0)
438 {
439 SoftBusThreadAttr threadAttr = {0};
440 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
441 EXPECT_EQ(SOFTBUS_OK, ret);
442 }
443
444 /*
445 * @tc.name: SoftBusThreadCreateTest001
446 * @tc.desc: thread is nullptr
447 * @tc.type: FUNC
448 * @tc.require: 1
449 */
450 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest001, TestSize.Level0)
451 {
452 SoftBusThreadAttr threadAttr = {0};
453 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
454 EXPECT_EQ(SOFTBUS_OK, ret);
455
456 ret = SoftBusThreadCreate(nullptr, &threadAttr, SoftBusThreadTask, nullptr);
457 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
458 }
459
460 /*
461 * @tc.name: SoftBusThreadCreateTest002
462 * @tc.desc: threadAttr is nullptr
463 * @tc.type: FUNC
464 * @tc.require: 1
465 */
466 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest002, TestSize.Level0)
467 {
468 SoftBusThread thread = 0;
469
470 int32_t ret = SoftBusThreadCreate(&thread, nullptr, SoftBusThreadTask, nullptr);
471 EXPECT_EQ(SOFTBUS_OK, ret);
472 EXPECT_TRUE(thread != 0);
473 }
474
475 /*
476 * @tc.name: SoftBusThreadCreateTest003
477 * @tc.desc: threadAttr is valid
478 * @tc.type: FUNC
479 * @tc.require: 1
480 */
481 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest003, TestSize.Level0)
482 {
483 SoftBusThread thread = 0;
484 SoftBusThreadAttr threadAttr = {0};
485 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
486 EXPECT_EQ(SOFTBUS_OK, ret);
487
488 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
489 EXPECT_EQ(SOFTBUS_OK, ret);
490 EXPECT_TRUE(thread != 0);
491 }
492
493 #if HAVE_PRO
494 /*
495 * @tc.name: SoftBusThreadCreateTest004
496 * @tc.desc: threadAttr add taskName
497 * @tc.type: FUNC
498 * @tc.require: 1
499 */
500 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest004, TestSize.Level0)
501 {
502 SoftBusThread thread = 0;
503 SoftBusThreadAttr threadAttr = {0};
504 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
505 EXPECT_EQ(SOFTBUS_OK, ret);
506
507 threadAttr.taskName = "ThreadTask";
508 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
509 EXPECT_EQ(SOFTBUS_ERR, ret);
510 EXPECT_TRUE(thread != 0);
511 }
512 #endif
513
514 /*
515 * @tc.name: SoftBusThreadCreateTest005
516 * @tc.desc: threadAttr modify prior
517 * @tc.type: FUNC
518 * @tc.require: 1
519 */
520 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest005, TestSize.Level0)
521 {
522 SoftBusThread thread = 0;
523 SoftBusThreadAttr threadAttr = {0};
524 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
525 EXPECT_EQ(SOFTBUS_OK, ret);
526
527 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
528 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
529 EXPECT_EQ(SOFTBUS_OK, ret);
530 EXPECT_TRUE(thread != 0);
531 }
532
533 /*
534 * @tc.name: SoftBusThreadCreateTest006
535 * @tc.desc: threadAttr modify prior
536 * @tc.type: FUNC
537 * @tc.require: 1
538 */
539 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest006, TestSize.Level0)
540 {
541 SoftBusThread thread = 0;
542 SoftBusThreadAttr threadAttr = {0};
543 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
544 EXPECT_EQ(SOFTBUS_OK, ret);
545
546 threadAttr.prior = SOFTBUS_PRIORITY_HIGH;
547 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
548 EXPECT_EQ(SOFTBUS_OK, ret);
549 EXPECT_TRUE(thread != 0);
550 }
551
552 /*
553 * @tc.name: SoftBusThreadCreateTest007
554 * @tc.desc: threadAttr modify prior
555 * @tc.type: FUNC
556 * @tc.require: 1
557 */
558 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest007, TestSize.Level0)
559 {
560 SoftBusThread thread = 0;
561 SoftBusThreadAttr threadAttr = {0};
562 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
563 EXPECT_EQ(SOFTBUS_OK, ret);
564
565 threadAttr.prior = SOFTBUS_PRIORITY_DEFAULT;
566 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
567 EXPECT_EQ(SOFTBUS_OK, ret);
568 EXPECT_TRUE(thread != 0);
569 }
570
571 /*
572 * @tc.name: SoftBusThreadCreateTest008
573 * @tc.desc: threadAttr modify prior
574 * @tc.type: FUNC
575 * @tc.require: 1
576 */
577 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest008, TestSize.Level0)
578 {
579 SoftBusThread thread = 0;
580 SoftBusThreadAttr threadAttr = {0};
581 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
582 EXPECT_EQ(SOFTBUS_OK, ret);
583
584 threadAttr.prior = SOFTBUS_PRIORITY_LOW;
585 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
586 EXPECT_EQ(SOFTBUS_OK, ret);
587 EXPECT_TRUE(thread != 0);
588 }
589
590 /*
591 * @tc.name: SoftBusThreadCreateTest009
592 * @tc.desc: threadAttr modify prior
593 * @tc.type: FUNC
594 * @tc.require: 1
595 */
596 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest009, TestSize.Level0)
597 {
598 SoftBusThread thread = 0;
599 SoftBusThreadAttr threadAttr = {0};
600 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
601 EXPECT_EQ(SOFTBUS_OK, ret);
602
603 threadAttr.prior = SOFTBUS_PRIORITY_LOWEST;
604 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
605 EXPECT_EQ(SOFTBUS_OK, ret);
606 EXPECT_TRUE(thread != 0);
607 }
608
609 #if HAVE_PRO
610 /*
611 * @tc.name: SoftBusThreadCreateTest010
612 * @tc.desc: threadEntry is nullptr
613 * @tc.type: FUNC
614 * @tc.require: 1
615 */
616 HWTEST_F(SoftbusThreadTest, SoftBusThreadCreateTest010, TestSize.Level0)
617 {
618 SoftBusThread thread = 0;
619 SoftBusThreadAttr threadAttr = {0};
620 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
621 EXPECT_EQ(SOFTBUS_OK, ret);
622
623 ret = SoftBusThreadCreate(&thread, &threadAttr, nullptr, nullptr);
624 EXPECT_EQ(SOFTBUS_ERR, ret);
625 }
626
627 /*
628 * @tc.name: SoftBusThreadSetNameTest001
629 * @tc.desc: name is nullptr
630 * @tc.type: FUNC
631 * @tc.require: 1
632 */
633 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest001, TestSize.Level0)
634 {
635 SoftBusThread thread = 0;
636 SoftBusThreadAttr threadAttr = {0};
637 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
638 EXPECT_EQ(SOFTBUS_OK, ret);
639
640 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
641
642 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
643 EXPECT_EQ(SOFTBUS_OK, ret);
644 EXPECT_TRUE(thread != 0);
645
646 ret = SoftBusThreadSetName(thread, nullptr);
647 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
648 }
649
650 /*
651 * @tc.name: SoftBusThreadSetNameTest002
652 * @tc.desc: name is large than TASK_NAME_MAX_LEN
653 * @tc.type: FUNC
654 * @tc.require: 1
655 */
656 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest002, TestSize.Level0)
657 {
658 const char *name = "abcdefghijklmnopqrstuvwxyz";
659 SoftBusThread thread = 0;
660 SoftBusThreadAttr threadAttr = {0};
661 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
662 EXPECT_EQ(SOFTBUS_OK, ret);
663
664 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
665
666 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
667 EXPECT_EQ(SOFTBUS_OK, ret);
668 EXPECT_TRUE(thread != 0);
669
670 ret = SoftBusThreadSetName(thread, name);
671 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
672 }
673
674 /*
675 * @tc.name: SoftBusThreadSetNameTest003
676 * @tc.desc: name is equal to TASK_NAME_MAX_LEN
677 * @tc.type: FUNC
678 * @tc.require: 1
679 */
680 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest003, TestSize.Level0)
681 {
682 const char *name = "abcdefghijklmnop";
683 SoftBusThread thread = 0;
684 SoftBusThreadAttr threadAttr = {0};
685 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
686 EXPECT_EQ(SOFTBUS_OK, ret);
687
688 threadAttr.prior = SOFTBUS_PRIORITY_HIGHEST;
689
690 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
691 EXPECT_EQ(SOFTBUS_OK, ret);
692 EXPECT_TRUE(thread != 0);
693
694 ret = SoftBusThreadSetName(thread, name);
695 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
696 }
697
698 /*
699 * @tc.name: SoftBusThreadSetNameTest004
700 * @tc.desc: name include chinese character
701 * @tc.type: FUNC
702 * @tc.require: 1
703 */
704 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest004, TestSize.Level0)
705 {
706 const char *name = "a中文p";
707 SoftBusThread thread = 0;
708 SoftBusThreadAttr threadAttr = {0};
709 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
710 EXPECT_EQ(SOFTBUS_OK, ret);
711
712 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
713 EXPECT_EQ(SOFTBUS_OK, ret);
714 EXPECT_TRUE(thread != 0);
715
716 ret = SoftBusThreadSetName(thread, name);
717 EXPECT_EQ(SOFTBUS_ERR, ret);
718 }
719
720 /*
721 * @tc.name: SoftBusThreadSetNameTest005
722 * @tc.desc: name is valid
723 * @tc.type: FUNC
724 * @tc.require: 1
725 */
726 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest005, TestSize.Level0)
727 {
728 const char *name = "testThread";
729 SoftBusThread thread = 0;
730 SoftBusThreadAttr threadAttr = {0};
731 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
732 EXPECT_EQ(SOFTBUS_OK, ret);
733
734 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
735 EXPECT_EQ(SOFTBUS_OK, ret);
736 EXPECT_TRUE(thread != 0);
737
738 ret = SoftBusThreadSetName(thread, name);
739 EXPECT_EQ(SOFTBUS_ERR, ret);
740 }
741
742 /*
743 * @tc.name: SoftBusThreadSetNameTest006
744 * @tc.desc: threadAttr is nullptr, name is valid
745 * @tc.type: FUNC
746 * @tc.require: 1
747 */
748 HWTEST_F(SoftbusThreadTest, SoftBusThreadSetNameTest006, TestSize.Level0)
749 {
750 const char *name = "testThread";
751 SoftBusThread thread = 0;
752
753 int32_t ret = SoftBusThreadCreate(&thread, nullptr, SoftBusThreadTask, nullptr);
754 EXPECT_EQ(SOFTBUS_OK, ret);
755 EXPECT_TRUE(thread != 0);
756
757 ret = SoftBusThreadSetName(thread, name);
758 EXPECT_EQ(SOFTBUS_ERR, ret);
759 }
760 #endif
761
762 /*
763 * @tc.name: SoftBusThreadGetSelfTest001
764 * @tc.desc: threadAttr modify prior
765 * @tc.type: FUNC
766 * @tc.require: 1
767 */
768 HWTEST_F(SoftbusThreadTest, SoftBusThreadGetSelfTest001, TestSize.Level0)
769 {
770 SoftBusThread thread = 0;
771
772 int32_t ret = SoftBusThreadCreate(&thread, NULL, ThreadSelfTest, nullptr);
773 EXPECT_EQ(SOFTBUS_OK, ret);
774 EXPECT_TRUE(thread != 0);
775 }
776
777 /*
778 * @tc.name: SoftBusCondInitTest001
779 * @tc.desc: cond is nullptr
780 * @tc.type: FUNC
781 * @tc.require: 1
782 */
783 HWTEST_F(SoftbusThreadTest, SoftBusCondInitTest001, TestSize.Level0)
784 {
785 int32_t ret = SoftBusCondInit(nullptr);
786 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
787 }
788
789 /*
790 * @tc.name: SoftBusCondInitTest002
791 * @tc.desc: cond is valid
792 * @tc.type: FUNC
793 * @tc.require: 1
794 */
795 HWTEST_F(SoftbusThreadTest, SoftBusCondInitTest002, TestSize.Level0)
796 {
797 SoftBusCond cond = 0;
798 int32_t ret = SoftBusCondInit(&cond);
799 EXPECT_EQ(SOFTBUS_OK, ret);
800 EXPECT_TRUE(cond != 0);
801 }
802
803 /*
804 * @tc.name: SoftBusCondSignalTest001
805 * @tc.desc: cond is nullptr
806 * @tc.type: FUNC
807 * @tc.require: 1
808 */
809 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest001, TestSize.Level0)
810 {
811 int32_t ret = SoftBusCondSignal(nullptr);
812 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
813 }
814
815 /*
816 * @tc.name: SoftBusCondSignalTest002
817 * @tc.desc: no wait thread
818 * @tc.type: FUNC
819 * @tc.require: 1
820 */
821 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest002, TestSize.Level0)
822 {
823 SoftBusCond cond = 0;
824 int32_t ret = SoftBusCondSignal(&cond);
825 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
826 }
827
828 /*
829 * @tc.name: SoftBusCondSignalTest003
830 * @tc.desc: no wait thread
831 * @tc.type: FUNC
832 * @tc.require: 1
833 */
834 HWTEST_F(SoftbusThreadTest, SoftBusCondSignalTest003, TestSize.Level0)
835 {
836 SoftBusCond cond = 0;
837 int32_t ret = SoftBusCondInit(&cond);
838 EXPECT_EQ(SOFTBUS_OK, ret);
839 EXPECT_TRUE(cond != 0);
840 ret = SoftBusCondSignal(&cond);
841 EXPECT_EQ(SOFTBUS_OK, ret);
842 }
843
844 /*
845 * @tc.name: SoftBusCondBroadcastTest001
846 * @tc.desc: cond is nullptr
847 * @tc.type: FUNC
848 * @tc.require: 1
849 */
850 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest001, TestSize.Level0)
851 {
852 int32_t ret = SoftBusCondBroadcast(nullptr);
853 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
854 }
855
856 /*
857 * @tc.name: SoftBusCondBroadcastTest002
858 * @tc.desc: cond is not init
859 * @tc.type: FUNC
860 * @tc.require: 1
861 */
862 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest002, TestSize.Level0)
863 {
864 SoftBusCond cond = 0;
865
866 int32_t ret = SoftBusCondBroadcast(&cond);
867 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
868 }
869
870 /*
871 * @tc.name: SoftBusCondBroadcastTest003
872 * @tc.desc: cond is init value
873 * @tc.type: FUNC
874 * @tc.require: 1
875 */
876 HWTEST_F(SoftbusThreadTest, SoftBusCondBroadcastTest003, TestSize.Level0)
877 {
878 SoftBusCond cond = 0;
879 int32_t ret = SoftBusCondInit(&cond);
880 EXPECT_EQ(SOFTBUS_OK, ret);
881 EXPECT_TRUE(cond != 0);
882
883 ret = SoftBusCondBroadcast(&cond);
884 EXPECT_EQ(SOFTBUS_OK, ret);
885 }
886
887 /*
888 * @tc.name: SoftBusCondWaitTest001
889 * @tc.desc: cond is nullptr
890 * @tc.type: FUNC
891 * @tc.require: 1
892 */
893 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest001, TestSize.Level0)
894 {
895 SoftBusMutex mutex = 0;
896 SoftBusMutexAttr mutexAttr = {
897 .type = SOFTBUS_MUTEX_NORMAL,
898 };
899 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
900 EXPECT_EQ(SOFTBUS_OK, ret);
901 ret = SoftBusCondWait(nullptr, &mutex, nullptr);
902 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
903 }
904
905 /*
906 * @tc.name: SoftBusCondWaitTest002
907 * @tc.desc: cond value is invalid
908 * @tc.type: FUNC
909 * @tc.require: 1
910 */
911 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest002, TestSize.Level0)
912 {
913 SoftBusCond cond = 0;
914 SoftBusMutex mutex = 0;
915 SoftBusMutexAttr mutexAttr = {
916 .type = SOFTBUS_MUTEX_NORMAL,
917 };
918 int32_t ret = SoftBusMutexInit(&mutex, &mutexAttr);
919 EXPECT_EQ(SOFTBUS_OK, ret);
920 ret = SoftBusCondWait(&cond, &mutex, nullptr);
921 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
922 }
923
924 /*
925 * @tc.name: SoftBusCondWaitTest003
926 * @tc.desc: mutex is nullptr
927 * @tc.type: FUNC
928 * @tc.require: 1
929 */
930 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest003, TestSize.Level0)
931 {
932 SoftBusCond cond = 0;
933 int32_t ret = SoftBusCondInit(&cond);
934 EXPECT_EQ(SOFTBUS_OK, ret);
935 EXPECT_TRUE(cond != 0);
936 ret = SoftBusCondWait(&cond, nullptr, nullptr);
937 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
938 }
939
940 /*
941 * @tc.name: SoftBusCondWaitTest004
942 * @tc.desc: mutex value is invalid
943 * @tc.type: FUNC
944 * @tc.require: 1
945 */
946 HWTEST_F(SoftbusThreadTest, SoftBusCondWaitTest004, TestSize.Level0)
947 {
948 SoftBusCond cond = 0;
949 SoftBusMutex mutex = 0;
950 int32_t ret = SoftBusCondInit(&cond);
951 EXPECT_EQ(SOFTBUS_OK, ret);
952 EXPECT_TRUE(cond != 0);
953
954 ret = SoftBusCondWait(&cond, &mutex, nullptr);
955 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
956 }
957
958 /*
959 * @tc.name: SoftBusCondDestroyTest001
960 * @tc.desc: cond is null
961 * @tc.type: FUNC
962 * @tc.require: 1
963 */
964 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest001, TestSize.Level0)
965 {
966 int32_t ret = SoftBusCondDestroy(nullptr);
967 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
968 }
969
970 /*
971 * @tc.name: SoftBusCondDestroyTest002
972 * @tc.desc: cond is valid
973 * @tc.type: FUNC
974 * @tc.require: 1
975 */
976 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest002, TestSize.Level0)
977 {
978 SoftBusCond cond = 0;
979 int32_t ret = SoftBusCondInit(&cond);
980 EXPECT_EQ(SOFTBUS_OK, ret);
981 EXPECT_TRUE(cond != 0);
982
983 ret = SoftBusCondDestroy(&cond);
984 EXPECT_EQ(SOFTBUS_OK, ret);
985 }
986
987 /*
988 * @tc.name: SoftBusCondDestroyTest003
989 * @tc.desc: cond is valid
990 * @tc.type: FUNC
991 * @tc.require: 1
992 */
993 HWTEST_F(SoftbusThreadTest, SoftBusCondDestroyTest003, TestSize.Level0)
994 {
995 SoftBusCond cond = 0;
996 int32_t ret = SoftBusCondDestroy(&cond);
997 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
998 }
999
1000 /*
1001 * @tc.name: SoftBusThreadJoinTest001
1002 * @tc.desc: value is nullptr
1003 * @tc.type: FUNC
1004 * @tc.require: 1
1005 */
1006 HWTEST_F(SoftbusThreadTest, SoftBusThreadJoinTest001, TestSize.Level0)
1007 {
1008 SoftBusThread thread = 0;
1009 SoftBusThreadAttr threadAttr = {0};
1010 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
1011 EXPECT_EQ(SOFTBUS_OK, ret);
1012
1013 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
1014 EXPECT_EQ(SOFTBUS_OK, ret);
1015 EXPECT_TRUE(thread != 0);
1016 ret = SoftBusThreadJoin(thread, nullptr);
1017 EXPECT_EQ(SOFTBUS_OK, ret);
1018 }
1019
1020 /*
1021 * @tc.name: SoftBusThreadJoinTest002
1022 * @tc.desc: value is not nullptr
1023 * @tc.type: FUNC
1024 * @tc.require: 1
1025 */
1026 HWTEST_F(SoftbusThreadTest, SoftBusThreadJoinTest002, TestSize.Level0)
1027 {
1028 char *value = nullptr;
1029 SoftBusThread thread = 0;
1030 SoftBusThreadAttr threadAttr = {0};
1031 int32_t ret = SoftBusThreadAttrInit(&threadAttr);
1032 EXPECT_EQ(SOFTBUS_OK, ret);
1033
1034 ret = SoftBusThreadCreate(&thread, &threadAttr, SoftBusThreadTask, nullptr);
1035 EXPECT_EQ(SOFTBUS_OK, ret);
1036 EXPECT_TRUE(thread != 0);
1037 ret = SoftBusThreadJoin(thread, (void **)&value);
1038 EXPECT_EQ(SOFTBUS_OK, ret);
1039 EXPECT_TRUE(value != nullptr);
1040 }
1041
1042 /*
1043 * @tc.name: SoftBusThreadFullTest001
1044 * @tc.desc: thread process test
1045 * @tc.type: FUNC
1046 * @tc.require: 1
1047 */
1048 HWTEST_F(SoftbusThreadTest, SoftBusThreadFullTest001, TestSize.Level0)
1049 {
1050 int32_t ret = SoftBusMutexInit(&g_mutex, NULL);
1051 EXPECT_EQ(SOFTBUS_OK, ret);
1052
1053 ret = SoftBusCondInit(&g_cond);
1054 EXPECT_EQ(SOFTBUS_OK, ret);
1055
1056 SoftBusThread threadWait = 0;
1057 SoftBusThread threadSignal = 0;
1058 SoftBusThreadAttr threadAttr = {0};
1059 ret = SoftBusThreadAttrInit(&threadAttr);
1060 EXPECT_EQ(SOFTBUS_OK, ret);
1061
1062 ret = SoftBusThreadCreate(&threadWait, &threadAttr, ThreadWaitTest, nullptr);
1063 EXPECT_EQ(SOFTBUS_OK, ret);
1064 EXPECT_TRUE(threadWait != 0);
1065
1066 ret = SoftBusThreadCreate(&threadSignal, &threadAttr, ThreadSignalTest, nullptr);
1067 EXPECT_EQ(SOFTBUS_OK, ret);
1068 EXPECT_TRUE(threadSignal != 0);
1069
1070 ret = SoftBusThreadJoin(threadWait, nullptr);
1071 EXPECT_EQ(SOFTBUS_OK, ret);
1072 ret = SoftBusThreadJoin(threadSignal, nullptr);
1073 EXPECT_EQ(SOFTBUS_OK, ret);
1074 }
1075 }
1076