| 1 | /* |
| 2 | * Copyright 2019 Efficios, Inc. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Test that remove a trace class or trace destruction listener from within |
| 20 | * a destruction listener of the same object works. |
| 21 | */ |
| 22 | |
| 23 | #include <babeltrace2/babeltrace.h> |
| 24 | #include <common/assert.h> |
| 25 | #include <tap/tap.h> |
| 26 | #include <stdbool.h> |
| 27 | |
| 28 | #define NR_TESTS 16 |
| 29 | |
| 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; |
| 35 | |
| 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; |
| 41 | |
| 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; |
| 47 | |
| 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; |
| 53 | |
| 54 | static |
| 55 | void trace_class_destroyed_1(const bt_trace_class *tc, void *data) |
| 56 | { |
| 57 | trace_class_destroyed_1_called = true; |
| 58 | } |
| 59 | |
| 60 | static |
| 61 | void trace_class_destroyed_2(const bt_trace_class *tc, void *data) |
| 62 | { |
| 63 | bt_trace_class_remove_listener_status remove_listener_status; |
| 64 | |
| 65 | trace_class_destroyed_2_called = true; |
| 66 | |
| 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"); |
| 72 | } |
| 73 | |
| 74 | static |
| 75 | void trace_class_destroyed_3(const bt_trace_class *tc, void *data) |
| 76 | { |
| 77 | bt_trace_class_remove_listener_status remove_listener_status; |
| 78 | |
| 79 | trace_class_destroyed_3_called = true; |
| 80 | |
| 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"); |
| 86 | } |
| 87 | |
| 88 | static |
| 89 | void trace_class_destroyed_4(const bt_trace_class *tc, void *data) |
| 90 | { |
| 91 | bt_trace_class_remove_listener_status remove_listener_status; |
| 92 | |
| 93 | trace_class_destroyed_4_called = true; |
| 94 | |
| 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"); |
| 100 | } |
| 101 | |
| 102 | static |
| 103 | void trace_class_destroyed_5(const bt_trace_class *tc, void *data) |
| 104 | { |
| 105 | trace_class_destroyed_5_called = true; |
| 106 | } |
| 107 | |
| 108 | static |
| 109 | void trace_destroyed_1(const bt_trace *t, void *data) |
| 110 | { |
| 111 | trace_destroyed_1_called = true; |
| 112 | } |
| 113 | |
| 114 | static |
| 115 | void trace_destroyed_2(const bt_trace *t, void *data) |
| 116 | { |
| 117 | bt_trace_remove_listener_status remove_listener_status; |
| 118 | |
| 119 | trace_destroyed_2_called = true; |
| 120 | |
| 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"); |
| 126 | } |
| 127 | |
| 128 | static |
| 129 | void trace_destroyed_3(const bt_trace *t, void *data) |
| 130 | { |
| 131 | bt_trace_remove_listener_status remove_listener_status; |
| 132 | |
| 133 | trace_destroyed_3_called = true; |
| 134 | |
| 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"); |
| 140 | } |
| 141 | |
| 142 | static |
| 143 | void trace_destroyed_4(const bt_trace *t, void *data) |
| 144 | { |
| 145 | bt_trace_remove_listener_status remove_listener_status; |
| 146 | |
| 147 | trace_destroyed_4_called = true; |
| 148 | |
| 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"); |
| 154 | } |
| 155 | |
| 156 | static |
| 157 | void trace_destroyed_5(const bt_trace *t, void *data) |
| 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, |
| 166 | const bt_value *params, void *init_method_data) |
| 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 |
| 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, |
| 246 | uint64_t *count) |
| 247 | { |
| 248 | BT_ASSERT(false); |
| 249 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
| 250 | } |
| 251 | |
| 252 | int main(int argc, char **argv) |
| 253 | { |
| 254 | bt_graph *graph; |
| 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; |
| 260 | |
| 261 | plan_tests(NR_TESTS); |
| 262 | |
| 263 | msg_iter_cls = bt_message_iterator_class_create(hello_iter_next); |
| 264 | BT_ASSERT(msg_iter_cls); |
| 265 | |
| 266 | source_cc = bt_component_class_source_create("Hello", msg_iter_cls); |
| 267 | BT_ASSERT(source_cc); |
| 268 | |
| 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); |
| 272 | |
| 273 | graph = bt_graph_create(0); |
| 274 | BT_ASSERT(graph); |
| 275 | |
| 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); |
| 280 | |
| 281 | bt_component_class_source_put_ref(source_cc); |
| 282 | bt_message_iterator_class_put_ref(msg_iter_cls); |
| 283 | bt_graph_put_ref(graph); |
| 284 | |
| 285 | return exit_status(); |
| 286 | } |