X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=tests%2Flib%2Ftest_trace_ir_ref.c;h=d5dfa9cf94afd3289403154710ac3cdfa3e0b2ad;hp=9f9da406d66eec8b78c61be24ca19f28b07ff0c0;hb=0235b0db7de5bcacdb3650c92461f2ce5eb2143d;hpb=862ca4ed7b3a8ef14f69529d252bccc48a33108a diff --git a/tests/lib/test_trace_ir_ref.c b/tests/lib/test_trace_ir_ref.c index 9f9da406..d5dfa9cf 100644 --- a/tests/lib/test_trace_ir_ref.c +++ b/tests/lib/test_trace_ir_ref.c @@ -1,39 +1,36 @@ /* - * test_trace_ir_ref.c + * SPDX-License-Identifier: GPL-2.0-only * - * Trace IR Reference Count test - * - * Copyright 2016 - Jérémie Galarneau - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; under version 2 of the License. + * Copyright (C) 2016 Jérémie Galarneau * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * Trace IR Reference Count test */ +#include #include "tap/tap.h" -#include -#include -#include -#include +#include +#include "lib/object.h" +#include "compat/stdlib.h" +#include "common/assert.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "common.h" #define NR_TESTS 37 struct user { - struct bt_trace_class *tc; - struct bt_stream_class *sc; - struct bt_event_class *ec; - struct bt_stream *stream; - struct bt_event *event; + bt_trace_class *tc; + bt_stream_class *sc; + bt_event_class *ec; + bt_stream *stream; + bt_event *event; }; struct writer_user { @@ -63,35 +60,35 @@ static const size_t WRITER_USER_NR_ELEMENTS = * - uint16_t payload_16; * - uint32_t payload_32; */ -static struct bt_field_class *create_integer_struct(void) +static bt_field_class *create_integer_struct(bt_trace_class *trace_class) { int ret; - struct bt_field_class *structure = NULL; - struct bt_field_class *ui8 = NULL, *ui16 = NULL, *ui32 = NULL; + bt_field_class *structure = NULL; + bt_field_class *ui8 = NULL, *ui16 = NULL, *ui32 = NULL; - structure = bt_field_class_structure_create(); + structure = bt_field_class_structure_create(trace_class); BT_ASSERT(structure); - ui8 = bt_field_class_unsigned_integer_create(); + ui8 = bt_field_class_integer_unsigned_create(trace_class); BT_ASSERT(ui8); bt_field_class_integer_set_field_value_range(ui8, 8); ret = bt_field_class_structure_append_member(structure, "payload_8", ui8); BT_ASSERT(ret == 0); - ui16 = bt_field_class_unsigned_integer_create(); + ui16 = bt_field_class_integer_unsigned_create(trace_class); BT_ASSERT(ui16); bt_field_class_integer_set_field_value_range(ui16, 16); ret = bt_field_class_structure_append_member(structure, "payload_16", ui16); BT_ASSERT(ret == 0); - ui32 = bt_field_class_unsigned_integer_create(); + ui32 = bt_field_class_integer_unsigned_create(trace_class); BT_ASSERT(ui32); bt_field_class_integer_set_field_value_range(ui32, 32); ret = bt_field_class_structure_append_member(structure, "payload_32", ui32); BT_ASSERT(ret == 0); - BT_OBJECT_PUT_REF_AND_RESET(ui8); - BT_OBJECT_PUT_REF_AND_RESET(ui16); - BT_OBJECT_PUT_REF_AND_RESET(ui32); + BT_FIELD_CLASS_PUT_REF_AND_RESET(ui8); + BT_FIELD_CLASS_PUT_REF_AND_RESET(ui16); + BT_FIELD_CLASS_PUT_REF_AND_RESET(ui32); return structure; } @@ -130,23 +127,23 @@ static struct bt_ctf_field_type *create_writer_integer_struct(void) * - uint16_t payload_16; * - uint32_t payload_32; */ -static struct bt_event_class *create_simple_event( - struct bt_stream_class *sc, const char *name) +static bt_event_class *create_simple_event( + bt_stream_class *sc, const char *name) { int ret; - struct bt_event_class *event = NULL; - struct bt_field_class *payload = NULL; + bt_event_class *event = NULL; + bt_field_class *payload = NULL; BT_ASSERT(name); event = bt_event_class_create(sc); BT_ASSERT(event); ret = bt_event_class_set_name(event, name); BT_ASSERT(ret == 0); - payload = create_integer_struct(); + payload = create_integer_struct(bt_stream_class_borrow_trace_class(sc)); BT_ASSERT(payload); ret = bt_event_class_set_payload_field_class(event, payload); BT_ASSERT(ret == 0); - BT_OBJECT_PUT_REF_AND_RESET(payload); + BT_FIELD_CLASS_PUT_REF_AND_RESET(payload); return event; } @@ -160,87 +157,43 @@ static struct bt_event_class *create_simple_event( * - uint16_t payload_16; * - uint32_t payload_32; */ -static struct bt_event_class *create_complex_event( - struct bt_stream_class *sc, +static bt_event_class *create_complex_event(bt_stream_class *sc, const char *name) { int ret; - struct bt_event_class *event = NULL; - struct bt_field_class *inner = NULL, *outer = NULL; + bt_event_class *event = NULL; + bt_field_class *inner = NULL, *outer = NULL; + bt_trace_class *trace_class = bt_stream_class_borrow_trace_class(sc); BT_ASSERT(name); event = bt_event_class_create(sc); BT_ASSERT(event); ret = bt_event_class_set_name(event, name); BT_ASSERT(ret == 0); - outer = create_integer_struct(); + outer = create_integer_struct(trace_class); BT_ASSERT(outer); - inner = create_integer_struct(); + inner = create_integer_struct(trace_class); BT_ASSERT(inner); ret = bt_field_class_structure_append_member(outer, "payload_struct", inner); BT_ASSERT(ret == 0); ret = bt_event_class_set_payload_field_class(event, outer); BT_ASSERT(ret == 0); - BT_OBJECT_PUT_REF_AND_RESET(inner); - BT_OBJECT_PUT_REF_AND_RESET(outer); + BT_FIELD_CLASS_PUT_REF_AND_RESET(inner); + BT_FIELD_CLASS_PUT_REF_AND_RESET(outer); return event; } -static void set_stream_class_field_classes( - struct bt_stream_class *stream_class) -{ - struct bt_field_class *packet_context_type; - struct bt_field_class *event_header_type; - struct bt_field_class *fc; - int ret; - - packet_context_type = bt_field_class_structure_create(); - BT_ASSERT(packet_context_type); - fc = bt_field_class_unsigned_integer_create(); - BT_ASSERT(fc); - bt_field_class_integer_set_field_value_range(fc, 32); - ret = bt_field_class_structure_append_member(packet_context_type, - "packet_size", fc); - BT_ASSERT(ret == 0); - bt_object_put_ref(fc); - fc = bt_field_class_unsigned_integer_create(); - BT_ASSERT(fc); - bt_field_class_integer_set_field_value_range(fc, 32); - ret = bt_field_class_structure_append_member(packet_context_type, - "content_size", fc); - BT_ASSERT(ret == 0); - bt_object_put_ref(fc); - event_header_type = bt_field_class_structure_create(); - BT_ASSERT(event_header_type); - fc = bt_field_class_unsigned_integer_create(); - BT_ASSERT(fc); - bt_field_class_integer_set_field_value_range(fc, 32); - ret = bt_field_class_structure_append_member(event_header_type, - "id", fc); - BT_ASSERT(ret == 0); - bt_object_put_ref(fc); - ret = bt_stream_class_set_packet_context_field_class( - stream_class, packet_context_type); - BT_ASSERT(ret == 0); - ret = bt_stream_class_set_event_header_field_class( - stream_class, event_header_type); - BT_ASSERT(ret == 0); - bt_object_put_ref(packet_context_type); - bt_object_put_ref(event_header_type); -} - -static void create_sc1(struct bt_trace_class *trace_class) +static void create_sc1(bt_trace_class *trace_class) { int ret; - struct bt_event_class *ec1 = NULL, *ec2 = NULL; - struct bt_stream_class *sc1 = NULL, *ret_stream = NULL; + bt_event_class *ec1 = NULL, *ec2 = NULL; + bt_stream_class *sc1 = NULL, *ret_stream = NULL; sc1 = bt_stream_class_create(trace_class); BT_ASSERT(sc1); ret = bt_stream_class_set_name(sc1, "sc1"); BT_ASSERT(ret == 0); - set_stream_class_field_classes(sc1); ec1 = create_complex_event(sc1, "ec1"); BT_ASSERT(ec1); ec2 = create_simple_event(sc1, "ec2"); @@ -249,70 +202,47 @@ static void create_sc1(struct bt_trace_class *trace_class) ok(ret_stream == sc1, "Borrow parent stream SC1 from EC1"); ret_stream = bt_event_class_borrow_stream_class(ec2); ok(ret_stream == sc1, "Borrow parent stream SC1 from EC2"); - BT_OBJECT_PUT_REF_AND_RESET(ec1); - BT_OBJECT_PUT_REF_AND_RESET(ec2); - BT_OBJECT_PUT_REF_AND_RESET(sc1); + BT_EVENT_CLASS_PUT_REF_AND_RESET(ec1); + BT_EVENT_CLASS_PUT_REF_AND_RESET(ec2); + BT_STREAM_CLASS_PUT_REF_AND_RESET(sc1); } -static void create_sc2(struct bt_trace_class *trace_class) +static void create_sc2(bt_trace_class *trace_class) { int ret; - struct bt_event_class *ec3 = NULL; - struct bt_stream_class *sc2 = NULL, *ret_stream = NULL; + bt_event_class *ec3 = NULL; + bt_stream_class *sc2 = NULL, *ret_stream = NULL; sc2 = bt_stream_class_create(trace_class); BT_ASSERT(sc2); ret = bt_stream_class_set_name(sc2, "sc2"); BT_ASSERT(ret == 0); - set_stream_class_field_classes(sc2); ec3 = create_simple_event(sc2, "ec3"); ret_stream = bt_event_class_borrow_stream_class(ec3); ok(ret_stream == sc2, "Borrow parent stream SC2 from EC3"); - BT_OBJECT_PUT_REF_AND_RESET(ec3); - BT_OBJECT_PUT_REF_AND_RESET(sc2); -} - -static void set_trace_packet_header(struct bt_trace_class *trace_class) -{ - struct bt_field_class *packet_header_type; - struct bt_field_class *fc; - int ret; - - packet_header_type = bt_field_class_structure_create(); - BT_ASSERT(packet_header_type); - fc = bt_field_class_unsigned_integer_create(); - BT_ASSERT(fc); - bt_field_class_integer_set_field_value_range(fc, 32); - ret = bt_field_class_structure_append_member(packet_header_type, - "stream_id", fc); - BT_ASSERT(ret == 0); - bt_object_put_ref(fc); - ret = bt_trace_class_set_packet_header_field_class(trace_class, - packet_header_type); - BT_ASSERT(ret == 0); - - bt_object_put_ref(packet_header_type); + BT_EVENT_CLASS_PUT_REF_AND_RESET(ec3); + BT_STREAM_CLASS_PUT_REF_AND_RESET(sc2); } -static struct bt_trace_class *create_tc1(void) +static bt_trace_class *create_tc1(bt_self_component_source *self_comp) { - struct bt_trace_class *tc1 = NULL; + bt_trace_class *tc1 = NULL; - tc1 = bt_trace_class_create(); + tc1 = bt_trace_class_create( + bt_self_component_source_as_self_component(self_comp)); BT_ASSERT(tc1); - set_trace_packet_header(tc1); create_sc1(tc1); create_sc2(tc1); return tc1; } -static void init_weak_refs(struct bt_trace_class *tc, - struct bt_trace_class **tc1, - struct bt_stream_class **sc1, - struct bt_stream_class **sc2, - struct bt_event_class **ec1, - struct bt_event_class **ec2, - struct bt_event_class **ec3) +static void init_weak_refs(bt_trace_class *tc, + bt_trace_class **tc1, + bt_stream_class **sc1, + bt_stream_class **sc2, + bt_event_class **ec1, + bt_event_class **ec2, + bt_event_class **ec3) { *tc1 = tc; *sc1 = bt_trace_class_borrow_stream_class_by_index(tc, 0); @@ -322,7 +252,7 @@ static void init_weak_refs(struct bt_trace_class *tc, *ec3 = bt_stream_class_borrow_event_class_by_index(*sc2, 0); } -static void test_example_scenario(void) +static void test_example_scenario(bt_self_component_source *self_comp) { /* * Weak pointers to trace IR objects are to be used very @@ -331,14 +261,14 @@ static void test_example_scenario(void) * of expected reference counts without affecting them by taking * "real" references to the objects. */ - struct bt_trace_class *tc1 = NULL, *weak_tc1 = NULL; - struct bt_stream_class *weak_sc1 = NULL, *weak_sc2 = NULL; - struct bt_event_class *weak_ec1 = NULL, *weak_ec2 = NULL, + bt_trace_class *tc1 = NULL, *weak_tc1 = NULL; + bt_stream_class *weak_sc1 = NULL, *weak_sc2 = NULL; + bt_event_class *weak_ec1 = NULL, *weak_ec2 = NULL, *weak_ec3 = NULL; struct user user_a = { 0 }, user_b = { 0 }, user_c = { 0 }; /* The only reference which exists at this point is on TC1. */ - tc1 = create_tc1(); + tc1 = create_tc1(self_comp); ok(tc1, "Initialize trace"); BT_ASSERT(tc1); init_weak_refs(tc1, &weak_tc1, &weak_sc1, &weak_sc2, &weak_ec1, @@ -362,7 +292,7 @@ static void test_example_scenario(void) /* User A acquires a reference to SC2 from TC1. */ user_a.sc = bt_trace_class_borrow_stream_class_by_index( user_a.tc, 1); - bt_object_get_ref(user_a.sc); + bt_stream_class_get_ref(user_a.sc); ok(user_a.sc, "User A acquires SC2 from TC1"); ok(bt_object_get_ref_count((void *) weak_tc1) == 2, "TC1 reference count is 2"); @@ -372,7 +302,7 @@ static void test_example_scenario(void) /* User A acquires a reference to EC3 from SC2. */ user_a.ec = bt_stream_class_borrow_event_class_by_index( user_a.sc, 0); - bt_object_get_ref(user_a.ec); + bt_event_class_get_ref(user_a.ec); ok(user_a.ec, "User A acquires EC3 from SC2"); ok(bt_object_get_ref_count((void *) weak_tc1) == 2, "TC1 reference count is 2"); @@ -383,7 +313,7 @@ static void test_example_scenario(void) /* User A releases its reference to SC2. */ diag("User A releases SC2"); - BT_OBJECT_PUT_REF_AND_RESET(user_a.sc); + BT_STREAM_CLASS_PUT_REF_AND_RESET(user_a.sc); /* * We keep the pointer to SC2 around to validate its reference * count. @@ -397,7 +327,7 @@ static void test_example_scenario(void) /* User A releases its reference to TC1. */ diag("User A releases TC1"); - BT_OBJECT_PUT_REF_AND_RESET(user_a.tc); + BT_TRACE_CLASS_PUT_REF_AND_RESET(user_a.tc); /* * We keep the pointer to TC1 around to validate its reference * count. @@ -412,7 +342,7 @@ static void test_example_scenario(void) /* User B acquires a reference to SC1. */ diag("User B acquires a reference to SC1"); user_b.sc = weak_sc1; - bt_object_get_ref(user_b.sc); + bt_stream_class_get_ref(user_b.sc); ok(bt_object_get_ref_count((void *) weak_tc1) == 2, "TC1 reference count is 2"); ok(bt_object_get_ref_count((void *) weak_sc1) == 1, @@ -422,7 +352,7 @@ static void test_example_scenario(void) diag("User C acquires a reference to EC1"); user_c.ec = bt_stream_class_borrow_event_class_by_index( user_b.sc, 0); - bt_object_get_ref(user_c.ec); + bt_event_class_get_ref(user_c.ec); ok(bt_object_get_ref_count((void *) weak_ec1) == 1, "EC1 reference count is 1"); ok(bt_object_get_ref_count((void *) weak_sc1) == 2, @@ -430,7 +360,7 @@ static void test_example_scenario(void) /* User A releases its reference on EC3. */ diag("User A releases its reference on EC3"); - BT_OBJECT_PUT_REF_AND_RESET(user_a.ec); + BT_EVENT_CLASS_PUT_REF_AND_RESET(user_a.ec); ok(bt_object_get_ref_count((void *) weak_ec3) == 0, "EC3 reference count is 1"); ok(bt_object_get_ref_count((void *) weak_sc2) == 0, @@ -440,7 +370,7 @@ static void test_example_scenario(void) /* User B releases its reference on SC1. */ diag("User B releases its reference on SC1"); - BT_OBJECT_PUT_REF_AND_RESET(user_b.sc); + BT_STREAM_CLASS_PUT_REF_AND_RESET(user_b.sc); ok(bt_object_get_ref_count((void *) weak_sc1) == 1, "SC1 reference count is 1"); @@ -462,7 +392,49 @@ static void test_example_scenario(void) "EC3 reference count is 0"); /* Reclaim last reference held by User C. */ - BT_OBJECT_PUT_REF_AND_RESET(user_c.ec); + BT_EVENT_CLASS_PUT_REF_AND_RESET(user_c.ec); +} + +static +bt_component_class_initialize_method_status src_init( + bt_self_component_source *self_comp, + bt_self_component_source_configuration *config, + const bt_value *params, void *init_method_data) +{ + test_example_scenario(self_comp); + return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; +} + +static +bt_message_iterator_class_next_method_status src_iter_next( + bt_self_message_iterator *self_iterator, + bt_message_array_const msgs, uint64_t capacity, + uint64_t *count) +{ + return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; +} + +static void test_example_scenario_in_graph(void) +{ + bt_message_iterator_class *msg_iter_cls; + bt_component_class_source *comp_cls; + bt_graph *graph; + int ret; + + msg_iter_cls = bt_message_iterator_class_create(src_iter_next); + BT_ASSERT(msg_iter_cls); + + comp_cls = bt_component_class_source_create("src", msg_iter_cls); + BT_ASSERT(comp_cls); + ret = bt_component_class_source_set_initialize_method(comp_cls, src_init); + BT_ASSERT(ret == 0); + graph = bt_graph_create(0); + ret = bt_graph_add_source_component(graph, comp_cls, "src-comp", + NULL, BT_LOGGING_LEVEL_NONE, NULL); + BT_ASSERT(ret == 0); + bt_graph_put_ref(graph); + bt_component_class_source_put_ref(comp_cls); + bt_message_iterator_class_put_ref(msg_iter_cls); } static void create_writer_user_full(struct writer_user *user) @@ -604,7 +576,7 @@ int main(int argc, char **argv) /* Initialize tap harness before any tests */ plan_tests(NR_TESTS); - test_example_scenario(); + test_example_scenario_in_graph(); test_put_order(); return exit_status();