1
0

hooklist_tc.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-12-22 Shell Support hook list
  9. */
  10. #include <rtthread.h>
  11. #include "rtconfig.h"
  12. #include "utest.h"
  13. #include "utest_assert.h"
  14. static int hooker1_ent_count;
  15. static int hooker2_ent_count;
  16. static struct rt_thread thr_tobe_inited;
  17. static void thread_inited_hooker1(rt_thread_t thread)
  18. {
  19. LOG_D("%s: count %d", __func__, hooker1_ent_count);
  20. hooker1_ent_count += 1;
  21. }
  22. RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_thread_inited, hooker1_node, thread_inited_hooker1);
  23. static void thread_inited_hooker2(rt_thread_t thread)
  24. {
  25. LOG_D("%s: count %d", __func__, hooker2_ent_count);
  26. hooker2_ent_count += 1;
  27. }
  28. RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_thread_inited, hooker2_node, thread_inited_hooker2);
  29. static char _thr_stack[UTEST_THR_STACK_SIZE];
  30. static void thr_tobe_inited_entry(void *param)
  31. {
  32. rt_kprintf("Hello!\n");
  33. }
  34. static void hooklist_test(void)
  35. {
  36. hooker1_ent_count = 0;
  37. hooker2_ent_count = 0;
  38. rt_thread_inited_sethook(&hooker1_node);
  39. rt_thread_inited_sethook(&hooker2_node);
  40. /* run 1 */
  41. rt_thread_init(&thr_tobe_inited,
  42. "thr_tobe_inited",
  43. thr_tobe_inited_entry,
  44. NULL,
  45. _thr_stack,
  46. sizeof(_thr_stack),
  47. 25,
  48. 100);
  49. uassert_int_equal(hooker1_ent_count, 1);
  50. uassert_int_equal(hooker2_ent_count, 1);
  51. rt_thread_detach(&thr_tobe_inited);
  52. rt_thread_mdelay(1); /* wait recycling done */
  53. /* run 2 */
  54. rt_thread_inited_rmhook(&hooker2_node);
  55. rt_thread_init(&thr_tobe_inited,
  56. "thr_tobe_inited",
  57. thr_tobe_inited_entry,
  58. NULL,
  59. _thr_stack,
  60. sizeof(_thr_stack),
  61. 25,
  62. 100);
  63. uassert_int_equal(hooker1_ent_count, 2);
  64. uassert_int_equal(hooker2_ent_count, 1);
  65. }
  66. static rt_err_t utest_tc_init(void)
  67. {
  68. hooker1_ent_count = 0;
  69. hooker2_ent_count = 0;
  70. return RT_EOK;
  71. }
  72. static rt_err_t utest_tc_cleanup(void)
  73. {
  74. rt_thread_detach(&thr_tobe_inited);
  75. rt_thread_inited_rmhook(&hooker1_node);
  76. rt_thread_inited_rmhook(&hooker2_node);
  77. return RT_EOK;
  78. }
  79. static void testcase(void)
  80. {
  81. UTEST_UNIT_RUN(hooklist_test);
  82. }
  83. UTEST_TC_EXPORT(testcase, "testcases.kernel.hooklist_tc", utest_tc_init, utest_tc_cleanup, 10);