Re: [PATCH v2 3/4] tests: Add nano_work test


Luiz Augusto von Dentz
 

Hi Vlad,

On Mon, May 9, 2016 at 11:29 AM, Vlad Dogaru <vlad.dogaru(a)intel.com> wrote:
Change-Id: I78359bdd75aa2aa5e2f4b34347d838747d60015f
Signed-off-by: Vlad Dogaru <vlad.dogaru(a)intel.com>
---
tests/kernel/test_nano_work/Makefile | 5 ++
tests/kernel/test_nano_work/README.txt | 51 ++++++++++++++
tests/kernel/test_nano_work/prj.conf | 1 +
tests/kernel/test_nano_work/src/Makefile | 3 +
tests/kernel/test_nano_work/src/main.c | 114 +++++++++++++++++++++++++++++++
tests/kernel/test_nano_work/testcase.ini | 2 +
6 files changed, 176 insertions(+)
create mode 100644 tests/kernel/test_nano_work/Makefile
create mode 100644 tests/kernel/test_nano_work/README.txt
create mode 100644 tests/kernel/test_nano_work/prj.conf
create mode 100644 tests/kernel/test_nano_work/src/Makefile
create mode 100644 tests/kernel/test_nano_work/src/main.c
create mode 100644 tests/kernel/test_nano_work/testcase.ini

diff --git a/tests/kernel/test_nano_work/Makefile b/tests/kernel/test_nano_work/Makefile
new file mode 100644
index 0000000..f2e25a1
--- /dev/null
+++ b/tests/kernel/test_nano_work/Makefile
@@ -0,0 +1,5 @@
+KERNEL_TYPE = nano
+BOARD ?= qemu_x86
+CONF_FILE = prj.conf
+
+include ${ZEPHYR_BASE}/Makefile.inc
diff --git a/tests/kernel/test_nano_work/README.txt b/tests/kernel/test_nano_work/README.txt
new file mode 100644
index 0000000..d8c42de
--- /dev/null
+++ b/tests/kernel/test_nano_work/README.txt
@@ -0,0 +1,51 @@
+Title: Test nano_work
+
+Description:
+
+A simple application verifying the workqueue API
+
+The test submits a few work items and checks that they are executed in order.
+
+--------------------------------------------------------------------------------
+
+Building and Running Project:
+
+This nanokernel project outputs to the console. It can be built and executed
+on QEMU as follows:
+
+ make qemu
+
+--------------------------------------------------------------------------------
+
+Troubleshooting:
+
+Problems caused by out-dated project information can be addressed by
+issuing one of the following commands then rebuilding the project:
+
+ make clean # discard results of previous builds
+ # but keep existing configuration info
+or
+ make pristine # discard results of previous builds
+ # and restore pre-defined configuration info
+
+--------------------------------------------------------------------------------
+
+Sample Output:
+
+Starting nano_work test
+ - Initializing test items
+ - Submitting test items
+ - Running test item 1
+ - Waiting for work to finish
+ - Done running test item 1
+ - Running test item 2
+ - Done running test item 2
+ - Running test item 3
+ - Done running test item 3
+ - Running test item 4
+ - Done running test item 4
+ - Checking results
I guess it would be wise to add tests for submitting work from both
fiber and task, it appear this is only done for a task. Also it is
super important that submitting work from the handler should also be
tested.

+===================================================================
+PASS - main.
+===================================================================
+PROJECT EXECUTION SUCCESSFUL
diff --git a/tests/kernel/test_nano_work/prj.conf b/tests/kernel/test_nano_work/prj.conf
new file mode 100644
index 0000000..3f9e6db
--- /dev/null
+++ b/tests/kernel/test_nano_work/prj.conf
@@ -0,0 +1 @@
+CONFIG_NANO_TIMEOUTS=y
diff --git a/tests/kernel/test_nano_work/src/Makefile b/tests/kernel/test_nano_work/src/Makefile
new file mode 100644
index 0000000..e04d7c6
--- /dev/null
+++ b/tests/kernel/test_nano_work/src/Makefile
@@ -0,0 +1,3 @@
+ccflags-y += -I${srctree}/tests/include
+
+obj-y = main.o
diff --git a/tests/kernel/test_nano_work/src/main.c b/tests/kernel/test_nano_work/src/main.c
new file mode 100644
index 0000000..3e3d8ee
--- /dev/null
+++ b/tests/kernel/test_nano_work/src/main.c
@@ -0,0 +1,114 @@
+/*
+ * 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.
+ */
+
+#include <stdbool.h>
+
+#include <zephyr.h>
+#include <nanokernel.h>
+#include <tc_util.h>
+#include <misc/util.h>
+#include <misc/nano_work.h>
+
+#define NUM_TEST_ITEMS 4
+#define WORK_ITEM_WAIT sys_clock_ticks_per_sec
+
+struct test_item {
+ int key;
+ struct nano_work work;
+};
+
+static struct test_item tests[NUM_TEST_ITEMS];
+
+static int results[NUM_TEST_ITEMS];
+static int num_results = 0;
+
+static void work_handler(struct nano_work *work)
+{
+ struct test_item *ti = CONTAINER_OF(work, struct test_item, work);
+
+ TC_PRINT(" - Running test item %d\n", ti->key);
+ fiber_sleep(WORK_ITEM_WAIT);
+
+ TC_PRINT(" - Done running test item %d\n", ti->key);
+ results[num_results++] = ti->key;
+}
+
+static void test_items_init(void)
+{
+ int i;
+
+ for (i = 0; i < NUM_TEST_ITEMS; i++) {
+ tests[i].key = i + 1;
+ tests[i].work.handler = work_handler;
+ }
+}
+
+static void test_items_submit(void)
+{
+ int i;
+
+ for (i = 0; i < NUM_TEST_ITEMS; i++) {
+ submit_work(&tests[i].work);
+ }
+}
+
+static bool check_results(void)
+{
+ int i;
+
+ if (num_results != NUM_TEST_ITEMS) {
+ TC_ERROR("*** work items finished: %d (expected: %d)\n",
+ num_results, NUM_TEST_ITEMS);
+ return false;
+ }
+
+ for (i = 0; i < NUM_TEST_ITEMS; i++) {
+ if (results[i] != tests[i].key) {
+ TC_ERROR("*** got result %d in position %d (expected %d)\n",
+ results[i], i, tests[i].key);
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void main(void)
+{
+ int status = TC_FAIL;
+
+ TC_PRINT("Starting nano_work test\n");
+
+ TC_PRINT(" - Initializing test items\n");
+ test_items_init();
+
+ TC_PRINT(" - Submitting test items\n");
+ test_items_submit();
+
+ TC_PRINT(" - Waiting for work to finish\n");
+ task_sleep((NUM_TEST_ITEMS + 1) * WORK_ITEM_WAIT);
+
+ TC_PRINT(" - Checking results\n");
+ if (!check_results()) {
+ goto end;
+ }
+
+ status = TC_PASS;
+
+end:
+ TC_END_RESULT(status);
+ TC_END_REPORT(status);
+}
diff --git a/tests/kernel/test_nano_work/testcase.ini b/tests/kernel/test_nano_work/testcase.ini
new file mode 100644
index 0000000..c75e11b
--- /dev/null
+++ b/tests/kernel/test_nano_work/testcase.ini
@@ -0,0 +1,2 @@
+[test]
+tags = core
\ No newline at end of file
--
1.9.1


--
Luiz Augusto von Dentz

Join devel@lists.zephyrproject.org to automatically receive all group messages.