Commit | Line | Data |
---|---|---|
8bd8ac79 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: GPL-2.0-only |
8bd8ac79 | 3 | * |
0235b0db | 4 | * Copyright (C) 2019 Efficios, Inc. |
8bd8ac79 SM |
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 | |
ecd7492f MJ |
44 | void trace_class_destroyed_1(const bt_trace_class *tc __attribute__((unused)), |
45 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
46 | { |
47 | trace_class_destroyed_1_called = true; | |
48 | } | |
49 | ||
50 | static | |
ecd7492f MJ |
51 | void trace_class_destroyed_2(const bt_trace_class *tc, |
52 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
53 | { |
54 | bt_trace_class_remove_listener_status remove_listener_status; | |
55 | ||
56 | trace_class_destroyed_2_called = true; | |
57 | ||
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"); | |
63 | } | |
64 | ||
65 | static | |
ecd7492f MJ |
66 | void trace_class_destroyed_3(const bt_trace_class *tc, |
67 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
68 | { |
69 | bt_trace_class_remove_listener_status remove_listener_status; | |
70 | ||
71 | trace_class_destroyed_3_called = true; | |
72 | ||
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"); | |
78 | } | |
79 | ||
80 | static | |
ecd7492f MJ |
81 | void trace_class_destroyed_4(const bt_trace_class *tc, |
82 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
83 | { |
84 | bt_trace_class_remove_listener_status remove_listener_status; | |
85 | ||
86 | trace_class_destroyed_4_called = true; | |
87 | ||
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"); | |
93 | } | |
94 | ||
95 | static | |
ecd7492f MJ |
96 | void trace_class_destroyed_5(const bt_trace_class *tc __attribute__((unused)), |
97 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
98 | { |
99 | trace_class_destroyed_5_called = true; | |
100 | } | |
101 | ||
102 | static | |
ecd7492f MJ |
103 | void trace_destroyed_1(const bt_trace *t __attribute__((unused)), |
104 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
105 | { |
106 | trace_destroyed_1_called = true; | |
107 | } | |
108 | ||
109 | static | |
ecd7492f MJ |
110 | void trace_destroyed_2(const bt_trace *t, |
111 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
112 | { |
113 | bt_trace_remove_listener_status remove_listener_status; | |
114 | ||
115 | trace_destroyed_2_called = true; | |
116 | ||
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"); | |
122 | } | |
123 | ||
124 | static | |
ecd7492f MJ |
125 | void trace_destroyed_3(const bt_trace *t, |
126 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
127 | { |
128 | bt_trace_remove_listener_status remove_listener_status; | |
129 | ||
130 | trace_destroyed_3_called = true; | |
131 | ||
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"); | |
137 | } | |
138 | ||
139 | static | |
ecd7492f MJ |
140 | void trace_destroyed_4(const bt_trace *t, |
141 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
142 | { |
143 | bt_trace_remove_listener_status remove_listener_status; | |
144 | ||
145 | trace_destroyed_4_called = true; | |
146 | ||
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"); | |
152 | } | |
153 | ||
154 | static | |
ecd7492f MJ |
155 | void trace_destroyed_5(const bt_trace *t __attribute__((unused)), |
156 | void *data __attribute__((unused))) | |
8bd8ac79 SM |
157 | { |
158 | trace_destroyed_5_called = true; | |
159 | } | |
160 | ||
161 | static | |
162 | bt_component_class_initialize_method_status hello_init( | |
163 | bt_self_component_source *self_component, | |
ecd7492f MJ |
164 | bt_self_component_source_configuration *config __attribute__((unused)), |
165 | const bt_value *params __attribute__((unused)), | |
166 | void *init_method_data __attribute__((unused))) | |
8bd8ac79 SM |
167 | { |
168 | bt_self_component *self_comp; | |
169 | bt_trace_class *tc; | |
170 | bt_trace *t; | |
171 | bt_trace_class_add_listener_status trace_class_add_listener_status; | |
172 | bt_trace_add_listener_status trace_add_listener_status; | |
173 | ||
174 | self_comp = bt_self_component_source_as_self_component(self_component); | |
175 | tc = bt_trace_class_create(self_comp); | |
176 | BT_ASSERT(tc); | |
177 | ||
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); | |
181 | ||
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); | |
185 | ||
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); | |
189 | ||
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); | |
193 | ||
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); | |
197 | ||
198 | t = bt_trace_create(tc); | |
199 | BT_ASSERT(t); | |
200 | ||
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); | |
204 | ||
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); | |
208 | ||
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); | |
212 | ||
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); | |
216 | ||
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); | |
220 | ||
221 | /* Destroy the trace. */ | |
222 | bt_trace_put_ref(t); | |
223 | ||
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"); | |
229 | ||
230 | /* Destroy the trace class. */ | |
231 | bt_trace_class_put_ref(tc); | |
232 | ||
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"); | |
238 | ||
239 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
240 | } | |
241 | ||
242 | static | |
a3f0c7db | 243 | bt_message_iterator_class_next_method_status hello_iter_next( |
ecd7492f MJ |
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))) | |
8bd8ac79 SM |
248 | { |
249 | BT_ASSERT(false); | |
a3f0c7db | 250 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
8bd8ac79 SM |
251 | } |
252 | ||
ecd7492f | 253 | int main(void) |
8bd8ac79 SM |
254 | { |
255 | bt_graph *graph; | |
a3f0c7db | 256 | bt_message_iterator_class *msg_iter_cls; |
8bd8ac79 SM |
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; | |
261 | ||
262 | plan_tests(NR_TESTS); | |
263 | ||
a3f0c7db SM |
264 | msg_iter_cls = bt_message_iterator_class_create(hello_iter_next); |
265 | BT_ASSERT(msg_iter_cls); | |
266 | ||
267 | source_cc = bt_component_class_source_create("Hello", msg_iter_cls); | |
8bd8ac79 SM |
268 | BT_ASSERT(source_cc); |
269 | ||
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); | |
273 | ||
274 | graph = bt_graph_create(0); | |
275 | BT_ASSERT(graph); | |
276 | ||
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); | |
281 | ||
8bd8ac79 | 282 | bt_component_class_source_put_ref(source_cc); |
a3f0c7db | 283 | bt_message_iterator_class_put_ref(msg_iter_cls); |
8bd8ac79 SM |
284 | bt_graph_put_ref(graph); |
285 | ||
286 | return exit_status(); | |
287 | } |