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
, void *data
)
46 trace_class_destroyed_1_called
= true;
50 void trace_class_destroyed_2(const bt_trace_class
*tc
, void *data
)
52 bt_trace_class_remove_listener_status remove_listener_status
;
54 trace_class_destroyed_2_called
= true;
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");
64 void trace_class_destroyed_3(const bt_trace_class
*tc
, void *data
)
66 bt_trace_class_remove_listener_status remove_listener_status
;
68 trace_class_destroyed_3_called
= true;
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");
78 void trace_class_destroyed_4(const bt_trace_class
*tc
, void *data
)
80 bt_trace_class_remove_listener_status remove_listener_status
;
82 trace_class_destroyed_4_called
= true;
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");
92 void trace_class_destroyed_5(const bt_trace_class
*tc
, void *data
)
94 trace_class_destroyed_5_called
= true;
98 void trace_destroyed_1(const bt_trace
*t
, void *data
)
100 trace_destroyed_1_called
= true;
104 void trace_destroyed_2(const bt_trace
*t
, void *data
)
106 bt_trace_remove_listener_status remove_listener_status
;
108 trace_destroyed_2_called
= true;
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");
118 void trace_destroyed_3(const bt_trace
*t
, void *data
)
120 bt_trace_remove_listener_status remove_listener_status
;
122 trace_destroyed_3_called
= true;
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");
132 void trace_destroyed_4(const bt_trace
*t
, void *data
)
134 bt_trace_remove_listener_status remove_listener_status
;
136 trace_destroyed_4_called
= true;
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");
146 void trace_destroyed_5(const bt_trace
*t
, void *data
)
148 trace_destroyed_5_called
= true;
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
)
157 bt_self_component
*self_comp
;
160 bt_trace_class_add_listener_status trace_class_add_listener_status
;
161 bt_trace_add_listener_status trace_add_listener_status
;
163 self_comp
= bt_self_component_source_as_self_component(self_component
);
164 tc
= bt_trace_class_create(self_comp
);
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
);
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
);
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
);
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
);
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
);
187 t
= bt_trace_create(tc
);
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
);
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
);
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
);
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
);
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
);
210 /* Destroy the trace. */
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");
219 /* Destroy the trace class. */
220 bt_trace_class_put_ref(tc
);
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");
228 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
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
,
238 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
241 int main(int argc
, char **argv
)
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
;
250 plan_tests(NR_TESTS
);
252 msg_iter_cls
= bt_message_iterator_class_create(hello_iter_next
);
253 BT_ASSERT(msg_iter_cls
);
255 source_cc
= bt_component_class_source_create("Hello", msg_iter_cls
);
256 BT_ASSERT(source_cc
);
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
);
262 graph
= bt_graph_create(0);
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
);
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
);
274 return exit_status();