refactor: 删除旧测试套,拆分process用例适配门禁90s规则

close: #I4RE80
Signed-off-by: arvinzzz <zhaotianyu9@huawei.com>

Change-Id: I353fe4aa10e4f03e7cbaca572c8e73289c599c29
This commit is contained in:
arvinzzz
2022-01-19 11:19:36 +08:00
parent 8e356c99c7
commit a28fcee1c5
1838 changed files with 367 additions and 224997 deletions

View File

@@ -0,0 +1,279 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
static int g_currThreadPri, g_currThreadPolicy;
static volatile int g_pthreadTestCount = 0;
static int g_threadTestCount1 = 0;
static void *ThreadFunc5(void *ptr)
{
pid_t pid = getpid();
int ret;
ICUNIT_GOTO_EQUAL(g_threadTestCount1, 2, g_threadTestCount1, EXIT); // 2, here assert the result.
g_threadTestCount1++;
ret = pthread_detach(pthread_self());
ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
EXIT:
return NULL;
}
static void *ThreadFunc4(void *arg)
{
pthread_t pthread = pthread_self();
int i = 0;
unsigned int ret;
pid_t pid = getpid();
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 9, g_pthreadTestCount, EXIT); // 9, here assert the result.
g_pthreadTestCount++; // 10
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
while (1) {
pthread_testcancel();
if (++i == 5) { // 5, in loop 5, set cancel state.
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
}
if (i == 10) { // 10, in loop 10, cancel pthread.
ret = pthread_cancel(pthread);
}
}
return (void *)i;
EXIT:
return NULL;
}
static void *ThreadFunc3(void *arg)
{
pthread_t pthread = pthread_self();
int i = 0;
unsigned int ret;
pid_t pid = getpid();
g_pthreadTestCount++; // 7
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
while (1) {
pthread_testcancel();
if (++i == 5) { // 5, in loop 5, set cancel state.
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
}
if (i == 10) { // 10, in loop 10, cancel pthread.
ret = pthread_cancel(pthread);
}
}
ICUNIT_GOTO_EQUAL(i, 10, i, EXIT); // 10, here assert the result.
return (void *)i;
EXIT:
return NULL;
}
static void *ThreadFunc2(void *arg)
{
pthread_t pthread = pthread_self();
pid_t pid = getpid();
unsigned int ret;
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 3, g_pthreadTestCount, EXIT); // 3, here assert the result.
g_pthreadTestCount++; // 4
ret = pthread_join(pthread_self(), 0);
ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
ret = pthread_detach(pthread_self());
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return NULL;
}
static void *ThreadFunc6(void *arg)
{
pid_t pid = getpid();
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 12, g_pthreadTestCount, EXIT); // 12, here assert the result.
g_pthreadTestCount++; // 13
return (void *)pthread_self();
EXIT:
return NULL;
}
static void *ThreadFunc(void *arg)
{
unsigned int ret;
void *res = NULL;
pthread_attr_t a = { 0 };
pthread_t newPthread;
struct sched_param param = { 0 };
pid_t pid = getpid();
int curThreadPri, curThreadPolicy;
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 2, g_pthreadTestCount, EXIT); // 2, here assert the result.
g_pthreadTestCount++; // 3
ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, -ret, EXIT);
curThreadPri = param.sched_priority;
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = curThreadPri - 1;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFunc2, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
#ifdef LOSCFG_USER_TEST_SMP
usleep(1000 * 10 * 2); // 1000 * 10 * 2, for timing control.
#endif
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 4, g_pthreadTestCount, EXIT); // 4, here assert the result.
g_pthreadTestCount++; // 5
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = curThreadPri + 1;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFunc3, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
g_pthreadTestCount++; // 6
ret = pthread_join(newPthread, &res);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 7, g_pthreadTestCount, EXIT); // 7, here assert the result.
g_pthreadTestCount++; // 8
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = curThreadPri + 2; // 2, adjust the priority.
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFunc4, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 8, g_pthreadTestCount, EXIT); // 8, here assert the result.
g_pthreadTestCount++; // 9
ret = pthread_join(newPthread, &res);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 10, g_pthreadTestCount, EXIT); // 10, here assert the result.
g_pthreadTestCount++; // 11
EXIT:
return NULL;
}
int PthreadTest001()
{
struct sched_param param = { 0 };
int ret;
void *res = NULL;
pthread_attr_t a = { 0 };
pthread_t newPthread, newPthread1;
int count = 0xf0000;
g_threadTestCount1 = 0;
g_pthreadTestCount = 0;
ret = pthread_getschedparam(pthread_self(), &g_currThreadPolicy, &param);
ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
g_currThreadPri = param.sched_priority;
g_pthreadTestCount++;
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = g_currThreadPri + 1;
pthread_attr_setschedparam(&a, &param);
pthread_attr_setschedpolicy(&a, g_currThreadPolicy);
ret = pthread_create(&newPthread, &a, ThreadFunc, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 1, g_pthreadTestCount);
g_pthreadTestCount++; // 2
ret = pthread_create(&newPthread1, &a, ThreadFunc5, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_threadTestCount1++;
ICUNIT_ASSERT_EQUAL(g_threadTestCount1, 1, g_threadTestCount1);
ret = pthread_detach(newPthread1);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_threadTestCount1++;
ICUNIT_ASSERT_EQUAL(g_threadTestCount1, 2, g_threadTestCount1); // 2, here assert the result.
ret = pthread_join(newPthread, &res);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_threadTestCount1, 3, g_threadTestCount1); // 3, here assert the result.
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 11, g_pthreadTestCount); // 11, here assert the result.
g_pthreadTestCount++; // 12
param.sched_priority = g_currThreadPri - 1;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread1, &a, ThreadFunc6, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
#ifdef LOSCFG_USER_TEST_SMP
usleep(1000 * 10 * 10); // 1000 * 10 * 10, for timing control.
#endif
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 13, g_pthreadTestCount); // 13, here assert the result.
g_pthreadTestCount++; // 14
ret = pthread_detach(newPthread1);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 14, g_pthreadTestCount); // 14, here assert the result.
return 0;
}
void ItTestPthread001(void)
{
TEST_ADD_CASE("IT_POSIX_PTHREAD_001", PthreadTest001, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,216 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
static int g_currThreadPri, g_currThreadPolicy;
static volatile int g_testCnt1 = 0;
static volatile int g_testCnt2 = 0;
static volatile int g_testPthredCount = 0;
static void ThreadWaitCount(int scount, volatile int *ent)
{
int count = 0xf0000;
while (*ent < scount) {
while (count > 0) {
count--;
}
count = 0xf0000;
(*ent)++;
}
}
static void *ThreadFuncTest2(void *arg)
{
(void)arg;
int ret;
struct sched_param param = { 0 };
int threadPolicy, threadPri;
int old;
const int waitCount1 = 20;
const int waitCount2 = 50;
g_testCnt2 = 0;
ThreadWaitCount(waitCount1 + g_testCnt2, &g_testCnt2);
while (g_testCnt1 < 10) { // 10, wait until g_testCnt1 >= 10.
printf("\r");
}
g_testPthredCount++;
param.sched_priority = g_currThreadPri + 1;
ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
old = g_testCnt1;
param.sched_priority = 0;
ret = pthread_getschedparam(pthread_self(), &threadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_FIFO, threadPolicy, EXIT);
ICUNIT_GOTO_EQUAL(param.sched_priority, (g_currThreadPri + 1), param.sched_priority, EXIT);
ThreadWaitCount(waitCount2 + g_testCnt2, &g_testCnt2);
ICUNIT_GOTO_EQUAL(g_testCnt1, old, g_testCnt1, EXIT);
param.sched_priority = g_currThreadPri;
ret = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
old = g_testCnt1;
param.sched_priority = 0;
ret = pthread_getschedparam(pthread_self(), &threadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_RR, threadPolicy, EXIT);
ICUNIT_GOTO_EQUAL(param.sched_priority, g_currThreadPri, param.sched_priority, EXIT);
ThreadWaitCount(waitCount2 + g_testCnt2, &g_testCnt2);
ICUNIT_GOTO_EQUAL(g_testCnt1, old, g_testCnt1, EXIT);
ret = pthread_setschedprio(pthread_self(), g_currThreadPri + 1);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ThreadWaitCount(waitCount2 + g_testCnt2, &g_testCnt2);
ICUNIT_GOTO_EQUAL(g_testCnt2, 170, g_testCnt2, EXIT); // 170, here assert the result.
g_testPthredCount++;
EXIT:
return NULL;
}
static void *ThreadFuncTest3(void *arg)
{
(void)arg;
struct sched_param param = { 0 };
int threadPolicy, threadPri;
int ret;
int old = 0;
const int waitCount1 = 20;
const int waitCount2 = 50;
g_testCnt1 = 0;
ThreadWaitCount(waitCount1 + g_testCnt1, &g_testCnt1);
while (g_testCnt2 < 10) { // 10, wait until g_testCnt2 >= 10.
printf("\r");
}
g_testPthredCount++;
param.sched_priority = g_currThreadPri + 1;
ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
old = g_testCnt2;
param.sched_priority = 0;
ret = pthread_getschedparam(pthread_self(), &threadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_FIFO, threadPolicy, EXIT);
ICUNIT_GOTO_EQUAL(param.sched_priority, (g_currThreadPri + 1), param.sched_priority, EXIT);
ThreadWaitCount(waitCount2 + g_testCnt1, &g_testCnt1);
ICUNIT_GOTO_EQUAL(g_testCnt2, old, g_testCnt2, EXIT);
param.sched_priority = g_currThreadPri;
ret = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
old = g_testCnt2;
param.sched_priority = 0;
ret = pthread_getschedparam(pthread_self(), &threadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_RR, threadPolicy, EXIT);
ICUNIT_GOTO_EQUAL(param.sched_priority, g_currThreadPri, param.sched_priority, EXIT);
ThreadWaitCount(waitCount2 + g_testCnt1, &g_testCnt1);
ICUNIT_GOTO_EQUAL(g_testCnt2, old, g_testCnt2, EXIT);
ICUNIT_GOTO_EQUAL(g_testCnt1, 120, g_testCnt1, EXIT); // 120, here assert the result.
g_testPthredCount++;
EXIT:
return NULL;
}
static int Testcase()
{
struct sched_param param = { 0 };
int ret;
void *res = NULL;
pthread_attr_t a = { 0 };
pthread_t newPthread, newPthread1;
g_testPthredCount = 0;
ret = pthread_getschedparam(pthread_self(), &g_currThreadPolicy, &param);
ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
g_currThreadPri = param.sched_priority;
g_testPthredCount++;
ret = pthread_attr_init(&a);
param.sched_priority = g_currThreadPri + 1;
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFuncTest2, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_testPthredCount, 1, g_testPthredCount);
g_testPthredCount++; // 2
ret = pthread_create(&newPthread1, &a, ThreadFuncTest3, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_testPthredCount, 2, g_testPthredCount); // 2, here assert the result.
g_testPthredCount++; // 3
ret = pthread_join(newPthread, &res);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(newPthread1, &res);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_testPthredCount, 7, g_testPthredCount); // 7, here assert the result.
return 0;
}
void ItTestPthread002(void)
{
TEST_ADD_CASE("IT_POSIX_PTHREAD_002", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,391 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
static int g_currThreadPri, g_currThreadPolicy;
static volatile int g_pthreadTestCount = 0;
static volatile int g_threadTestCount1 = 0;
static pthread_spinlock_t g_spinTestLock;
static void *ThreadFunc8(void *arg)
{
pthread_t pthread = pthread_self();
int ret;
ret = pthread_detach(pthread);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
g_threadTestCount1++;
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return NULL;
}
static void *ThreadFunc7(void *arg)
{
pthread_t pthread = pthread_self();
int ret;
pthread_attr_t a = { 0 };
struct sched_param param = { 0 };
pthread_t newPthread;
int curThreadPri, curThreadPolicy;
ret = pthread_detach(pthread);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, -ret, EXIT);
curThreadPri = param.sched_priority;
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = curThreadPri;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFunc8, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
g_threadTestCount1++;
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
sleep(2); // 2, delay
EXIT:
return NULL;
}
static void *ThreadFunc5(void *arg)
{
pthread_attr_t a = { 0 };
struct sched_param param = { 0 };
int ret;
void *res = NULL;
pthread_t newPthread;
int curThreadPri, curThreadPolicy;
ret = pthread_detach(pthread_self());
ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, -ret, EXIT);
curThreadPri = param.sched_priority;
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = curThreadPri + 1;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFunc7, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_join(newPthread, &res);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
g_threadTestCount1++;
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return NULL;
}
static void *ThreadFunc6(void *arg)
{
pthread_t pthread = pthread_self();
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 12, g_pthreadTestCount, EXIT); // 12, here assert the result.
g_pthreadTestCount++; // 13
return (void *)(uintptr_t)pthread;
EXIT:
return NULL;
}
static void *ThreadFunc4(void *arg)
{
pthread_t pthread = pthread_self();
int i = 0;
unsigned int ret;
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 9, g_pthreadTestCount, EXIT); // 9, here assert the result.
g_pthreadTestCount++; // 10
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
while (1) {
pthread_testcancel();
if (++i == 5) { // 5, in loop 5, set cancel state.
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
}
if (i == 10) { // 10, in loop 10, cancel pthread.
ret = pthread_cancel(pthread);
}
}
return (void *)i;
EXIT:
return NULL;
}
static void *ThreadFunc3(void *arg)
{
pthread_t pthread = pthread_self();
int i = 0;
unsigned int ret;
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 6, g_pthreadTestCount, EXIT); // 6, here assert the result.
g_pthreadTestCount++; // 7
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
while (1) {
pthread_testcancel();
if (++i == 5) { // 5, in loop 5, set cancel state.
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
}
if (i == 10) { // 10, in loop 10, cancel pthread.
ret = pthread_cancel(pthread);
}
}
ICUNIT_GOTO_EQUAL(i, 10, i, EXIT); // 10, here assert the result.
return (void *)i;
EXIT:
return NULL;
}
static void *threadFunc2(void *arg)
{
unsigned int ret;
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 3, g_pthreadTestCount, EXIT); // 3, here assert the result.
g_pthreadTestCount++;
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_join(pthread_self(), 0);
ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
ret = pthread_detach(pthread_self());
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return NULL;
}
static void *threadFunc(void *arg)
{
unsigned int ret;
void *res = NULL;
pthread_attr_t a = { 0 };
pthread_t newPthread;
struct sched_param param = { 0 };
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 2, g_pthreadTestCount, EXIT); // 2, here assert the result.
g_pthreadTestCount++;
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = g_currThreadPri;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, threadFunc2, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
#ifdef LOSCFG_USER_TEST_SMP
sleep(1);
#endif
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 4, g_pthreadTestCount, EXIT); // 4, here assert the result.
g_pthreadTestCount++; // 5
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = g_currThreadPri + 2; // 2, adjust priority.
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFunc3, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 5, g_pthreadTestCount, EXIT); // 5, here assert the result.
g_pthreadTestCount++; // 6
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_join(newPthread, &res);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 7, g_pthreadTestCount, EXIT); // 7, here assert the result.
g_pthreadTestCount++; // 8
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = g_currThreadPri + 3; // 3, adjust priority.
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, ThreadFunc4, 0);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 8, g_pthreadTestCount, EXIT); // 8, here assert the result.
g_pthreadTestCount++; // 9
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_join(newPthread, &res);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_spin_lock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 10, g_pthreadTestCount, EXIT); // 10, here assert the result.
g_pthreadTestCount++; // 11
ret = pthread_spin_unlock(&g_spinTestLock);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return NULL;
}
int PthreadTest004()
{
int exitCount = 0;
struct sched_param param = { 0 };
int ret;
void *res = NULL;
pthread_attr_t a = { 0 };
pthread_t newPthread, newPthread1;
const int testCount = 5;
int count = 5;
pthread_spin_init(&g_spinTestLock, 0);
g_threadTestCount1 = 0;
g_pthreadTestCount = 0;
ret = pthread_getschedparam(pthread_self(), &g_currThreadPolicy, &param);
ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
g_currThreadPri = param.sched_priority;
g_pthreadTestCount++;
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = g_currThreadPri + 1;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, threadFunc, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 1, g_pthreadTestCount);
g_pthreadTestCount++;
while (count > 0) {
ret = pthread_create(&newPthread1, &a, ThreadFunc5, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_detach(newPthread1);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
count--;
}
g_threadTestCount1++;
ICUNIT_ASSERT_EQUAL(g_threadTestCount1, 1, g_threadTestCount1);
ret = pthread_join(newPthread, &res);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
while (g_threadTestCount1 != (1 + (testCount * 3))) { // 3
sleep(2); // 2, delay
printf("Wait for the concurrent thread to end \n");
exitCount++;
if (exitCount > 10) { // wait time > 10 * 2s, cancel wait.
break;
}
}
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 11, g_pthreadTestCount); // 11, here assert the result.
g_pthreadTestCount++; // 12
param.sched_priority = g_currThreadPri - 1;
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread1, &a, ThreadFunc6, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(newPthread1, &res);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 13, g_pthreadTestCount); // 13, here assert the result.
g_pthreadTestCount++; // 14
ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 14, g_pthreadTestCount); // 14, here assert the result.
pthread_spin_destroy(&g_spinTestLock);
return 0;
}
void ItTestPthread004(void)
{
TEST_ADD_CASE("IT_POSIX_PTHREAD_004", PthreadTest004, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
extern int PthreadTest001(void);
extern int PthreadTest004(void);
static void *threadFunc(void *a)
{
int ret;
int status;
int pid;
pid = fork();
if (pid == 0) {
(void)PthreadTest004();
(void)PthreadTest001();
exit(12); // 12, set a special exit code.
}
ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, The pid will never exceed 100000.
sleep(10); // 10, delay for timimg control.
ret = PthreadTest001();
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
(void)waitpid(pid, &status, 0);
return NULL;
}
static int Testcase(void)
{
int ret;
int status;
int pid;
pthread_t tid;
ret = fork();
if (ret == 0) {
ret = pthread_create(&tid, NULL, threadFunc, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(tid, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
exit(10); // 10, set a special exit code.
} else if (ret > 0) {
pid = ret;
sleep(10); // 10, delay for timimg control.
(void)PthreadTest004();
ret = wait(&status);
status = WEXITSTATUS(status);
ICUNIT_ASSERT_EQUAL(ret, pid, ret);
ICUNIT_ASSERT_EQUAL(status, 10, status); // 10, assert the exit code.
}
ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, The pid will never exceed 100000.
return 0;
}
void ItTestPthread005(void)
{
TEST_ADD_CASE("IT_POSIX_PTHREAD_005", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
}

View File

@@ -0,0 +1,161 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
#include "threads.h"
static const int TEST_LOOP = 5000;
static int g_testCnt = 0;
static void *ThreadFunc(void *arg)
{
int count = 1;
while (g_testCnt < TEST_LOOP) {
if (g_testCnt != count) {
printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
return NULL;
}
g_testCnt++;
count += 5; // total 5 pthreads.
thrd_yield();
}
g_testCnt = TEST_LOOP + 1000; // 1000, here set a special num.
return NULL;
}
static void *ThreadFunc1(void *arg)
{
int count = 2;
while (g_testCnt < TEST_LOOP) {
if (g_testCnt != count) {
printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
return NULL;
}
g_testCnt++;
count += 5; // total 5 pthreads.
thrd_yield();
}
return NULL;
}
static void *ThreadFunc2(void *arg)
{
int count = 3;
while (g_testCnt < TEST_LOOP) {
if (g_testCnt != count) {
printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
return NULL;
}
g_testCnt++;
count += 5; // total 5 pthreads.
thrd_yield();
}
return NULL;
}
static void *ThreadFunc3(void *arg)
{
int count = 4;
while (g_testCnt < TEST_LOOP) {
if (g_testCnt != count) {
printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
return NULL;
}
g_testCnt++;
count += 5; // total 5 pthreads.
thrd_yield();
}
return NULL;
}
static int TestCase(void)
{
int ret;
pthread_t tid, tid1, tid2, tid3;
pthread_attr_t attr = { 0 };
int scope = 0;
int threadPolicy = 0;
struct sched_param param = { 0 };
g_testCnt = 0;
ret = pthread_getschedparam(pthread_self(), &threadPolicy, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_create(&tid, NULL, ThreadFunc, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&tid1, NULL, ThreadFunc1, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&tid2, NULL, ThreadFunc2, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&tid3, NULL, ThreadFunc3, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
while (g_testCnt < TEST_LOOP) {
g_testCnt++;
thrd_yield();
}
param.sched_priority -= 2; // 2, adjust the priority.
ret = pthread_setschedparam(tid, SCHED_FIFO, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ICUNIT_GOTO_EQUAL(g_testCnt, TEST_LOOP + 1000, g_testCnt, EXIT); // 1000, here assert the special num.
ret = pthread_join(tid, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(tid1, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(tid2, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(tid3, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
param.sched_priority += 2; // 2, adjust the priority.
ret = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return 0;
}
void ItTestPthread014(void)
{
TEST_ADD_CASE("IT_POSIX_PTHREAD_014", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
}