| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-only |
| 3 | * |
| 4 | * Copyright (C) 2019 Efficios, Inc. |
| 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 <stdbool.h> |
| 15 | |
| 16 | #include "tap/tap.h" |
| 17 | |
| 18 | #define NR_TESTS 16 |
| 19 | |
| 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; |
| 25 | |
| 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; |
| 31 | |
| 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; |
| 37 | |
| 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; |
| 43 | |
| 44 | static |
| 45 | void trace_class_destroyed_1(const bt_trace_class *tc __attribute__((unused)), |
| 46 | void *data __attribute__((unused))) |
| 47 | { |
| 48 | trace_class_destroyed_1_called = true; |
| 49 | } |
| 50 | |
| 51 | static |
| 52 | void trace_class_destroyed_2(const bt_trace_class *tc, |
| 53 | void *data __attribute__((unused))) |
| 54 | { |
| 55 | bt_trace_class_remove_listener_status remove_listener_status; |
| 56 | |
| 57 | trace_class_destroyed_2_called = true; |
| 58 | |
| 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"); |
| 64 | } |
| 65 | |
| 66 | static |
| 67 | void trace_class_destroyed_3(const bt_trace_class *tc, |
| 68 | void *data __attribute__((unused))) |
| 69 | { |
| 70 | bt_trace_class_remove_listener_status remove_listener_status; |
| 71 | |
| 72 | trace_class_destroyed_3_called = true; |
| 73 | |
| 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"); |
| 79 | } |
| 80 | |
| 81 | static |
| 82 | void trace_class_destroyed_4(const bt_trace_class *tc, |
| 83 | void *data __attribute__((unused))) |
| 84 | { |
| 85 | bt_trace_class_remove_listener_status remove_listener_status; |
| 86 | |
| 87 | trace_class_destroyed_4_called = true; |
| 88 | |
| 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"); |
| 94 | } |
| 95 | |
| 96 | static |
| 97 | void trace_class_destroyed_5(const bt_trace_class *tc __attribute__((unused)), |
| 98 | void *data __attribute__((unused))) |
| 99 | { |
| 100 | trace_class_destroyed_5_called = true; |
| 101 | } |
| 102 | |
| 103 | static |
| 104 | void trace_destroyed_1(const bt_trace *t __attribute__((unused)), |
| 105 | void *data __attribute__((unused))) |
| 106 | { |
| 107 | trace_destroyed_1_called = true; |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | void trace_destroyed_2(const bt_trace *t, |
| 112 | void *data __attribute__((unused))) |
| 113 | { |
| 114 | bt_trace_remove_listener_status remove_listener_status; |
| 115 | |
| 116 | trace_destroyed_2_called = true; |
| 117 | |
| 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"); |
| 123 | } |
| 124 | |
| 125 | static |
| 126 | void trace_destroyed_3(const bt_trace *t, |
| 127 | void *data __attribute__((unused))) |
| 128 | { |
| 129 | bt_trace_remove_listener_status remove_listener_status; |
| 130 | |
| 131 | trace_destroyed_3_called = true; |
| 132 | |
| 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"); |
| 138 | } |
| 139 | |
| 140 | static |
| 141 | void trace_destroyed_4(const bt_trace *t, |
| 142 | void *data __attribute__((unused))) |
| 143 | { |
| 144 | bt_trace_remove_listener_status remove_listener_status; |
| 145 | |
| 146 | trace_destroyed_4_called = true; |
| 147 | |
| 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"); |
| 153 | } |
| 154 | |
| 155 | static |
| 156 | void trace_destroyed_5(const bt_trace *t __attribute__((unused)), |
| 157 | void *data __attribute__((unused))) |
| 158 | { |
| 159 | trace_destroyed_5_called = true; |
| 160 | } |
| 161 | |
| 162 | static |
| 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))) |
| 168 | { |
| 169 | bt_self_component *self_comp; |
| 170 | bt_trace_class *tc; |
| 171 | bt_trace *t; |
| 172 | bt_trace_class_add_listener_status trace_class_add_listener_status; |
| 173 | bt_trace_add_listener_status trace_add_listener_status; |
| 174 | |
| 175 | self_comp = bt_self_component_source_as_self_component(self_component); |
| 176 | tc = bt_trace_class_create(self_comp); |
| 177 | BT_ASSERT(tc); |
| 178 | |
| 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); |
| 182 | |
| 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); |
| 186 | |
| 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); |
| 190 | |
| 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); |
| 194 | |
| 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); |
| 198 | |
| 199 | t = bt_trace_create(tc); |
| 200 | BT_ASSERT(t); |
| 201 | |
| 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); |
| 205 | |
| 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); |
| 209 | |
| 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); |
| 213 | |
| 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); |
| 217 | |
| 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); |
| 221 | |
| 222 | /* Destroy the trace. */ |
| 223 | bt_trace_put_ref(t); |
| 224 | |
| 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"); |
| 230 | |
| 231 | /* Destroy the trace class. */ |
| 232 | bt_trace_class_put_ref(tc); |
| 233 | |
| 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"); |
| 239 | |
| 240 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 241 | } |
| 242 | |
| 243 | static |
| 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))) |
| 249 | { |
| 250 | BT_ASSERT(false); |
| 251 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
| 252 | } |
| 253 | |
| 254 | int main(void) |
| 255 | { |
| 256 | bt_graph *graph; |
| 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; |
| 262 | |
| 263 | plan_tests(NR_TESTS); |
| 264 | |
| 265 | msg_iter_cls = bt_message_iterator_class_create(hello_iter_next); |
| 266 | BT_ASSERT(msg_iter_cls); |
| 267 | |
| 268 | source_cc = bt_component_class_source_create("Hello", msg_iter_cls); |
| 269 | BT_ASSERT(source_cc); |
| 270 | |
| 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); |
| 274 | |
| 275 | graph = bt_graph_create(0); |
| 276 | BT_ASSERT(graph); |
| 277 | |
| 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); |
| 282 | |
| 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); |
| 286 | |
| 287 | return exit_status(); |
| 288 | } |