Skip to content

Commit a941b6a

Browse files
Ying mingMaureenHelm
authored andcommitted
test: kheap: add testcase to improve coverage
Add a testcase to prove the thread can be waited on waitq when there isn't enough space on heap. Signed-off-by: Ying ming <[email protected]>
1 parent 345c9c0 commit a941b6a

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tests/kernel/mem_heap/k_heap_api/src/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern void test_k_heap_alloc(void);
99
extern void test_k_heap_alloc_fail(void);
1010
extern void test_k_heap_free(void);
1111
extern void test_kheap_alloc_in_isr_nowait(void);
12+
extern void test_k_heap_alloc_pending(void);
1213

1314
/**
1415
* @brief k heap api tests
@@ -26,6 +27,7 @@ void test_main(void)
2627
ztest_unit_test(test_k_heap_alloc),
2728
ztest_unit_test(test_k_heap_alloc_fail),
2829
ztest_unit_test(test_k_heap_free),
29-
ztest_unit_test(test_kheap_alloc_in_isr_nowait));
30+
ztest_unit_test(test_kheap_alloc_in_isr_nowait),
31+
ztest_unit_test(test_k_heap_alloc_pending));
3032
ztest_run_test_suite(k_heap_api);
3133
}

tests/kernel/mem_heap/k_heap_api/src/test_kheap_api.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <irq_offload.h>
99
#include "test_kheap.h"
1010

11+
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
12+
K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
13+
struct k_thread tdata;
14+
1115
K_HEAP_DEFINE(k_heap_test, HEAP_SIZE);
1216

1317
#define ALLOC_SIZE_1 1024
@@ -24,6 +28,16 @@ static void tIsr_kheap_alloc_nowait(void *data)
2428
k_heap_free(&k_heap_test, p);
2529
}
2630

31+
static void thread_alloc_heap(void *p1, void *p2, void *p3)
32+
{
33+
k_timeout_t timeout = Z_TIMEOUT_MS(200);
34+
35+
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_2, timeout);
36+
37+
zassert_not_null(p, "k_heap_alloc operation failed");
38+
k_heap_free(&k_heap_test, p);
39+
}
40+
2741
/*test cases*/
2842

2943
/**
@@ -105,9 +119,38 @@ void test_k_heap_free(void)
105119
/**
106120
* @brief Validate allocation and free heap memory in isr context.
107121
*
122+
* @details The test validates k_heap_alloc() in isr context, the timeout
123+
* param should be K_NO_WAIT, because this situation isn't allow to wait.
124+
*
108125
* @ingroup kernel_heap_tests
109126
*/
110127
void test_kheap_alloc_in_isr_nowait(void)
111128
{
112129
irq_offload((irq_offload_routine_t)tIsr_kheap_alloc_nowait, NULL);
113130
}
131+
132+
/**
133+
* @brief Validate the k_heap support wait between different threads.
134+
*
135+
* @details In main thread alloc a buffer from the heap, then run the
136+
* child thread. If there isn't enough space in the heap, the child thread
137+
* will wait timeout long until main thread free the buffer to heap.
138+
*
139+
* @ingroup kernel_heap_tests
140+
*/
141+
void test_k_heap_alloc_pending(void)
142+
{
143+
k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
144+
thread_alloc_heap, NULL, NULL, NULL,
145+
K_PRIO_PREEMPT(5), 0, K_NO_WAIT);
146+
147+
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_2, K_NO_WAIT);
148+
149+
zassert_not_null(p, "k_heap_alloc operation failed");
150+
151+
/* make the child thread run */
152+
k_msleep(1);
153+
k_heap_free(&k_heap_test, p);
154+
155+
k_thread_join(tid, K_FOREVER);
156+
}

0 commit comments

Comments
 (0)