Skip to content

Commit

Permalink
Update unit test for TASK and ISR lock macros (#1319)
Browse files Browse the repository at this point in the history
This is needed for PR - FreeRTOS/FreeRTOS-Kernel#1212.
  • Loading branch information
chinglee-iot authored Dec 30, 2024
1 parent 365a479 commit 2dcc47e
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 83 deletions.
8 changes: 4 additions & 4 deletions FreeRTOS/Test/CMock/config/fake_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );

void portSetupTCB_CB( void * tcb );

void vFakePortGetISRLock( void );
void vFakePortReleaseISRLock( void );
void vFakePortGetTaskLock( void );
void vFakePortReleaseTaskLock( void );
void vFakePortGetISRLock( BaseType_t xCoreID );
void vFakePortReleaseISRLock( BaseType_t xCoreID );
void vFakePortGetTaskLock( BaseType_t xCoreID );
void vFakePortReleaseTaskLock( BaseType_t xCoreID );

void vFakePortAssertIfISR();
BaseType_t vFakePortCheckIfInISR( void );
Expand Down
40 changes: 20 additions & 20 deletions FreeRTOS/Test/CMock/config/portmacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,37 +125,37 @@ typedef unsigned long UBaseType_t;
/*-----------------------------------------------------------*/

#define portSAVE_CONTEXT()
#define portYIELD() vFakePortYield()
#define portYIELD_WITHIN_API() vFakePortYieldWithinAPI()
#define portYIELD_FROM_ISR() vFakePortYieldFromISR()
#define portYIELD() vFakePortYield()
#define portYIELD_WITHIN_API() vFakePortYieldWithinAPI()
#define portYIELD_FROM_ISR() vFakePortYieldFromISR()

/* Critical section handling. */
#define portDISABLE_INTERRUPTS() vFakePortDisableInterrupts()
#define portENABLE_INTERRUPTS() vFakePortEnableInterrupts()
#define portDISABLE_INTERRUPTS() vFakePortDisableInterrupts()
#define portENABLE_INTERRUPTS() vFakePortEnableInterrupts()
#define portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) \
vFakePortClearInterruptMaskFromISR( x )
#define portSET_INTERRUPT_MASK_FROM_ISR() \
ulFakePortSetInterruptMaskFromISR()
#define portSET_INTERRUPT_MASK() ulFakePortSetInterruptMask()
#define portCLEAR_INTERRUPT_MASK( x ) vFakePortClearInterruptMask( x )
#define portSET_INTERRUPT_MASK() ulFakePortSetInterruptMask()
#define portCLEAR_INTERRUPT_MASK( x ) vFakePortClearInterruptMask( x )
#define portASSERT_IF_INTERRUPT_PRIORITY_INVALID() \
vFakePortAssertIfInterruptPriorityInvalid()
#define portENTER_CRITICAL() vFakePortEnterCriticalSection()
#define portEXIT_CRITICAL() vFakePortExitCriticalSection()
#define portGET_ISR_LOCK() vFakePortGetISRLock()
#define portRELEASE_ISR_LOCK() vFakePortReleaseISRLock()
#define portGET_TASK_LOCK() vFakePortGetTaskLock()
#define portRELEASE_TASK_LOCK() vFakePortReleaseTaskLock()

#define portCHECK_IF_IN_ISR() vFakePortCheckIfInISR()
#define portRESTORE_INTERRUPTS( x ) vFakePortRestoreInterrupts( x )
#define portENTER_CRITICAL() vFakePortEnterCriticalSection()
#define portEXIT_CRITICAL() vFakePortExitCriticalSection()
#define portGET_ISR_LOCK( xCoreID ) vFakePortGetISRLock( xCoreID )
#define portRELEASE_ISR_LOCK( xCoreID ) vFakePortReleaseISRLock( xCoreID )
#define portGET_TASK_LOCK( xCoreID ) vFakePortGetTaskLock( xCoreID )
#define portRELEASE_TASK_LOCK( xCoreID ) vFakePortReleaseTaskLock( xCoreID )

#define portCHECK_IF_IN_ISR() vFakePortCheckIfInISR()
#define portRESTORE_INTERRUPTS( x ) vFakePortRestoreInterrupts( x )
#define portPRE_TASK_DELETE_HOOK( pvTaskToDelete, pxPendYield ) \
vPortCurrentTaskDying( ( pvTaskToDelete ), ( pxPendYield ) )
#define portSETUP_TCB( pxTCB ) portSetupTCB_CB( pxTCB );
#define portASSERT_IF_IN_ISR() vFakePortAssertIfISR();
#define portSETUP_TCB( pxTCB ) portSetupTCB_CB( pxTCB );
#define portASSERT_IF_IN_ISR() vFakePortAssertIfISR();

