tests: add zephyr message queue test case with unified kernel

the commit cover basic message queue api testing across contexts
and some typical scenario

Change-Id: I82bb0c6a5df9d4436f5a98f78d1ad989e32282c8
Signed-off-by: jing wang <jing.j.wang@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
jing wang 2016-12-20 11:22:25 +08:00 committed by Anas Nashif
commit 333e8d985d
9 changed files with 344 additions and 0 deletions

View file

@ -0,0 +1,4 @@
BOARD ?= qemu_x86
CONF_FILE = prj.conf
include ${ZEPHYR_BASE}/Makefile.inc

View file

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

View file

@ -0,0 +1,3 @@
include $(ZEPHYR_BASE)/tests/Makefile.test
obj-y = main.o test_msgq_contexts.o test_msgq_fail.o test_msgq_purge.o

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @addtogroup t_kernel_msgq
* @{
* @defgroup t_msgq_api test_msgq_api
* @}
*/
#include <ztest.h>
extern void test_msgq_thread(void);
extern void test_msgq_isr(void);
extern void test_msgq_put_fail(void);
extern void test_msgq_get_fail(void);
extern void test_msgq_purge_when_put(void);
/*test case main entry*/
void test_main(void *p1, void *p2, void *p3)
{
ztest_test_suite(test_msgq_api,
ztest_unit_test(test_msgq_thread),
ztest_unit_test(test_msgq_isr),
ztest_unit_test(test_msgq_put_fail),
ztest_unit_test(test_msgq_get_fail),
ztest_unit_test(test_msgq_purge_when_put));
ztest_run_test_suite(test_msgq_api);
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2015-2016 Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __TEST_MSGQ_H__
#define __TEST_MSGQ_H__
#include <zephyr.h>
#include <irq_offload.h>
#include <ztest.h>
#define TIMEOUT 100
#define STACK_SIZE 512
#define MSG_SIZE 4
#define MSGQ_LEN 2
#define MSG0 0xABCD
#define MSG1 0x1234
#endif /* __TEST_MSGQ_H__ */

View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @addtogroup t_kernel_msgq
* @{
* @defgroup t_msgq_context test_msgq_context
* @brief TestPurpose: verify zephyr msgq apis across contexts
* @}
*/
#include "test_msgq.h"
/**TESTPOINT: init via K_MSGQ_DEFINE*/
K_MSGQ_DEFINE(kmsgq, MSG_SIZE, MSGQ_LEN, 4);
static char __noinit __stack tstack[STACK_SIZE];
static char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN];
static uint32_t data[MSGQ_LEN] = { MSG0, MSG1 };
static struct k_sem end_sema;
static void put_msgq(struct k_msgq *pmsgq)
{
int ret;
for (int i = 0; i < MSGQ_LEN; i++) {
ret = k_msgq_put(pmsgq, (void *)&data[i], K_NO_WAIT);
assert_false(ret, NULL);
/**TESTPOINT: msgq free get*/
assert_equal(k_msgq_num_free_get(pmsgq), MSGQ_LEN - 1 - i, NULL);
/**TESTPOINT: msgq used get*/
assert_equal(k_msgq_num_used_get(pmsgq), i + 1, NULL);
}
}
static void get_msgq(struct k_msgq *pmsgq)
{
uint32_t rx_data;
int ret;
for (int i = 0; i < MSGQ_LEN; i++) {
ret = k_msgq_get(pmsgq, &rx_data, K_FOREVER);
assert_false(ret, NULL);
assert_equal(rx_data, data[i], NULL);
/**TESTPOINT: msgq free get*/
assert_equal(k_msgq_num_free_get(pmsgq), i + 1, NULL);
/**TESTPOINT: msgq used get*/
assert_equal(k_msgq_num_used_get(pmsgq), MSGQ_LEN - 1 - i, NULL);
}
}
static void purge_msgq(struct k_msgq *pmsgq)
{
k_msgq_purge(pmsgq);
assert_equal(k_msgq_num_free_get(pmsgq), MSGQ_LEN, NULL);
assert_equal(k_msgq_num_used_get(pmsgq), 0, NULL);
}
static void tIsr_entry(void *p)
{
put_msgq((struct k_msgq *)p);
}
static void tThread_entry(void *p1, void *p2, void *p3)
{
get_msgq((struct k_msgq *)p1);
k_sem_give(&end_sema);
}
static void msgq_thread(struct k_msgq *pmsgq)
{
k_sem_init(&end_sema, 0, 1);
/**TESTPOINT: thread-thread data passing via message queue*/
k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE,
tThread_entry, pmsgq, NULL, NULL,
K_PRIO_PREEMPT(0), 0, 0);
put_msgq(pmsgq);
k_sem_take(&end_sema, K_FOREVER);
k_thread_abort(tid);
/**TESTPOINT: msgq purge*/
purge_msgq(pmsgq);
}
static void msgq_isr(struct k_msgq *pmsgq)
{
/**TESTPOINT: thread-isr data passing via message queue*/
irq_offload(tIsr_entry, pmsgq);
get_msgq(pmsgq);
/**TESTPOINT: msgq purge*/
purge_msgq(pmsgq);
}
/*test cases*/
void test_msgq_thread(void)
{
struct k_msgq msgq;
/**TESTPOINT: init via k_msgq_init*/
k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN);
msgq_thread(&msgq);
msgq_thread(&kmsgq);
}
void test_msgq_isr(void)
{
struct k_msgq msgq;
/**TESTPOINT: init via k_msgq_init*/
k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN);
msgq_isr(&msgq);
msgq_isr(&kmsgq);
}

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @addtogroup t_msgq_api
* @{
* @defgroup t_msgq_fail test_msgq_fail
* @brief TestPurpose: verify zephyr msgq return code under negative tests
* @}
*/
#include "test_msgq.h"
static char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN];
static uint32_t data[MSGQ_LEN] = { MSG0, MSG1 };
/*test cases*/
void test_msgq_put_fail(void *p1, void *p2, void *p3)
{
struct k_msgq msgq;
k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN);
int ret = k_msgq_put(&msgq, (void *)&data[0], K_NO_WAIT);
assert_false(ret, NULL);
ret = k_msgq_put(&msgq, (void *)&data[0], K_NO_WAIT);
assert_false(ret, NULL);
/**TESTPOINT: msgq put returns -ENOMSG*/
ret = k_msgq_put(&msgq, (void *)&data[1], K_NO_WAIT);
assert_equal(ret, -ENOMSG, NULL);
/**TESTPOINT: msgq put returns -EAGAIN*/
ret = k_msgq_put(&msgq, (void *)&data[0], TIMEOUT);
assert_equal(ret, -EAGAIN, NULL);
k_msgq_purge(&msgq);
}
void test_msgq_get_fail(void *p1, void *p2, void *p3)
{
struct k_msgq msgq;
uint32_t rx_data;
k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN);
/**TESTPOINT: msgq get returns -ENOMSG*/
int ret = k_msgq_get(&msgq, &rx_data, K_NO_WAIT);
assert_equal(ret, -ENOMSG, NULL);
/**TESTPOINT: msgq get returns -EAGAIN*/
ret = k_msgq_get(&msgq, &rx_data, TIMEOUT);
assert_equal(ret, -EAGAIN, NULL);
}

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @addtogroup t_msgq_api
* @{
* @defgroup t_msgq_purge test_msgq_purge
* @brief TestPurpose: verify zephyr msgq purge under different scenario
* @}
*/
#include "test_msgq.h"
static char __noinit __stack tstack[STACK_SIZE];
static char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN];
static uint32_t data[MSGQ_LEN] = { MSG0, MSG1 };
static void tThread_entry(void *p1, void *p2, void *p3)
{
int ret = k_msgq_put((struct k_msgq *)p1, (void *)&data[0], TIMEOUT);
assert_equal(ret, -ENOMSG, NULL);
}
/*test cases*/
void test_msgq_purge_when_put(void)
{
struct k_msgq msgq;
int ret;
k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN);
/*fill the queue to full*/
for (int i = 0; i < MSGQ_LEN; i++) {
ret = k_msgq_put(&msgq, (void *)&data[i], K_NO_WAIT);
}
/*create another thread waiting to put msg*/
k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE,
tThread_entry, &msgq, NULL, NULL,
K_PRIO_PREEMPT(0), 0, 0);
k_sleep(TIMEOUT >> 1);
/**TESTPOINT: msgq purge while another thread waiting to put msg*/
k_msgq_purge(&msgq);
k_sleep(TIMEOUT >> 1);
k_thread_abort(tid);
/*verify msg put after purge*/
for (int i = 0; i < MSGQ_LEN; i++) {
ret = k_msgq_put(&msgq, (void *)&data[i], K_NO_WAIT);
assert_false(ret, NULL);
}
}

View file

@ -0,0 +1,2 @@
[test]
tags = kernel