]> begriffs open source - cmsis-freertos/blob - Test/CMock/queue/generic/queue_send_blocking_utest.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Test / CMock / queue / generic / queue_send_blocking_utest.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 /*! @file queue_send_blocking_utest.c */
27
28 /* C runtime includes. */
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <string.h>
32
33 #include "../queue_utest_common.h"
34
35 /* Queue includes */
36 #include "FreeRTOS.h"
37 #include "FreeRTOSConfig.h"
38 #include "queue.h"
39 #include "mock_fake_port.h"
40
41 /* ============================  GLOBAL VARIABLES =========================== */
42
43 /* Used to share a QueueHandle_t between a test case and it's callbacks */
44 static QueueHandle_t xQueueHandleStatic;
45
46 /* ==========================  CALLBACK FUNCTIONS =========================== */
47
48 /* ============================= Unity Fixtures ============================= */
49
50 void setUp( void )
51 {
52     commonSetUp();
53     vFakePortAssertIfInterruptPriorityInvalid_Ignore();
54     xQueueHandleStatic = NULL;
55 }
56
57 void tearDown( void )
58 {
59     commonTearDown();
60 }
61
62 void suiteSetUp()
63 {
64     commonSuiteSetUp();
65 }
66
67 int suiteTearDown( int numFailures )
68 {
69     return commonSuiteTearDown( numFailures );
70 }
71
72 /* ==========================  Helper functions =========================== */
73
74 /* =============================  Test Cases ============================== */
75
76 /**
77  *  @brief Callback for test_macro_xQueueSend_blocking_success which empties it's test queue.
78  */
79 static BaseType_t xQueueSend_locked_xTaskCheckForTimeOutCB( TimeOut_t * const pxTimeOut,
80                                                             TickType_t * const pxTicksToWait,
81                                                             int cmock_num_calls )
82 {
83     BaseType_t xReturnValue = td_task_xTaskCheckForTimeOutStub( pxTimeOut, pxTicksToWait, cmock_num_calls );
84
85     if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
86     {
87         uint32_t checkVal = INVALID_UINT32;
88         ( void ) xQueueReceiveFromISR( xQueueHandleStatic, &checkVal, NULL );
89         TEST_ASSERT_EQUAL( getLastMonotonicTestValue(), checkVal );
90     }
91
92     return xReturnValue;
93 }
94
95 /**
96  *  @brief Test xQueueSend with timeout=10 on a full queue which is locked and
97  *  has no tasks on it's WaitingToSend event list.
98  *  @coverage prvUnlockQueue
99  */
100 void test_macro_xQueueSend_blocking_success_locked_no_pending( void )
101 {
102     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
103
104     /* Export for callbacks */
105     xQueueHandleStatic = xQueue;
106
107     uint32_t testVal = getNextMonotonicTestValue();
108
109     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
110
111     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal, 0 ) );
112
113     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
114
115     xTaskCheckForTimeOut_Stub( &xQueueSend_locked_xTaskCheckForTimeOutCB );
116     xTaskResumeAll_Stub( &td_task_xTaskResumeAllStub );
117
118     uint32_t testVal2 = getLastMonotonicTestValue() + 12345;
119
120     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal2, TICKS_TO_WAIT ) );
121
122     TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
123
124     /* Check that vPortYieldWithinAPI was called and would have yielded */
125     TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
126
127     /* Check that vTaskMissedYield was not called */
128     TEST_ASSERT_EQUAL( 0, td_task_getCount_vTaskMissedYield() );
129
130     vQueueDelete( xQueue );
131 }
132
133 /**
134  *  @brief Callback used for xQueueSend blocking_locked tests.
135  */
136 static BaseType_t xQueueSend_xTaskResumeAllCallback( int cmock_num_calls )
137 {
138     BaseType_t xReturnValue = td_task_xTaskResumeAllStub( cmock_num_calls );
139
140     /* If td_task_xTaskResumeAllStub returns pdTRUE, a higher priority task is pending
141      * Send from an ISR to block */
142     if( pdTRUE == xReturnValue )
143     {
144         if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
145         {
146             uint32_t testVal = getNextMonotonicTestValue();
147             ( void ) xQueueSendFromISR( xQueueHandleStatic, &testVal, NULL );
148         }
149     }
150
151     return xReturnValue;
152 }
153
154 /**
155  *  @brief Test xQueueSend with timeout=10 on a full queue which is locked and
156  *  has a higher priority task on it's WaitingToSend event list.
157  *  @coverage prvUnlockQueue
158  */
159 void test_macro_xQueueSend_blocking_fail_locked_high_prio_pending( void )
160 {
161     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
162
163     /* Export for callbacks */
164     xQueueHandleStatic = xQueue;
165
166     uint32_t testVal = getNextMonotonicTestValue();
167
168     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal, 0 ) );
169
170     xTaskCheckForTimeOut_Stub( &xQueueSend_locked_xTaskCheckForTimeOutCB );
171     xTaskResumeAll_Stub( &xQueueSend_xTaskResumeAllCallback );
172
173     /* this task is lower priority than the pending task */
174     td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
175
176     td_task_addFakeTaskWaitingToSendToQueue( xQueue );
177
178     uint32_t testVal2 = getLastMonotonicTestValue() + 12345;
179
180     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
181
182     TEST_ASSERT_EQUAL( pdFALSE, xQueueSend( xQueue, &testVal2, TICKS_TO_WAIT ) );
183
184     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
185
186     TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
187
188     /* Check that xTaskResumeAll was called and would have yielded */
189     TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT + 1, td_task_getCount_YieldFromTaskResumeAll() );
190
191     /* Check that vPortYieldWithinAPI was called and would have yielded */
192     TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT - 1, td_task_getCount_vPortYieldWithinAPI() );
193
194     /* Check that vTaskMissedYield was called */
195     TEST_ASSERT_EQUAL( 1, td_task_getCount_vTaskMissedYield() );
196
197     vQueueDelete( xQueue );
198 }
199
200 /**
201  *  @brief Test xQueueSend with timeout=10 on a full queue which is locked and
202  *  has a lower priority task on it's WaitingToSend event list.
203  *  @coverage prvUnlockQueue
204  */
205 void test_macro_xQueueSend_blocking_success_locked_low_prio_pending( void )
206 {
207     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
208
209     /* Export for callbacks */
210     xQueueHandleStatic = xQueue;
211
212     uint32_t testVal = getNextMonotonicTestValue();
213
214     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal, 0 ) );
215
216     xTaskCheckForTimeOut_Stub( &xQueueSend_locked_xTaskCheckForTimeOutCB );
217     xTaskResumeAll_Stub( &xQueueSend_xTaskResumeAllCallback );
218
219     /* The pending task is lower priority */
220     td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
221
222     td_task_addFakeTaskWaitingToSendToQueue( xQueue );
223
224     uint32_t testVal2 = getLastMonotonicTestValue() + 12345;
225
226     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
227
228     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal2, TICKS_TO_WAIT ) );
229
230     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
231
232     TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
233
234     /* Check that vPortYieldWithinAPI was called and would have yielded */
235     TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
236
237     /* Check that vTaskMissedYield was not called */
238     TEST_ASSERT_EQUAL( 0, td_task_getCount_vTaskMissedYield() );
239
240     vQueueDelete( xQueue );
241 }
242
243 /**
244  *  @brief Test xQueueSend with taskSCHEDULER_SUSPENDED and timeout=10
245  *  @details This should cause xQueueSend to configASSERT because it would
246  *  block forever when the queue is full.
247  * @coverage xQueueGenericSend
248  */
249 void test_macro_xQueueSend_blocking_suspended_assert( void )
250 {
251     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
252
253     uint32_t testVal = getNextMonotonicTestValue();
254
255     fakeAssertExpectFail();
256
257     td_task_setSchedulerState( taskSCHEDULER_SUSPENDED );
258
259     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
260
261     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal, 10 ) );
262
263     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
264
265     TEST_ASSERT_EQUAL( pdTRUE, fakeAssertGetFlagAndClear() );
266
267     td_task_setSchedulerState( taskSCHEDULER_RUNNING );
268
269     vQueueDelete( xQueue );
270 }
271
272 /**
273  *  @brief Test xQueueSend with taskSCHEDULER_RUNNING and timeout=10 on a full queue.
274  *  @coverage xQueueGenericSend
275  */
276 void test_macro_xQueueSend_blocking_timeout( void )
277 {
278     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
279
280     uint32_t testVal = getNextMonotonicTestValue();
281
282     /* Fill up the queue */
283     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal, 0 ) );
284
285     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
286
287     TEST_ASSERT_EQUAL( errQUEUE_FULL, xQueueSend( xQueue, &testVal, TICKS_TO_WAIT ) );
288
289     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
290
291     TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
292
293     /* Check that vPortYieldWithinAPI was called and would have yielded */
294     TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
295
296     /* Check that vTaskMissedYield was not called */
297     TEST_ASSERT_EQUAL( 0, td_task_getCount_vTaskMissedYield() );
298
299     vQueueDelete( xQueue );
300 }
301
302 /**
303  * @brief Test xQueueSend in blocking mode with a full locked queue.
304  * @details This test case verifies a situation that should never occur ( xQueueSend called on a locked queue ).
305  * @coverage xQueueGenericSend
306  */
307 void test_macro_xQueueSend_blocking_locked( void )
308 {
309     /* Create a new binary Queue */
310     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
311
312     uint32_t testVal1 = getNextMonotonicTestValue();
313
314     /* Fill the queue */
315     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, &testVal1, TICKS_TO_WAIT ) );
316
317     /* Set private lock counters */
318     vSetQueueRxLock( xQueue, queueLOCKED_UNMODIFIED );
319     vSetQueueTxLock( xQueue, queueLOCKED_UNMODIFIED );
320
321     uint32_t testVal2 = getNextMonotonicTestValue();
322
323     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
324
325     /* Call xQueueSend in blocking mode with the queue locked */
326     TEST_ASSERT_EQUAL( pdFALSE, xQueueSend( xQueue, &testVal2, TICKS_TO_WAIT ) );
327
328     TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xQueue ) );
329
330     TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
331
332     TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
333
334     /* Verify that the queue is now unlocked */
335     TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueRxLock( xQueue ) );
336     TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueTxLock( xQueue ) );
337
338     vQueueDelete( xQueue );
339 }