From e7c4b1963a1f11cb976d1ebdf6e48d8eb2eb84d2 Mon Sep 17 00:00:00 2001 From: vcbchang Date: Tue, 6 Jul 2021 21:55:38 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E4=BF=AE=E5=A4=8Dmqueue=5Funittest?= =?UTF-8?q?=E5=85=A8=E9=87=8F=E7=94=A8=E4=BE=8BItPosixQueue075=E4=B8=8EItP?= =?UTF-8?q?osixQueue097,=E5=86=92=E7=83=9F=E7=94=A8=E4=BE=8B=20ItPosixQueu?= =?UTF-8?q?e053=E7=94=A8=E4=BE=8B=E5=AD=98=E5=9C=A8=E5=81=B6=E5=B0=94?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E9=80=9A=E8=BF=87=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【背景】mqueue_unittest用例ItPosixQueue075、ItPosixQueue053 与ItPosixQueue097用例存在偶尔无法通过的问题,现在将其修复 【修改方案】ItPosixQueue075中原来是通过延时来实现线程同步,现在更改为静态全局变量实现; ItPosixQueue097中的关于g_testCount利用延时来置标志位,实现线程同步。这里通过延时来期望线程调度,写的不合理,这里更改为等待标志位来实现线程同步。 re #I3Z9H9 Signed-off-by: vcbchang Change-Id: Ib1bb945a5393cb03f4d92e4332e20acd82eb1845 --- testsuites/unittest/common/include/osTest.h | 1 + testsuites/unittest/common/osTest.cpp | 7 +++++++ .../unittest/posix/mqueue/full/It_posix_queue_075.cpp | 9 +++++++-- .../unittest/posix/mqueue/full/It_posix_queue_097.cpp | 5 +---- .../unittest/posix/mqueue/smoke/It_posix_queue_053.cpp | 10 +++------- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/testsuites/unittest/common/include/osTest.h b/testsuites/unittest/common/include/osTest.h index 0623b40a..3ee6fb5d 100644 --- a/testsuites/unittest/common/include/osTest.h +++ b/testsuites/unittest/common/include/osTest.h @@ -132,6 +132,7 @@ extern void TestBusyTaskDelay(UINT32 tick); extern void *malloc(size_t size); extern void TEST_DumpCpuid(void); extern u_long T_random(void); +extern VOID TestAssertWaitDelay(UINT32 *testCount, UINT32 flag); UINT32 LosTaskDelay(UINT32 tick); #define TEST_HwiDelete(ID) TEST_TEST_HwiDelete(ID, NULL) diff --git a/testsuites/unittest/common/osTest.cpp b/testsuites/unittest/common/osTest.cpp index a771a147..0c02e5e6 100644 --- a/testsuites/unittest/common/osTest.cpp +++ b/testsuites/unittest/common/osTest.cpp @@ -215,6 +215,13 @@ VOID TestAssertBusyTaskDelay(UINT32 timeout, UINT32 flag) } } +VOID TestAssertWaitDelay(UINT32 *testCount, UINT32 flag) +{ + while (*testCount != flag) { + usleep(1); + } +} + UINT32 PosixPthreadInit(pthread_attr_t *attr, int pri) { UINT32 uwRet = 0; diff --git a/testsuites/unittest/posix/mqueue/full/It_posix_queue_075.cpp b/testsuites/unittest/posix/mqueue/full/It_posix_queue_075.cpp index 137b60a2..2fea2090 100644 --- a/testsuites/unittest/posix/mqueue/full/It_posix_queue_075.cpp +++ b/testsuites/unittest/posix/mqueue/full/It_posix_queue_075.cpp @@ -29,6 +29,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "It_posix_queue.h" +static int g_testFlag = 0; static VOID *PthreadF01(VOID *arg) { @@ -37,12 +38,12 @@ static VOID *PthreadF01(VOID *arg) CHAR msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0}; struct timespec ts = { 0 }; ts.tv_sec = 0xffff; - for (i = 0; i < MQUEUE_STANDARD_NAME_LENGTH * 2; i++) { // 2, The loop frequency name length. ret = mq_timedreceive(g_gqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL, &ts); ICUNIT_GOTO_EQUAL(ret, MQUEUE_SHORT_ARRAY_LENGTH, ret, EXIT); ICUNIT_GOTO_STRING_EQUAL(msgrcd, MQUEUE_SEND_STRING_TEST, msgrcd, EXIT); } + g_testFlag = 0; // 0 means the sub thread has executed return NULL; EXIT: return NULL; @@ -62,6 +63,7 @@ static UINT32 Testcase(VOID) attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH; attr.mq_maxmsg = MQUEUE_STANDARD_NAME_LENGTH; + g_testFlag = 1; // 1 initialize the flag LOS_TaskLock(); snprintf(mqname, MQUEUE_STANDARD_NAME_LENGTH, "/mq075_%d", LosCurTaskIDGet()); @@ -85,7 +87,10 @@ static UINT32 Testcase(VOID) ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1); } - TestExtraTaskDelay(10); // 10, Set delay time. + // waitting for the flag is 0, means the sub thread has executed + while (g_testFlag) { + usleep(1); + } ret = mq_close(g_gqueue); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1); diff --git a/testsuites/unittest/posix/mqueue/full/It_posix_queue_097.cpp b/testsuites/unittest/posix/mqueue/full/It_posix_queue_097.cpp index e11c5034..00fad7b1 100644 --- a/testsuites/unittest/posix/mqueue/full/It_posix_queue_097.cpp +++ b/testsuites/unittest/posix/mqueue/full/It_posix_queue_097.cpp @@ -107,9 +107,6 @@ static UINT32 Testcase(VOID) ret = pthread_create(&newTh, &attr1, PthreadF01, NULL); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1); - LosTaskDelay(1); - ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT1); - ret = PosixPthreadInit(&attr1, MQUEUE_PTHREAD_PRIORITY_TEST2); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2); @@ -119,7 +116,7 @@ static UINT32 Testcase(VOID) ret = LosTaskDelay(10); // 10, Set delay time. ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2); - ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT2); // 4, Here, assert the g_testCount. + TestAssertWaitDelay(&g_testCount, 4); // 4, Here, assert the g_testCount. ret = PosixPthreadDestroy(&attr1, newTh2); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2); diff --git a/testsuites/unittest/posix/mqueue/smoke/It_posix_queue_053.cpp b/testsuites/unittest/posix/mqueue/smoke/It_posix_queue_053.cpp index 2308073d..01dd6aa2 100644 --- a/testsuites/unittest/posix/mqueue/smoke/It_posix_queue_053.cpp +++ b/testsuites/unittest/posix/mqueue/smoke/It_posix_queue_053.cpp @@ -37,10 +37,7 @@ static VOID *PthreadF01(VOID *argument) g_testCount = 1; - ret = LosTaskDelay(5); // 5, Set the timeout of task delay; - ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT); - - ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 3, Here, assert the g_testCount. + TestAssertWaitDelay(&g_testCount, 3); // 3, Here, assert the g_testCount. ret = mq_send(g_gqueue, msgptr, strlen(msgptr), 0); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT); @@ -105,8 +102,7 @@ static UINT32 Testcase(VOID) ret = pthread_create(&newTh1, &attr1, PthreadF01, NULL); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1); - LosTaskDelay(1); - ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT1); + TestAssertWaitDelay(&g_testCount, 1); ret = PosixPthreadInit(&attr1, MQUEUE_PTHREAD_PRIORITY_TEST2); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2); @@ -121,7 +117,7 @@ static UINT32 Testcase(VOID) #endif ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2); - ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT2); // 4, Here, assert the g_testCount. + TestAssertWaitDelay(&g_testCount, 4); // 4, Here, assert the g_testCount. ret = PosixPthreadDestroy(&attr1, newTh2); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);