settings: Add test for settings commit priority

Add a test to validate settings commit priority

Signed-off-by: Laczen JMS <laczenjms@gmail.com>
This commit is contained in:
Laczen JMS 2024-10-18 09:46:07 +02:00 committed by David Leach
commit 435587d368
5 changed files with 125 additions and 0 deletions

View file

@ -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})

View file

@ -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.

View file

@ -0,0 +1,5 @@
CONFIG_TEST=y
CONFIG_ZTEST=y
CONFIG_LOG=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_DYNAMIC_HANDLERS=y

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2024 Laczen
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/settings/settings.h>
#include <zephyr/ztest.h>
#include <zephyr/logging/log.h>
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);

View file

@ -0,0 +1,4 @@
tests:
settings.settings_commit_prio:
platform_allow: native_sim
tags: settings