tests: kernel: added test case k_is_preempt_thread

tests/kernel/threads_scheduling/schedule_api:
added test case to cover k_is_preempt_thread.

Jira: ZEP-1242

Change-Id: I4327438dffaa59abcfe1e41813b45abee88506b2
Signed-off-by: Sharron LIU <sharron.liu@intel.com>
This commit is contained in:
Sharron LIU 2017-02-13 16:04:50 +08:00 committed by Anas Nashif
commit d15d264da9
5 changed files with 91 additions and 1 deletions

View file

@ -1 +1,2 @@
CONFIG_ZTEST=y CONFIG_ZTEST=y
CONFIG_IRQ_OFFLOAD=y

View file

@ -1,3 +1,4 @@
include $(ZEPHYR_BASE)/tests/Makefile.test include $(ZEPHYR_BASE)/tests/Makefile.test
obj-y = main.o test_sched_priority.o test_sched_timeslice_and_lock.o obj-y = main.o test_sched_priority.o test_sched_timeslice_and_lock.o
obj-y += test_sched_is_preempt_thread.o

View file

@ -27,7 +27,8 @@ void test_main(void *p1, void *p2, void *p3)
ztest_unit_test(test_time_slicing_preemptible), ztest_unit_test(test_time_slicing_preemptible),
ztest_unit_test(test_time_slicing_disable_preemptible), ztest_unit_test(test_time_slicing_disable_preemptible),
ztest_unit_test(test_lock_preemptible), ztest_unit_test(test_lock_preemptible),
ztest_unit_test(test_unlock_preemptible) ztest_unit_test(test_unlock_preemptible),
ztest_unit_test(test_sched_is_preempt_thread)
); );
ztest_run_test_suite(test_threads_scheduling); ztest_run_test_suite(test_threads_scheduling);
} }

View file

@ -31,5 +31,6 @@ void test_time_slicing_preemptible(void);
void test_time_slicing_disable_preemptible(void); void test_time_slicing_disable_preemptible(void);
void test_lock_preemptible(void); void test_lock_preemptible(void);
void test_unlock_preemptible(void); void test_unlock_preemptible(void);
void test_sched_is_preempt_thread(void);
#endif /* __TEST_SCHED_H__ */ #endif /* __TEST_SCHED_H__ */

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @addtogroup t_sched_api
* @{
* @defgroup t_sched_is_preempt_thread test_sched_is_preempt_thread
* @brief TestPurpose: verify context type is preempt thread
* - API coverage
* -# k_is_preempt_thread
* @}
*/
#include <ztest.h>
#include <irq_offload.h>
/*macro definition*/
#define STACK_SIZE 512
/*local variables*/
static char __noinit __stack tstack[STACK_SIZE];
static struct k_sem end_sema;
static void tIsr(void *data)
{
/** TESTPOINT: The code is running at ISR.*/
assert_false(k_is_preempt_thread(), NULL);
}
static void tpreempt_ctx(void *p1, void *p2, void *p3)
{
/** TESTPOINT: The thread's priority is in the preemptible range.*/
assert_true(k_is_preempt_thread(), NULL);
k_sched_lock();
/** TESTPOINT: The thread has locked the scheduler.*/
assert_false(k_is_preempt_thread(), NULL);
k_sched_unlock();
/** TESTPOINT: The thread has not locked the scheduler.*/
assert_true(k_is_preempt_thread(), NULL);
k_thread_priority_set(k_current_get(), K_PRIO_COOP(1));
/** TESTPOINT: The thread's priority is in the cooperative range.*/
assert_false(k_is_preempt_thread(), NULL);
k_sem_give(&end_sema);
}
static void tcoop_ctx(void *p1, void *p2, void *p3)
{
/** TESTPOINT: The thread's priority is in the cooperative range.*/
assert_false(k_is_preempt_thread(), NULL);
k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(1));
/** TESTPOINT: The thread's priority is in the preemptible range.*/
assert_true(k_is_preempt_thread(), NULL);
k_sched_lock();
/** TESTPOINT: The thread has locked the scheduler.*/
assert_false(k_is_preempt_thread(), NULL);
k_sched_unlock();
/** TESTPOINT: The thread has not locked the scheduler.*/
assert_true(k_is_preempt_thread(), NULL);
k_sem_give(&end_sema);
}
/*test cases*/
void test_sched_is_preempt_thread(void)
{
k_sem_init(&end_sema, 0, 1);
/*create preempt thread*/
k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE,
tpreempt_ctx, NULL, NULL, NULL,
K_PRIO_PREEMPT(1), 0, 0);
k_sem_take(&end_sema, K_FOREVER);
k_thread_abort(tid);
/*create coop thread*/
tid = k_thread_spawn(tstack, STACK_SIZE,
tcoop_ctx, NULL, NULL, NULL,
K_PRIO_COOP(1), 0, 0);
k_sem_take(&end_sema, K_FOREVER);
k_thread_abort(tid);
/*invoke isr*/
irq_offload(tIsr, NULL);
}