2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2019 Efficios, Inc.
8 * Test that remove a trace class or trace destruction listener from within
9 * a destruction listener of the same object works.
12 #include <babeltrace2/babeltrace.h>
13 #include <common/assert.h>
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
;
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
;
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;
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;
44 void trace_class_destroyed_1(const bt_trace_class
*tc
__attribute__((unused
)),
45 void *data
__attribute__((unused
)))
47 trace_class_destroyed_1_called
= true;
51 void trace_class_destroyed_2(const bt_trace_class
*tc
,
52 void *data
__attribute__((unused
)))
54 bt_trace_class_remove_listener_status remove_listener_status
;
56 trace_class_destroyed_2_called
= true;
58 /* Remove self. You shall not crash. */
59 remove_listener_status
= bt_trace_class_remove_destruction_listener(
60 tc
, trace_class_destroyed_2_id
);
61 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
62 "remove trace class listener 2 from 2");
66 void trace_class_destroyed_3(const bt_trace_class
*tc
,
67 void *data
__attribute__((unused
)))
69 bt_trace_class_remove_listener_status remove_listener_status
;
71 trace_class_destroyed_3_called
= true;
73 /* Remove an already called listener. */
74 remove_listener_status
= bt_trace_class_remove_destruction_listener(
75 tc
, trace_class_destroyed_1_id
);
76 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
77 "remove trace class listener 1 from 3");
81 void trace_class_destroyed_4(const bt_trace_class
*tc
,
82 void *data
__attribute__((unused
)))
84 bt_trace_class_remove_listener_status remove_listener_status
;
86 trace_class_destroyed_4_called
= true;
88 /* Remove a not yet called listener. */
89 remove_listener_status
= bt_trace_class_remove_destruction_listener(
90 tc
, trace_class_destroyed_5_id
);
91 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
92 "remove trace class listener 5 from 4");
96 void trace_class_destroyed_5(const bt_trace_class
*tc
__attribute__((unused
)),
97 void *data
__attribute__((unused
)))
99 trace_class_destroyed_5_called
= true;
103 void trace_destroyed_1(const bt_trace
*t
__attribute__((unused
)),
104 void *data
__attribute__((unused
)))
106 trace_destroyed_1_called
= true;
110 void trace_destroyed_2(const bt_trace
*t
,
111 void *data
__attribute__((unused
)))
113 bt_trace_remove_listener_status remove_listener_status
;
115 trace_destroyed_2_called
= true;
117 /* Remove self. You shall not crash. */
118 remove_listener_status
= bt_trace_remove_destruction_listener(
119 t
, trace_destroyed_2_id
);
120 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
121 "remove trace listener 2 from 2");
125 void trace_destroyed_3(const bt_trace
*t
,
126 void *data
__attribute__((unused
)))
128 bt_trace_remove_listener_status remove_listener_status
;
130 trace_destroyed_3_called
= true;
132 /* Remove an already called listener. */
133 remove_listener_status
= bt_trace_remove_destruction_listener(
134 t
, trace_destroyed_1_id
);
135 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
136 "remove trace listener 1 from 3");
140 void trace_destroyed_4(const bt_trace
*t
,
141 void *data
__attribute__((unused
)))
143 bt_trace_remove_listener_status remove_listener_status
;
145 trace_destroyed_4_called
= true;
147 /* Remove a not yet called listener. */
148 remove_listener_status
= bt_trace_remove_destruction_listener(
149 t
, trace_destroyed_5_id
);
150 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
151 "remove trace listener 5 from 4");
155 void trace_destroyed_5(const bt_trace
*t
__attribute__((unused
)),
156 void *data
__attribute__((unused
)))
158 trace_destroyed_5_called
= true;
162 bt_component_class_initialize_method_status
hello_init(
163 bt_self_component_source
*self_component
,
164 bt_self_component_source_configuration
*config
__attribute__((unused
)),
165 const bt_value
*params
__attribute__((unused
)),
166 void *init_method_data
__attribute__((unused
)))
168 bt_self_component
*self_comp
;
171 bt_trace_class_add_listener_status trace_class_add_listener_status
;
172 bt_trace_add_listener_status trace_add_listener_status
;
174 self_comp
= bt_self_component_source_as_self_component(self_component
);
175 tc
= bt_trace_class_create(self_comp
);
178 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
179 tc
, trace_class_destroyed_1
, NULL
, &trace_class_destroyed_1_id
);
180 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
182 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
183 tc
, trace_class_destroyed_2
, NULL
, &trace_class_destroyed_2_id
);
184 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
186 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
187 tc
, trace_class_destroyed_3
, NULL
, &trace_class_destroyed_3_id
);
188 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
190 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
191 tc
, trace_class_destroyed_4
, NULL
, &trace_class_destroyed_4_id
);
192 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
194 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
195 tc
, trace_class_destroyed_5
, NULL
, &trace_class_destroyed_5_id
);
196 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
198 t
= bt_trace_create(tc
);
201 trace_add_listener_status
= bt_trace_add_destruction_listener(
202 t
, trace_destroyed_1
, NULL
, &trace_destroyed_1_id
);
203 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
205 trace_add_listener_status
= bt_trace_add_destruction_listener(
206 t
, trace_destroyed_2
, NULL
, &trace_destroyed_2_id
);
207 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
209 trace_add_listener_status
= bt_trace_add_destruction_listener(
210 t
, trace_destroyed_3
, NULL
, &trace_destroyed_3_id
);
211 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
213 trace_add_listener_status
= bt_trace_add_destruction_listener(
214 t
, trace_destroyed_4
, NULL
, &trace_destroyed_4_id
);
215 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
217 trace_add_listener_status
= bt_trace_add_destruction_listener(
218 t
, trace_destroyed_5
, NULL
, &trace_destroyed_5_id
);
219 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
221 /* Destroy the trace. */
224 ok(trace_destroyed_1_called
, "trace destruction listener 1 called");
225 ok(trace_destroyed_2_called
, "trace destruction listener 2 called");
226 ok(trace_destroyed_3_called
, "trace destruction listener 3 called");
227 ok(trace_destroyed_4_called
, "trace destruction listener 4 called");
228 ok(!trace_destroyed_5_called
, "trace destruction listener 5 not called");
230 /* Destroy the trace class. */
231 bt_trace_class_put_ref(tc
);
233 ok(trace_class_destroyed_1_called
, "trace class destruction listener 1 called");
234 ok(trace_class_destroyed_2_called
, "trace class destruction listener 2 called");
235 ok(trace_class_destroyed_3_called
, "trace class destruction listener 3 called");
236 ok(trace_class_destroyed_4_called
, "trace class destruction listener 4 called");
237 ok(!trace_class_destroyed_5_called
, "trace class destruction listener 5 not called");
239 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
243 bt_message_iterator_class_next_method_status
hello_iter_next(
244 bt_self_message_iterator
*message_iterator
__attribute__((unused
)),
245 bt_message_array_const msgs
__attribute__((unused
)),
246 uint64_t capacity
__attribute__((unused
)),
247 uint64_t *count
__attribute__((unused
)))
250 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
256 bt_message_iterator_class
*msg_iter_cls
;
257 bt_component_class_source
*source_cc
;
258 bt_component_class_set_method_status set_method_status
;
259 bt_graph_add_component_status add_component_status
;
260 const bt_component_source
*source
;
262 plan_tests(NR_TESTS
);
264 msg_iter_cls
= bt_message_iterator_class_create(hello_iter_next
);
265 BT_ASSERT(msg_iter_cls
);
267 source_cc
= bt_component_class_source_create("Hello", msg_iter_cls
);
268 BT_ASSERT(source_cc
);
270 set_method_status
= bt_component_class_source_set_initialize_method(
271 source_cc
, hello_init
);
272 BT_ASSERT(set_method_status
== BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK
);
274 graph
= bt_graph_create(0);
277 add_component_status
= bt_graph_add_source_component(
278 graph
, source_cc
, "name", NULL
,
279 BT_LOGGING_LEVEL_WARNING
, &source
);
280 BT_ASSERT(add_component_status
== BT_GRAPH_ADD_COMPONENT_STATUS_OK
);
282 bt_component_class_source_put_ref(source_cc
);
283 bt_message_iterator_class_put_ref(msg_iter_cls
);
284 bt_graph_put_ref(graph
);
286 return exit_status();