Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / lib / test_remove_destruction_listener_in_destruction_listener.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2019 Efficios, Inc.
5 */
6
7 /*
8 * Test that remove a trace class or trace destruction listener from within
9 * a destruction listener of the same object works.
10 */
11
12 #include <babeltrace2/babeltrace.h>
13 #include <common/assert.h>
14 #include <tap/tap.h>
15 #include <stdbool.h>
16
17 #define NR_TESTS 16
18
19 static bt_listener_id trace_class_destroyed_1_id;
20 static bt_listener_id trace_class_destroyed_2_id;
21 static bt_listener_id trace_class_destroyed_3_id;
22 static bt_listener_id trace_class_destroyed_4_id;
23 static bt_listener_id trace_class_destroyed_5_id;
24
25 static bt_listener_id trace_destroyed_1_id;
26 static bt_listener_id trace_destroyed_2_id;
27 static bt_listener_id trace_destroyed_3_id;
28 static bt_listener_id trace_destroyed_4_id;
29 static bt_listener_id trace_destroyed_5_id;
30
31 static bool trace_class_destroyed_1_called = false;
32 static bool trace_class_destroyed_2_called = false;
33 static bool trace_class_destroyed_3_called = false;
34 static bool trace_class_destroyed_4_called = false;
35 static bool trace_class_destroyed_5_called = false;
36
37 static bool trace_destroyed_1_called = false;
38 static bool trace_destroyed_2_called = false;
39 static bool trace_destroyed_3_called = false;
40 static bool trace_destroyed_4_called = false;
41 static bool trace_destroyed_5_called = false;
42
43 static
44 void trace_class_destroyed_1(const bt_trace_class *tc, void *data)
45 {
46 trace_class_destroyed_1_called = true;
47 }
48
49 static
50 void trace_class_destroyed_2(const bt_trace_class *tc, void *data)
51 {
52 bt_trace_class_remove_listener_status remove_listener_status;
53
54 trace_class_destroyed_2_called = true;
55
56 /* Remove self. You shall not crash. */
57 remove_listener_status = bt_trace_class_remove_destruction_listener(
58 tc, trace_class_destroyed_2_id);
59 ok(remove_listener_status == BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK,
60 "remove trace class listener 2 from 2");
61 }
62
63 static
64 void trace_class_destroyed_3(const bt_trace_class *tc, void *data)
65 {
66 bt_trace_class_remove_listener_status remove_listener_status;
67
68 trace_class_destroyed_3_called = true;
69
70 /* Remove an already called listener. */
71 remove_listener_status = bt_trace_class_remove_destruction_listener(
72 tc, trace_class_destroyed_1_id);
73 ok(remove_listener_status == BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK,
74 "remove trace class listener 1 from 3");
75 }
76
77 static
78 void trace_class_destroyed_4(const bt_trace_class *tc, void *data)
79 {
80 bt_trace_class_remove_listener_status remove_listener_status;
81
82 trace_class_destroyed_4_called = true;
83
84 /* Remove a not yet called listener. */
85 remove_listener_status = bt_trace_class_remove_destruction_listener(
86 tc, trace_class_destroyed_5_id);
87 ok(remove_listener_status == BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK,
88 "remove trace class listener 5 from 4");
89 }
90
91 static
92 void trace_class_destroyed_5(const bt_trace_class *tc, void *data)
93 {
94 trace_class_destroyed_5_called = true;
95 }
96
97 static
98 void trace_destroyed_1(const bt_trace *t, void *data)
99 {
100 trace_destroyed_1_called = true;
101 }
102
103 static
104 void trace_destroyed_2(const bt_trace *t, void *data)
105 {
106 bt_trace_remove_listener_status remove_listener_status;
107
108 trace_destroyed_2_called = true;
109
110 /* Remove self. You shall not crash. */
111 remove_listener_status = bt_trace_remove_destruction_listener(
112 t, trace_destroyed_2_id);
113 ok(remove_listener_status == BT_TRACE_REMOVE_LISTENER_STATUS_OK,
114 "remove trace listener 2 from 2");
115 }
116
117 static
118 void trace_destroyed_3(const bt_trace *t, void *data)
119 {
120 bt_trace_remove_listener_status remove_listener_status;
121
122 trace_destroyed_3_called = true;
123
124 /* Remove an already called listener. */
125 remove_listener_status = bt_trace_remove_destruction_listener(
126 t, trace_destroyed_1_id);
127 ok(remove_listener_status == BT_TRACE_REMOVE_LISTENER_STATUS_OK,
128 "remove trace listener 1 from 3");
129 }
130
131 static
132 void trace_destroyed_4(const bt_trace *t, void *data)
133 {
134 bt_trace_remove_listener_status remove_listener_status;
135
136 trace_destroyed_4_called = true;
137
138 /* Remove a not yet called listener. */
139 remove_listener_status = bt_trace_remove_destruction_listener(
140 t, trace_destroyed_5_id);
141 ok(remove_listener_status == BT_TRACE_REMOVE_LISTENER_STATUS_OK,
142 "remove trace listener 5 from 4");
143 }
144
145 static
146 void trace_destroyed_5(const bt_trace *t, void *data)
147 {
148 trace_destroyed_5_called = true;
149 }
150
151 static
152 bt_component_class_initialize_method_status hello_init(
153 bt_self_component_source *self_component,
154 bt_self_component_source_configuration *config,
155 const bt_value *params, void *init_method_data)
156 {
157 bt_self_component *self_comp;
158 bt_trace_class *tc;
159 bt_trace *t;
160 bt_trace_class_add_listener_status trace_class_add_listener_status;
161 bt_trace_add_listener_status trace_add_listener_status;
162
163 self_comp = bt_self_component_source_as_self_component(self_component);
164 tc = bt_trace_class_create(self_comp);
165 BT_ASSERT(tc);
166
167 trace_class_add_listener_status = bt_trace_class_add_destruction_listener(
168 tc, trace_class_destroyed_1, NULL, &trace_class_destroyed_1_id);
169 BT_ASSERT(trace_class_add_listener_status == BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK);
170
171 trace_class_add_listener_status = bt_trace_class_add_destruction_listener(
172 tc, trace_class_destroyed_2, NULL, &trace_class_destroyed_2_id);
173 BT_ASSERT(trace_class_add_listener_status == BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK);
174
175 trace_class_add_listener_status = bt_trace_class_add_destruction_listener(
176 tc, trace_class_destroyed_3, NULL, &trace_class_destroyed_3_id);
177 BT_ASSERT(trace_class_add_listener_status == BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK);
178
179 trace_class_add_listener_status = bt_trace_class_add_destruction_listener(
180 tc, trace_class_destroyed_4, NULL, &trace_class_destroyed_4_id);
181 BT_ASSERT(trace_class_add_listener_status == BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK);
182
183 trace_class_add_listener_status = bt_trace_class_add_destruction_listener(
184 tc, trace_class_destroyed_5, NULL, &trace_class_destroyed_5_id);
185 BT_ASSERT(trace_class_add_listener_status == BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK);
186
187 t = bt_trace_create(tc);
188 BT_ASSERT(t);
189
190 trace_add_listener_status = bt_trace_add_destruction_listener(
191 t, trace_destroyed_1, NULL, &trace_destroyed_1_id);
192 BT_ASSERT(trace_add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK);
193
194 trace_add_listener_status = bt_trace_add_destruction_listener(
195 t, trace_destroyed_2, NULL, &trace_destroyed_2_id);
196 BT_ASSERT(trace_add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK);
197
198 trace_add_listener_status = bt_trace_add_destruction_listener(
199 t, trace_destroyed_3, NULL, &trace_destroyed_3_id);
200 BT_ASSERT(trace_add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK);
201
202 trace_add_listener_status = bt_trace_add_destruction_listener(
203 t, trace_destroyed_4, NULL, &trace_destroyed_4_id);
204 BT_ASSERT(trace_add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK);
205
206 trace_add_listener_status = bt_trace_add_destruction_listener(
207 t, trace_destroyed_5, NULL, &trace_destroyed_5_id);
208 BT_ASSERT(trace_add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK);
209
210 /* Destroy the trace. */
211 bt_trace_put_ref(t);
212
213 ok(trace_destroyed_1_called, "trace destruction listener 1 called");
214 ok(trace_destroyed_2_called, "trace destruction listener 2 called");
215 ok(trace_destroyed_3_called, "trace destruction listener 3 called");
216 ok(trace_destroyed_4_called, "trace destruction listener 4 called");
217 ok(!trace_destroyed_5_called, "trace destruction listener 5 not called");
218
219 /* Destroy the trace class. */
220 bt_trace_class_put_ref(tc);
221
222 ok(trace_class_destroyed_1_called, "trace class destruction listener 1 called");
223 ok(trace_class_destroyed_2_called, "trace class destruction listener 2 called");
224 ok(trace_class_destroyed_3_called, "trace class destruction listener 3 called");
225 ok(trace_class_destroyed_4_called, "trace class destruction listener 4 called");
226 ok(!trace_class_destroyed_5_called, "trace class destruction listener 5 not called");
227
228 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
229 }
230
231 static
232 bt_message_iterator_class_next_method_status hello_iter_next(
233 bt_self_message_iterator *message_iterator,
234 bt_message_array_const msgs, uint64_t capacity,
235 uint64_t *count)
236 {
237 BT_ASSERT(false);
238 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
239 }
240
241 int main(int argc, char **argv)
242 {
243 bt_graph *graph;
244 bt_message_iterator_class *msg_iter_cls;
245 bt_component_class_source *source_cc;
246 bt_component_class_set_method_status set_method_status;
247 bt_graph_add_component_status add_component_status;
248 const bt_component_source *source;
249
250 plan_tests(NR_TESTS);
251
252 msg_iter_cls = bt_message_iterator_class_create(hello_iter_next);
253 BT_ASSERT(msg_iter_cls);
254
255 source_cc = bt_component_class_source_create("Hello", msg_iter_cls);
256 BT_ASSERT(source_cc);
257
258 set_method_status = bt_component_class_source_set_initialize_method(
259 source_cc, hello_init);
260 BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
261
262 graph = bt_graph_create(0);
263 BT_ASSERT(graph);
264
265 add_component_status = bt_graph_add_source_component(
266 graph, source_cc, "name", NULL,
267 BT_LOGGING_LEVEL_WARNING, &source);
268 BT_ASSERT(add_component_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
269
270 bt_component_class_source_put_ref(source_cc);
271 bt_message_iterator_class_put_ref(msg_iter_cls);
272 bt_graph_put_ref(graph);
273
274 return exit_status();
275 }
This page took 0.03545 seconds and 4 git commands to generate.