#define portGET_CORE_ID() vFakePortGetCoreID()
#define portYIELD_CORE( x ) vFakePortYieldCore( x )
#define portGET_CORE_ID() vFakePortGetCoreID()
#define portYIELD_CORE( x ) vFakePortYieldCore( x )

#define portENTER_CRITICAL_FROM_ISR vFakePortEnterCriticalFromISR
#define portEXIT_CRITICAL_FROM_ISR vFakePortExitCriticalFromISR
Expand Down
19 changes: 10 additions & 9 deletions FreeRTOS/Test/CMock/smp/config_assert/config_assert_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ void test_prvSelectHighestPriorityTask_assert_scheduler_running_false( void )
xSchedulerRunning = pdFALSE; /* causes the assert */
uxSchedulerSuspended = pdFALSE;

vFakePortGetTaskLock_Expect();
vFakePortGetISRLock_Expect();
vFakePortGetTaskLock_Expect( 1 );
vFakePortGetISRLock_Expect( 1 );

EXPECT_ASSERT_BREAK( vTaskSwitchContext( 1 ) );
validate_and_clear_assertions();
Expand Down Expand Up @@ -344,8 +344,8 @@ void test_prvSelectHighestPriorityTask_assert_coreid_ne_runstate( void )
xSchedulerRunning = pdTRUE;
uxSchedulerSuspended = pdFALSE;

vFakePortGetTaskLock_Expect();
vFakePortGetISRLock_Expect();
vFakePortGetTaskLock_Expect( 0 );
vFakePortGetISRLock_Expect( 0 );

listIS_CONTAINED_WITHIN_ExpectAnyArgsAndReturn( pdFALSE );
listLIST_IS_EMPTY_ExpectAnyArgsAndReturn( pdFALSE );
Expand Down Expand Up @@ -467,8 +467,8 @@ void test_vTaskSwitchContext_assert_nexting_count_ne_zero( void )

pxCurrentTCBs[ 1 ] = &currentTCB;

vFakePortGetTaskLock_Expect();
vFakePortGetISRLock_Expect();
vFakePortGetTaskLock_Expect( 1 );
vFakePortGetISRLock_Expect( 1 );

EXPECT_ASSERT_BREAK( vTaskSwitchContext( 1 ) );

Expand Down Expand Up @@ -576,9 +576,10 @@ void test_prvGetExpectedIdleTime_assert_nextUnblock_lt_xTickCount( void )
vFakePortAssertIfISR_Expect();
ulFakePortSetInterruptMask_ExpectAndReturn( 0 );
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortGetTaskLock_Expect();
vFakePortGetISRLock_Expect();
vFakePortReleaseISRLock_Expect();
vFakePortGetTaskLock_Expect( 0 );
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortGetISRLock_Expect( 0 );
vFakePortReleaseISRLock_Expect( 0 );
vFakePortClearInterruptMask_Expect( 0 );

/* API Call */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3328,7 +3328,7 @@ void test_coverage_xTaskResumeAll_task_in_pending_ready_list( void )
vFakePortExitCriticalSection_StubWithCallback( NULL );

/* Expectations. */
vFakePortReleaseTaskLock_Expect();
vFakePortReleaseTaskLock_Expect( 0 );
vFakePortExitCriticalSection_Expect();

