Skip to content

Commit

Permalink
tests: posix: add tests to ensure pthread_key_delete() works
Browse files Browse the repository at this point in the history
Ensure that the correct key is deleted via pthread_key_delete().

Signed-off-by: Christopher Friedt <[email protected]>
(cherry picked from commit 0e11bcf)
  • Loading branch information
cfriedt authored and github-actions[bot] committed Nov 24, 2023
1 parent 38ed301 commit 58ba519
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/posix/common/src/pthread_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,34 @@ ZTEST(posix_apis, test_posix_single_thread_multiple_keys)
}
printk("\n");
}

ZTEST(posix_apis, test_key_resource_leak)
{
pthread_key_t key;

for (size_t i = 0; i < CONFIG_MAX_PTHREAD_KEY_COUNT; ++i) {
zassert_ok(pthread_key_create(&key, NULL), "failed to create key %zu", i);
zassert_ok(pthread_key_delete(key), "failed to delete key %zu", i);
}
}

ZTEST(posix_apis, test_correct_key_is_deleted)
{
pthread_key_t key;
size_t j = CONFIG_MAX_PTHREAD_KEY_COUNT - 1;
pthread_key_t keys[CONFIG_MAX_PTHREAD_KEY_COUNT];

for (size_t i = 0; i < ARRAY_SIZE(keys); ++i) {
zassert_ok(pthread_key_create(&keys[i], NULL), "failed to create key %zu", i);
}

key = keys[j];
zassert_ok(pthread_key_delete(keys[j]));
zassert_ok(pthread_key_create(&keys[j], NULL), "failed to create key %zu", j);

zassert_equal(key, keys[j], "deleted key %x instead of key %x", keys[j], key);

for (size_t i = 0; i < ARRAY_SIZE(keys); ++i) {
zassert_ok(pthread_key_delete(keys[i]), "failed to delete key %zu", i);
}
}

0 comments on commit 58ba519

Please sign in to comment.