diff --git a/ut_assert/inc/utbsp.h b/ut_assert/inc/utbsp.h index 35108a16b..bec41899a 100644 --- a/ut_assert/inc/utbsp.h +++ b/ut_assert/inc/utbsp.h @@ -97,4 +97,18 @@ void UT_BSP_DoText(uint8 MessageType, const char *OutputMessage); */ void UT_BSP_EndTest(const UtAssert_TestCounter_t *TestCounters); +/** + * UT mutex lock for multi-threaded test support + * + * Lock that should be acquired before modifying any global test state variables + */ +void UT_BSP_Lock(void); + +/** + * UT mutex unlock for multi-threaded test support + * + * Must be called after UT_BSP_Lock to allow other threads access to the global + */ +void UT_BSP_Unlock(void); + #endif /* UTBSP_H */ diff --git a/ut_assert/src/utassert.c b/ut_assert/src/utassert.c index a4a88ddda..09be48c4a 100644 --- a/ut_assert/src/utassert.c +++ b/ut_assert/src/utassert.c @@ -108,10 +108,18 @@ const UtAssert_TestCounter_t *UtAssert_GetCounters(void) void UtAssert_BeginTest(const char *SegmentName) { + uint32 TestSegmentCount; + + UT_BSP_Lock(); + memset(&UT_SegmentCounters, 0, sizeof(UT_SegmentCounters)); strncpy(CurrentSegment, SegmentName, sizeof(CurrentSegment) - 1); CurrentSegment[sizeof(CurrentSegment) - 1] = 0; - UT_BSP_StartTestSegment(1 + UT_TotalCounters.TestSegmentCount, SegmentName); + TestSegmentCount = 1 + UT_TotalCounters.TestSegmentCount; + + UT_BSP_Unlock(); + + UT_BSP_StartTestSegment(TestSegmentCount, SegmentName); } const char *UtAssert_GetSegmentName(void) @@ -121,9 +129,15 @@ const char *UtAssert_GetSegmentName(void) void UtAssert_EndTest(void) { - uint32 Ct; + uint32 Ct; + bool SegmentValid; + UtAssert_TestCounter_t Local_SegmentCounters; + char Local_SegmentName[sizeof(CurrentSegment)]; + + UT_BSP_Lock(); - if (UT_SegmentCounters.TotalTestCases > 0) + SegmentValid = (UT_SegmentCounters.TotalTestCases > 0); + if (SegmentValid) { ++UT_TotalCounters.TestSegmentCount; UT_SegmentCounters.TestSegmentCount = UT_TotalCounters.TestSegmentCount; @@ -132,14 +146,27 @@ void UtAssert_EndTest(void) { UT_TotalCounters.CaseCount[Ct] += UT_SegmentCounters.CaseCount[Ct]; } - UtAssert_DoTestSegmentReport(CurrentSegment, &UT_SegmentCounters); + memcpy(&Local_SegmentCounters, &UT_SegmentCounters, sizeof(Local_SegmentCounters)); + + /* + * note, strcpy is OK because both are fixed size buffers of the same size, + * and the null termination on CurrentSegment was locally enforced already + */ + strcpy(Local_SegmentName, CurrentSegment); + } + + memset(&UT_SegmentCounters, 0, sizeof(UT_SegmentCounters)); + + UT_BSP_Unlock(); + + if (SegmentValid) + { + UtAssert_DoTestSegmentReport(Local_SegmentName, &Local_SegmentCounters); } else { UT_BSP_DoText(UTASSERT_CASETYPE_END, "No test cases\n"); } - - memset(&UT_SegmentCounters, 0, sizeof(UT_SegmentCounters)); } void UtAssert_SetContext(UtAssert_CaseType_t Context) @@ -162,6 +189,10 @@ bool UtAssertEx(bool Expression, UtAssert_CaseType_t CaseType, const char *File, { va_list va; char FinalMessage[256]; + uint32 TestSegmentCount; + uint32 TotalTestCases; + + UT_BSP_Lock(); ++UT_SegmentCounters.TotalTestCases; @@ -175,12 +206,16 @@ bool UtAssertEx(bool Expression, UtAssert_CaseType_t CaseType, const char *File, ++UT_SegmentCounters.CaseCount[(uint32)CaseType]; } + TestSegmentCount = 1 + UT_TotalCounters.TestSegmentCount; + TotalTestCases = UT_SegmentCounters.TotalTestCases; + + UT_BSP_Unlock(); + va_start(va, MessageFormat); vsnprintf(FinalMessage, sizeof(FinalMessage), MessageFormat, va); va_end(va); - UtAssert_DoReport(File, Line, 1 + UT_TotalCounters.TestSegmentCount, UT_SegmentCounters.TotalTestCases, CaseType, - CurrentSegment, FinalMessage); + UtAssert_DoReport(File, Line, TestSegmentCount, TotalTestCases, CaseType, CurrentSegment, FinalMessage); return Expression; } diff --git a/ut_assert/src/utbsp.c b/ut_assert/src/utbsp.c index fde612fe4..e64a1e8db 100644 --- a/ut_assert/src/utbsp.c +++ b/ut_assert/src/utbsp.c @@ -50,6 +50,16 @@ typedef struct BSP_UT_GlobalData_t BSP_UT_Global; +void UT_BSP_Lock(void) +{ + OS_BSP_Lock_Impl(); +} + +void UT_BSP_Unlock(void) +{ + OS_BSP_Unlock_Impl(); +} + void UT_BSP_Setup(void) { uint8 UserShift; @@ -114,7 +124,7 @@ void UT_BSP_DoText(uint8 MessageType, const char *OutputMessage) if (MsgEnabled & 1) { - OS_BSP_Lock_Impl(); + UT_BSP_Lock(); switch (MessageType) { @@ -190,7 +200,7 @@ void UT_BSP_DoText(uint8 MessageType, const char *OutputMessage) OS_BSP_ConsoleOutput_Impl("\n", 1); } - OS_BSP_Unlock_Impl(); + UT_BSP_Unlock(); } /* @@ -219,9 +229,9 @@ void UT_BSP_EndTest(const UtAssert_TestCounter_t *TestCounters) snprintf(Message, sizeof(Message), "COMPLETE: %u tests Segment(s) executed\n\n", (unsigned int)TestCounters->TestSegmentCount); - OS_BSP_Lock_Impl(); + UT_BSP_Lock(); OS_BSP_ConsoleOutput_Impl(Message, strlen(Message)); - OS_BSP_Unlock_Impl(); + UT_BSP_Unlock(); if ((TestCounters->CaseCount[UTASSERT_CASETYPE_FAILURE] > 0) || (TestCounters->CaseCount[UTASSERT_CASETYPE_TSF] > 0) || (TestCounters->CaseCount[UTASSERT_CASETYPE_TTF] > 0)) diff --git a/ut_assert/src/uttest.c b/ut_assert/src/uttest.c index 5aa2ab21f..a2e8554d3 100644 --- a/ut_assert/src/uttest.c +++ b/ut_assert/src/uttest.c @@ -62,7 +62,9 @@ void UtTest_AddCommon(void (*Test)(void), void (*Setup)(void), void (*Teardown)( strncpy(UtTestDataBaseEntry.TestName, TestName, sizeof(UtTestDataBaseEntry.TestName) - 1); } + UT_BSP_Lock(); UtList_Add(UtAssert_Global.DataBasePtr, &UtTestDataBaseEntry, sizeof(UtTestDataBaseEntry_t), EntryType); + UT_BSP_Unlock(); } void UtTest_Add(void (*Test)(void), void (*Setup)(void), void (*Teardown)(void), const char *SequenceName) @@ -86,6 +88,8 @@ void UtTest_Run(void) UtListNode_t * UtListNode; UtTestDataBaseEntry_t *UtTestDataBaseEntry; + UT_BSP_Lock(); + /* * The overall test sequence goes SETUP->TEST->TEARDOWN * @@ -99,6 +103,8 @@ void UtTest_Run(void) UtList_Merge(UtListMain, UtList_GetHead(UtAssert_Global.DataBasePtr, UTASSERT_GROUP_TEST)); UtList_Merge(UtListMain, UtList_GetHead(UtAssert_Global.DataBasePtr, UTASSERT_GROUP_TEARDOWN)); + UT_BSP_Unlock(); + /* * Run through the merged list in order */ @@ -132,7 +138,9 @@ void UtTest_Run(void) } } + UT_BSP_Lock(); UtList_Destroy(UtAssert_Global.DataBasePtr); + UT_BSP_Unlock(); UT_BSP_EndTest(UtAssert_GetCounters()); }