Skip to content

Commit

Permalink
settings: add test for commit priority
Browse files Browse the repository at this point in the history
Add a test to validate commit priorities

Signed-off-by: Laczen JMS <[email protected]>
  • Loading branch information
Laczen committed Oct 15, 2024
1 parent 60c7cb1 commit ef58f81
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/subsys/settings_commit_prio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
18 changes: 18 additions & 0 deletions tests/subsys/settings_commit_prio/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. _settings_commit_prio_test:

Settings Subsystem commit priority Test Sample
##############################################

Overview
********

This test sample 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.
5 changes: 5 additions & 0 deletions tests/subsys/settings_commit_prio/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CONFIG_TEST=y
CONFIG_ZTEST=y
CONFIG_LOG=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_DYNAMIC_HANDLERS=y
105 changes: 105 additions & 0 deletions tests/subsys/settings_commit_prio/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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__);
if (prio != 0) {
return -EINVAL;
}

prio++;
return 0;
}

int commit1(void)
{
LOG_INF("%s Called", __func__);
if (prio != 1) {
return -EINVAL;
}

prio++;
return 0;
}

int commit2(void)
{
LOG_INF("%s Called", __func__);
if (prio != 2) {
return -EINVAL;
}

prio++;
return 0;
}

int commit3(void)
{
LOG_INF("%s Called", __func__);
if (prio != 3) {
return -EINVAL;
}

prio++;
return 0;
}

int commit5(void)
{
LOG_INF("%s Called", __func__);
if (prio > 1) {
return -EINVAL;
}

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);
4 changes: 4 additions & 0 deletions tests/subsys/settings_commit_prio/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tests:
settings.settings_commit_prio:
platform_allow: native_sim
tags: settings

0 comments on commit ef58f81

Please sign in to comment.