LCOV - code coverage report
Current view: top level - app/test - test_alarm.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 20 20 100.0 %
Date: 2025-03-01 20:23:48 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 12 22 54.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdbool.h>
       7                 :            : #include <stdint.h>
       8                 :            : 
       9                 :            : #include <rte_alarm.h>
      10                 :            : #include <rte_atomic.h>
      11                 :            : #include <rte_common.h>
      12                 :            : #include <rte_cycles.h>
      13                 :            : #include <rte_errno.h>
      14                 :            : 
      15                 :            : #include "test.h"
      16                 :            : 
      17                 :            : #ifndef US_PER_SEC
      18                 :            : #define US_PER_SEC      1000000u
      19                 :            : #endif
      20                 :            : 
      21                 :            : static void
      22                 :          1 : test_alarm_callback(void *cb_arg)
      23                 :            : {
      24                 :            :         RTE_ATOMIC(bool) *triggered = cb_arg;
      25                 :            : 
      26                 :          1 :         rte_atomic_store_explicit(triggered, 1, rte_memory_order_release);
      27                 :          1 : }
      28                 :            : 
      29                 :            : 
      30                 :            : static int
      31                 :          1 : test_alarm(void)
      32                 :            : {
      33                 :          1 :         RTE_ATOMIC(bool) triggered = false;
      34                 :          1 :         RTE_ATOMIC(bool) later = false;
      35                 :            :         int ret;
      36                 :            : 
      37                 :            :         /* check if it will fail to set alarm with wrong us values */
      38         [ -  + ]:          1 :         TEST_ASSERT_FAIL(rte_eal_alarm_set(0, test_alarm_callback, NULL),
      39                 :            :                          "Expected rte_eal_alarm_set to fail with 0 us value");
      40                 :            : 
      41                 :            :         /* check it if will fail with a very large timeout value */
      42         [ -  + ]:          1 :         TEST_ASSERT_FAIL(rte_eal_alarm_set(UINT64_MAX - 1, test_alarm_callback, NULL),
      43                 :            :                          "Expected rte_eal_alarm_set to fail with (UINT64_MAX-1) us value");
      44                 :            : 
      45                 :            :         /* check if it will fail to set alarm with null callback parameter */
      46         [ -  + ]:          1 :         TEST_ASSERT_FAIL(rte_eal_alarm_set(US_PER_SEC, NULL, NULL),
      47                 :            :                          "Expected rte_eal_alarm_set to fail with null callback parameter");
      48                 :            : 
      49                 :            :         /* check if it will fail to remove alarm with null callback parameter */
      50         [ -  + ]:          1 :         TEST_ASSERT_FAIL(rte_eal_alarm_cancel(NULL, NULL),
      51                 :            :                          "Expected rte_eal_alarm_cancel to fail with null callback parameter");
      52                 :            : 
      53                 :            :         /* check if can set a alarm for one second */
      54         [ -  + ]:          1 :         TEST_ASSERT_SUCCESS(rte_eal_alarm_set(US_PER_SEC, test_alarm_callback, &triggered),
      55                 :            :                             "Setting one second alarm failed");
      56                 :            : 
      57                 :            :         /* set a longer alarm that will be canceled. */
      58         [ -  + ]:          1 :         TEST_ASSERT_SUCCESS(rte_eal_alarm_set(10 * US_PER_SEC, test_alarm_callback, &later),
      59                 :            :                             "Setting ten second alarm failed");
      60                 :            : 
      61                 :            :         /* wait for alarm to happen */
      62         [ +  + ]:         11 :         while (rte_atomic_load_explicit(&triggered, rte_memory_order_acquire) == false)
      63                 :         10 :                 rte_delay_us_sleep(US_PER_SEC / 10);
      64                 :            : 
      65         [ -  + ]:          1 :         TEST_ASSERT(!rte_atomic_load_explicit(&later, rte_memory_order_acquire),
      66                 :            :                     "Only one alarm should have fired.");
      67                 :            : 
      68                 :          1 :         ret = rte_eal_alarm_cancel(test_alarm_callback, &triggered);
      69   [ +  -  -  + ]:          1 :         TEST_ASSERT(ret == 0 && rte_errno == ENOENT,
      70                 :            :                     "Canceling alarm after run ret %d: %s", ret, rte_strerror(rte_errno));
      71                 :            : 
      72                 :          1 :         ret = rte_eal_alarm_cancel(test_alarm_callback, &later);
      73         [ -  + ]:          1 :         TEST_ASSERT(ret == 1, "Canceling ten second alarm failed %d: %s",
      74                 :            :                     ret, rte_strerror(rte_errno));
      75                 :            : 
      76                 :            :         return 0;
      77                 :            : }
      78                 :            : 
      79                 :        252 : REGISTER_FAST_TEST(alarm_autotest, true, true, test_alarm);

Generated by: LCOV version 1.14