tests: kernel: threads: Additional tests for set_priority

Added some additional tests for setting priority of threads.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
This commit is contained in:
Rajavardhan Gundi 2018-03-28 13:58:14 +05:30 committed by Anas Nashif
commit 3c8b3875c6
2 changed files with 110 additions and 0 deletions

View file

@ -28,6 +28,7 @@ extern void test_threads_abort_others(void);
extern void test_threads_abort_repeat(void);
extern void test_abort_handler(void);
extern void test_essential_thread_operation(void);
extern void test_threads_priority_set(void);
__kernel struct k_thread tdata;
#define STACK_SIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE)
@ -50,6 +51,7 @@ void test_main(void)
ztest_unit_test(test_thread_start),
ztest_unit_test(test_threads_suspend_resume_cooperative),
ztest_unit_test(test_threads_suspend_resume_preemptible),
ztest_unit_test(test_threads_priority_set),
ztest_user_unit_test(test_threads_cancel_undelayed),
ztest_user_unit_test(test_threads_cancel_delayed),
ztest_user_unit_test(test_threads_cancel_started),

View file

@ -0,0 +1,108 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @addtogroup t_threads_lifecycle
* @{
* @defgroup t_threads_priority_set test_threads_priority_set
* @}
*/
#include <ztest.h>
#define STACK_SIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE)
K_THREAD_STACK_EXTERN(tstack);
extern struct k_thread tdata;
static int thread2_data;
K_SEM_DEFINE(sem_thread2, 0, 1);
K_SEM_DEFINE(sem_thread1, 0, 1);
/**
*
* @brief thread2 portion to test setting the priority
*
*/
void thread2_set_prio_test(void)
{
/* lower thread2 priority by 5 */
k_sem_take(&sem_thread2, K_FOREVER);
thread2_data = k_thread_priority_get(k_current_get());
k_sem_give(&sem_thread1);
/* raise thread2 priority by 10 */
k_sem_take(&sem_thread2, K_FOREVER);
thread2_data = k_thread_priority_get(k_current_get());
k_sem_give(&sem_thread1);
/* restore thread2 priority */
k_sem_take(&sem_thread2, K_FOREVER);
thread2_data = k_thread_priority_get(k_current_get());
k_sem_give(&sem_thread1);
}
/**
*
* @brief Test the k_thread_priority_set() API
*
*/
void test_threads_priority_set(void)
{
int rv;
int prio = k_thread_priority_get(k_current_get());
/* Lower the priority of the current thread (thread1) */
k_thread_priority_set(k_current_get(), prio + 2);
rv = k_thread_priority_get(k_current_get());
zassert_equal(rv, prio + 2,
"Expected priority to be changed to %d, not %d\n",
prio + 2, rv);
/* Raise the priority of the current thread (thread1) */
k_thread_priority_set(k_current_get(), prio - 2);
rv = k_thread_priority_get(k_current_get());
zassert_equal(rv, prio - 2,
"Expected priority to be changed to %d, not %d\n",
prio - 2, rv);
/* Restore the priority of the current thread (thread1) */
k_thread_priority_set(k_current_get(), prio);
rv = k_thread_priority_get(k_current_get());
zassert_equal(rv, prio,
"Expected priority to be changed to %d, not %d\n",
prio, rv);
/* create thread with lower priority */
int thread2_prio = prio + 1;
k_tid_t thread2_id = k_thread_create(&tdata, tstack, STACK_SIZE,
(k_thread_entry_t)thread2_set_prio_test,
NULL, NULL, NULL, thread2_prio, 0, 0);
/* Lower the priority of thread2 */
k_thread_priority_set(thread2_id, thread2_prio + 2);
k_sem_give(&sem_thread2);
k_sem_take(&sem_thread1, K_FOREVER);
zassert_equal(thread2_data, thread2_prio + 2,
"Expected priority to be changed to %d, not %d\n",
thread2_prio + 2, thread2_data);
/* Raise the priority of thread2 */
k_thread_priority_set(thread2_id, thread2_prio - 2);
k_sem_give(&sem_thread2);
k_sem_take(&sem_thread1, K_FOREVER);
zassert_equal(thread2_data, thread2_prio - 2,
"Expected priority to be changed to %d, not %d\n",
thread2_prio - 2, thread2_data);
/* Restore the priority of thread2 */
k_thread_priority_set(thread2_id, thread2_prio);
k_sem_give(&sem_thread2);
k_sem_take(&sem_thread1, K_FOREVER);
zassert_equal(thread2_data, thread2_prio,
"Expected priority to be changed to %d, not %d\n",
thread2_prio, thread2_data);
}