Skip to content

Commit 909185f

Browse files
cfriedtstephanosio
authored andcommitted
tests: posix: cond: test to ensure there is no resource leakage
Add a test to ensure that `pthread_cond_t` can be used over and over again and that there is no resource leakage with proper usage. Signed-off-by: Chris Friedt <[email protected]>
1 parent 3a56d27 commit 909185f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tests/posix/common/src/cond.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
9+
#include <pthread.h>
10+
11+
/**
12+
* @brief Test to demonstrate limited condition variable resources
13+
*
14+
* @details Exactly CONFIG_MAX_PTHREAD_COND_COUNT can be in use at once.
15+
*/
16+
ZTEST(posix_apis, test_posix_cond_resource_exhausted)
17+
{
18+
size_t i;
19+
pthread_cond_t m[CONFIG_MAX_PTHREAD_COND_COUNT + 1];
20+
21+
for (i = 0; i < CONFIG_MAX_PTHREAD_COND_COUNT; ++i) {
22+
zassert_ok(pthread_cond_init(&m[i], NULL), "failed to init cond %zu", i);
23+
}
24+
25+
/* try to initialize one more than CONFIG_MAX_PTHREAD_COND_COUNT */
26+
zassert_equal(i, CONFIG_MAX_PTHREAD_COND_COUNT);
27+
zassert_not_equal(0, pthread_cond_init(&m[i], NULL), "should not have initialized cond %zu",
28+
i);
29+
30+
for (; i > 0; --i) {
31+
zassert_ok(pthread_cond_destroy(&m[i - 1]), "failed to destroy cond %zu", i - 1);
32+
}
33+
}
34+
35+
/**
36+
* @brief Test to that there are no condition variable resource leaks
37+
*
38+
* @details Demonstrate that condition variables may be used over and over again.
39+
*/
40+
ZTEST(posix_apis, test_posix_cond_resource_leak)
41+
{
42+
pthread_cond_t m;
43+
44+
for (size_t i = 0; i < 2 * CONFIG_MAX_PTHREAD_COND_COUNT; ++i) {
45+
zassert_ok(pthread_cond_init(&m, NULL), "failed to init cond %zu", i);
46+
zassert_ok(pthread_cond_destroy(&m), "failed to destroy cond %zu", i);
47+
}
48+
}

0 commit comments

Comments
 (0)