From 435587d3681e8949edf868827d960f54e701940b Mon Sep 17 00:00:00 2001 From: Laczen JMS Date: Fri, 18 Oct 2024 09:46:07 +0200 Subject: [PATCH] settings: Add test for settings commit priority Add a test to validate settings commit priority Signed-off-by: Laczen JMS --- .../settings_commit_prio/CMakeLists.txt | 8 ++ tests/subsys/settings_commit_prio/README.rst | 18 ++++ tests/subsys/settings_commit_prio/prj.conf | 5 ++ tests/subsys/settings_commit_prio/src/main.c | 90 +++++++++++++++++++ .../subsys/settings_commit_prio/testcase.yaml | 4 + 5 files changed, 125 insertions(+) create mode 100644 tests/subsys/settings_commit_prio/CMakeLists.txt create mode 100644 tests/subsys/settings_commit_prio/README.rst create mode 100644 tests/subsys/settings_commit_prio/prj.conf create mode 100644 tests/subsys/settings_commit_prio/src/main.c create mode 100644 tests/subsys/settings_commit_prio/testcase.yaml diff --git a/tests/subsys/settings_commit_prio/CMakeLists.txt b/tests/subsys/settings_commit_prio/CMakeLists.txt new file mode 100644 index 00000000000..f36a52380d3 --- /dev/null +++ b/tests/subsys/settings_commit_prio/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(test_settings_commit_prio) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/subsys/settings_commit_prio/README.rst b/tests/subsys/settings_commit_prio/README.rst new file mode 100644 index 00000000000..afc2c4c75c6 --- /dev/null +++ b/tests/subsys/settings_commit_prio/README.rst @@ -0,0 +1,18 @@ +.. _settings_commit_prio_test: + +Settings Subsystem commit priority Test +####################################### + +Overview +******** + +This test is used to test the Settings Subsystem commit priority. + +Building and Testing +******************** + +This application can be built and executed on native_sim as follows: + +.. code-block:: console + $ ./scripts/twister -p native_sim -T tests/subsys/settings_commit_prio +To build for another board, change ``native_sim`` above to that board's name. diff --git a/tests/subsys/settings_commit_prio/prj.conf b/tests/subsys/settings_commit_prio/prj.conf new file mode 100644 index 00000000000..8914d4f20f2 --- /dev/null +++ b/tests/subsys/settings_commit_prio/prj.conf @@ -0,0 +1,5 @@ +CONFIG_TEST=y +CONFIG_ZTEST=y +CONFIG_LOG=y +CONFIG_SETTINGS=y +CONFIG_SETTINGS_DYNAMIC_HANDLERS=y diff --git a/tests/subsys/settings_commit_prio/src/main.c b/tests/subsys/settings_commit_prio/src/main.c new file mode 100644 index 00000000000..9722727901b --- /dev/null +++ b/tests/subsys/settings_commit_prio/src/main.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2024 Laczen + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +LOG_MODULE_REGISTER(test); + +static int prio; + +int commit0(void) +{ + LOG_INF("%s Called", __func__); + zassert_equal(prio, 0, "Bad commit order"); + prio++; + return 0; +} + +int commit1(void) +{ + LOG_INF("%s Called", __func__); + zassert_equal(prio, 1, "Bad commit order"); + prio++; + return 0; +} + +int commit2(void) +{ + LOG_INF("%s Called", __func__); + zassert_equal(prio, 2, "Bad commit order"); + prio++; + return 0; +} + +int commit3(void) +{ + LOG_INF("%s Called", __func__); + zassert_equal(prio, 3, "Bad commit order"); + prio++; + return 0; +} + +int commit5(void) +{ + LOG_INF("%s Called", __func__); + zassert_equal(prio, 0, "Bad commit order"); + return 0; +} + +SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(h0, "h0", NULL, NULL, commit0, NULL, 0); +SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(h2, "h2", NULL, NULL, commit2, NULL, 2); + +static struct settings_handler h1 = { + .name = "h1", + .h_commit = commit1, +}; + +static struct settings_handler h3 = { + .name = "h3", + .h_commit = commit3, +}; + +SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(h5, "h5", NULL, NULL, commit5, NULL, -1); + +/** + * @brief Test Settings commit order + * + * This test verifies the settings commit order. + */ +ZTEST(settings_commit_prio, test_commit_order) +{ + int rc; + + prio = 0; + rc = settings_register_with_cprio(&h1, 1); + zassert_equal(rc, 0, "Failed to register handler"); + rc = settings_register_with_cprio(&h3, 3); + zassert_equal(rc, 0, "Failed to register handler"); + + rc = settings_commit(); + zassert_equal(rc, 0, "Commit failed with code [%d]", rc); + zassert_equal(prio, 4, "Incorrect prio level reached [%d]", prio); +} + +ZTEST_SUITE(settings_commit_prio, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/subsys/settings_commit_prio/testcase.yaml b/tests/subsys/settings_commit_prio/testcase.yaml new file mode 100644 index 00000000000..089329a5142 --- /dev/null +++ b/tests/subsys/settings_commit_prio/testcase.yaml @@ -0,0 +1,4 @@ +tests: + settings.settings_commit_prio: + platform_allow: native_sim + tags: settings