From a50e13ceedfeedf6e42128ddc706116086109b41 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Thu, 18 Nov 2021 12:57:19 +0100 Subject: [PATCH] testsuite: ztest: Add logs flushing after each test case When deferred mode is used, logging is using lowest priority thread to process the logs. When test cases are performed one by one processor never reaches lowest priority thread until whole suite is completed. Added flushing after each test case. Feature is optional (by default enabled). CONFIG_TEST_LOGGING_FLUSH_AFTER_TEST=n to disable. Signed-off-by: Krzysztof Chruscinski --- subsys/testsuite/Kconfig | 5 +++++ subsys/testsuite/ztest/src/ztest.c | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/subsys/testsuite/Kconfig b/subsys/testsuite/Kconfig index 235e74060a5..9e50a1c4203 100644 --- a/subsys/testsuite/Kconfig +++ b/subsys/testsuite/Kconfig @@ -87,6 +87,11 @@ config TEST_LOGGING_DEFAULTS logging configuration, or no logging at all, disable this in the project-level defconfig. +config TEST_LOGGING_FLUSH_AFTER_TEST + bool "When enabled logs are flushed after each test case" + default y + depends on MULTITHREADING + config TEST_ENABLE_USERSPACE bool depends on TEST_USERSPACE diff --git a/subsys/testsuite/ztest/src/ztest.c b/subsys/testsuite/ztest/src/ztest.c index 2bd75e19794..5c0a2c9b2ff 100644 --- a/subsys/testsuite/ztest/src/ztest.c +++ b/subsys/testsuite/ztest/src/ztest.c @@ -11,6 +11,7 @@ #include #endif #include +#include #ifdef KERNEL static struct k_thread ztest_thread; @@ -368,15 +369,22 @@ static int run_test(struct unit_test *test) } k_thread_start(&ztest_thread); k_thread_join(&ztest_thread, K_FOREVER); + } else { test_result = 1; run_test_functions(test); } + phase = TEST_PHASE_TEARDOWN; test->teardown(); phase = TEST_PHASE_FRAMEWORK; + /* Flush all logs in case deferred mode is used. */ + while (IS_ENABLED(CONFIG_TEST_LOGGING_FLUSH_AFTER_TEST) && log_data_pending()) { + k_msleep(100); + } + if (test_result == -1) { ret = TC_FAIL; }