LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/util/fd_group.c - fd_group_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 89 91 97.8 %
Date: 2024-12-09 16:12:14 Functions: 3 4 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 33 66 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2024 Samsung Electronics Co., Ltd.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : #include "spdk_internal/cunit.h"
       8                 :            : #include "util/fd_group.c"
       9                 :            : 
      10                 :            : static int
      11                 :          0 : fd_group_cb_fn(void *ctx)
      12                 :            : {
      13                 :          0 :         return 0;
      14                 :            : }
      15                 :            : 
      16                 :            : static void
      17                 :          3 : test_fd_group_basic(void)
      18                 :            : {
      19                 :          3 :         struct spdk_fd_group *fgrp;
      20                 :          3 :         struct event_handler *ehdlr = NULL;
      21                 :            :         int fd;
      22                 :            :         int rc;
      23                 :          3 :         int cb_arg;
      24                 :            : 
      25                 :          3 :         rc = spdk_fd_group_create(&fgrp);
      26         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      27                 :            : 
      28                 :          3 :         fd = epoll_create1(0);
      29         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(fd >= 0);
      30                 :            : 
      31                 :          3 :         rc = SPDK_FD_GROUP_ADD(fgrp, fd, fd_group_cb_fn, &cb_arg);
      32         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      33         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(fgrp->num_fds == 1);
      34                 :            : 
      35                 :            :         /* Verify that event handler is initialized correctly */
      36                 :          3 :         ehdlr = TAILQ_FIRST(&fgrp->event_handlers);
      37         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(ehdlr != NULL);
      38                 :          3 :         CU_ASSERT(ehdlr->fd == fd);
      39                 :          3 :         CU_ASSERT(ehdlr->state == EVENT_HANDLER_STATE_WAITING);
      40                 :          3 :         CU_ASSERT(ehdlr->events == EPOLLIN);
      41                 :            : 
      42                 :            :         /* Modify event type and see if event handler is updated correctly */
      43                 :          3 :         rc = spdk_fd_group_event_modify(fgrp, fd, EPOLLIN | EPOLLERR);
      44         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      45                 :            : 
      46                 :          3 :         ehdlr = TAILQ_FIRST(&fgrp->event_handlers);
      47         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(ehdlr != NULL);
      48                 :          3 :         CU_ASSERT(ehdlr->events == (EPOLLIN | EPOLLERR));
      49                 :            : 
      50                 :          3 :         spdk_fd_group_remove(fgrp, fd);
      51         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(fgrp->num_fds == 0);
      52                 :            : 
      53                 :          3 :         rc = close(fd);
      54                 :          3 :         CU_ASSERT(rc == 0);
      55                 :            : 
      56                 :          3 :         spdk_fd_group_destroy(fgrp);
      57                 :          3 : }
      58                 :            : 
      59                 :            : static void
      60                 :          3 : test_fd_group_nest_unnest(void)
      61                 :            : {
      62                 :          3 :         struct spdk_fd_group *parent, *child, *not_parent;
      63                 :            :         int fd_parent, fd_child, fd_child_2;
      64                 :            :         int rc;
      65                 :          3 :         int cb_arg;
      66                 :            : 
      67                 :          3 :         rc = spdk_fd_group_create(&parent);
      68         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      69                 :            : 
      70                 :          3 :         rc = spdk_fd_group_create(&child);
      71         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      72                 :            : 
      73                 :          3 :         rc = spdk_fd_group_create(&not_parent);
      74         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      75                 :            : 
      76                 :          3 :         fd_parent = epoll_create1(0);
      77         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(fd_parent >= 0);
      78                 :            : 
      79                 :          3 :         fd_child = epoll_create1(0);
      80         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(fd_child >= 0);
      81                 :            : 
      82                 :          3 :         fd_child_2 = epoll_create1(0);
      83         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(fd_child_2 >= 0);
      84                 :            : 
      85                 :          3 :         rc = SPDK_FD_GROUP_ADD(parent, fd_parent, fd_group_cb_fn, &cb_arg);
      86         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      87         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(parent->num_fds == 1);
      88                 :            : 
      89                 :          3 :         rc = SPDK_FD_GROUP_ADD(child, fd_child, fd_group_cb_fn, &cb_arg);
      90         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      91         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->num_fds == 1);
      92                 :            : 
      93                 :            :         /* Nest child fd group to a parent fd group and verify their relation */
      94                 :          3 :         rc = spdk_fd_group_nest(parent, child);
      95         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
      96         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->parent == parent);
      97         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(parent->num_fds == 2);
      98         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->num_fds == 0);
      99                 :            : 
     100                 :            :         /* Register second child fd to the child fd group and verify that the parent fd group
     101                 :            :          * has the correct number of fds.
     102                 :            :          */
     103                 :          3 :         rc = SPDK_FD_GROUP_ADD(child, fd_child_2, fd_group_cb_fn, &cb_arg);
     104         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
     105         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->num_fds == 0);
     106         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(parent->num_fds == 3);
     107                 :            : 
     108                 :            :         /* Unnest child fd group from wrong parent fd group and verify that it fails. */
     109                 :          3 :         rc = spdk_fd_group_unnest(not_parent, child);
     110         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == -EINVAL);
     111                 :            : 
     112                 :            :         /* Unnest child fd group from its parent fd group and verify it. */
     113                 :          3 :         rc = spdk_fd_group_unnest(parent, child);
     114         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(rc == 0);
     115         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->parent == NULL);
     116         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(parent->num_fds == 1);
     117         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->num_fds == 2);
     118                 :            : 
     119                 :          3 :         spdk_fd_group_remove(child, fd_child);
     120         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->num_fds == 1);
     121                 :            : 
     122                 :          3 :         spdk_fd_group_remove(child, fd_child_2);
     123         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(child->num_fds == 0);
     124                 :            : 
     125                 :          3 :         spdk_fd_group_remove(parent, fd_parent);
     126         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(parent->num_fds == 0);
     127                 :            : 
     128                 :          3 :         rc = close(fd_child);
     129                 :          3 :         CU_ASSERT(rc == 0);
     130                 :            : 
     131                 :          3 :         rc = close(fd_child_2);
     132                 :          3 :         CU_ASSERT(rc == 0);
     133                 :            : 
     134                 :          3 :         rc = close(fd_parent);
     135                 :          3 :         CU_ASSERT(rc == 0);
     136                 :            : 
     137                 :          3 :         spdk_fd_group_destroy(child);
     138                 :          3 :         spdk_fd_group_destroy(parent);
     139                 :          3 :         spdk_fd_group_destroy(not_parent);
     140                 :          3 : }
     141                 :            : 
     142                 :            : int
     143                 :          3 : main(int argc, char **argv)
     144                 :            : {
     145                 :          3 :         CU_pSuite       suite = NULL;
     146                 :            :         unsigned int    num_failures;
     147                 :            : 
     148                 :          3 :         CU_initialize_registry();
     149                 :            : 
     150                 :          3 :         suite = CU_add_suite("fd_group", NULL, NULL);
     151                 :            : 
     152                 :          3 :         CU_ADD_TEST(suite, test_fd_group_basic);
     153                 :          3 :         CU_ADD_TEST(suite, test_fd_group_nest_unnest);
     154                 :            : 
     155                 :          3 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
     156                 :            : 
     157                 :          3 :         CU_cleanup_registry();
     158                 :            : 
     159                 :          3 :         return num_failures;
     160                 :            : }

Generated by: LCOV version 1.14