2 * Copyright 2019 Efficios, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; under version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * Test that remove a trace class or trace destruction listener from within
20 * a destruction listener of the same object works.
23 #include <babeltrace2/babeltrace.h>
24 #include <common/assert.h>
30 static bt_listener_id trace_class_destroyed_1_id
;
31 static bt_listener_id trace_class_destroyed_2_id
;
32 static bt_listener_id trace_class_destroyed_3_id
;
33 static bt_listener_id trace_class_destroyed_4_id
;
34 static bt_listener_id trace_class_destroyed_5_id
;
36 static bt_listener_id trace_destroyed_1_id
;
37 static bt_listener_id trace_destroyed_2_id
;
38 static bt_listener_id trace_destroyed_3_id
;
39 static bt_listener_id trace_destroyed_4_id
;
40 static bt_listener_id trace_destroyed_5_id
;
42 static bool trace_class_destroyed_1_called
= false;
43 static bool trace_class_destroyed_2_called
= false;
44 static bool trace_class_destroyed_3_called
= false;
45 static bool trace_class_destroyed_4_called
= false;
46 static bool trace_class_destroyed_5_called
= false;
48 static bool trace_destroyed_1_called
= false;
49 static bool trace_destroyed_2_called
= false;
50 static bool trace_destroyed_3_called
= false;
51 static bool trace_destroyed_4_called
= false;
52 static bool trace_destroyed_5_called
= false;
55 void trace_class_destroyed_1(const bt_trace_class
*tc
, void *data
)
57 trace_class_destroyed_1_called
= true;
61 void trace_class_destroyed_2(const bt_trace_class
*tc
, void *data
)
63 bt_trace_class_remove_listener_status remove_listener_status
;
65 trace_class_destroyed_2_called
= true;
67 /* Remove self. You shall not crash. */
68 remove_listener_status
= bt_trace_class_remove_destruction_listener(
69 tc
, trace_class_destroyed_2_id
);
70 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
71 "remove trace class listener 2 from 2");
75 void trace_class_destroyed_3(const bt_trace_class
*tc
, void *data
)
77 bt_trace_class_remove_listener_status remove_listener_status
;
79 trace_class_destroyed_3_called
= true;
81 /* Remove an already called listener. */
82 remove_listener_status
= bt_trace_class_remove_destruction_listener(
83 tc
, trace_class_destroyed_1_id
);
84 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
85 "remove trace class listener 1 from 3");
89 void trace_class_destroyed_4(const bt_trace_class
*tc
, void *data
)
91 bt_trace_class_remove_listener_status remove_listener_status
;
93 trace_class_destroyed_4_called
= true;
95 /* Remove a not yet called listener. */
96 remove_listener_status
= bt_trace_class_remove_destruction_listener(
97 tc
, trace_class_destroyed_5_id
);
98 ok(remove_listener_status
== BT_TRACE_CLASS_REMOVE_LISTENER_STATUS_OK
,
99 "remove trace class listener 5 from 4");
103 void trace_class_destroyed_5(const bt_trace_class
*tc
, void *data
)
105 trace_class_destroyed_5_called
= true;
109 void trace_destroyed_1(const bt_trace
*t
, void *data
)
111 trace_destroyed_1_called
= true;
115 void trace_destroyed_2(const bt_trace
*t
, void *data
)
117 bt_trace_remove_listener_status remove_listener_status
;
119 trace_destroyed_2_called
= true;
121 /* Remove self. You shall not crash. */
122 remove_listener_status
= bt_trace_remove_destruction_listener(
123 t
, trace_destroyed_2_id
);
124 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
125 "remove trace listener 2 from 2");
129 void trace_destroyed_3(const bt_trace
*t
, void *data
)
131 bt_trace_remove_listener_status remove_listener_status
;
133 trace_destroyed_3_called
= true;
135 /* Remove an already called listener. */
136 remove_listener_status
= bt_trace_remove_destruction_listener(
137 t
, trace_destroyed_1_id
);
138 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
139 "remove trace listener 1 from 3");
143 void trace_destroyed_4(const bt_trace
*t
, void *data
)
145 bt_trace_remove_listener_status remove_listener_status
;
147 trace_destroyed_4_called
= true;
149 /* Remove a not yet called listener. */
150 remove_listener_status
= bt_trace_remove_destruction_listener(
151 t
, trace_destroyed_5_id
);
152 ok(remove_listener_status
== BT_TRACE_REMOVE_LISTENER_STATUS_OK
,
153 "remove trace listener 5 from 4");
157 void trace_destroyed_5(const bt_trace
*t
, void *data
)
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
,
166 const bt_value
*params
, void *init_method_data
)
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
,
245 bt_message_array_const msgs
, uint64_t capacity
,
249 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
252 int main(int argc
, char **argv
)
255 bt_message_iterator_class
*msg_iter_cls
;
256 bt_component_class_source
*source_cc
;
257 bt_component_class_set_method_status set_method_status
;
258 bt_graph_add_component_status add_component_status
;
259 const bt_component_source
*source
;
261 plan_tests(NR_TESTS
);
263 msg_iter_cls
= bt_message_iterator_class_create(hello_iter_next
);
264 BT_ASSERT(msg_iter_cls
);
266 source_cc
= bt_component_class_source_create("Hello", msg_iter_cls
);
267 BT_ASSERT(source_cc
);
269 set_method_status
= bt_component_class_source_set_initialize_method(
270 source_cc
, hello_init
);
271 BT_ASSERT(set_method_status
== BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK
);
273 graph
= bt_graph_create(0);
276 add_component_status
= bt_graph_add_source_component(
277 graph
, source_cc
, "name", NULL
,
278 BT_LOGGING_LEVEL_WARNING
, &source
);
279 BT_ASSERT(add_component_status
== BT_GRAPH_ADD_COMPONENT_STATUS_OK
);
281 bt_component_source_put_ref(source
);
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();