/* API call. */
Expand Down Expand Up @@ -3398,7 +3398,7 @@ void test_coverage_xTaskResumeAll_task_in_pending_ready_list_uxpriority_lesser(
vFakePortExitCriticalSection_StubWithCallback( NULL );

/* Expectations. */
vFakePortReleaseTaskLock_Expect();
vFakePortReleaseTaskLock_Expect( 0 );
vFakePortExitCriticalSection_Expect();

/* API call. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,13 +802,14 @@ void test_coverage_prvGetExpectedIdleTime_ready_list_eq_1( void )
vFakePortAssertIfISR_Expect();
ulFakePortSetInterruptMask_ExpectAndReturn( 0 );
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortGetTaskLock_Expect();
vFakePortGetTaskLock_Expect( 0 );
/* prvCheckForRunStateChange */
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortAssertIfISR_Expect();
/* End of prvCheckForRunStateChange */
vFakePortGetISRLock_Expect();
vFakePortReleaseISRLock_Expect();
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortGetISRLock_Expect( 0 );
vFakePortReleaseISRLock_Expect( 0 );
vFakePortClearInterruptMask_Expect( 0 );
/* End of vTaskSuspendAll */

Expand All @@ -826,7 +827,7 @@ void test_coverage_prvGetExpectedIdleTime_ready_list_eq_1( void )

vFakePortEnterCriticalSection_Expect();
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortReleaseTaskLock_Expect();
vFakePortReleaseTaskLock_Expect( 0 );
vFakePortExitCriticalSection_Expect();

listCURRENT_LIST_LENGTH_ExpectAndThrow( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ),
Expand Down Expand Up @@ -908,12 +909,13 @@ void test_coverage_prvGetExpectedIdleTime_ready_list_eq_2( void )
vFakePortAssertIfISR_Stub( port_assert_if_isr_cb );
ulFakePortSetInterruptMask_ExpectAndReturn( 0 );
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortGetTaskLock_Expect();
vFakePortGetTaskLock_Expect( 0 );
/* prvCheckForRunStateChange */
vFakePortGetCoreID_ExpectAndReturn( 0 );
/* End of prvCheckForRunStateChange */
vFakePortGetISRLock_Expect();
vFakePortReleaseISRLock_Expect();
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortGetISRLock_Expect( 0 );
vFakePortReleaseISRLock_Expect( 0 );
vFakePortClearInterruptMask_Expect( 0 );
/* End of vTaskSuspendAll */

Expand All @@ -926,7 +928,7 @@ void test_coverage_prvGetExpectedIdleTime_ready_list_eq_2( void )

vFakePortEnterCriticalSection_Expect();
vFakePortGetCoreID_ExpectAndReturn( 0 );
vFakePortReleaseTaskLock_Expect();
vFakePortReleaseTaskLock_Expect( 0 );
vFakePortExitCriticalSection_Expect();

listCURRENT_LIST_LENGTH_ExpectAndThrow( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ void test_coverage_prvCheckForRunStateChange_first_time_critical_section( void )
/* Expection. */
vFakePortEnableInterrupts_StubWithCallback( prvPortEnableInterruptsCb );

vFakePortReleaseISRLock_Expect();
vFakePortReleaseTaskLock_Expect();
vFakePortGetTaskLock_Expect();
vFakePortGetISRLock_Expect();
vFakePortReleaseISRLock_Expect( 0 );
vFakePortReleaseTaskLock_Expect( 0 );
vFakePortGetTaskLock_Expect( 0 );
vFakePortGetISRLock_Expect( 0 );

/* API Call. */
prvCheckForRunStateChange();
Expand Down Expand Up @@ -192,10 +192,10 @@ void test_coverage_prvCheckForRunStateChange_first_time_suspend_scheduler( void
/* Expection. */
vFakePortEnableInterrupts_StubWithCallback( prvPortEnableInterruptsCb );

vFakePortReleaseTaskLock_Expect();
vFakePortGetTaskLock_Expect();
vFakePortGetISRLock_Expect();
vFakePortReleaseISRLock_Expect();
vFakePortReleaseTaskLock_Expect( 0 );
vFakePortGetTaskLock_Expect( 0 );
vFakePortGetISRLock_Expect( 0 );
vFakePortReleaseISRLock_Expect( 0 );

/* API Call. */
prvCheckForRunStateChange();
Expand Down Expand Up @@ -234,6 +234,7 @@ void test_task_get_system_state( void )

/*Get System states */
int no_of_tasks = uxTaskGetSystemState( tsk_status_array, MAX_TASKS, NULL );

TEST_ASSERT( ( no_of_tasks > 0 ) && ( no_of_tasks <= MAX_TASKS ) );
}

Expand All @@ -257,6 +258,7 @@ void test_task_get_system_state_custom_time( void )

/*Get System states */
int no_of_tasks = uxTaskGetSystemState( tsk_status_array, MAX_TASKS, &ulTotalRunTime );

TEST_ASSERT( ( no_of_tasks > 0 ) && ( no_of_tasks <= MAX_TASKS ) );
}

Expand All @@ -279,6 +281,7 @@ void test_task_get_system_state_unavilable_task_space( void )

/*Get System states */
int no_of_tasks = uxTaskGetSystemState( tsk_status_array, MAX_TASKS - 1, NULL );

TEST_ASSERT( ( no_of_tasks == 0 ) && ( no_of_tasks <= MAX_TASKS ) );
}

