Skip to content

Commit

Permalink
Merge pull request #1065 from jphickey/fix-1061-utassert-mutex
Browse files Browse the repository at this point in the history
Fix #1061, add mutex lock around UtAssert globals
  • Loading branch information
astrogeco authored Jun 10, 2021
2 parents 0759719 + 771ce48 commit 2b64d91
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
14 changes: 14 additions & 0 deletions ut_assert/inc/utbsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
51 changes: 43 additions & 8 deletions ut_assert/src/utassert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
18 changes: 14 additions & 4 deletions ut_assert/src/utbsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}

/*
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions ut_assert/src/uttest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
*
Expand All @@ -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
*/
Expand Down Expand Up @@ -132,7 +138,9 @@ void UtTest_Run(void)
}
}

UT_BSP_Lock();
UtList_Destroy(UtAssert_Global.DataBasePtr);
UT_BSP_Unlock();

UT_BSP_EndTest(UtAssert_GetCounters());
}
Expand Down

0 comments on commit 2b64d91

Please sign in to comment.