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>
20 static bt_listener_id trace_class_destroyed_1_id
;
21 static bt_listener_id trace_class_destroyed_2_id
;
22 static bt_listener_id trace_class_destroyed_3_id
;
23 static bt_listener_id trace_class_destroyed_4_id
;
24 static bt_listener_id trace_class_destroyed_5_id
;
26 static bt_listener_id trace_destroyed_1_id
;
27 static bt_listener_id trace_destroyed_2_id
;
28 static bt_listener_id trace_destroyed_3_id
;
29 static bt_listener_id trace_destroyed_4_id
;
30 static bt_listener_id trace_destroyed_5_id
;
32 static bool trace_class_destroyed_1_called
= false;
33 static bool trace_class_destroyed_2_called
= false;
34 static bool trace_class_destroyed_3_called
= false;
35 static bool trace_class_destroyed_4_called
= false;
36 static bool trace_class_destroyed_5_called
= false;
38 static bool trace_destroyed_1_called
= false;
39 static bool trace_destroyed_2_called
= false;
40 static bool trace_destroyed_3_called
= false;
41 static bool trace_destroyed_4_called
= false;
42 static bool trace_destroyed_5_called
= false;
45 void trace_class_destroyed_1(const bt_trace_class
*tc
__attribute__((unused
)),
46 void *data
__attribute__((unused
)))
48 trace_class_destroyed_1_called
= true;
52 void trace_class_destroyed_2(const bt_trace_class
*tc
,
53 void *data
__attribute__((unused
)))
55 bt_trace_class_remove_listener_status remove_listener_status
;
57 trace_class_destroyed_2_called
= true;
59 /* Remove self. You shall not crash. */
60 remove_listener_status
= bt_trace_class_remove_destruction_listener(
61 tc
, trace_class_destroyed_2_id
);
62 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
63 "remove trace class listener 2 from 2");
67 void trace_class_destroyed_3(const bt_trace_class
*tc
,
68 void *data
__attribute__((unused
)))
70 bt_trace_class_remove_listener_status remove_listener_status
;
72 trace_class_destroyed_3_called
= true;
74 /* Remove an already called listener. */
75 remove_listener_status
= bt_trace_class_remove_destruction_listener(
76 tc
, trace_class_destroyed_1_id
);
77 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
78 "remove trace class listener 1 from 3");
82 void trace_class_destroyed_4(const bt_trace_class
*tc
,
83 void *data
__attribute__((unused
)))
85 bt_trace_class_remove_listener_status remove_listener_status
;
87 trace_class_destroyed_4_called
= true;
89 /* Remove a not yet called listener. */
90 remove_listener_status
= bt_trace_class_remove_destruction_listener(
91 tc
, trace_class_destroyed_5_id
);
92 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
93 "remove trace class listener 5 from 4");
97 void trace_class_destroyed_5(const bt_trace_class
*tc
__attribute__((unused
)),
98 void *data
__attribute__((unused
)))
100 trace_class_destroyed_5_called
= true;
104 void trace_destroyed_1(const bt_trace
*t
__attribute__((unused
)),
105 void *data
__attribute__((unused
)))
107 trace_destroyed_1_called
= true;
111 void trace_destroyed_2(const bt_trace
*t
,
112 void *data
__attribute__((unused
)))
114 bt_trace_remove_listener_status remove_listener_status
;
116 trace_destroyed_2_called
= true;
118 /* Remove self. You shall not crash. */
119 remove_listener_status
= bt_trace_remove_destruction_listener(
120 t
, trace_destroyed_2_id
);
121 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
122 "remove trace listener 2 from 2");
126 void trace_destroyed_3(const bt_trace
*t
,
127 void *data
__attribute__((unused
)))
129 bt_trace_remove_listener_status remove_listener_status
;
131 trace_destroyed_3_called
= true;
133 /* Remove an already called listener. */
134 remove_listener_status
= bt_trace_remove_destruction_listener(
135 t
, trace_destroyed_1_id
);
136 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
137 "remove trace listener 1 from 3");
141 void trace_destroyed_4(const bt_trace
*t
,
142 void *data
__attribute__((unused
)))
144 bt_trace_remove_listener_status remove_listener_status
;
146 trace_destroyed_4_called
= true;
148 /* Remove a not yet called listener. */
149 remove_listener_status
= bt_trace_remove_destruction_listener(
150 t
, trace_destroyed_5_id
);
151 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
152 "remove trace listener 5 from 4");
156 void trace_destroyed_5(const bt_trace
*t
__attribute__((unused
)),
157 void *data
__attribute__((unused
)))
159 trace_destroyed_5_called
= true;
163 bt_component_class_initialize_method_status
hello_init(
164 bt_self_component_source
*self_component
,
165 bt_self_component_source_configuration
*config
__attribute__((unused
)),
166 const bt_value
*params
__attribute__((unused
)),
167 void *init_method_data
__attribute__((unused
)))
169 bt_self_component
*self_comp
;
172 bt_trace_class_add_listener_status trace_class_add_listener_status
;
173 bt_trace_add_listener_status trace_add_listener_status
;
175 self_comp
= bt_self_component_source_as_self_component(self_component
);
176 tc
= bt_trace_class_create(self_comp
);
179 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
180 tc
, trace_class_destroyed_1
, NULL
, &trace_class_destroyed_1_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_2
, NULL
, &trace_class_destroyed_2_id
);
185 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
187 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
188 tc
, trace_class_destroyed_3
, NULL
, &trace_class_destroyed_3_id
);
189 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
191 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
192 tc
, trace_class_destroyed_4
, NULL
, &trace_class_destroyed_4_id
);
193 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
195 trace_class_add_listener_status
= bt_trace_class_add_destruction_listener(
196 tc
, trace_class_destroyed_5
, NULL
, &trace_class_destroyed_5_id
);
197 BT_ASSERT(trace_class_add_listener_status
== BT_TRACE_CLASS_ADD_LISTENER_STATUS_OK
);
199 t
= bt_trace_create(tc
);
202 trace_add_listener_status
= bt_trace_add_destruction_listener(
203 t
, trace_destroyed_1
, NULL
, &trace_destroyed_1_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_2
, NULL
, &trace_destroyed_2_id
);
208 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
210 trace_add_listener_status
= bt_trace_add_destruction_listener(
211 t
, trace_destroyed_3
, NULL
, &trace_destroyed_3_id
);
212 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
214 trace_add_listener_status
= bt_trace_add_destruction_listener(
215 t
, trace_destroyed_4
, NULL
, &trace_destroyed_4_id
);
216 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
218 trace_add_listener_status
= bt_trace_add_destruction_listener(
219 t
, trace_destroyed_5
, NULL
, &trace_destroyed_5_id
);
220 BT_ASSERT(trace_add_listener_status
== BT_TRACE_ADD_LISTENER_STATUS_OK
);
222 /* Destroy the trace. */
225 ok(trace_destroyed_1_called
, "trace destruction listener 1 called");
226 ok(trace_destroyed_2_called
, "trace destruction listener 2 called");
227 ok(trace_destroyed_3_called
, "trace destruction listener 3 called");
228 ok(trace_destroyed_4_called
, "trace destruction listener 4 called");
229 ok(!trace_destroyed_5_called
, "trace destruction listener 5 not called");
231 /* Destroy the trace class. */
232 bt_trace_class_put_ref(tc
);
234 ok(trace_class_destroyed_1_called
, "trace class destruction listener 1 called");
235 ok(trace_class_destroyed_2_called
, "trace class destruction listener 2 called");
236 ok(trace_class_destroyed_3_called
, "trace class destruction listener 3 called");
237 ok(trace_class_destroyed_4_called
, "trace class destruction listener 4 called");
238 ok(!trace_class_destroyed_5_called
, "trace class destruction listener 5 not called");
240 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
244 bt_message_iterator_class_next_method_status
hello_iter_next(
245 bt_self_message_iterator
*message_iterator
__attribute__((unused
)),
246 bt_message_array_const msgs
__attribute__((unused
)),
247 uint64_t capacity
__attribute__((unused
)),
248 uint64_t *count
__attribute__((unused
)))
251 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
257 bt_message_iterator_class
*msg_iter_cls
;
258 bt_component_class_source
*source_cc
;
259 bt_component_class_set_method_status set_method_status
;
260 bt_graph_add_component_status add_component_status
;
261 const bt_component_source
*source
;
263 plan_tests(NR_TESTS
);
265 msg_iter_cls
= bt_message_iterator_class_create(hello_iter_next
);
266 BT_ASSERT(msg_iter_cls
);
268 source_cc
= bt_component_class_source_create("Hello", msg_iter_cls
);
269 BT_ASSERT(source_cc
);
271 set_method_status
= bt_component_class_source_set_initialize_method(
272 source_cc
, hello_init
);
273 BT_ASSERT(set_method_status
== BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK
);
275 graph
= bt_graph_create(0);
278 add_component_status
= bt_graph_add_source_component(
279 graph
, source_cc
, "name", NULL
,
280 BT_LOGGING_LEVEL_WARNING
, &source
);
281 BT_ASSERT(add_component_status
== BT_GRAPH_ADD_COMPONENT_STATUS_OK
);
283 bt_component_class_source_put_ref(source_cc
);
284 bt_message_iterator_class_put_ref(msg_iter_cls
);
285 bt_graph_put_ref(graph
);
287 return exit_status();