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> | |
24 | #include <assert.h> | |
25 | #include <babeltrace/ctf-ir/event-class.h> | |
26 | #include <babeltrace/ctf-ir/event.h> | |
27 | #include <babeltrace/ctf-ir/field-types.h> | |
28 | #include <babeltrace/ctf-ir/fields.h> | |
29 | #include <babeltrace/ctf-ir/packet.h> | |
30 | #include <babeltrace/ctf-ir/stream-class.h> | |
31 | #include <babeltrace/ctf-ir/stream.h> | |
32 | #include <babeltrace/ctf-ir/trace.h> | |
33 | #include <babeltrace/graph/clock-class-priority-map.h> | |
34 | #include <babeltrace/graph/component-class-filter.h> | |
35 | #include <babeltrace/graph/component-class-sink.h> | |
36 | #include <babeltrace/graph/component-class-source.h> | |
37 | #include <babeltrace/graph/component-class.h> | |
38 | #include <babeltrace/graph/component-sink.h> | |
39 | #include <babeltrace/graph/component-source.h> | |
40 | #include <babeltrace/graph/component.h> | |
73d5c1ad | 41 | #include <babeltrace/graph/connection.h> |
223c70b2 PP |
42 | #include <babeltrace/graph/graph.h> |
43 | #include <babeltrace/graph/notification-event.h> | |
44 | #include <babeltrace/graph/notification-inactivity.h> | |
45 | #include <babeltrace/graph/notification-iterator.h> | |
46 | #include <babeltrace/graph/notification-packet.h> | |
47 | #include <babeltrace/graph/notification-stream.h> | |
71afbadd | 48 | #include <babeltrace/graph/output-port-notification-iterator.h> |
223c70b2 PP |
49 | #include <babeltrace/graph/port.h> |
50 | #include <babeltrace/graph/private-component-source.h> | |
b9d103be | 51 | #include <babeltrace/graph/private-component-sink.h> |
223c70b2 PP |
52 | #include <babeltrace/graph/private-component.h> |
53 | #include <babeltrace/graph/private-connection.h> | |
fe7265b5 | 54 | #include <babeltrace/graph/private-connection-private-notification-iterator.h> |
223c70b2 PP |
55 | #include <babeltrace/graph/private-port.h> |
56 | #include <babeltrace/plugin/plugin.h> | |
57 | #include <babeltrace/ref.h> | |
58 | #include <glib.h> | |
59 | ||
60 | #include "tap/tap.h" | |
61 | ||
6ff151ad | 62 | #define NR_TESTS 6 |
223c70b2 PP |
63 | |
64 | enum test { | |
65 | TEST_NO_AUTO_NOTIFS, | |
71afbadd | 66 | TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR, |
223c70b2 PP |
67 | }; |
68 | ||
69 | enum test_event_type { | |
70 | TEST_EV_TYPE_NOTIF_UNEXPECTED, | |
71 | TEST_EV_TYPE_NOTIF_EVENT, | |
72 | TEST_EV_TYPE_NOTIF_INACTIVITY, | |
73 | TEST_EV_TYPE_NOTIF_STREAM_BEGIN, | |
74 | TEST_EV_TYPE_NOTIF_PACKET_BEGIN, | |
75 | TEST_EV_TYPE_NOTIF_PACKET_END, | |
76 | TEST_EV_TYPE_NOTIF_STREAM_END, | |
77 | TEST_EV_TYPE_END, | |
78 | TEST_EV_TYPE_SENTINEL, | |
79 | }; | |
80 | ||
81 | struct test_event { | |
82 | enum test_event_type type; | |
839d52a5 PP |
83 | struct bt_stream *stream; |
84 | struct bt_packet *packet; | |
223c70b2 PP |
85 | }; |
86 | ||
223c70b2 PP |
87 | static bool debug = false; |
88 | static enum test current_test; | |
89 | static GArray *test_events; | |
90 | static struct bt_clock_class_priority_map *src_empty_cc_prio_map; | |
839d52a5 PP |
91 | static struct bt_stream_class *src_stream_class; |
92 | static struct bt_event_class *src_event_class; | |
93 | static struct bt_stream *src_stream1; | |
94 | static struct bt_stream *src_stream2; | |
95 | static struct bt_packet *src_stream1_packet1; | |
96 | static struct bt_packet *src_stream1_packet2; | |
97 | static struct bt_packet *src_stream2_packet1; | |
98 | static struct bt_packet *src_stream2_packet2; | |
223c70b2 PP |
99 | |
100 | enum { | |
101 | SEQ_END = -1, | |
102 | SEQ_STREAM1_BEGIN = -2, | |
103 | SEQ_STREAM2_BEGIN = -3, | |
104 | SEQ_STREAM1_END = -4, | |
105 | SEQ_STREAM2_END = -5, | |
106 | SEQ_STREAM1_PACKET1_BEGIN = -6, | |
107 | SEQ_STREAM1_PACKET2_BEGIN = -7, | |
108 | SEQ_STREAM2_PACKET1_BEGIN = -8, | |
109 | SEQ_STREAM2_PACKET2_BEGIN = -9, | |
110 | SEQ_STREAM1_PACKET1_END = -10, | |
111 | SEQ_STREAM1_PACKET2_END = -11, | |
112 | SEQ_STREAM2_PACKET1_END = -12, | |
113 | SEQ_STREAM2_PACKET2_END = -13, | |
114 | SEQ_EVENT_STREAM1_PACKET1 = -14, | |
115 | SEQ_EVENT_STREAM1_PACKET2 = -15, | |
116 | SEQ_EVENT_STREAM2_PACKET1 = -16, | |
117 | SEQ_EVENT_STREAM2_PACKET2 = -17, | |
118 | SEQ_INACTIVITY = -18, | |
119 | }; | |
120 | ||
121 | struct src_iter_user_data { | |
122 | int64_t *seq; | |
123 | size_t at; | |
124 | }; | |
125 | ||
126 | struct sink_user_data { | |
127 | struct bt_notification_iterator *notif_iter; | |
128 | }; | |
129 | ||
130 | /* | |
131 | * No automatic notifications generated in this block. | |
132 | * Stream 2 notifications are more indented. | |
133 | */ | |
134 | static int64_t seq_no_auto_notifs[] = { | |
135 | SEQ_STREAM1_BEGIN, | |
136 | SEQ_STREAM1_PACKET1_BEGIN, | |
137 | SEQ_EVENT_STREAM1_PACKET1, | |
138 | SEQ_EVENT_STREAM1_PACKET1, | |
139 | SEQ_STREAM2_BEGIN, | |
140 | SEQ_EVENT_STREAM1_PACKET1, | |
141 | SEQ_STREAM2_PACKET2_BEGIN, | |
142 | SEQ_EVENT_STREAM2_PACKET2, | |
143 | SEQ_EVENT_STREAM1_PACKET1, | |
144 | SEQ_STREAM1_PACKET1_END, | |
145 | SEQ_STREAM2_PACKET2_END, | |
146 | SEQ_STREAM1_PACKET2_BEGIN, | |
147 | SEQ_EVENT_STREAM1_PACKET2, | |
148 | SEQ_STREAM2_END, | |
149 | SEQ_STREAM1_PACKET2_END, | |
150 | SEQ_STREAM1_END, | |
151 | SEQ_END, | |
152 | }; | |
153 | ||
223c70b2 PP |
154 | static |
155 | void clear_test_events(void) | |
156 | { | |
157 | g_array_set_size(test_events, 0); | |
158 | } | |
159 | ||
160 | static | |
161 | void print_test_event(FILE *fp, const struct test_event *event) | |
162 | { | |
163 | fprintf(fp, "{ type = "); | |
164 | ||
165 | switch (event->type) { | |
166 | case TEST_EV_TYPE_NOTIF_UNEXPECTED: | |
167 | fprintf(fp, "TEST_EV_TYPE_NOTIF_UNEXPECTED"); | |
168 | break; | |
169 | case TEST_EV_TYPE_NOTIF_EVENT: | |
170 | fprintf(fp, "TEST_EV_TYPE_NOTIF_EVENT"); | |
171 | break; | |
172 | case TEST_EV_TYPE_NOTIF_INACTIVITY: | |
173 | fprintf(fp, "TEST_EV_TYPE_NOTIF_INACTIVITY"); | |
174 | break; | |
175 | case TEST_EV_TYPE_NOTIF_STREAM_BEGIN: | |
176 | fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN"); | |
177 | break; | |
178 | case TEST_EV_TYPE_NOTIF_STREAM_END: | |
179 | fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_END"); | |
180 | break; | |
181 | case TEST_EV_TYPE_NOTIF_PACKET_BEGIN: | |
182 | fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN"); | |
183 | break; | |
184 | case TEST_EV_TYPE_NOTIF_PACKET_END: | |
185 | fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_END"); | |
186 | break; | |
187 | case TEST_EV_TYPE_END: | |
188 | fprintf(fp, "TEST_EV_TYPE_END"); | |
189 | break; | |
190 | case TEST_EV_TYPE_SENTINEL: | |
191 | fprintf(fp, "TEST_EV_TYPE_SENTINEL"); | |
192 | break; | |
193 | default: | |
194 | fprintf(fp, "(UNKNOWN)"); | |
195 | break; | |
196 | } | |
197 | ||
198 | fprintf(fp, ", stream = %p, packet = %p }", event->stream, | |
199 | event->packet); | |
200 | } | |
201 | ||
202 | static | |
203 | void append_test_event(struct test_event *event) | |
204 | { | |
205 | g_array_append_val(test_events, *event); | |
206 | } | |
207 | ||
208 | static | |
209 | bool compare_single_test_events(const struct test_event *ev_a, | |
210 | const struct test_event *ev_b) | |
211 | { | |
212 | if (debug) { | |
213 | fprintf(stderr, ":: Comparing test events: "); | |
214 | print_test_event(stderr, ev_a); | |
215 | fprintf(stderr, " vs. "); | |
216 | print_test_event(stderr, ev_b); | |
217 | fprintf(stderr, "\n"); | |
218 | } | |
219 | ||
220 | if (ev_a->type != ev_b->type) { | |
221 | return false; | |
222 | } | |
223 | ||
224 | switch (ev_a->type) { | |
225 | case TEST_EV_TYPE_END: | |
226 | case TEST_EV_TYPE_SENTINEL: | |
227 | break; | |
228 | default: | |
229 | if (ev_a->stream != ev_b->stream) { | |
230 | return false; | |
231 | } | |
232 | ||
233 | if (ev_a->packet != ev_b->packet) { | |
234 | return false; | |
235 | } | |
236 | break; | |
237 | } | |
238 | ||
239 | return true; | |
240 | } | |
241 | ||
242 | static | |
243 | bool compare_test_events(const struct test_event *expected_events) | |
244 | { | |
245 | const struct test_event *expected_event = expected_events; | |
246 | size_t i = 0; | |
247 | ||
248 | assert(expected_events); | |
249 | ||
250 | while (true) { | |
251 | const struct test_event *event; | |
252 | ||
253 | if (expected_event->type == TEST_EV_TYPE_SENTINEL) { | |
254 | break; | |
255 | } | |
256 | ||
257 | if (i >= test_events->len) { | |
258 | return false; | |
259 | } | |
260 | ||
261 | event = &g_array_index(test_events, struct test_event, i); | |
262 | ||
263 | if (!compare_single_test_events(event, expected_event)) { | |
264 | return false; | |
265 | } | |
266 | ||
267 | i++; | |
268 | expected_event++; | |
269 | } | |
270 | ||
271 | if (i != test_events->len) { | |
272 | return false; | |
273 | } | |
274 | ||
275 | return true; | |
276 | } | |
277 | ||
278 | static | |
279 | void init_static_data(void) | |
280 | { | |
281 | int ret; | |
839d52a5 PP |
282 | struct bt_trace *trace; |
283 | struct bt_field_type *empty_struct_ft; | |
223c70b2 PP |
284 | |
285 | /* Test events */ | |
286 | test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event)); | |
287 | assert(test_events); | |
288 | ||
289 | /* Metadata */ | |
839d52a5 | 290 | empty_struct_ft = bt_field_type_structure_create(); |
223c70b2 | 291 | assert(empty_struct_ft); |
839d52a5 | 292 | trace = bt_trace_create(); |
223c70b2 | 293 | assert(trace); |
839d52a5 | 294 | ret = bt_trace_set_packet_header_type(trace, empty_struct_ft); |
223c70b2 PP |
295 | assert(ret == 0); |
296 | src_empty_cc_prio_map = bt_clock_class_priority_map_create(); | |
297 | assert(src_empty_cc_prio_map); | |
839d52a5 | 298 | src_stream_class = bt_stream_class_create("my-stream-class"); |
223c70b2 | 299 | assert(src_stream_class); |
839d52a5 | 300 | ret = bt_stream_class_set_packet_context_type(src_stream_class, |
223c70b2 PP |
301 | empty_struct_ft); |
302 | assert(ret == 0); | |
839d52a5 | 303 | ret = bt_stream_class_set_event_header_type(src_stream_class, |
223c70b2 PP |
304 | empty_struct_ft); |
305 | assert(ret == 0); | |
839d52a5 | 306 | ret = bt_stream_class_set_event_context_type(src_stream_class, |
223c70b2 PP |
307 | empty_struct_ft); |
308 | assert(ret == 0); | |
839d52a5 PP |
309 | src_event_class = bt_event_class_create("my-event-class"); |
310 | ret = bt_event_class_set_context_type(src_event_class, | |
223c70b2 PP |
311 | empty_struct_ft); |
312 | assert(ret == 0); | |
839d52a5 | 313 | ret = bt_event_class_set_context_type(src_event_class, |
223c70b2 PP |
314 | empty_struct_ft); |
315 | assert(ret == 0); | |
839d52a5 | 316 | ret = bt_stream_class_add_event_class(src_stream_class, |
223c70b2 PP |
317 | src_event_class); |
318 | assert(ret == 0); | |
839d52a5 | 319 | ret = bt_trace_add_stream_class(trace, src_stream_class); |
223c70b2 | 320 | assert(ret == 0); |
839d52a5 | 321 | src_stream1 = bt_stream_create(src_stream_class, "stream-1"); |
223c70b2 | 322 | assert(src_stream1); |
839d52a5 | 323 | src_stream2 = bt_stream_create(src_stream_class, "stream-2"); |
223c70b2 | 324 | assert(src_stream2); |
839d52a5 | 325 | src_stream1_packet1 = bt_packet_create(src_stream1); |
223c70b2 | 326 | assert(src_stream1_packet1); |
839d52a5 | 327 | src_stream1_packet2 = bt_packet_create(src_stream1); |
223c70b2 | 328 | assert(src_stream1_packet2); |
839d52a5 | 329 | src_stream2_packet1 = bt_packet_create(src_stream2); |
223c70b2 | 330 | assert(src_stream2_packet1); |
839d52a5 | 331 | src_stream2_packet2 = bt_packet_create(src_stream2); |
223c70b2 PP |
332 | assert(src_stream2_packet2); |
333 | ||
334 | if (debug) { | |
335 | fprintf(stderr, ":: stream 1: %p\n", src_stream1); | |
336 | fprintf(stderr, ":: stream 2: %p\n", src_stream2); | |
337 | fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1); | |
338 | fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2); | |
339 | fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1); | |
340 | fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2); | |
341 | } | |
342 | ||
343 | bt_put(trace); | |
344 | bt_put(empty_struct_ft); | |
345 | } | |
346 | ||
347 | static | |
348 | void fini_static_data(void) | |
349 | { | |
350 | /* Test events */ | |
351 | g_array_free(test_events, TRUE); | |
352 | ||
353 | /* Metadata */ | |
354 | bt_put(src_empty_cc_prio_map); | |
355 | bt_put(src_stream_class); | |
356 | bt_put(src_event_class); | |
357 | bt_put(src_stream1); | |
358 | bt_put(src_stream2); | |
359 | bt_put(src_stream1_packet1); | |
360 | bt_put(src_stream1_packet2); | |
361 | bt_put(src_stream2_packet1); | |
362 | bt_put(src_stream2_packet2); | |
363 | } | |
364 | ||
365 | static | |
366 | void src_iter_finalize( | |
fe7265b5 | 367 | struct bt_private_connection_private_notification_iterator *private_notification_iterator) |
223c70b2 PP |
368 | { |
369 | struct src_iter_user_data *user_data = | |
fe7265b5 | 370 | bt_private_connection_private_notification_iterator_get_user_data( |
223c70b2 PP |
371 | private_notification_iterator); |
372 | ||
373 | if (user_data) { | |
374 | g_free(user_data); | |
375 | } | |
376 | } | |
377 | ||
378 | static | |
379 | enum bt_notification_iterator_status src_iter_init( | |
fe7265b5 | 380 | struct bt_private_connection_private_notification_iterator *priv_notif_iter, |
223c70b2 PP |
381 | struct bt_private_port *private_port) |
382 | { | |
383 | struct src_iter_user_data *user_data = | |
384 | g_new0(struct src_iter_user_data, 1); | |
385 | int ret; | |
386 | ||
387 | assert(user_data); | |
71afbadd PP |
388 | ret = bt_private_connection_private_notification_iterator_set_user_data( |
389 | priv_notif_iter, user_data); | |
223c70b2 PP |
390 | assert(ret == 0); |
391 | ||
392 | switch (current_test) { | |
393 | case TEST_NO_AUTO_NOTIFS: | |
71afbadd | 394 | case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR: |
223c70b2 PP |
395 | user_data->seq = seq_no_auto_notifs; |
396 | break; | |
223c70b2 | 397 | default: |
0fbb9a9f | 398 | abort(); |
223c70b2 PP |
399 | } |
400 | ||
401 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
402 | } | |
403 | ||
404 | static | |
839d52a5 | 405 | struct bt_event *src_create_event(struct bt_packet *packet) |
223c70b2 | 406 | { |
839d52a5 | 407 | struct bt_event *event = bt_event_create(src_event_class); |
223c70b2 PP |
408 | int ret; |
409 | ||
410 | assert(event); | |
839d52a5 | 411 | ret = bt_event_set_packet(event, packet); |
223c70b2 PP |
412 | assert(ret == 0); |
413 | return event; | |
414 | } | |
415 | ||
416 | static | |
fe7265b5 | 417 | struct bt_notification_iterator_next_method_return src_iter_next_seq( |
223c70b2 PP |
418 | struct src_iter_user_data *user_data) |
419 | { | |
fe7265b5 | 420 | struct bt_notification_iterator_next_method_return next_return = { |
223c70b2 PP |
421 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, |
422 | }; | |
423 | int64_t cur_ts_ns; | |
839d52a5 | 424 | struct bt_packet *event_packet = NULL; |
223c70b2 PP |
425 | |
426 | assert(user_data->seq); | |
427 | cur_ts_ns = user_data->seq[user_data->at]; | |
428 | ||
429 | switch (cur_ts_ns) { | |
430 | case SEQ_END: | |
431 | next_return.status = | |
432 | BT_NOTIFICATION_ITERATOR_STATUS_END; | |
433 | break; | |
434 | case SEQ_INACTIVITY: | |
435 | next_return.notification = | |
436 | bt_notification_inactivity_create(src_empty_cc_prio_map); | |
437 | assert(next_return.notification); | |
438 | break; | |
439 | case SEQ_STREAM1_BEGIN: | |
440 | next_return.notification = | |
441 | bt_notification_stream_begin_create(src_stream1); | |
442 | assert(next_return.notification); | |
443 | break; | |
444 | case SEQ_STREAM2_BEGIN: | |
445 | next_return.notification = | |
446 | bt_notification_stream_begin_create(src_stream2); | |
447 | assert(next_return.notification); | |
448 | break; | |
449 | case SEQ_STREAM1_END: | |
450 | next_return.notification = | |
451 | bt_notification_stream_end_create(src_stream1); | |
452 | assert(next_return.notification); | |
453 | break; | |
454 | case SEQ_STREAM2_END: | |
455 | next_return.notification = | |
456 | bt_notification_stream_end_create(src_stream2); | |
457 | assert(next_return.notification); | |
458 | break; | |
459 | case SEQ_STREAM1_PACKET1_BEGIN: | |
460 | next_return.notification = | |
461 | bt_notification_packet_begin_create(src_stream1_packet1); | |
462 | assert(next_return.notification); | |
463 | break; | |
464 | case SEQ_STREAM1_PACKET2_BEGIN: | |
465 | next_return.notification = | |
466 | bt_notification_packet_begin_create(src_stream1_packet2); | |
467 | assert(next_return.notification); | |
468 | break; | |
469 | case SEQ_STREAM2_PACKET1_BEGIN: | |
470 | next_return.notification = | |
471 | bt_notification_packet_begin_create(src_stream2_packet1); | |
472 | assert(next_return.notification); | |
473 | break; | |
474 | case SEQ_STREAM2_PACKET2_BEGIN: | |
475 | next_return.notification = | |
476 | bt_notification_packet_begin_create(src_stream2_packet2); | |
477 | assert(next_return.notification); | |
478 | break; | |
479 | case SEQ_STREAM1_PACKET1_END: | |
480 | next_return.notification = | |
481 | bt_notification_packet_end_create(src_stream1_packet1); | |
482 | assert(next_return.notification); | |
483 | break; | |
484 | case SEQ_STREAM1_PACKET2_END: | |
485 | next_return.notification = | |
486 | bt_notification_packet_end_create(src_stream1_packet2); | |
487 | assert(next_return.notification); | |
488 | break; | |
489 | case SEQ_STREAM2_PACKET1_END: | |
490 | next_return.notification = | |
491 | bt_notification_packet_end_create(src_stream2_packet1); | |
492 | assert(next_return.notification); | |
493 | break; | |
494 | case SEQ_STREAM2_PACKET2_END: | |
495 | next_return.notification = | |
496 | bt_notification_packet_end_create(src_stream2_packet2); | |
497 | assert(next_return.notification); | |
498 | break; | |
499 | case SEQ_EVENT_STREAM1_PACKET1: | |
500 | event_packet = src_stream1_packet1; | |
501 | break; | |
502 | case SEQ_EVENT_STREAM1_PACKET2: | |
503 | event_packet = src_stream1_packet2; | |
504 | break; | |
505 | case SEQ_EVENT_STREAM2_PACKET1: | |
506 | event_packet = src_stream2_packet1; | |
507 | break; | |
508 | case SEQ_EVENT_STREAM2_PACKET2: | |
509 | event_packet = src_stream2_packet2; | |
510 | break; | |
511 | default: | |
0fbb9a9f | 512 | abort(); |
223c70b2 PP |
513 | } |
514 | ||
515 | if (event_packet) { | |
839d52a5 | 516 | struct bt_event *event = src_create_event(event_packet); |
223c70b2 PP |
517 | |
518 | assert(event); | |
519 | next_return.notification = bt_notification_event_create(event, | |
520 | src_empty_cc_prio_map); | |
521 | bt_put(event); | |
522 | assert(next_return.notification); | |
523 | } | |
524 | ||
525 | if (next_return.status != BT_NOTIFICATION_ITERATOR_STATUS_END) { | |
526 | user_data->at++; | |
527 | } | |
528 | ||
529 | return next_return; | |
530 | } | |
531 | ||
532 | static | |
fe7265b5 PP |
533 | struct bt_notification_iterator_next_method_return src_iter_next( |
534 | struct bt_private_connection_private_notification_iterator *priv_iterator) | |
223c70b2 | 535 | { |
fe7265b5 | 536 | struct bt_notification_iterator_next_method_return next_return = { |
223c70b2 PP |
537 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, |
538 | .notification = NULL, | |
539 | }; | |
540 | struct src_iter_user_data *user_data = | |
fe7265b5 | 541 | bt_private_connection_private_notification_iterator_get_user_data(priv_iterator); |
223c70b2 PP |
542 | |
543 | assert(user_data); | |
544 | next_return = src_iter_next_seq(user_data); | |
545 | return next_return; | |
546 | } | |
547 | ||
548 | static | |
549 | enum bt_component_status src_init( | |
550 | struct bt_private_component *private_component, | |
551 | struct bt_value *params, void *init_method_data) | |
552 | { | |
147337a3 | 553 | int ret; |
b9d103be | 554 | |
147337a3 PP |
555 | ret = bt_private_component_source_add_output_private_port( |
556 | private_component, "out", NULL, NULL); | |
557 | assert(ret == 0); | |
223c70b2 PP |
558 | return BT_COMPONENT_STATUS_OK; |
559 | } | |
560 | ||
561 | static | |
562 | void src_finalize(struct bt_private_component *private_component) | |
563 | { | |
564 | } | |
565 | ||
566 | static | |
71afbadd PP |
567 | enum bt_notification_iterator_status common_consume( |
568 | struct bt_notification_iterator *notif_iter) | |
223c70b2 | 569 | { |
71afbadd | 570 | enum bt_notification_iterator_status ret; |
223c70b2 | 571 | struct bt_notification *notification = NULL; |
223c70b2 PP |
572 | struct test_event test_event = { 0 }; |
573 | bool do_append_test_event = true; | |
71afbadd | 574 | assert(notif_iter); |
223c70b2 | 575 | |
71afbadd PP |
576 | ret = bt_notification_iterator_next(notif_iter); |
577 | if (ret < 0) { | |
223c70b2 PP |
578 | do_append_test_event = false; |
579 | goto end; | |
580 | } | |
581 | ||
71afbadd | 582 | switch (ret) { |
223c70b2 PP |
583 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
584 | test_event.type = TEST_EV_TYPE_END; | |
223c70b2 PP |
585 | goto end; |
586 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
0fbb9a9f | 587 | abort(); |
223c70b2 PP |
588 | default: |
589 | break; | |
590 | } | |
591 | ||
592 | notification = bt_notification_iterator_get_notification( | |
71afbadd | 593 | notif_iter); |
223c70b2 PP |
594 | assert(notification); |
595 | ||
596 | switch (bt_notification_get_type(notification)) { | |
597 | case BT_NOTIFICATION_TYPE_EVENT: | |
598 | { | |
839d52a5 | 599 | struct bt_event *event; |
223c70b2 PP |
600 | |
601 | test_event.type = TEST_EV_TYPE_NOTIF_EVENT; | |
602 | event = bt_notification_event_get_event(notification); | |
603 | assert(event); | |
839d52a5 | 604 | test_event.packet = bt_event_get_packet(event); |
223c70b2 PP |
605 | bt_put(event); |
606 | assert(test_event.packet); | |
607 | bt_put(test_event.packet); | |
608 | break; | |
609 | } | |
610 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
611 | test_event.type = TEST_EV_TYPE_NOTIF_INACTIVITY; | |
612 | break; | |
613 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
614 | test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN; | |
615 | test_event.stream = | |
616 | bt_notification_stream_begin_get_stream(notification); | |
617 | assert(test_event.stream); | |
618 | bt_put(test_event.stream); | |
619 | break; | |
620 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
621 | test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END; | |
622 | test_event.stream = | |
623 | bt_notification_stream_end_get_stream(notification); | |
624 | assert(test_event.stream); | |
625 | bt_put(test_event.stream); | |
626 | break; | |
627 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
628 | test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN; | |
629 | test_event.packet = | |
630 | bt_notification_packet_begin_get_packet(notification); | |
631 | assert(test_event.packet); | |
632 | bt_put(test_event.packet); | |
633 | break; | |
634 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
635 | test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END; | |
636 | test_event.packet = | |
637 | bt_notification_packet_end_get_packet(notification); | |
638 | assert(test_event.packet); | |
639 | bt_put(test_event.packet); | |
640 | break; | |
641 | default: | |
642 | test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED; | |
643 | break; | |
644 | } | |
645 | ||
646 | if (test_event.packet) { | |
839d52a5 | 647 | test_event.stream = bt_packet_get_stream(test_event.packet); |
223c70b2 PP |
648 | assert(test_event.stream); |
649 | bt_put(test_event.stream); | |
650 | } | |
651 | ||
652 | end: | |
653 | if (do_append_test_event) { | |
654 | append_test_event(&test_event); | |
655 | } | |
656 | ||
657 | bt_put(notification); | |
658 | return ret; | |
659 | } | |
660 | ||
71afbadd PP |
661 | static |
662 | enum bt_component_status sink_consume( | |
663 | struct bt_private_component *priv_component) | |
664 | { | |
665 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
666 | struct sink_user_data *user_data = | |
667 | bt_private_component_get_user_data(priv_component); | |
668 | enum bt_notification_iterator_status it_ret; | |
669 | ||
670 | assert(user_data && user_data->notif_iter); | |
671 | it_ret = common_consume(user_data->notif_iter); | |
672 | ||
673 | if (it_ret < 0) { | |
674 | ret = BT_COMPONENT_STATUS_ERROR; | |
675 | goto end; | |
676 | } | |
677 | ||
678 | switch (it_ret) { | |
679 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
680 | ret = BT_COMPONENT_STATUS_END; | |
681 | BT_PUT(user_data->notif_iter); | |
682 | goto end; | |
683 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
684 | abort(); | |
685 | default: | |
686 | break; | |
687 | } | |
688 | ||
689 | end: | |
690 | return ret; | |
691 | } | |
692 | ||
223c70b2 PP |
693 | static |
694 | void sink_port_connected(struct bt_private_component *private_component, | |
695 | struct bt_private_port *self_private_port, | |
696 | struct bt_port *other_port) | |
697 | { | |
698 | struct bt_private_connection *priv_conn = | |
699 | bt_private_port_get_private_connection(self_private_port); | |
700 | struct sink_user_data *user_data = bt_private_component_get_user_data( | |
701 | private_component); | |
73d5c1ad | 702 | enum bt_connection_status conn_status; |
223c70b2 PP |
703 | |
704 | assert(user_data); | |
705 | assert(priv_conn); | |
73d5c1ad | 706 | conn_status = bt_private_connection_create_notification_iterator( |
6ff151ad | 707 | priv_conn, &user_data->notif_iter); |
73d5c1ad | 708 | assert(conn_status == 0); |
223c70b2 PP |
709 | bt_put(priv_conn); |
710 | } | |
711 | ||
712 | static | |
713 | enum bt_component_status sink_init( | |
714 | struct bt_private_component *private_component, | |
715 | struct bt_value *params, void *init_method_data) | |
716 | { | |
717 | struct sink_user_data *user_data = g_new0(struct sink_user_data, 1); | |
718 | int ret; | |
719 | ||
720 | assert(user_data); | |
721 | ret = bt_private_component_set_user_data(private_component, | |
722 | user_data); | |
723 | assert(ret == 0); | |
147337a3 PP |
724 | ret = bt_private_component_sink_add_input_private_port( |
725 | private_component, "in", NULL, NULL); | |
726 | assert(ret == 0); | |
223c70b2 PP |
727 | return BT_COMPONENT_STATUS_OK; |
728 | } | |
729 | ||
730 | static | |
731 | void sink_finalize(struct bt_private_component *private_component) | |
732 | { | |
733 | struct sink_user_data *user_data = bt_private_component_get_user_data( | |
734 | private_component); | |
735 | ||
736 | if (user_data) { | |
737 | bt_put(user_data->notif_iter); | |
738 | g_free(user_data); | |
739 | } | |
740 | } | |
741 | ||
742 | static | |
36712f1d | 743 | void create_source_sink(struct bt_graph *graph, struct bt_component **source, |
223c70b2 PP |
744 | struct bt_component **sink) |
745 | { | |
746 | struct bt_component_class *src_comp_class; | |
747 | struct bt_component_class *sink_comp_class; | |
748 | int ret; | |
749 | ||
750 | /* Create source component */ | |
71afbadd PP |
751 | if (source) { |
752 | src_comp_class = bt_component_class_source_create("src", | |
753 | src_iter_next); | |
754 | assert(src_comp_class); | |
755 | ret = bt_component_class_set_init_method(src_comp_class, | |
756 | src_init); | |
757 | assert(ret == 0); | |
758 | ret = bt_component_class_set_finalize_method(src_comp_class, | |
759 | src_finalize); | |
760 | assert(ret == 0); | |
761 | ret = bt_component_class_source_set_notification_iterator_init_method( | |
762 | src_comp_class, src_iter_init); | |
763 | assert(ret == 0); | |
764 | ret = bt_component_class_source_set_notification_iterator_finalize_method( | |
765 | src_comp_class, src_iter_finalize); | |
766 | assert(ret == 0); | |
767 | ret = bt_graph_add_component(graph, src_comp_class, "source", | |
768 | NULL, source); | |
769 | assert(ret == 0); | |
770 | bt_put(src_comp_class); | |
771 | } | |
223c70b2 PP |
772 | |
773 | /* Create sink component */ | |
71afbadd PP |
774 | if (sink) { |
775 | sink_comp_class = bt_component_class_sink_create("sink", | |
776 | sink_consume); | |
777 | assert(sink_comp_class); | |
778 | ret = bt_component_class_set_init_method(sink_comp_class, | |
779 | sink_init); | |
780 | assert(ret == 0); | |
781 | ret = bt_component_class_set_finalize_method(sink_comp_class, | |
782 | sink_finalize); | |
783 | ret = bt_component_class_set_port_connected_method( | |
784 | sink_comp_class, sink_port_connected); | |
785 | assert(ret == 0); | |
786 | ret = bt_graph_add_component(graph, sink_comp_class, "sink", | |
787 | NULL, sink); | |
788 | assert(ret == 0); | |
789 | bt_put(sink_comp_class); | |
790 | } | |
223c70b2 PP |
791 | } |
792 | ||
793 | static | |
794 | void do_std_test(enum test test, const char *name, | |
795 | const struct test_event *expected_test_events) | |
796 | { | |
797 | struct bt_component *src_comp; | |
798 | struct bt_component *sink_comp; | |
799 | struct bt_port *upstream_port; | |
800 | struct bt_port *downstream_port; | |
801 | struct bt_graph *graph; | |
223c70b2 PP |
802 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
803 | ||
804 | clear_test_events(); | |
805 | current_test = test; | |
806 | diag("test: %s", name); | |
223c70b2 PP |
807 | graph = bt_graph_create(); |
808 | assert(graph); | |
36712f1d | 809 | create_source_sink(graph, &src_comp, &sink_comp); |
223c70b2 PP |
810 | |
811 | /* Connect source to sink */ | |
b9d103be | 812 | upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out"); |
223c70b2 | 813 | assert(upstream_port); |
b9d103be | 814 | downstream_port = bt_component_sink_get_input_port_by_name(sink_comp, "in"); |
223c70b2 | 815 | assert(downstream_port); |
a256a42d PP |
816 | graph_status = bt_graph_connect_ports(graph, upstream_port, |
817 | downstream_port, NULL); | |
223c70b2 PP |
818 | bt_put(upstream_port); |
819 | bt_put(downstream_port); | |
820 | ||
821 | /* Run the graph until the end */ | |
822 | while (graph_status == BT_GRAPH_STATUS_OK || | |
823 | graph_status == BT_GRAPH_STATUS_AGAIN) { | |
824 | graph_status = bt_graph_run(graph); | |
825 | } | |
826 | ||
827 | ok(graph_status == BT_GRAPH_STATUS_END, "graph finishes without any error"); | |
828 | ||
829 | /* Compare the resulting test events */ | |
830 | if (expected_test_events) { | |
831 | ok(compare_test_events(expected_test_events), | |
832 | "the produced sequence of test events is the expected one"); | |
833 | } | |
834 | ||
835 | bt_put(src_comp); | |
836 | bt_put(sink_comp); | |
837 | bt_put(graph); | |
838 | } | |
839 | ||
840 | static | |
841 | void test_no_auto_notifs(void) | |
842 | { | |
843 | const struct test_event expected_test_events[] = { | |
844 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, | |
845 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
846 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
847 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
848 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, | |
849 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
850 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
851 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
852 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
853 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
854 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
855 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
856 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
857 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, }, | |
858 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
859 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, }, | |
860 | { .type = TEST_EV_TYPE_END, }, | |
861 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
862 | }; | |
863 | ||
864 | do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications", | |
865 | expected_test_events); | |
866 | } | |
867 | ||
71afbadd PP |
868 | static |
869 | void test_output_port_notification_iterator(void) | |
870 | { | |
871 | const struct test_event expected_test_events[] = { | |
872 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, | |
873 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
874 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
875 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
876 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, | |
877 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
878 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
879 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
880 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
881 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
882 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
883 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
884 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
885 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, }, | |
886 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
887 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, }, | |
888 | { .type = TEST_EV_TYPE_END, }, | |
889 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
890 | }; | |
891 | struct bt_component *src_comp; | |
892 | struct bt_notification_iterator *notif_iter; | |
893 | enum bt_notification_iterator_status iter_status = | |
894 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
895 | struct bt_port *upstream_port; | |
896 | struct bt_graph *graph; | |
897 | ||
898 | clear_test_events(); | |
899 | current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR; | |
900 | diag("test: output port notification iterator"); | |
901 | graph = bt_graph_create(); | |
902 | assert(graph); | |
903 | create_source_sink(graph, &src_comp, NULL); | |
904 | ||
905 | /* Create notification iterator on source's output port */ | |
906 | upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out"); | |
907 | notif_iter = bt_output_port_notification_iterator_create(upstream_port, | |
6ff151ad | 908 | NULL); |
71afbadd PP |
909 | ok(notif_iter, "bt_output_port_notification_iterator_create() succeeds"); |
910 | bt_put(upstream_port); | |
911 | ||
912 | /* Consume the notification iterator */ | |
913 | while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
914 | iter_status = common_consume(notif_iter); | |
915 | } | |
916 | ||
917 | ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END, | |
918 | "output port notification iterator finishes without any error"); | |
919 | ||
920 | /* Compare the resulting test events */ | |
921 | ok(compare_test_events(expected_test_events), | |
922 | "the produced sequence of test events is the expected one"); | |
923 | ||
924 | bt_put(src_comp); | |
925 | bt_put(graph); | |
926 | bt_put(notif_iter); | |
927 | } | |
928 | ||
929 | static | |
930 | void test_output_port_notification_iterator_cannot_consume(void) | |
931 | { | |
932 | struct bt_component *src_comp; | |
933 | struct bt_notification_iterator *notif_iter; | |
934 | struct bt_port *upstream_port; | |
935 | struct bt_graph *graph; | |
936 | ||
937 | clear_test_events(); | |
938 | current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR; | |
939 | diag("test: cannot consume graph with existing output port notification iterator"); | |
940 | graph = bt_graph_create(); | |
941 | assert(graph); | |
942 | create_source_sink(graph, &src_comp, NULL); | |
943 | ||
944 | /* Create notification iterator on source's output port */ | |
945 | upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out"); | |
946 | notif_iter = bt_output_port_notification_iterator_create(upstream_port, | |
6ff151ad | 947 | NULL); |
71afbadd PP |
948 | assert(notif_iter); |
949 | bt_put(upstream_port); | |
950 | ||
951 | /* | |
952 | * This should fail because the graph is now managed by the | |
953 | * notification iterator. | |
954 | */ | |
955 | ok(bt_graph_run(graph) == BT_GRAPH_STATUS_CANNOT_CONSUME, | |
956 | "bt_graph_run() returns BT_GRAPH_STATUS_CANNOT_CONSUME when there's an output port notification iterator"); | |
957 | ||
958 | bt_put(src_comp); | |
959 | bt_put(graph); | |
960 | bt_put(notif_iter); | |
961 | } | |
962 | ||
223c70b2 PP |
963 | #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG" |
964 | ||
965 | int main(int argc, char **argv) | |
966 | { | |
967 | if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) { | |
968 | debug = true; | |
969 | } | |
970 | ||
971 | plan_tests(NR_TESTS); | |
972 | init_static_data(); | |
973 | test_no_auto_notifs(); | |
71afbadd | 974 | test_output_port_notification_iterator(); |
71afbadd | 975 | test_output_port_notification_iterator_cannot_consume(); |
223c70b2 PP |
976 | fini_static_data(); |
977 | return exit_status(); | |
978 | } |