diff --git a/tests/kernel/fifo/fifo_timeout/src/main.c b/tests/kernel/fifo/fifo_timeout/src/main.c index 4d8cf432cde9..f8bc66fcb187 100644 --- a/tests/kernel/fifo/fifo_timeout/src/main.c +++ b/tests/kernel/fifo/fifo_timeout/src/main.c @@ -135,7 +135,8 @@ static void test_thread_pend_and_timeout(void *p1, void *p2, void *p3) static int test_multiple_threads_pending(struct timeout_order_data *test_data, int test_data_size) { - int ii; + int ii, j; + u32_t diff_ms; for (ii = 0; ii < test_data_size; ii++) { tid[ii] = k_thread_create(&ttdata[ii], ttstack[ii], TSTACK_SIZE, @@ -144,6 +145,14 @@ static int test_multiple_threads_pending(struct timeout_order_data *test_data, FIFO_THREAD_PRIO, K_INHERIT_PERMS, 0); } + /* In general, there is no guarantee of wakeup order when multiple + * threads are woken up on the same tick. This can especially happen + * when the system is loaded. However, in this particular test, we + * are controlling the system state and hence we can make a reasonable + * estimation of a timeout occurring with the max deviation of an + * additional tick. Hence the timeout order may slightly be different + * from what we normally expect. + */ for (ii = 0; ii < test_data_size; ii++) { struct timeout_order_data *data = k_fifo_get(&timeout_order_fifo, K_FOREVER); @@ -152,9 +161,31 @@ static int test_multiple_threads_pending(struct timeout_order_data *test_data, TC_PRINT(" thread (q order: %d, t/o: %d, fifo %p)\n", data->q_order, data->timeout, data->fifo); } else { - TC_ERROR(" *** thread %d woke up, expected %d\n", - data->timeout_order, ii); - return TC_FAIL; + /* Get the index of the thread which should have + * actually timed out. + */ + for (j = 0; j < test_data_size; j++) { + if (test_data[j].timeout_order == ii) { + break; + } + } + + if (data->timeout > test_data[j].timeout) { + diff_ms = data->timeout - test_data[j].timeout; + } else { + diff_ms = test_data[j].timeout - data->timeout; + } + + if (_ms_to_ticks(diff_ms) == 1) { + TC_PRINT( + " thread (q order: %d, t/o: %d, fifo %p)\n", + data->q_order, data->timeout, data->fifo); + } else { + TC_ERROR( + " *** thread %d woke up, expected %d\n", + data->timeout_order, ii); + return TC_FAIL; + } } }