Tests: add trace listener interface test
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 29 Jul 2016 20:54:37 +0000 (16:54 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 27 May 2017 17:41:38 +0000 (13:41 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/lib/Makefile.am
tests/lib/test_trace_listener.c

index b177414026941c33dc183cb89c93360ee34254d9..d240cbc0e1d9850078ae502b17a5df362e747845 100644 (file)
@@ -28,8 +28,11 @@ test_bt_ctf_field_type_validation_LDADD = $(COMMON_TEST_LDADD)
 
 test_ir_visit_LDADD = $(COMMON_TEST_LDADD)
 
+test_trace_listener_LDADD = $(COMMON_TEST_LDADD)
+
 noinst_PROGRAMS = test_seek test_bitfield test_ctf_writer test_bt_values \
-       test_ctf_ir_ref test_bt_ctf_field_type_validation test_ir_visit
+       test_ctf_ir_ref test_bt_ctf_field_type_validation test_ir_visit \
+       test_trace_listener
 
 test_seek_SOURCES = test_seek.c
 test_bitfield_SOURCES = test_bitfield.c
@@ -38,6 +41,7 @@ test_bt_values_SOURCES = test_bt_values.c
 test_ctf_ir_ref_SOURCES = test_ctf_ir_ref.c
 test_bt_ctf_field_type_validation_SOURCES = test_bt_ctf_field_type_validation.c
 test_ir_visit_SOURCES = test_ir_visit.c
+test_trace_listener_SOURCES = test_trace_listener.c
 
 check_SCRIPTS = test_seek_big_trace \
                test_seek_empty_packet \
index 49738d8f696bfdd2d968f9748149b0320156f2f1..03eaaf6b6e373d9173eea8e65978ba5e6494cdba 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * test_ir_visit.c
+ * test_trace_lister.c
  *
- * CTF IR visitor interface test
+ * CTF IR trace listener interface test
  *
  * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
 #include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf-ir/trace.h>
-#include <babeltrace/ctf-ir/visitor.h>
 #include <stdlib.h>
 #include <string.h>
 
-#define NR_TESTS 13
+#define NR_TESTS 21
 
 struct visitor_state {
        int i;
@@ -46,6 +45,10 @@ struct expected_result expected_results[] = {
        { "sc2", BT_CTF_IR_TYPE_STREAM_CLASS },
        { "ec2", BT_CTF_IR_TYPE_EVENT_CLASS },
        { "ec3", BT_CTF_IR_TYPE_EVENT_CLASS },
+       /* Elements added after the initial add_listener call. */
+       { "sc3", BT_CTF_IR_TYPE_STREAM_CLASS },
+       { "ec4", BT_CTF_IR_TYPE_EVENT_CLASS },
+       { "ec5", BT_CTF_IR_TYPE_EVENT_CLASS },
 };
 
 const char *element_type_str(enum bt_ctf_ir_type type)
@@ -140,9 +143,8 @@ error:
        goto end;
 }
 
-int visitor(struct bt_ctf_ir_element *element, void *data)
+void visitor(struct bt_ctf_ir_element *element, void *data)
 {
-       int ret = 0;
        bool names_match;
        const char *element_name;
        struct visitor_state *state = data;
@@ -157,8 +159,7 @@ int visitor(struct bt_ctf_ir_element *element, void *data)
                element_name = bt_ctf_stream_class_get_name(
                                bt_ctf_ir_element_get_element(element));
                if (!element_name) {
-                       ret = -1;
-                       goto end;
+                       return;
                }
 
                names_match = !strcmp(element_name, expected->element_name);
@@ -167,16 +168,14 @@ int visitor(struct bt_ctf_ir_element *element, void *data)
                element_name = bt_ctf_event_class_get_name(
                                bt_ctf_ir_element_get_element(element));
                if (!element_name) {
-                       ret = -1;
-                       goto end;
+                       return;
                }
 
                names_match = !strcmp(element_name, expected->element_name);
                break;
        default:
                diag("Encountered an unexpected type while visiting trace");
-               ret = -1;
-               goto end;
+               return;
        }
 
        ok(expected->element_type == bt_ctf_ir_element_get_type(element),
@@ -186,30 +185,67 @@ int visitor(struct bt_ctf_ir_element *element, void *data)
        ok(names_match, "Element name is %s, expected %s",
                        element_name ? : "NULL",
                        expected->element_name ? : "NULL");
-end:
-       return ret;
 }
 
 int main(int argc, char **argv)
 {
-       int ret;
+       int ret, index;
        struct bt_ctf_trace *trace;
        struct visitor_state state = { 0 };
-       
+       struct bt_ctf_stream_class *sc3;
+       struct bt_ctf_event_class *ec4, *ec5;
+
        plan_tests(NR_TESTS);
 
-       /*
-        * Initialize a reference trace which we'll walk using the
-        * bt_ctf_*_visit() interface.
-        */
        trace = init_trace();
        if (!trace) {
                diag("Failed to initialize reference trace, aborting.");
                exit(-1);
        }
 
-       ret = bt_ctf_trace_visit(trace, visitor, &state);
-       ok(!ret, "bt_ctf_trace_visit returned success");
+       ret = bt_ctf_trace_add_listener(trace, visitor, &state);
+       ok(!ret, "bt_ctf_trace_add_listener returned success");
+
+       /*
+        * Validate that listeners are notified when new elements are added to a
+        * trace.
+        */
+       sc3 = bt_ctf_stream_class_create("sc3");
+       if (!sc3) {
+               diag("Failed to create stream class, aborting.");
+               exit(-1);
+       }
+
+       ec4 = init_event_class("ec4");
+       ec5 = init_event_class("ec5");
+       if (!ec4 || !ec5) {
+               diag("Failed to create event classes, aborting.");
+               exit(-1);
+       }
+
+       ret = bt_ctf_stream_class_add_event_class(sc3, ec4);
+       if (ret) {
+               diag("Failed to add event class to stream class, aborting.");
+       }
+
+       index = state.i;
+       ret = bt_ctf_trace_add_stream_class(trace, sc3);
+       if (ret) {
+               diag("Failed to add stream class sc3 to trace, aborting.");
+               exit(-1);
+       }
+
+       /* Listener should have been invoked two times (sc3 + ec4). */
+       ok(index + 2 == state.i, "trace modification listener has been invoked twice after addition of a stream class");
+
+       index = state.i;
+       ret = bt_ctf_stream_class_add_event_class(sc3, ec5);
+       if (ret) {
+               diag("Failed to add event class to stream class, aborting.");
+               exit(-1);
+       }
+
+       ok(index + 1 == state.i, "trace modification has been invoked once after addition of an event class");
 
        BT_PUT(trace);
        return exit_status();
This page took 0.027276 seconds and 4 git commands to generate.