From 5137cc8717ce377512d2f2a3cbe6652b8d712d41 Mon Sep 17 00:00:00 2001 From: SpaceOceanCorp Date: Tue, 10 Nov 2026 17:34:19 -0600 Subject: [PATCH] Fix #442, insert a transaction that outranks every queued one at the front CF_InsertSortPrio searches the queue from the back for the last transaction whose priority is equal or higher (lower or equal number) and inserts after it. When there is none, the new transaction outranks every queued one, but it was inserted at the back. Insert it at the front instead. Adds CF_CList_InsertFront_Ex and a UT handler for CF_CList_InsertFront; the unit test for this case now expects the front insert. --- fsw/src/cf_utils.c | 3 ++- fsw/src/cf_utils.h | 6 +++++ unit-test/cf_utils_tests.c | 41 ++++++++++++++++------------- unit-test/stubs/cf_clist_handlers.c | 17 ++++++++++++ unit-test/stubs/cf_clist_stubs.c | 3 ++- unit-test/utilities/cf_test_utils.h | 6 +++++ 6 files changed, 56 insertions(+), 20 deletions(-) diff --git a/fsw/src/cf_utils.c b/fsw/src/cf_utils.c index c315dac6..d4d1b9ef 100644 --- a/fsw/src/cf_utils.c +++ b/fsw/src/cf_utils.c @@ -436,7 +436,8 @@ void CF_InsertSortPrio(CF_Transaction_t *txn, CF_QueueIdx_t queue) } else { - insert_back = true; + /* every queued transaction has a lower priority (higher number), so this one goes first */ + CF_CList_InsertFront_Ex(chan, queue, &txn->cl_node); } } diff --git a/fsw/src/cf_utils.h b/fsw/src/cf_utils.h index 476b26d8..f56d526b 100644 --- a/fsw/src/cf_utils.h +++ b/fsw/src/cf_utils.h @@ -185,6 +185,12 @@ static inline void CF_CList_InsertBack_Ex(CF_Channel_t *chan, CF_QueueIdx_t queu ++chan->stat.q_size[queueidx]; } +static inline void CF_CList_InsertFront_Ex(CF_Channel_t *chan, CF_QueueIdx_t queueidx, CF_CListNode_t *node) +{ + CF_CList_InsertFront(&chan->qs[queueidx], node); + ++chan->stat.q_size[queueidx]; +} + /************************************************************************/ /** @brief Find an unused transaction on a channel. * diff --git a/unit-test/cf_utils_tests.c b/unit-test/cf_utils_tests.c index 79642487..029f5fae 100644 --- a/unit-test/cf_utils_tests.c +++ b/unit-test/cf_utils_tests.c @@ -821,7 +821,7 @@ void Test_CF_InsertSortPrio_Call_CF_CList_InsertAfter_Ex_AndSet_q_index_To_q(voi arg_q); } -void Test_CF_InsertSortPrio_When_p_t_Is_NULL_Call_CF_CList_InsertBack_Ex(void) +void Test_CF_InsertSortPrio_When_No_Equal_Or_Higher_Priority_Found_Call_CF_CList_InsertFront_Ex(void) { /* Arrange */ CF_Transaction_t txn; @@ -832,11 +832,11 @@ void Test_CF_InsertSortPrio_When_p_t_Is_NULL_Call_CF_CList_InsertBack_Ex(void) CF_Channel_t *chan = CF_GetChannelPtr(chan_num); CF_CListNode_t *expected_end; CF_CListFn_t expected_fn; - CF_CListNode_t **expected_insert_back_head; - CF_CListNode_t *expected_insert_back_node; + CF_CListNode_t **expected_insert_front_head; + CF_CListNode_t *expected_insert_front_node; - CF_CList_Traverse_R_context_t context_cf_clist_traverse_r; - CF_CList_InsertBack_context_t context_clist_insert_back; + CF_CList_Traverse_R_context_t context_cf_clist_traverse_r; + CF_CList_InsertFront_context_t context_clist_insert_front; UT_SetDataBuffer(UT_KEY(CF_CList_Traverse_R), &context_cf_clist_traverse_r, @@ -851,17 +851,21 @@ void Test_CF_InsertSortPrio_When_p_t_Is_NULL_Call_CF_CList_InsertBack_Ex(void) * &qs makes the list NOT empty */ chan->qs[arg_q] = (CF_CListNode_t *)&qs; - /* setup CF_Traverse_PriorityArg_t altered value */ + /* No queued transaction has an equal or higher priority (lower or equal number), so the + * search finds nothing and the new transaction must go to the front of the queue (#442) */ context_cf_clist_traverse_r.context_t = NULL; /* set expected values */ - expected_end = chan->qs[arg_q]; - expected_fn = CF_PrioSearch; - expected_insert_back_head = &chan->qs[arg_q]; - expected_insert_back_node = &arg_t->cl_node; - - /* Arrange for CF_CList_InsertBack_Ex */ - UT_SetDataBuffer(UT_KEY(CF_CList_InsertBack), &context_clist_insert_back, sizeof(context_clist_insert_back), false); + expected_end = chan->qs[arg_q]; + expected_fn = CF_PrioSearch; + expected_insert_front_head = &chan->qs[arg_q]; + expected_insert_front_node = &arg_t->cl_node; + + /* Arrange for CF_CList_InsertFront_Ex */ + UT_SetDataBuffer(UT_KEY(CF_CList_InsertFront), + &context_clist_insert_front, + sizeof(context_clist_insert_front), + false); /* Act */ CF_InsertSortPrio(arg_t, arg_q); @@ -871,9 +875,10 @@ void Test_CF_InsertSortPrio_When_p_t_Is_NULL_Call_CF_CList_InsertBack_Ex(void) UtAssert_ADDRESS_EQ(context_cf_clist_traverse_r.end, expected_end); UtAssert_True(context_cf_clist_traverse_r.fn == expected_fn, "context_cf_clist_traverse_r.fn == expected_fn"); UtAssert_STUB_COUNT(CF_CList_InsertAfter, 0); - UtAssert_STUB_COUNT(CF_CList_InsertBack, 1); - UtAssert_ADDRESS_EQ(context_clist_insert_back.head, expected_insert_back_head); - UtAssert_ADDRESS_EQ(context_clist_insert_back.node, expected_insert_back_node); + UtAssert_STUB_COUNT(CF_CList_InsertBack, 0); + UtAssert_STUB_COUNT(CF_CList_InsertFront, 1); + UtAssert_ADDRESS_EQ(context_clist_insert_front.head, expected_insert_front_head); + UtAssert_ADDRESS_EQ(context_clist_insert_front.node, expected_insert_front_node); UtAssert_True(arg_t->flags.com.q_index == arg_q, "txn->flags.com.q_index is %u and should be %u (q)", arg_t->flags.com.q_index, @@ -1331,10 +1336,10 @@ void add_CF_InsertSortPrio_tests(void) cf_utils_tests_Setup, cf_utils_tests_Teardown, "Test_CF_InsertSortPrio_Call_CF_CList_InsertAfter_Ex_AndSet_q_index_To_q"); - UtTest_Add(Test_CF_InsertSortPrio_When_p_t_Is_NULL_Call_CF_CList_InsertBack_Ex, + UtTest_Add(Test_CF_InsertSortPrio_When_No_Equal_Or_Higher_Priority_Found_Call_CF_CList_InsertFront_Ex, cf_utils_tests_Setup, cf_utils_tests_Teardown, - "Test_CF_InsertSortPrio_When_p_t_Is_NULL_Call_CF_CList_InsertBack_Ex"); + "Test_CF_InsertSortPrio_When_No_Equal_Or_Higher_Priority_Found_Call_CF_CList_InsertFront_Ex"); } void add_CF_TraverseAllTransactions_Impl_tests(void) diff --git a/unit-test/stubs/cf_clist_handlers.c b/unit-test/stubs/cf_clist_handlers.c index 8ed1928b..ba7e8854 100644 --- a/unit-test/stubs/cf_clist_handlers.c +++ b/unit-test/stubs/cf_clist_handlers.c @@ -72,6 +72,23 @@ void UT_DefaultHandler_CF_CList_InsertBack(void *UserObj, UT_EntryKey_t FuncKey, } } +/*---------------------------------------------------------------- + * + * For compatibility with other tests, this has a mechanism to save its + * arguments to a test-provided context capture buffer. + * + *-----------------------------------------------------------------*/ +void UT_DefaultHandler_CF_CList_InsertFront(void *UserObj, UT_EntryKey_t FuncKey, const UT_StubContext_t *Context) +{ + CF_CList_InsertFront_context_t *ctxt = UT_CF_GetContextBuffer(FuncKey, CF_CList_InsertFront_context_t); + + if (ctxt) + { + ctxt->head = UT_Hook_GetArgValueByName(Context, "head", CF_CListNode_t **); + ctxt->node = UT_Hook_GetArgValueByName(Context, "node", CF_CListNode_t *); + } +} + /*---------------------------------------------------------------- * * For compatibility with other tests, this has a mechanism to save its diff --git a/unit-test/stubs/cf_clist_stubs.c b/unit-test/stubs/cf_clist_stubs.c index b1a037b1..7a6c145d 100644 --- a/unit-test/stubs/cf_clist_stubs.c +++ b/unit-test/stubs/cf_clist_stubs.c @@ -29,6 +29,7 @@ void UT_DefaultHandler_CF_CList_InitNode(void *, UT_EntryKey_t, const UT_StubContext_t *); void UT_DefaultHandler_CF_CList_InsertAfter(void *, UT_EntryKey_t, const UT_StubContext_t *); void UT_DefaultHandler_CF_CList_InsertBack(void *, UT_EntryKey_t, const UT_StubContext_t *); +void UT_DefaultHandler_CF_CList_InsertFront(void *, UT_EntryKey_t, const UT_StubContext_t *); void UT_DefaultHandler_CF_CList_Remove(void *, UT_EntryKey_t, const UT_StubContext_t *); void UT_DefaultHandler_CF_CList_Traverse(void *, UT_EntryKey_t, const UT_StubContext_t *); void UT_DefaultHandler_CF_CList_Traverse_R(void *, UT_EntryKey_t, const UT_StubContext_t *); @@ -82,7 +83,7 @@ void CF_CList_InsertFront(CF_CListNode_t **head, CF_CListNode_t *node) UT_GenStub_AddParam(CF_CList_InsertFront, CF_CListNode_t **, head); UT_GenStub_AddParam(CF_CList_InsertFront, CF_CListNode_t *, node); - UT_GenStub_Execute(CF_CList_InsertFront, Basic, NULL); + UT_GenStub_Execute(CF_CList_InsertFront, Basic, UT_DefaultHandler_CF_CList_InsertFront); } /* diff --git a/unit-test/utilities/cf_test_utils.h b/unit-test/utilities/cf_test_utils.h index e946bd07..a66841cf 100644 --- a/unit-test/utilities/cf_test_utils.h +++ b/unit-test/utilities/cf_test_utils.h @@ -155,6 +155,12 @@ typedef struct CF_CListNode_t *node; } CF_CList_InsertBack_context_t; +typedef struct +{ + CF_CListNode_t **head; + CF_CListNode_t *node; +} CF_CList_InsertFront_context_t; + typedef struct { CF_CListNode_t **head;