From f2576eb05b458694f4cc741c0fcb93423a0f07a1 Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Mon, 27 Jan 2020 23:36:40 -0500 Subject: [PATCH] ut/thread: Fix bugs in test case for_each_channel_remove() The test case for_each_channel_remove() did not have any assertion and the count was not incremented correctly. This patch fixes several issues in for_each_channel_remove() by counting spdk_get/put_io_channel() and spdk_for_each_channel() correctly and achieves what we wanted to do in this test case. Signed-off-by: Shuhei Matsumoto Change-Id: Iceffd924f8887452bd7dad48e30d121874ab0b05 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/483 Tested-by: SPDK CI Jenkins Reviewed-by: Aleksey Marchuk Reviewed-by: Ben Walker --- test/unit/lib/thread/thread.c/thread_ut.c | 53 ++++++++++++++++------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/test/unit/lib/thread/thread.c/thread_ut.c b/test/unit/lib/thread/thread.c/thread_ut.c index 9da4c42d1..fa605efb4 100644 --- a/test/unit/lib/thread/thread.c/thread_ut.c +++ b/test/unit/lib/thread/thread.c/thread_ut.c @@ -383,45 +383,53 @@ thread_for_each(void) static int channel_create(void *io_device, void *ctx_buf) { + int *ch_count = io_device; + + (*ch_count)++; return 0; } static void channel_destroy(void *io_device, void *ctx_buf) { + int *ch_count = io_device; + + (*ch_count)--; } static void channel_msg(struct spdk_io_channel_iter *i) { - struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); - int *count = spdk_io_channel_get_ctx(ch); - - (*count)++; + int *msg_count = spdk_io_channel_iter_get_ctx(i); + (*msg_count)++; spdk_for_each_channel_continue(i, 0); } static void channel_cpl(struct spdk_io_channel_iter *i, int status) { + int *msg_count = spdk_io_channel_iter_get_ctx(i); + + (*msg_count)++; } static void for_each_channel_remove(void) { struct spdk_io_channel *ch0, *ch1, *ch2; - int io_target; - int count = 0; + int ch_count = 0; + int msg_count = 0; allocate_threads(3); set_thread(0); - spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int), NULL); - ch0 = spdk_get_io_channel(&io_target); + spdk_io_device_register(&ch_count, channel_create, channel_destroy, sizeof(int), NULL); + ch0 = spdk_get_io_channel(&ch_count); set_thread(1); - ch1 = spdk_get_io_channel(&io_target); + ch1 = spdk_get_io_channel(&ch_count); set_thread(2); - ch2 = spdk_get_io_channel(&io_target); + ch2 = spdk_get_io_channel(&ch_count); + CU_ASSERT(ch_count == 3); /* * Test that io_channel handles the case where we start to iterate through @@ -434,24 +442,39 @@ for_each_channel_remove(void) */ set_thread(0); spdk_put_io_channel(ch0); + CU_ASSERT(ch_count == 3); poll_threads(); - spdk_for_each_channel(&io_target, channel_msg, &count, channel_cpl); + CU_ASSERT(ch_count == 2); + spdk_for_each_channel(&ch_count, channel_msg, &msg_count, channel_cpl); + CU_ASSERT(msg_count == 0); poll_threads(); + CU_ASSERT(msg_count == 3); + + msg_count = 0; /* * Case #2: Put the I/O channel after spdk_for_each_channel, but before * thread 0 is polled. */ - ch0 = spdk_get_io_channel(&io_target); - spdk_for_each_channel(&io_target, channel_msg, &count, channel_cpl); + ch0 = spdk_get_io_channel(&ch_count); + CU_ASSERT(ch_count == 3); + spdk_for_each_channel(&ch_count, channel_msg, &msg_count, channel_cpl); spdk_put_io_channel(ch0); - poll_threads(); + CU_ASSERT(ch_count == 3); + poll_threads(); + CU_ASSERT(ch_count == 2); + CU_ASSERT(msg_count == 4); set_thread(1); spdk_put_io_channel(ch1); + CU_ASSERT(ch_count == 2); set_thread(2); spdk_put_io_channel(ch2); - spdk_io_device_unregister(&io_target, NULL); + CU_ASSERT(ch_count == 2); + poll_threads(); + CU_ASSERT(ch_count == 0); + + spdk_io_device_unregister(&ch_count, NULL); poll_threads(); free_threads();