xref: /spdk/test/unit/lib/notify/notify.c/notify_ut.c (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2019 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 #include "spdk_cunit.h"
8 #include "common/lib/test_env.c"
9 #include "unit/lib/json_mock.c"
10 #include "notify/notify.c"
11 
12 static int
13 event_cb(uint64_t idx, const struct spdk_notify_event *event, void *ctx)
14 {
15 	const struct spdk_notify_event **_event = ctx;
16 
17 	*_event = event;
18 	return 0;
19 }
20 
21 static void
22 notify(void)
23 {
24 	struct spdk_notify_type *n1, *n2;
25 	const struct spdk_notify_event *event;
26 	const char *name;
27 	uint64_t cnt;
28 
29 	n1 = spdk_notify_type_register("one");
30 	n2 = spdk_notify_type_register("two");
31 
32 	name = spdk_notify_type_get_name(n1);
33 	CU_ASSERT(strcmp(name, "one") == 0);
34 
35 	name = spdk_notify_type_get_name(n2);
36 	CU_ASSERT(strcmp(name, "two") == 0);
37 
38 
39 	spdk_notify_send("one", "one_context");
40 	spdk_notify_send("two", "two_context");
41 
42 	event = NULL;
43 	cnt = spdk_notify_foreach_event(0, 1, event_cb, &event);
44 	SPDK_CU_ASSERT_FATAL(cnt == 1);
45 	SPDK_CU_ASSERT_FATAL(event != NULL);
46 	CU_ASSERT(strcmp(event->type, "one") == 0);
47 	CU_ASSERT(strcmp(event->ctx, "one_context") == 0);
48 
49 	event = NULL;
50 	cnt = spdk_notify_foreach_event(1, 1, event_cb, &event);
51 	SPDK_CU_ASSERT_FATAL(cnt == 1);
52 	SPDK_CU_ASSERT_FATAL(event != NULL);
53 	CU_ASSERT(strcmp(event->type, "two") == 0);
54 	CU_ASSERT(strcmp(event->ctx, "two_context") == 0);
55 
56 	/* This event should not exist yet */
57 	event = NULL;
58 	cnt = spdk_notify_foreach_event(2, 1, event_cb, &event);
59 	CU_ASSERT(cnt == 0);
60 	CU_ASSERT(event == NULL);
61 
62 	SPDK_CU_ASSERT_FATAL(event == NULL);
63 }
64 
65 int
66 main(int argc, char **argv)
67 {
68 	CU_pSuite suite = NULL;
69 	unsigned int num_failures;
70 
71 	CU_set_error_action(CUEA_ABORT);
72 	CU_initialize_registry();
73 
74 	suite = CU_add_suite("app_suite", NULL, NULL);
75 	CU_ADD_TEST(suite, notify);
76 
77 	CU_basic_set_mode(CU_BRM_VERBOSE);
78 	CU_basic_run_tests();
79 	num_failures = CU_get_number_of_failures();
80 	CU_cleanup_registry();
81 
82 	return num_failures;
83 }
84