diff options
author | Tristan Honscheid <honscheid@google.com> | 2022-09-14 10:25:47 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-19 22:49:21 +0000 |
commit | 6c7bf3401adf84730ff5f3766f926fc26db8646d (patch) | |
tree | d1e33d13190aff548d1396aeb32b80c278c9fb53 | |
parent | e49ffc01c1b2f0e174fdd11c5ac15db9eeaf12ca (diff) | |
download | chrome-ec-6c7bf3401adf84730ff5f3766f926fc26db8646d.tar.gz |
zephyr: tests: Delay post-main tests until hooks finish
Add a semaphore mechanism to test_main() that delays execution of the
post-main tests until the last `HOOK_CHIPSET_STARTUP` hook has
completed. This allows certain drivers to finish initializing before
tests run, eliminating a pesky race condition.
This only affects tests under `zephyr/test/drivers`
BRANCH=None
BUG=None
TEST=./twister -T zephyr/test/drivers
Signed-off-by: Tristan Honscheid <honscheid@google.com>
Change-Id: I2f79cb2bde7d3c1326070992a03d3b556774e3a7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3892153
Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r-- | zephyr/test/drivers/common/src/main.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/zephyr/test/drivers/common/src/main.c b/zephyr/test/drivers/common/src/main.c index 978c699d24..1c8497ab3f 100644 --- a/zephyr/test/drivers/common/src/main.c +++ b/zephyr/test/drivers/common/src/main.c @@ -6,8 +6,25 @@ #include <zephyr/kernel.h> #include <zephyr/ztest.h> #include "ec_app_main.h" +#include "hooks.h" #include "test/drivers/test_state.h" +/** + * @brief Semaphore that signals when hooks have completed + */ +static struct k_sem init_hooks_completed; + +/** + * @brief Hook callback function. Gets registered with the lowest priority so + * that we know all actual hooks have finished. Increments the semaphore. + */ +static void hook_completed_callback(void) +{ + /* Signal that hooks are completed */ + k_sem_give(&init_hooks_completed); +} +DECLARE_HOOK(HOOK_CHIPSET_STARTUP, hook_completed_callback, HOOK_PRIO_LAST); + bool drivers_predicate_pre_main(const void *state) { return ((struct test_state *)state)->ec_app_main_run == false; @@ -20,6 +37,8 @@ bool drivers_predicate_post_main(const void *state) void test_main(void) { + k_sem_init(&init_hooks_completed, 0, 1); + struct test_state state = { .ec_app_main_run = false, }; @@ -30,6 +49,15 @@ void test_main(void) ec_app_main(); state.ec_app_main_run = true; +/* Delay the post-main tests until hooks finish. Allow a generous + * timeout before failing. Tests with mocked power states interfere + * with this mechanism, so proceed normally in this case. + */ +#if !IS_ENABLED(CONFIG_POWER_SEQUENCE_MOCK) + zassert_ok(k_sem_take(&init_hooks_completed, K_SECONDS(10)), + "Timed out waiting for hooks to finish"); +#endif /* !IS_ENABLED(CONFIG_POWER_SEQUENCE_MOCK) */ + /* Run all the suites that depend on main being called */ ztest_run_all(&state); } |