test_eTaskGetState.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "unity.h"
  9. #include "test_utils.h"
  10. /*
  11. Test eTaskGetState()
  12. Purpose:
  13. - Test that eTaskGetState() returns the correct state for a particular task
  14. Procedure:
  15. - Create tasks in every state (and repeat for each core)
  16. - Note: eDeleted is not tested due to needing to control when the idle tasks run
  17. - Call eTaskGetState() on each created task
  18. Expected:
  19. - eTaskGetState() should return the correct state for each created task
  20. */
  21. static void blocked_task(void *arg)
  22. {
  23. vTaskDelay(portMAX_DELAY-1);
  24. // Shouldn't need to self delete, but added for extra safety
  25. vTaskDelete(NULL);
  26. }
  27. static void suspended_task(void *arg)
  28. {
  29. vTaskSuspend(NULL);
  30. // Shouldn't need to self delete, but added for extra safety
  31. vTaskDelete(NULL);
  32. }
  33. static void loop_task(void *arg)
  34. {
  35. // Short delay to allow other created tasks to run
  36. vTaskDelay(2);
  37. while (1) {
  38. ;
  39. }
  40. }
  41. TEST_CASE("Test eTaskGetState", "[freertos]")
  42. {
  43. TaskHandle_t blocked_tasks[portNUM_PROCESSORS];
  44. TaskHandle_t suspended_tasks[portNUM_PROCESSORS];
  45. TaskHandle_t ready_tasks[portNUM_PROCESSORS];
  46. TaskHandle_t running_tasks[portNUM_PROCESSORS];
  47. // Create tasks of each state on each core
  48. for (int i = 0; i < portNUM_PROCESSORS; i++) {
  49. TEST_ASSERT_EQUAL(pdPASS, xTaskCreatePinnedToCore(blocked_task, "blkd", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY - 1, &blocked_tasks[i], i));
  50. TEST_ASSERT_EQUAL(pdPASS, xTaskCreatePinnedToCore(suspended_task, "susp", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY - 1, &suspended_tasks[i], i));
  51. TEST_ASSERT_EQUAL(pdPASS, xTaskCreatePinnedToCore(loop_task, "rdy", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY - 1, &ready_tasks[i], i));
  52. if (i == UNITY_FREERTOS_CPU) {
  53. running_tasks[i] = xTaskGetCurrentTaskHandle();
  54. } else {
  55. xTaskCreatePinnedToCore(loop_task, "run", configMINIMAL_STACK_SIZE, NULL, UNITY_FREERTOS_PRIORITY, &running_tasks[i], i);
  56. }
  57. }
  58. // Short delay to allow created tasks to run
  59. vTaskDelay(10);
  60. // Check the state of the created tasks
  61. for (int i = 0; i < portNUM_PROCESSORS; i++) {
  62. TEST_ASSERT_EQUAL(eBlocked, eTaskGetState(blocked_tasks[i]));
  63. TEST_ASSERT_EQUAL(eSuspended, eTaskGetState(suspended_tasks[i]));
  64. TEST_ASSERT_EQUAL(eReady, eTaskGetState(ready_tasks[i]));
  65. TEST_ASSERT_EQUAL(eRunning, eTaskGetState(running_tasks[i]));
  66. }
  67. // Clean up created tasks
  68. for (int i = 0; i < portNUM_PROCESSORS; i++) {
  69. vTaskDelete(blocked_tasks[i]);
  70. vTaskDelete(suspended_tasks[i]);
  71. vTaskDelete(ready_tasks[i]);
  72. if (i != UNITY_FREERTOS_CPU) {
  73. vTaskDelete(running_tasks[i]);
  74. }
  75. }
  76. // Short delay to allow task memory to be cleaned
  77. vTaskDelay(10);
  78. }