Commit 1239ea17 authored by Ankit Kumar's avatar Ankit Kumar Committed by Konrad Sztyber
Browse files

test/unit: add missing fd_group unit tests



Change-Id: I4b8d84ec0777848907e3be45a4771bbb20f7521b
Signed-off-by: default avatarAnkit Kumar <ankit.kumar@samsung.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/25214


Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Community-CI: Community CI Samsung <spdk.community.ci.samsung@gmail.com>
Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
parent 872a40a0
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -9,6 +9,10 @@ include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
DIRS-y = base64.c bit_array.c cpuset.c crc16.c crc32_ieee.c crc32c.c crc64.c dif.c \
	 file.c iov.c math.c net.c pipe.c string.c xor.c

ifeq ($(OS), Linux)
DIRS-y += fd_group.c
endif

.PHONY: all clean $(DIRS-y)

all: $(DIRS-y)
+10 −0
Original line number Diff line number Diff line
#  SPDX-License-Identifier: BSD-3-Clause
#  Copyright (C) 2024 Samsung Electronics Co., Ltd.
#  All rights reserved.
#

SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..)

TEST_FILE = fd_group_ut.c

include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk
+139 −0
Original line number Diff line number Diff line
/*   SPDX-License-Identifier: BSD-3-Clause
 *   Copyright (C) 2024 Samsung Electronics Co., Ltd.
 *   All rights reserved.
 */

#include "spdk/stdinc.h"
#include "spdk_internal/cunit.h"
#include "util/fd_group.c"

static int
fd_group_cb_fn(void *ctx)
{
	return 0;
}

static void
test_fd_group_basic(void)
{
	struct spdk_fd_group *fgrp;
	struct event_handler *ehdlr = NULL;
	int fd;
	int rc;
	int cb_arg;

	rc = spdk_fd_group_create(&fgrp);
	SPDK_CU_ASSERT_FATAL(rc == 0);

	fd = epoll_create1(0);
	SPDK_CU_ASSERT_FATAL(fd >= 0);

	rc = SPDK_FD_GROUP_ADD(fgrp, fd, fd_group_cb_fn, &cb_arg);
	SPDK_CU_ASSERT_FATAL(rc == 0);
	SPDK_CU_ASSERT_FATAL(fgrp->num_fds == 1);

	/* Verify that event handler is initialized correctly */
	ehdlr = TAILQ_FIRST(&fgrp->event_handlers);
	SPDK_CU_ASSERT_FATAL(ehdlr != NULL);
	CU_ASSERT(ehdlr->fd == fd);
	CU_ASSERT(ehdlr->state == EVENT_HANDLER_STATE_WAITING);
	CU_ASSERT(ehdlr->events == EPOLLIN);

	/* Modify event type and see if event handler is updated correctly */
	rc = spdk_fd_group_event_modify(fgrp, fd, EPOLLIN | EPOLLERR);
	SPDK_CU_ASSERT_FATAL(rc == 0);

	ehdlr = TAILQ_FIRST(&fgrp->event_handlers);
	SPDK_CU_ASSERT_FATAL(ehdlr != NULL);
	CU_ASSERT(ehdlr->events == (EPOLLIN | EPOLLERR));

	spdk_fd_group_remove(fgrp, fd);
	SPDK_CU_ASSERT_FATAL(fgrp->num_fds == 0);

	rc = close(fd);
	CU_ASSERT(rc == 0);

	spdk_fd_group_destroy(fgrp);
}

static void
test_fd_group_nest_unnest(void)
{
	struct spdk_fd_group *parent, *child, *not_parent;
	int fd_parent, fd_child;
	int rc;
	int cb_arg;

	rc = spdk_fd_group_create(&parent);
	SPDK_CU_ASSERT_FATAL(rc == 0);

	rc = spdk_fd_group_create(&child);
	SPDK_CU_ASSERT_FATAL(rc == 0);

	rc = spdk_fd_group_create(&not_parent);
	SPDK_CU_ASSERT_FATAL(rc == 0);

	fd_parent = epoll_create1(0);
	SPDK_CU_ASSERT_FATAL(fd_parent >= 0);

	fd_child = epoll_create1(0);
	SPDK_CU_ASSERT_FATAL(fd_child >= 0);

	rc = SPDK_FD_GROUP_ADD(parent, fd_parent, fd_group_cb_fn, &cb_arg);
	SPDK_CU_ASSERT_FATAL(rc == 0);
	SPDK_CU_ASSERT_FATAL(parent->num_fds == 1);

	rc = SPDK_FD_GROUP_ADD(child, fd_child, fd_group_cb_fn, &cb_arg);
	SPDK_CU_ASSERT_FATAL(rc == 0);
	SPDK_CU_ASSERT_FATAL(child->num_fds == 1);

	/* Nest child fd group to a parent fd group and verify their relation */
	rc = spdk_fd_group_nest(parent, child);
	SPDK_CU_ASSERT_FATAL(rc == 0);
	SPDK_CU_ASSERT_FATAL(child->parent == parent);

	/* Unnest child fd group from wrong parent fd group and verify that it fails. */
	rc = spdk_fd_group_unnest(not_parent, child);
	SPDK_CU_ASSERT_FATAL(rc == -EINVAL);

	/* Unnest child fd group from its parent fd group and verify it. */
	rc = spdk_fd_group_unnest(parent, child);
	SPDK_CU_ASSERT_FATAL(rc == 0);
	SPDK_CU_ASSERT_FATAL(child->parent == NULL);

	spdk_fd_group_remove(child, fd_child);
	SPDK_CU_ASSERT_FATAL(child->num_fds == 0);

	spdk_fd_group_remove(parent, fd_parent);
	SPDK_CU_ASSERT_FATAL(parent->num_fds == 0);

	rc = close(fd_child);
	CU_ASSERT(rc == 0);

	rc = close(fd_parent);
	CU_ASSERT(rc == 0);

	spdk_fd_group_destroy(child);
	spdk_fd_group_destroy(parent);
	spdk_fd_group_destroy(not_parent);
}

int
main(int argc, char **argv)
{
	CU_pSuite	suite = NULL;
	unsigned int	num_failures;

	CU_initialize_registry();

	suite = CU_add_suite("fd_group", NULL, NULL);

	CU_ADD_TEST(suite, test_fd_group_basic);
	CU_ADD_TEST(suite, test_fd_group_nest_unnest);

	num_failures = spdk_ut_run_tests(argc, argv, NULL);

	CU_cleanup_registry();

	return num_failures;
}
+3 −1
Original line number Diff line number Diff line
@@ -143,7 +143,9 @@ function unittest_util() {
	$valgrind $testdir/lib/util/iov.c/iov_ut
	$valgrind $testdir/lib/util/math.c/math_ut
	$valgrind $testdir/lib/util/pipe.c/pipe_ut
	$valgrind $testdir/lib/util/xor.c/xor_ut
	if [ $(uname -s) = Linux ]; then
		$valgrind $testdir/lib/util/fd_group.c/fd_group_ut
	fi
}

function unittest_fsdev() {