1 /*
2  * Copyright (c) 2024 Laczen
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <zephyr/settings/settings.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/logging/log.h>
11 
12 LOG_MODULE_REGISTER(test);
13 
14 static int prio;
15 
commit0(void)16 int commit0(void)
17 {
18 	LOG_INF("%s Called", __func__);
19 	zassert_equal(prio, 0, "Bad commit order");
20 	prio++;
21 	return 0;
22 }
23 
commit1(void)24 int commit1(void)
25 {
26 	LOG_INF("%s Called", __func__);
27 	zassert_equal(prio, 1, "Bad commit order");
28 	prio++;
29 	return 0;
30 }
31 
commit2(void)32 int commit2(void)
33 {
34 	LOG_INF("%s Called", __func__);
35 	zassert_equal(prio, 2, "Bad commit order");
36 	prio++;
37 	return 0;
38 }
39 
commit3(void)40 int commit3(void)
41 {
42 	LOG_INF("%s Called", __func__);
43 	zassert_equal(prio, 3, "Bad commit order");
44 	prio++;
45 	return 0;
46 }
47 
commit5(void)48 int commit5(void)
49 {
50 	LOG_INF("%s Called", __func__);
51 	zassert_equal(prio, 0, "Bad commit order");
52 	return 0;
53 }
54 
55 SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(h0, "h0", NULL, NULL, commit0, NULL, 0);
56 SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(h2, "h2", NULL, NULL, commit2, NULL, 2);
57 
58 static struct settings_handler h1 = {
59 	.name = "h1",
60 	.h_commit = commit1,
61 };
62 
63 static struct settings_handler h3 = {
64 	.name = "h3",
65 	.h_commit = commit3,
66 };
67 
68 SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(h5, "h5", NULL, NULL, commit5, NULL, -1);
69 
70 /**
71  * @brief Test Settings commit order
72  *
73  * This test verifies the settings commit order.
74  */
ZTEST(settings_commit_prio,test_commit_order)75 ZTEST(settings_commit_prio, test_commit_order)
76 {
77 	int rc;
78 
79 	prio = 0;
80 	rc = settings_register_with_cprio(&h1, 1);
81 	zassert_equal(rc, 0, "Failed to register handler");
82 	rc = settings_register_with_cprio(&h3, 3);
83 	zassert_equal(rc, 0, "Failed to register handler");
84 
85 	rc = settings_commit();
86 	zassert_equal(rc, 0, "Commit failed with code [%d]", rc);
87 	zassert_equal(prio, 4, "Incorrect prio level reached [%d]", prio);
88 }
89 
90 ZTEST_SUITE(settings_commit_prio, NULL, NULL, NULL, NULL, NULL);
91