freertos: Refactor eTaskGetState() unit test

This commit refactors the eTaskGetState() unit test to work on FreeRTOS SMP
This commit is contained in:
Darian Leung
2022-05-24 18:46:47 +08:00
parent f3d06ad78d
commit ef6afa1324
2 changed files with 92 additions and 81 deletions

View File

@@ -0,0 +1,92 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "unity.h"
#include "test_utils.h"
/*
Test eTaskGetState()
Purpose:
- Test that eTaskGetState() returns the correct state for a particular task
Procedure:
- Create tasks in every state (and repeat for each core)
- Note: eDeleted is not tested due to needing to control when the idle tasks run
- Call eTaskGetState() on each created task
Expected:
- eTaskGetState() should return the correct state for each created task
*/
static void blocked_task(void *arg)
{
vTaskDelay(portMAX_DELAY-1);
// Shouldn't need to self delete, but added for extra safety
vTaskDelete(NULL);
}
static void suspended_task(void *arg)
{
vTaskSuspend(NULL);
// Shouldn't need to self delete, but added for extra safety
vTaskDelete(NULL);
}
static void loop_task(void *arg)
{
// Short delay to allow other created tasks to run
vTaskDelay(2);
while (1) {
;
}
}
TEST_CASE("Test eTaskGetState", "[freertos]")
{
TaskHandle_t blocked_tasks[portNUM_PROCESSORS];
TaskHandle_t suspended_tasks[portNUM_PROCESSORS];
TaskHandle_t ready_tasks[portNUM_PROCESSORS];
TaskHandle_t running_tasks[portNUM_PROCESSORS];
// Create tasks of each state on each core
for (int i = 0; i < portNUM_PROCESSORS; i++) {
TEST_ASSERT_EQUAL(pdPASS, xTaskCreatePinnedToCore(blocked_task, "blkd", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY - 1, &blocked_tasks[i], i));
TEST_ASSERT_EQUAL(pdPASS, xTaskCreatePinnedToCore(suspended_task, "susp", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY - 1, &suspended_tasks[i], i));
TEST_ASSERT_EQUAL(pdPASS, xTaskCreatePinnedToCore(loop_task, "rdy", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY - 1, &ready_tasks[i], i));
if (i == UNITY_FREERTOS_CPU) {
running_tasks[i] = xTaskGetCurrentTaskHandle();
} else {
xTaskCreatePinnedToCore(loop_task, "run", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY, &running_tasks[i], i);
}
}
// Short delay to allow created tasks to run
vTaskDelay(10);
// Check the state of the created tasks
for (int i = 0; i < portNUM_PROCESSORS; i++) {
TEST_ASSERT_EQUAL(eBlocked, eTaskGetState(blocked_tasks[i]));
TEST_ASSERT_EQUAL(eSuspended, eTaskGetState(suspended_tasks[i]));
TEST_ASSERT_EQUAL(eReady, eTaskGetState(ready_tasks[i]));
TEST_ASSERT_EQUAL(eRunning, eTaskGetState(running_tasks[i]));
}
// Clean up created tasks
for (int i = 0; i < portNUM_PROCESSORS; i++) {
vTaskDelete(blocked_tasks[i]);
vTaskDelete(suspended_tasks[i]);
vTaskDelete(ready_tasks[i]);
if (i != UNITY_FREERTOS_CPU) {
vTaskDelete(running_tasks[i]);
}
}
// Short delay to allow task memory to be cleaned
vTaskDelay(10);
}

View File

@@ -1,81 +0,0 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "unity.h"
#include "test_utils.h"
#define TSK_PRIORITY (UNITY_FREERTOS_PRIORITY + 1)
static TaskHandle_t blocked_task_handle;
static TaskHandle_t suspended_task_handle;
static SemaphoreHandle_t done_sem;
void test_task_get_state(void* arg)
{
//Current task should return eRunning
TEST_ASSERT(eTaskGetState(xTaskGetCurrentTaskHandle()) == eRunning);
//Idle task of current core should return eReady
#ifdef CONFIG_FREERTOS_SMP
TaskHandle_t *idle_handle_list = xTaskGetIdleTaskHandle();
TEST_ASSERT_EQUAL(eReady, eTaskGetState(idle_handle_list[xPortGetCoreID()]));
#else
TEST_ASSERT(eTaskGetState(xTaskGetIdleTaskHandle()) == eReady);
#endif
//Blocked Task should return eBlocked
TEST_ASSERT(eTaskGetState(blocked_task_handle) == eBlocked);
//Suspended Task should return eSuspended
TEST_ASSERT(eTaskGetState(suspended_task_handle) == eSuspended);
xSemaphoreGive(done_sem);
vTaskDelete(NULL);
}
void blocked_task(void *arg)
{
uint32_t notify_value;
while(1){
xTaskNotifyWait(0, 0xFFFFFFFF, &notify_value, portMAX_DELAY);
}
}
void suspended_task(void *arg)
{
while(1){
vTaskSuspend(NULL);
}
}
TEST_CASE("Test eTaskGetState", "[freertos]")
{
done_sem = xQueueCreateCountingSemaphore(portNUM_PROCESSORS, 0);
//Create blocked and suspended task
xTaskCreatePinnedToCore(blocked_task, "Blocked task", 1024, NULL, TSK_PRIORITY, &blocked_task_handle, tskNO_AFFINITY);
xTaskCreatePinnedToCore(suspended_task, "Suspended task", 1024, NULL, TSK_PRIORITY, &suspended_task_handle, tskNO_AFFINITY);
//Create testing task
for(int i = 0; i < portNUM_PROCESSORS; i++){
xTaskCreatePinnedToCore(test_task_get_state, "Test task", 1024, NULL, TSK_PRIORITY, NULL, i);
}
//Wait until done
for(int i = 0; i < portNUM_PROCESSORS; i++){
xSemaphoreTake(done_sem, portMAX_DELAY);
}
//Clean up blocked and suspended tasks
vTaskDelete(blocked_task_handle);
blocked_task_handle = NULL;
vTaskDelete(suspended_task_handle);
suspended_task_handle = NULL;
vSemaphoreDelete(done_sem);
vTaskDelay(10); //Give time for idle to actually delete the tasks
}