Commit | Line | Data |
---|---|---|
223c70b2 PP |
1 | /* |
2 | * Copyright 2017 - Philippe Proulx <pproulx@efficios.com> | |
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 | #include <stdio.h> | |
19 | #include <stdlib.h> | |
20 | #include <stdint.h> | |
c55a9f58 | 21 | #include <stdbool.h> |
223c70b2 PP |
22 | #include <inttypes.h> |
23 | #include <string.h> | |
e5be10ef | 24 | #include <babeltrace/babeltrace.h> |
25583cd0 | 25 | #include <babeltrace/assert-internal.h> |
223c70b2 PP |
26 | #include <glib.h> |
27 | ||
28 | #include "tap/tap.h" | |
29 | ||
ad847455 | 30 | #define NR_TESTS 5 |
223c70b2 PP |
31 | |
32 | enum test { | |
d6e69534 PP |
33 | TEST_NO_AUTO_MSGS, |
34 | TEST_OUTPUT_PORT_MESSAGE_ITERATOR, | |
223c70b2 PP |
35 | }; |
36 | ||
37 | enum test_event_type { | |
d6e69534 PP |
38 | TEST_EV_TYPE_MSG_UNEXPECTED, |
39 | TEST_EV_TYPE_MSG_EVENT, | |
40 | TEST_EV_TYPE_MSG_STREAM_BEGIN, | |
41 | TEST_EV_TYPE_MSG_PACKET_BEGIN, | |
42 | TEST_EV_TYPE_MSG_PACKET_END, | |
43 | TEST_EV_TYPE_MSG_STREAM_END, | |
223c70b2 PP |
44 | TEST_EV_TYPE_END, |
45 | TEST_EV_TYPE_SENTINEL, | |
46 | }; | |
47 | ||
48 | struct test_event { | |
49 | enum test_event_type type; | |
b19ff26f PP |
50 | const bt_stream *stream; |
51 | const bt_packet *packet; | |
223c70b2 PP |
52 | }; |
53 | ||
223c70b2 PP |
54 | static bool debug = false; |
55 | static enum test current_test; | |
56 | static GArray *test_events; | |
b19ff26f PP |
57 | static bt_graph *graph; |
58 | static bt_stream_class *src_stream_class; | |
59 | static bt_event_class *src_event_class; | |
60 | static bt_stream *src_stream1; | |
61 | static bt_stream *src_stream2; | |
62 | static bt_packet *src_stream1_packet1; | |
63 | static bt_packet *src_stream1_packet2; | |
64 | static bt_packet *src_stream2_packet1; | |
65 | static bt_packet *src_stream2_packet2; | |
223c70b2 PP |
66 | |
67 | enum { | |
68 | SEQ_END = -1, | |
69 | SEQ_STREAM1_BEGIN = -2, | |
70 | SEQ_STREAM2_BEGIN = -3, | |
71 | SEQ_STREAM1_END = -4, | |
72 | SEQ_STREAM2_END = -5, | |
73 | SEQ_STREAM1_PACKET1_BEGIN = -6, | |
74 | SEQ_STREAM1_PACKET2_BEGIN = -7, | |
75 | SEQ_STREAM2_PACKET1_BEGIN = -8, | |
76 | SEQ_STREAM2_PACKET2_BEGIN = -9, | |
77 | SEQ_STREAM1_PACKET1_END = -10, | |
78 | SEQ_STREAM1_PACKET2_END = -11, | |
79 | SEQ_STREAM2_PACKET1_END = -12, | |
80 | SEQ_STREAM2_PACKET2_END = -13, | |
81 | SEQ_EVENT_STREAM1_PACKET1 = -14, | |
82 | SEQ_EVENT_STREAM1_PACKET2 = -15, | |
83 | SEQ_EVENT_STREAM2_PACKET1 = -16, | |
84 | SEQ_EVENT_STREAM2_PACKET2 = -17, | |
223c70b2 PP |
85 | }; |
86 | ||
87 | struct src_iter_user_data { | |
88 | int64_t *seq; | |
89 | size_t at; | |
90 | }; | |
91 | ||
92 | struct sink_user_data { | |
d6e69534 | 93 | bt_self_component_port_input_message_iterator *msg_iter; |
223c70b2 PP |
94 | }; |
95 | ||
96 | /* | |
d6e69534 PP |
97 | * No automatic messages generated in this block. |
98 | * Stream 2 messages are more indented. | |
223c70b2 | 99 | */ |
d6e69534 | 100 | static int64_t seq_no_auto_msgs[] = { |
223c70b2 PP |
101 | SEQ_STREAM1_BEGIN, |
102 | SEQ_STREAM1_PACKET1_BEGIN, | |
103 | SEQ_EVENT_STREAM1_PACKET1, | |
104 | SEQ_EVENT_STREAM1_PACKET1, | |
105 | SEQ_STREAM2_BEGIN, | |
106 | SEQ_EVENT_STREAM1_PACKET1, | |
107 | SEQ_STREAM2_PACKET2_BEGIN, | |
108 | SEQ_EVENT_STREAM2_PACKET2, | |
109 | SEQ_EVENT_STREAM1_PACKET1, | |
110 | SEQ_STREAM1_PACKET1_END, | |
111 | SEQ_STREAM2_PACKET2_END, | |
112 | SEQ_STREAM1_PACKET2_BEGIN, | |
113 | SEQ_EVENT_STREAM1_PACKET2, | |
114 | SEQ_STREAM2_END, | |
115 | SEQ_STREAM1_PACKET2_END, | |
116 | SEQ_STREAM1_END, | |
117 | SEQ_END, | |
118 | }; | |
119 | ||
223c70b2 PP |
120 | static |
121 | void clear_test_events(void) | |
122 | { | |
123 | g_array_set_size(test_events, 0); | |
124 | } | |
125 | ||
126 | static | |
127 | void print_test_event(FILE *fp, const struct test_event *event) | |
128 | { | |
129 | fprintf(fp, "{ type = "); | |
130 | ||
131 | switch (event->type) { | |
d6e69534 PP |
132 | case TEST_EV_TYPE_MSG_UNEXPECTED: |
133 | fprintf(fp, "TEST_EV_TYPE_MSG_UNEXPECTED"); | |
223c70b2 | 134 | break; |
d6e69534 PP |
135 | case TEST_EV_TYPE_MSG_EVENT: |
136 | fprintf(fp, "TEST_EV_TYPE_MSG_EVENT"); | |
223c70b2 | 137 | break; |
d6e69534 PP |
138 | case TEST_EV_TYPE_MSG_STREAM_BEGIN: |
139 | fprintf(fp, "TEST_EV_TYPE_MSG_STREAM_BEGIN"); | |
223c70b2 | 140 | break; |
d6e69534 PP |
141 | case TEST_EV_TYPE_MSG_STREAM_END: |
142 | fprintf(fp, "TEST_EV_TYPE_MSG_STREAM_END"); | |
223c70b2 | 143 | break; |
d6e69534 PP |
144 | case TEST_EV_TYPE_MSG_PACKET_BEGIN: |
145 | fprintf(fp, "TEST_EV_TYPE_MSG_PACKET_BEGIN"); | |
223c70b2 | 146 | break; |
d6e69534 PP |
147 | case TEST_EV_TYPE_MSG_PACKET_END: |
148 | fprintf(fp, "TEST_EV_TYPE_MSG_PACKET_END"); | |
223c70b2 PP |
149 | break; |
150 | case TEST_EV_TYPE_END: | |
151 | fprintf(fp, "TEST_EV_TYPE_END"); | |
152 | break; | |
153 | case TEST_EV_TYPE_SENTINEL: | |
154 | fprintf(fp, "TEST_EV_TYPE_SENTINEL"); | |
155 | break; | |
156 | default: | |
157 | fprintf(fp, "(UNKNOWN)"); | |
158 | break; | |
159 | } | |
160 | ||
161 | fprintf(fp, ", stream = %p, packet = %p }", event->stream, | |
162 | event->packet); | |
163 | } | |
164 | ||
165 | static | |
166 | void append_test_event(struct test_event *event) | |
167 | { | |
168 | g_array_append_val(test_events, *event); | |
169 | } | |
170 | ||
171 | static | |
172 | bool compare_single_test_events(const struct test_event *ev_a, | |
173 | const struct test_event *ev_b) | |
174 | { | |
175 | if (debug) { | |
176 | fprintf(stderr, ":: Comparing test events: "); | |
177 | print_test_event(stderr, ev_a); | |
178 | fprintf(stderr, " vs. "); | |
179 | print_test_event(stderr, ev_b); | |
180 | fprintf(stderr, "\n"); | |
181 | } | |
182 | ||
183 | if (ev_a->type != ev_b->type) { | |
184 | return false; | |
185 | } | |
186 | ||
187 | switch (ev_a->type) { | |
188 | case TEST_EV_TYPE_END: | |
189 | case TEST_EV_TYPE_SENTINEL: | |
190 | break; | |
191 | default: | |
192 | if (ev_a->stream != ev_b->stream) { | |
193 | return false; | |
194 | } | |
195 | ||
196 | if (ev_a->packet != ev_b->packet) { | |
197 | return false; | |
198 | } | |
199 | break; | |
200 | } | |
201 | ||
202 | return true; | |
203 | } | |
204 | ||
205 | static | |
206 | bool compare_test_events(const struct test_event *expected_events) | |
207 | { | |
208 | const struct test_event *expected_event = expected_events; | |
209 | size_t i = 0; | |
210 | ||
25583cd0 | 211 | BT_ASSERT(expected_events); |
223c70b2 PP |
212 | |
213 | while (true) { | |
214 | const struct test_event *event; | |
215 | ||
216 | if (expected_event->type == TEST_EV_TYPE_SENTINEL) { | |
217 | break; | |
218 | } | |
219 | ||
220 | if (i >= test_events->len) { | |
221 | return false; | |
222 | } | |
223 | ||
224 | event = &g_array_index(test_events, struct test_event, i); | |
225 | ||
226 | if (!compare_single_test_events(event, expected_event)) { | |
227 | return false; | |
228 | } | |
229 | ||
230 | i++; | |
231 | expected_event++; | |
232 | } | |
233 | ||
234 | if (i != test_events->len) { | |
235 | return false; | |
236 | } | |
237 | ||
238 | return true; | |
239 | } | |
240 | ||
241 | static | |
41693723 | 242 | void init_static_data(bt_self_component_source *self_comp) |
223c70b2 | 243 | { |
b19ff26f PP |
244 | bt_trace_class *trace_class; |
245 | bt_trace *trace; | |
223c70b2 | 246 | |
862ca4ed | 247 | /* Metadata, streams, and packets*/ |
41693723 PP |
248 | trace_class = bt_trace_class_create( |
249 | bt_self_component_source_as_self_component(self_comp)); | |
250 | BT_ASSERT(trace_class); | |
862ca4ed | 251 | src_stream_class = bt_stream_class_create(trace_class); |
25583cd0 | 252 | BT_ASSERT(src_stream_class); |
40f4ba76 | 253 | src_event_class = bt_event_class_create(src_stream_class); |
44c440bc | 254 | BT_ASSERT(src_event_class); |
862ca4ed PP |
255 | trace = bt_trace_create(trace_class); |
256 | BT_ASSERT(trace); | |
257 | src_stream1 = bt_stream_create(src_stream_class, trace); | |
25583cd0 | 258 | BT_ASSERT(src_stream1); |
862ca4ed | 259 | src_stream2 = bt_stream_create(src_stream_class, trace); |
25583cd0 | 260 | BT_ASSERT(src_stream2); |
40f4ba76 | 261 | src_stream1_packet1 = bt_packet_create(src_stream1); |
25583cd0 | 262 | BT_ASSERT(src_stream1_packet1); |
40f4ba76 | 263 | src_stream1_packet2 = bt_packet_create(src_stream1); |
25583cd0 | 264 | BT_ASSERT(src_stream1_packet2); |
40f4ba76 | 265 | src_stream2_packet1 = bt_packet_create(src_stream2); |
25583cd0 | 266 | BT_ASSERT(src_stream2_packet1); |
40f4ba76 | 267 | src_stream2_packet2 = bt_packet_create(src_stream2); |
25583cd0 | 268 | BT_ASSERT(src_stream2_packet2); |
223c70b2 PP |
269 | |
270 | if (debug) { | |
271 | fprintf(stderr, ":: stream 1: %p\n", src_stream1); | |
272 | fprintf(stderr, ":: stream 2: %p\n", src_stream2); | |
273 | fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1); | |
274 | fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2); | |
275 | fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1); | |
276 | fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2); | |
277 | } | |
278 | ||
c5b9b441 PP |
279 | bt_trace_put_ref(trace); |
280 | bt_trace_class_put_ref(trace_class); | |
223c70b2 PP |
281 | } |
282 | ||
283 | static | |
284 | void fini_static_data(void) | |
285 | { | |
223c70b2 | 286 | /* Metadata */ |
c5b9b441 PP |
287 | bt_stream_class_put_ref(src_stream_class); |
288 | bt_event_class_put_ref(src_event_class); | |
289 | bt_stream_put_ref(src_stream1); | |
290 | bt_stream_put_ref(src_stream2); | |
291 | bt_packet_put_ref(src_stream1_packet1); | |
292 | bt_packet_put_ref(src_stream1_packet2); | |
293 | bt_packet_put_ref(src_stream2_packet1); | |
294 | bt_packet_put_ref(src_stream2_packet2); | |
223c70b2 PP |
295 | } |
296 | ||
297 | static | |
d6e69534 | 298 | void src_iter_finalize(bt_self_message_iterator *self_msg_iter) |
223c70b2 PP |
299 | { |
300 | struct src_iter_user_data *user_data = | |
d6e69534 PP |
301 | bt_self_message_iterator_get_data( |
302 | self_msg_iter); | |
223c70b2 PP |
303 | |
304 | if (user_data) { | |
305 | g_free(user_data); | |
306 | } | |
307 | } | |
308 | ||
309 | static | |
4cdfc5e8 | 310 | bt_self_message_iterator_status src_iter_init( |
d6e69534 | 311 | bt_self_message_iterator *self_msg_iter, |
b19ff26f PP |
312 | bt_self_component_source *self_comp, |
313 | bt_self_component_port_output *self_port) | |
223c70b2 PP |
314 | { |
315 | struct src_iter_user_data *user_data = | |
316 | g_new0(struct src_iter_user_data, 1); | |
223c70b2 | 317 | |
25583cd0 | 318 | BT_ASSERT(user_data); |
d6e69534 | 319 | bt_self_message_iterator_set_data(self_msg_iter, user_data); |
223c70b2 PP |
320 | |
321 | switch (current_test) { | |
d6e69534 PP |
322 | case TEST_NO_AUTO_MSGS: |
323 | case TEST_OUTPUT_PORT_MESSAGE_ITERATOR: | |
324 | user_data->seq = seq_no_auto_msgs; | |
223c70b2 | 325 | break; |
223c70b2 | 326 | default: |
0fbb9a9f | 327 | abort(); |
223c70b2 PP |
328 | } |
329 | ||
d6e69534 | 330 | return BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
223c70b2 PP |
331 | } |
332 | ||
223c70b2 | 333 | static |
d6e69534 | 334 | void src_iter_next_seq_one(bt_self_message_iterator* msg_iter, |
d94d92ac | 335 | struct src_iter_user_data *user_data, |
d6e69534 | 336 | const bt_message **msg) |
223c70b2 | 337 | { |
b19ff26f | 338 | bt_packet *event_packet = NULL; |
223c70b2 | 339 | |
d4393e08 | 340 | switch (user_data->seq[user_data->at]) { |
223c70b2 | 341 | case SEQ_STREAM1_BEGIN: |
d6e69534 | 342 | *msg = bt_message_stream_beginning_create(msg_iter, |
0d72b8c3 | 343 | src_stream1); |
223c70b2 PP |
344 | break; |
345 | case SEQ_STREAM2_BEGIN: | |
d6e69534 | 346 | *msg = bt_message_stream_beginning_create(msg_iter, |
0d72b8c3 | 347 | src_stream2); |
223c70b2 PP |
348 | break; |
349 | case SEQ_STREAM1_END: | |
d6e69534 | 350 | *msg = bt_message_stream_end_create(msg_iter, |
0d72b8c3 | 351 | src_stream1); |
223c70b2 PP |
352 | break; |
353 | case SEQ_STREAM2_END: | |
d6e69534 | 354 | *msg = bt_message_stream_end_create(msg_iter, |
0d72b8c3 | 355 | src_stream2); |
223c70b2 PP |
356 | break; |
357 | case SEQ_STREAM1_PACKET1_BEGIN: | |
d6e69534 | 358 | *msg = bt_message_packet_beginning_create(msg_iter, |
0d72b8c3 | 359 | src_stream1_packet1); |
223c70b2 PP |
360 | break; |
361 | case SEQ_STREAM1_PACKET2_BEGIN: | |
d6e69534 | 362 | *msg = bt_message_packet_beginning_create(msg_iter, |
0d72b8c3 | 363 | src_stream1_packet2); |
223c70b2 PP |
364 | break; |
365 | case SEQ_STREAM2_PACKET1_BEGIN: | |
d6e69534 | 366 | *msg = bt_message_packet_beginning_create(msg_iter, |
0d72b8c3 | 367 | src_stream2_packet1); |
223c70b2 PP |
368 | break; |
369 | case SEQ_STREAM2_PACKET2_BEGIN: | |
d6e69534 | 370 | *msg = bt_message_packet_beginning_create(msg_iter, |
0d72b8c3 | 371 | src_stream2_packet2); |
223c70b2 PP |
372 | break; |
373 | case SEQ_STREAM1_PACKET1_END: | |
d6e69534 | 374 | *msg = bt_message_packet_end_create(msg_iter, |
0d72b8c3 | 375 | src_stream1_packet1); |
223c70b2 PP |
376 | break; |
377 | case SEQ_STREAM1_PACKET2_END: | |
d6e69534 | 378 | *msg = bt_message_packet_end_create(msg_iter, |
0d72b8c3 | 379 | src_stream1_packet2); |
223c70b2 PP |
380 | break; |
381 | case SEQ_STREAM2_PACKET1_END: | |
d6e69534 | 382 | *msg = bt_message_packet_end_create(msg_iter, |
0d72b8c3 | 383 | src_stream2_packet1); |
223c70b2 PP |
384 | break; |
385 | case SEQ_STREAM2_PACKET2_END: | |
d6e69534 | 386 | *msg = bt_message_packet_end_create(msg_iter, |
0d72b8c3 | 387 | src_stream2_packet2); |
223c70b2 PP |
388 | break; |
389 | case SEQ_EVENT_STREAM1_PACKET1: | |
390 | event_packet = src_stream1_packet1; | |
391 | break; | |
392 | case SEQ_EVENT_STREAM1_PACKET2: | |
393 | event_packet = src_stream1_packet2; | |
394 | break; | |
395 | case SEQ_EVENT_STREAM2_PACKET1: | |
396 | event_packet = src_stream2_packet1; | |
397 | break; | |
398 | case SEQ_EVENT_STREAM2_PACKET2: | |
399 | event_packet = src_stream2_packet2; | |
400 | break; | |
401 | default: | |
0fbb9a9f | 402 | abort(); |
223c70b2 PP |
403 | } |
404 | ||
405 | if (event_packet) { | |
d6e69534 | 406 | *msg = bt_message_event_create(msg_iter, |
0d72b8c3 PP |
407 | src_event_class, |
408 | event_packet); | |
d4393e08 PP |
409 | } |
410 | ||
d6e69534 | 411 | BT_ASSERT(*msg); |
d4393e08 PP |
412 | user_data->at++; |
413 | } | |
414 | ||
415 | static | |
4cdfc5e8 | 416 | bt_self_message_iterator_status src_iter_next_seq( |
d6e69534 | 417 | bt_self_message_iterator *msg_iter, |
d4393e08 | 418 | struct src_iter_user_data *user_data, |
d6e69534 | 419 | bt_message_array_const msgs, uint64_t capacity, |
d4393e08 PP |
420 | uint64_t *count) |
421 | { | |
4cdfc5e8 | 422 | bt_self_message_iterator_status status = |
d6e69534 | 423 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
424 | uint64_t i = 0; |
425 | ||
426 | BT_ASSERT(user_data->seq); | |
427 | ||
428 | if (user_data->seq[user_data->at] == SEQ_END) { | |
d6e69534 | 429 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
d4393e08 | 430 | goto end; |
223c70b2 PP |
431 | } |
432 | ||
d4393e08 | 433 | while (i < capacity && user_data->seq[user_data->at] != SEQ_END) { |
d6e69534 | 434 | src_iter_next_seq_one(msg_iter, user_data, &msgs[i]); |
d4393e08 | 435 | i++; |
223c70b2 PP |
436 | } |
437 | ||
d4393e08 PP |
438 | BT_ASSERT(i > 0 && i <= capacity); |
439 | *count = i; | |
440 | ||
441 | end: | |
442 | return status; | |
223c70b2 PP |
443 | } |
444 | ||
445 | static | |
4cdfc5e8 | 446 | bt_self_message_iterator_status src_iter_next( |
d6e69534 PP |
447 | bt_self_message_iterator *self_msg_iter, |
448 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 | 449 | uint64_t *count) |
223c70b2 | 450 | { |
223c70b2 | 451 | struct src_iter_user_data *user_data = |
d6e69534 | 452 | bt_self_message_iterator_get_data(self_msg_iter); |
223c70b2 | 453 | |
25583cd0 | 454 | BT_ASSERT(user_data); |
d6e69534 | 455 | return src_iter_next_seq(self_msg_iter, user_data, msgs, |
d94d92ac | 456 | capacity, count); |
223c70b2 PP |
457 | } |
458 | ||
459 | static | |
4cdfc5e8 | 460 | bt_self_component_status src_init( |
b19ff26f PP |
461 | bt_self_component_source *self_comp, |
462 | const bt_value *params, void *init_method_data) | |
223c70b2 | 463 | { |
147337a3 | 464 | int ret; |
b9d103be | 465 | |
41693723 | 466 | init_static_data(self_comp); |
d94d92ac PP |
467 | ret = bt_self_component_source_add_output_port( |
468 | self_comp, "out", NULL, NULL); | |
25583cd0 | 469 | BT_ASSERT(ret == 0); |
d94d92ac | 470 | return BT_SELF_COMPONENT_STATUS_OK; |
223c70b2 PP |
471 | } |
472 | ||
473 | static | |
b19ff26f | 474 | void src_finalize(bt_self_component_source *self_comp) |
223c70b2 PP |
475 | { |
476 | } | |
477 | ||
478 | static | |
d6e69534 | 479 | void append_test_events_from_message(const bt_message *message) |
223c70b2 | 480 | { |
223c70b2 | 481 | struct test_event test_event = { 0 }; |
223c70b2 | 482 | |
d6e69534 PP |
483 | switch (bt_message_get_type(message)) { |
484 | case BT_MESSAGE_TYPE_EVENT: | |
223c70b2 | 485 | { |
b19ff26f | 486 | const bt_event *event; |
223c70b2 | 487 | |
d6e69534 PP |
488 | test_event.type = TEST_EV_TYPE_MSG_EVENT; |
489 | event = bt_message_event_borrow_event_const(message); | |
25583cd0 | 490 | BT_ASSERT(event); |
40f4ba76 | 491 | test_event.packet = bt_event_borrow_packet_const(event); |
25583cd0 | 492 | BT_ASSERT(test_event.packet); |
223c70b2 PP |
493 | break; |
494 | } | |
d6e69534 PP |
495 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
496 | test_event.type = TEST_EV_TYPE_MSG_STREAM_BEGIN; | |
223c70b2 | 497 | test_event.stream = |
d6e69534 | 498 | bt_message_stream_beginning_borrow_stream_const(message); |
25583cd0 | 499 | BT_ASSERT(test_event.stream); |
223c70b2 | 500 | break; |
d6e69534 PP |
501 | case BT_MESSAGE_TYPE_STREAM_END: |
502 | test_event.type = TEST_EV_TYPE_MSG_STREAM_END; | |
223c70b2 | 503 | test_event.stream = |
d6e69534 | 504 | bt_message_stream_end_borrow_stream_const(message); |
25583cd0 | 505 | BT_ASSERT(test_event.stream); |
223c70b2 | 506 | break; |
d6e69534 PP |
507 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
508 | test_event.type = TEST_EV_TYPE_MSG_PACKET_BEGIN; | |
223c70b2 | 509 | test_event.packet = |
d6e69534 | 510 | bt_message_packet_beginning_borrow_packet_const(message); |
25583cd0 | 511 | BT_ASSERT(test_event.packet); |
223c70b2 | 512 | break; |
d6e69534 PP |
513 | case BT_MESSAGE_TYPE_PACKET_END: |
514 | test_event.type = TEST_EV_TYPE_MSG_PACKET_END; | |
223c70b2 | 515 | test_event.packet = |
d6e69534 | 516 | bt_message_packet_end_borrow_packet_const(message); |
25583cd0 | 517 | BT_ASSERT(test_event.packet); |
223c70b2 PP |
518 | break; |
519 | default: | |
d6e69534 | 520 | test_event.type = TEST_EV_TYPE_MSG_UNEXPECTED; |
223c70b2 PP |
521 | break; |
522 | } | |
523 | ||
524 | if (test_event.packet) { | |
40f4ba76 PP |
525 | test_event.stream = bt_packet_borrow_stream_const( |
526 | test_event.packet); | |
25583cd0 | 527 | BT_ASSERT(test_event.stream); |
223c70b2 PP |
528 | } |
529 | ||
d4393e08 PP |
530 | append_test_event(&test_event); |
531 | } | |
532 | ||
533 | static | |
4cdfc5e8 | 534 | bt_message_iterator_status common_consume( |
d6e69534 | 535 | void *msg_iter, bool is_output_port_msg_iter) |
d4393e08 | 536 | { |
4cdfc5e8 | 537 | bt_message_iterator_status ret; |
d6e69534 | 538 | bt_message_array_const messages = NULL; |
d4393e08 PP |
539 | uint64_t count = 0; |
540 | struct test_event test_event = { 0 }; | |
541 | uint64_t i; | |
542 | ||
d6e69534 | 543 | BT_ASSERT(msg_iter); |
d4393e08 | 544 | |
d6e69534 PP |
545 | if (is_output_port_msg_iter) { |
546 | ret = bt_port_output_message_iterator_next(msg_iter, | |
547 | &messages, &count); | |
d4393e08 | 548 | } else { |
d6e69534 PP |
549 | ret = bt_self_component_port_input_message_iterator_next( |
550 | msg_iter, &messages, &count); | |
d4393e08 PP |
551 | } |
552 | ||
553 | if (ret < 0) { | |
554 | goto end; | |
555 | } | |
556 | ||
557 | switch (ret) { | |
d6e69534 | 558 | case BT_MESSAGE_ITERATOR_STATUS_END: |
d4393e08 | 559 | test_event.type = TEST_EV_TYPE_END; |
223c70b2 | 560 | append_test_event(&test_event); |
d4393e08 | 561 | goto end; |
d6e69534 | 562 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
d4393e08 PP |
563 | abort(); |
564 | default: | |
565 | break; | |
223c70b2 PP |
566 | } |
567 | ||
d6e69534 | 568 | BT_ASSERT(messages); |
d4393e08 PP |
569 | BT_ASSERT(count > 0); |
570 | ||
571 | for (i = 0; i < count; i++) { | |
d6e69534 PP |
572 | append_test_events_from_message(messages[i]); |
573 | bt_message_put_ref(messages[i]); | |
d4393e08 PP |
574 | } |
575 | ||
576 | end: | |
223c70b2 PP |
577 | return ret; |
578 | } | |
579 | ||
e893886e | 580 | static |
4cdfc5e8 | 581 | bt_self_component_status sink_consume( |
b19ff26f | 582 | bt_self_component_sink *self_comp) |
e893886e | 583 | { |
4cdfc5e8 | 584 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
e893886e | 585 | struct sink_user_data *user_data = |
d94d92ac | 586 | bt_self_component_get_data( |
707b7d35 | 587 | bt_self_component_sink_as_self_component( |
d94d92ac | 588 | self_comp)); |
4cdfc5e8 | 589 | bt_message_iterator_status it_ret; |
e893886e | 590 | |
d6e69534 PP |
591 | BT_ASSERT(user_data && user_data->msg_iter); |
592 | it_ret = common_consume(user_data->msg_iter, false); | |
e893886e PP |
593 | |
594 | if (it_ret < 0) { | |
d94d92ac | 595 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
e893886e PP |
596 | goto end; |
597 | } | |
598 | ||
599 | switch (it_ret) { | |
d6e69534 | 600 | case BT_MESSAGE_ITERATOR_STATUS_END: |
d94d92ac | 601 | ret = BT_SELF_COMPONENT_STATUS_END; |
d6e69534 PP |
602 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET( |
603 | user_data->msg_iter); | |
e893886e | 604 | goto end; |
d6e69534 | 605 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
e893886e PP |
606 | abort(); |
607 | default: | |
608 | break; | |
609 | } | |
610 | ||
611 | end: | |
612 | return ret; | |
613 | } | |
614 | ||
223c70b2 | 615 | static |
4cdfc5e8 | 616 | bt_self_component_status sink_port_connected( |
b19ff26f PP |
617 | bt_self_component_sink *self_comp, |
618 | bt_self_component_port_input *self_port, | |
619 | const bt_port_output *other_port) | |
223c70b2 | 620 | { |
d94d92ac PP |
621 | struct sink_user_data *user_data = |
622 | bt_self_component_get_data( | |
707b7d35 | 623 | bt_self_component_sink_as_self_component( |
d94d92ac | 624 | self_comp)); |
223c70b2 | 625 | |
25583cd0 | 626 | BT_ASSERT(user_data); |
d6e69534 PP |
627 | user_data->msg_iter = |
628 | bt_self_component_port_input_message_iterator_create( | |
d94d92ac PP |
629 | self_port); |
630 | return BT_SELF_COMPONENT_STATUS_OK; | |
223c70b2 PP |
631 | } |
632 | ||
633 | static | |
4cdfc5e8 | 634 | bt_self_component_status sink_init( |
b19ff26f PP |
635 | bt_self_component_sink *self_comp, |
636 | const bt_value *params, void *init_method_data) | |
223c70b2 PP |
637 | { |
638 | struct sink_user_data *user_data = g_new0(struct sink_user_data, 1); | |
639 | int ret; | |
640 | ||
25583cd0 | 641 | BT_ASSERT(user_data); |
d94d92ac | 642 | bt_self_component_set_data( |
707b7d35 | 643 | bt_self_component_sink_as_self_component(self_comp), |
223c70b2 | 644 | user_data); |
d94d92ac PP |
645 | ret = bt_self_component_sink_add_input_port( |
646 | self_comp, "in", NULL, NULL); | |
25583cd0 | 647 | BT_ASSERT(ret == 0); |
d94d92ac | 648 | return BT_SELF_COMPONENT_STATUS_OK; |
223c70b2 PP |
649 | } |
650 | ||
651 | static | |
b19ff26f | 652 | void sink_finalize(bt_self_component_sink *self_comp) |
223c70b2 | 653 | { |
d94d92ac PP |
654 | struct sink_user_data *user_data = |
655 | bt_self_component_get_data( | |
707b7d35 | 656 | bt_self_component_sink_as_self_component( |
d94d92ac | 657 | self_comp)); |
223c70b2 PP |
658 | |
659 | if (user_data) { | |
d6e69534 PP |
660 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET( |
661 | user_data->msg_iter); | |
223c70b2 PP |
662 | g_free(user_data); |
663 | } | |
664 | } | |
665 | ||
666 | static | |
b19ff26f PP |
667 | void create_source_sink(bt_graph *graph, |
668 | const bt_component_source **source, | |
669 | const bt_component_sink **sink) | |
223c70b2 | 670 | { |
b19ff26f PP |
671 | bt_component_class_source *src_comp_class; |
672 | bt_component_class_sink *sink_comp_class; | |
223c70b2 PP |
673 | int ret; |
674 | ||
675 | /* Create source component */ | |
e893886e | 676 | if (source) { |
0d72b8c3 | 677 | src_comp_class = bt_component_class_source_create("src", |
e893886e | 678 | src_iter_next); |
25583cd0 | 679 | BT_ASSERT(src_comp_class); |
0d72b8c3 | 680 | ret = bt_component_class_source_set_init_method( |
d94d92ac | 681 | src_comp_class, src_init); |
25583cd0 | 682 | BT_ASSERT(ret == 0); |
0d72b8c3 | 683 | ret = bt_component_class_source_set_finalize_method( |
d94d92ac | 684 | src_comp_class, src_finalize); |
25583cd0 | 685 | BT_ASSERT(ret == 0); |
d6e69534 | 686 | ret = bt_component_class_source_set_message_iterator_init_method( |
e893886e | 687 | src_comp_class, src_iter_init); |
25583cd0 | 688 | BT_ASSERT(ret == 0); |
d6e69534 | 689 | ret = bt_component_class_source_set_message_iterator_finalize_method( |
e893886e | 690 | src_comp_class, src_iter_finalize); |
25583cd0 | 691 | BT_ASSERT(ret == 0); |
0d72b8c3 PP |
692 | ret = bt_graph_add_source_component(graph, |
693 | src_comp_class, "source", NULL, source); | |
25583cd0 | 694 | BT_ASSERT(ret == 0); |
c5b9b441 | 695 | bt_component_class_source_put_ref(src_comp_class); |
e893886e | 696 | } |
223c70b2 PP |
697 | |
698 | /* Create sink component */ | |
e893886e | 699 | if (sink) { |
0d72b8c3 | 700 | sink_comp_class = bt_component_class_sink_create("sink", |
e893886e | 701 | sink_consume); |
25583cd0 | 702 | BT_ASSERT(sink_comp_class); |
0d72b8c3 | 703 | ret = bt_component_class_sink_set_init_method( |
d94d92ac | 704 | sink_comp_class, sink_init); |
25583cd0 | 705 | BT_ASSERT(ret == 0); |
0d72b8c3 | 706 | ret = bt_component_class_sink_set_finalize_method( |
d94d92ac | 707 | sink_comp_class, sink_finalize); |
0d72b8c3 | 708 | ret = bt_component_class_sink_set_input_port_connected_method( |
e893886e | 709 | sink_comp_class, sink_port_connected); |
25583cd0 | 710 | BT_ASSERT(ret == 0); |
0d72b8c3 PP |
711 | ret = bt_graph_add_sink_component(graph, |
712 | sink_comp_class, | |
d94d92ac | 713 | "sink", NULL, sink); |
25583cd0 | 714 | BT_ASSERT(ret == 0); |
c5b9b441 | 715 | bt_component_class_sink_put_ref(sink_comp_class); |
e893886e | 716 | } |
223c70b2 PP |
717 | } |
718 | ||
41693723 PP |
719 | typedef void (*compare_func_t)(void); |
720 | ||
223c70b2 | 721 | static |
41693723 | 722 | void do_std_test(enum test test, const char *name, compare_func_t compare_func) |
223c70b2 | 723 | { |
b19ff26f PP |
724 | const bt_component_source *src_comp; |
725 | const bt_component_sink *sink_comp; | |
726 | const bt_port_output *upstream_port; | |
727 | const bt_port_input *downstream_port; | |
4cdfc5e8 | 728 | bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
223c70b2 PP |
729 | |
730 | clear_test_events(); | |
731 | current_test = test; | |
732 | diag("test: %s", name); | |
25583cd0 | 733 | BT_ASSERT(!graph); |
0d72b8c3 | 734 | graph = bt_graph_create(); |
25583cd0 | 735 | BT_ASSERT(graph); |
36712f1d | 736 | create_source_sink(graph, &src_comp, &sink_comp); |
223c70b2 PP |
737 | |
738 | /* Connect source to sink */ | |
0d72b8c3 PP |
739 | upstream_port = |
740 | bt_component_source_borrow_output_port_by_name_const( | |
741 | src_comp, "out"); | |
25583cd0 | 742 | BT_ASSERT(upstream_port); |
0d72b8c3 | 743 | downstream_port = bt_component_sink_borrow_input_port_by_name_const( |
d94d92ac | 744 | sink_comp, "in"); |
25583cd0 | 745 | BT_ASSERT(downstream_port); |
0d72b8c3 | 746 | graph_status = bt_graph_connect_ports(graph, upstream_port, |
a256a42d | 747 | downstream_port, NULL); |
223c70b2 PP |
748 | |
749 | /* Run the graph until the end */ | |
750 | while (graph_status == BT_GRAPH_STATUS_OK || | |
751 | graph_status == BT_GRAPH_STATUS_AGAIN) { | |
0d72b8c3 | 752 | graph_status = bt_graph_run(graph); |
223c70b2 PP |
753 | } |
754 | ||
d94d92ac PP |
755 | ok(graph_status == BT_GRAPH_STATUS_END, |
756 | "graph finishes without any error"); | |
223c70b2 PP |
757 | |
758 | /* Compare the resulting test events */ | |
41693723 PP |
759 | if (compare_func) { |
760 | compare_func(); | |
223c70b2 PP |
761 | } |
762 | ||
c5b9b441 PP |
763 | bt_component_source_put_ref(src_comp); |
764 | bt_component_sink_put_ref(sink_comp); | |
41693723 | 765 | fini_static_data(); |
c5b9b441 | 766 | BT_GRAPH_PUT_REF_AND_RESET(graph); |
223c70b2 PP |
767 | } |
768 | ||
769 | static | |
41693723 | 770 | void test_no_auto_msgs_compare(void) |
223c70b2 PP |
771 | { |
772 | const struct test_event expected_test_events[] = { | |
d6e69534 PP |
773 | { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, |
774 | { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
775 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
776 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
777 | { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, | |
778 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
779 | { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
780 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
781 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
782 | { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
783 | { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
784 | { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
785 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
786 | { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream2, .packet = NULL, }, | |
787 | { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
788 | { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream1, .packet = NULL, }, | |
223c70b2 PP |
789 | { .type = TEST_EV_TYPE_END, }, |
790 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
791 | }; | |
792 | ||
41693723 PP |
793 | ok(compare_test_events(expected_test_events), |
794 | "the produced sequence of test events is the expected one"); | |
795 | } | |
796 | ||
797 | static | |
798 | void test_no_auto_msgs(void) | |
799 | { | |
d6e69534 | 800 | do_std_test(TEST_NO_AUTO_MSGS, "no automatic messages", |
41693723 | 801 | test_no_auto_msgs_compare); |
223c70b2 PP |
802 | } |
803 | ||
e893886e | 804 | static |
d6e69534 | 805 | void test_output_port_message_iterator(void) |
e893886e | 806 | { |
b19ff26f | 807 | const bt_component_source *src_comp; |
d6e69534 | 808 | bt_port_output_message_iterator *msg_iter; |
4cdfc5e8 | 809 | bt_message_iterator_status iter_status = |
d6e69534 | 810 | BT_MESSAGE_ITERATOR_STATUS_OK; |
b19ff26f | 811 | const bt_port_output *upstream_port; |
e893886e PP |
812 | |
813 | clear_test_events(); | |
d6e69534 PP |
814 | current_test = TEST_OUTPUT_PORT_MESSAGE_ITERATOR; |
815 | diag("test: output port message iterator"); | |
25583cd0 | 816 | BT_ASSERT(!graph); |
0d72b8c3 | 817 | graph = bt_graph_create(); |
25583cd0 | 818 | BT_ASSERT(graph); |
e893886e PP |
819 | create_source_sink(graph, &src_comp, NULL); |
820 | ||
d6e69534 | 821 | /* Create message iterator on source's output port */ |
41693723 PP |
822 | upstream_port = bt_component_source_borrow_output_port_by_name_const( |
823 | src_comp, "out"); | |
824 | msg_iter = bt_port_output_message_iterator_create(graph, upstream_port); | |
d6e69534 | 825 | ok(msg_iter, "bt_private_output_port_message_iterator_create() succeeds"); |
e893886e | 826 | |
d6e69534 PP |
827 | /* Consume the message iterator */ |
828 | while (iter_status == BT_MESSAGE_ITERATOR_STATUS_OK) { | |
829 | iter_status = common_consume(msg_iter, true); | |
e893886e PP |
830 | } |
831 | ||
d6e69534 PP |
832 | ok(iter_status == BT_MESSAGE_ITERATOR_STATUS_END, |
833 | "output port message iterator finishes without any error"); | |
e893886e PP |
834 | |
835 | /* Compare the resulting test events */ | |
41693723 PP |
836 | { |
837 | const struct test_event expected_test_events[] = { | |
838 | { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, | |
839 | { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
840 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
841 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
842 | { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, | |
843 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
844 | { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
845 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
846 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
847 | { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
848 | { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
849 | { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
850 | { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
851 | { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream2, .packet = NULL, }, | |
852 | { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
853 | { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream1, .packet = NULL, }, | |
854 | { .type = TEST_EV_TYPE_END, }, | |
855 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
856 | }; | |
857 | ||
858 | ok(compare_test_events(expected_test_events), | |
859 | "the produced sequence of test events is the expected one"); | |
860 | } | |
e893886e | 861 | |
41693723 | 862 | fini_static_data(); |
c5b9b441 PP |
863 | bt_component_source_put_ref(src_comp); |
864 | BT_GRAPH_PUT_REF_AND_RESET(graph); | |
d6e69534 | 865 | bt_port_output_message_iterator_put_ref(msg_iter); |
e893886e PP |
866 | } |
867 | ||
d6e69534 | 868 | #define DEBUG_ENV_VAR "TEST_BT_MESSAGE_ITERATOR_DEBUG" |
223c70b2 PP |
869 | |
870 | int main(int argc, char **argv) | |
871 | { | |
872 | if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) { | |
873 | debug = true; | |
874 | } | |
875 | ||
876 | plan_tests(NR_TESTS); | |
41693723 PP |
877 | test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event)); |
878 | BT_ASSERT(test_events); | |
d6e69534 PP |
879 | test_no_auto_msgs(); |
880 | test_output_port_message_iterator(); | |
41693723 | 881 | g_array_free(test_events, TRUE); |
223c70b2 PP |
882 | return exit_status(); |
883 | } |