Expand Down
37 changes: 21 additions & 16 deletions FreeRTOS/Test/CMock/smp/smp_utest_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ unsigned int vFakePortGetCoreIDCallback( int cmock_num_calls )
return ( unsigned int ) xCurrentCoreId;
}

void vFakePortGetISRLockCallback( int cmock_num_calls )
void vFakePortGetISRLockCallback( BaseType_t xCoreID,
int cmock_num_calls )
{
int i;

Expand All @@ -250,25 +251,27 @@ void vFakePortGetISRLockCallback( int cmock_num_calls )
/* Ensure that no other core is in the critical section. */
for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
if( i != xCurrentCoreId )
if( i != xCoreID )
{
TEST_ASSERT_MESSAGE( xIsrLockCount[ i ] == 0, "vFakePortGetISRLock xIsrLockCount[ i ] > 0" );
TEST_ASSERT_MESSAGE( xTaskLockCount[ i ] == 0, "vFakePortGetISRLock xTaskLockCount[ i ] > 0" );
}
}

xIsrLockCount[ xCurrentCoreId ]++;
xIsrLockCount[ xCoreID ]++;
}

void vFakePortReleaseISRLockCallback( int cmock_num_calls )
void vFakePortReleaseISRLockCallback( BaseType_t xCoreID,
int cmock_num_calls )
{
( void ) cmock_num_calls;

TEST_ASSERT_MESSAGE( xIsrLockCount[ xCurrentCoreId ] > 0, "xIsrLockCount[ xCurrentCoreId ] <= 0" );
xIsrLockCount[ xCurrentCoreId ]--;
TEST_ASSERT_MESSAGE( xIsrLockCount[ xCoreID ] > 0, "xIsrLockCount[ xCoreID ] <= 0" );
xIsrLockCount[ xCoreID ]--;
}

void vFakePortGetTaskLockCallback( int cmock_num_calls )
void vFakePortGetTaskLockCallback( BaseType_t xCoreID,
int cmock_num_calls )
{
int i;

Expand All @@ -277,36 +280,38 @@ void vFakePortGetTaskLockCallback( int cmock_num_calls )
/* Ensure that no other core is in the critical section. */
for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
if( i != xCurrentCoreId )
if( i != xCoreID )
{
TEST_ASSERT_MESSAGE( xIsrLockCount[ i ] == 0, "vFakePortGetTaskLock xIsrLockCount[ i ] > 0" );
TEST_ASSERT_MESSAGE( xTaskLockCount[ i ] == 0, "vFakePortGetTaskLock xTaskLockCount[ i ] > 0" );
}
}

xTaskLockCount[ xCurrentCoreId ]++;
xTaskLockCount[ xCoreID ]++;
}

void vFakePortReleaseTaskLockCallback( int cmock_num_calls )
void vFakePortReleaseTaskLockCallback( BaseType_t xCoreID,
int cmock_num_calls )
{
( void ) cmock_num_calls;

TEST_ASSERT_MESSAGE( xTaskLockCount[ xCurrentCoreId ] > 0, "xTaskLockCount[ xCurrentCoreId ] <= 0" );
xTaskLockCount[ xCurrentCoreId ]--;
TEST_ASSERT_MESSAGE( xTaskLockCount[ xCoreID ] > 0, "xTaskLockCount[ xCoreID ] <= 0" );
xTaskLockCount[ xCoreID ]--;

/* When releasing the ISR lock, check if any core is waiting to yield. */
if( xTaskLockCount[ xCurrentCoreId ] == 0 )
if( xTaskLockCount[ xCoreID ] == 0 )
{
vYieldCores();
}
}

void vFakePortReleaseTaskLockAsyncCallback( int cmock_num_calls )
void vFakePortReleaseTaskLockAsyncCallback( BaseType_t xCoreID,
int cmock_num_calls )
{
( void ) cmock_num_calls;

TEST_ASSERT_MESSAGE( xTaskLockCount[ xCurrentCoreId ] > 0, "xTaskLockCount[ xCurrentCoreId ] <= 0" );
xTaskLockCount[ xCurrentCoreId ]--;
TEST_ASSERT_MESSAGE( xTaskLockCount[ xCoreID ] > 0, "xTaskLockCount[ xCoreID ] <= 0" );
xTaskLockCount[ xCoreID ]--;
}

portBASE_TYPE vFakePortEnterCriticalFromISRCallback( int cmock_num_calls )
Expand Down
Loading

0 comments on commit 2dcc47e

Please sign in to comment.