1 /* GStreamer message bus unit tests
2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
3 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <gst/check/gstcheck.h>
23 static GstBus *test_bus = NULL;
24 static GMainLoop *main_loop;
26 #define NUM_MESSAGES 1000
27 #define NUM_THREADS 10
30 pound_bus_with_messages (gpointer data)
32 gint thread_id = GPOINTER_TO_INT (data);
35 for (i = 0; i < NUM_MESSAGES; i++) {
39 s = gst_structure_new ("test_message",
40 "thread_id", G_TYPE_INT, thread_id, "msg_id", G_TYPE_INT, i, NULL);
41 m = gst_message_new_application (NULL, s);
42 gst_bus_post (test_bus, m);
51 const GstStructure *s;
52 guint message_ids[NUM_THREADS];
55 for (i = 0; i < NUM_THREADS; i++)
61 m = gst_bus_pop (test_bus);
64 g_return_if_fail (GST_MESSAGE_TYPE (m) == GST_MESSAGE_APPLICATION);
66 s = gst_message_get_structure (m);
67 if (!gst_structure_get_int (s, "thread_id", &_t))
68 g_critical ("Invalid message");
69 if (!gst_structure_get_int (s, "msg_id", &_i))
70 g_critical ("Invalid message");
72 g_return_if_fail (_t < NUM_THREADS);
73 g_return_if_fail (_i == message_ids[_t]++);
75 gst_message_unref (m);
78 for (i = 0; i < NUM_THREADS; i++)
79 g_return_if_fail (message_ids[i] == NUM_MESSAGES);
82 GST_START_TEST (test_hammer_bus)
84 GThread *threads[NUM_THREADS];
87 test_bus = gst_bus_new ();
89 for (i = 0; i < NUM_THREADS; i++)
90 threads[i] = g_thread_create (pound_bus_with_messages, GINT_TO_POINTER (i),
93 for (i = 0; i < NUM_THREADS; i++)
94 g_thread_join (threads[i]);
98 gst_object_unref ((GstObject *) test_bus);
104 message_func_eos (GstBus * bus, GstMessage * message, guint * p_counter)
106 const GstStructure *s;
109 g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS, FALSE);
111 GST_DEBUG ("got EOS message");
113 s = gst_message_get_structure (message);
114 if (!gst_structure_get_int (s, "msg_id", &i))
115 g_critical ("Invalid message");
117 if (p_counter != NULL)
124 message_func_app (GstBus * bus, GstMessage * message, guint * p_counter)
126 const GstStructure *s;
129 g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_APPLICATION,
132 GST_DEBUG ("got APP message");
134 s = gst_message_get_structure (message);
135 if (!gst_structure_get_int (s, "msg_id", &i))
136 g_critical ("Invalid message");
138 if (p_counter != NULL)
145 send_messages (gpointer data)
151 for (i = 0; i < 10; i++) {
152 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
153 m = gst_message_new_application (NULL, s);
154 gst_bus_post (test_bus, m);
155 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
156 m = gst_message_new_custom (GST_MESSAGE_EOS, NULL, s);
157 gst_bus_post (test_bus, m);
163 /* test if adding a signal watch for different message types calls the
164 * respective callbacks. */
165 GST_START_TEST (test_watch)
171 test_bus = gst_bus_new ();
173 main_loop = g_main_loop_new (NULL, FALSE);
175 id = gst_bus_add_watch (test_bus, gst_bus_async_signal_func, NULL);
177 g_signal_connect (test_bus, "message::eos", (GCallback) message_func_eos,
179 g_signal_connect (test_bus, "message::application",
180 (GCallback) message_func_app, &num_app);
182 g_idle_add ((GSourceFunc) send_messages, NULL);
183 while (g_main_context_pending (NULL))
184 g_main_context_iteration (NULL, FALSE);
186 fail_unless_equals_int (num_eos, 10);
187 fail_unless_equals_int (num_app, 10);
189 g_source_remove (id);
190 g_main_loop_unref (main_loop);
192 gst_object_unref ((GstObject *) test_bus);
197 /* test if adding a signal watch for different message types calls the
198 * respective callbacks. */
199 GST_START_TEST (test_watch_with_custom_context)
207 test_bus = gst_bus_new ();
209 ctx = g_main_context_new ();
210 main_loop = g_main_loop_new (ctx, FALSE);
212 source = gst_bus_create_watch (test_bus);
213 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func, NULL,
215 id = g_source_attach (source, ctx);
216 g_source_unref (source);
219 g_signal_connect (test_bus, "message::eos", (GCallback) message_func_eos,
221 g_signal_connect (test_bus, "message::application",
222 (GCallback) message_func_app, &num_app);
224 source = g_idle_source_new ();
225 g_source_set_callback (source, (GSourceFunc) send_messages, NULL, NULL);
226 g_source_attach (source, ctx);
227 g_source_unref (source);
229 while (g_main_context_pending (ctx))
230 g_main_context_iteration (ctx, FALSE);
232 fail_unless_equals_int (num_eos, 10);
233 fail_unless_equals_int (num_app, 10);
235 g_source_remove (id);
236 g_main_loop_unref (main_loop);
237 g_main_context_unref (ctx);
239 gst_object_unref (test_bus);
244 /* test if adding a signal watch for different message types calls the
245 * respective callbacks. */
246 GST_START_TEST (test_add_watch_with_custom_context)
253 test_bus = gst_bus_new ();
255 ctx = g_main_context_new ();
256 main_loop = g_main_loop_new (ctx, FALSE);
258 g_main_context_push_thread_default (ctx);
259 gst_bus_add_signal_watch (test_bus);
260 g_main_context_pop_thread_default (ctx);
262 g_signal_connect (test_bus, "message::eos", (GCallback) message_func_eos,
264 g_signal_connect (test_bus, "message::application",
265 (GCallback) message_func_app, &num_app);
267 source = g_idle_source_new ();
268 g_source_set_callback (source, (GSourceFunc) send_messages, NULL, NULL);
269 g_source_attach (source, ctx);
270 g_source_unref (source);
272 while (g_main_context_pending (ctx))
273 g_main_context_iteration (ctx, FALSE);
275 fail_unless_equals_int (num_eos, 10);
276 fail_unless_equals_int (num_app, 10);
278 g_main_loop_unref (main_loop);
279 g_main_context_unref (ctx);
281 gst_object_unref (test_bus);
286 static gint messages_seen;
289 message_func (GstBus * bus, GstMessage * message, gpointer data)
291 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_APPLICATION);
297 send_5app_1el_1err_2app_messages (guint interval_usecs)
303 for (i = 0; i < 5; i++) {
304 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
305 m = gst_message_new_application (NULL, s);
306 GST_LOG ("posting application message");
307 gst_bus_post (test_bus, m);
308 g_usleep (interval_usecs);
310 for (i = 0; i < 1; i++) {
311 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
312 m = gst_message_new_element (NULL, s);
313 GST_LOG ("posting element message");
314 gst_bus_post (test_bus, m);
315 g_usleep (interval_usecs);
317 for (i = 0; i < 1; i++) {
318 m = gst_message_new_error (NULL, NULL, "debug string");
319 GST_LOG ("posting error message");
320 gst_bus_post (test_bus, m);
321 g_usleep (interval_usecs);
323 for (i = 0; i < 2; i++) {
324 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
325 m = gst_message_new_application (NULL, s);
326 GST_LOG ("posting application message");
327 gst_bus_post (test_bus, m);
328 g_usleep (interval_usecs);
333 send_10_app_messages (void)
339 for (i = 0; i < 10; i++) {
340 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
341 m = gst_message_new_application (NULL, s);
342 gst_bus_post (test_bus, m);
346 /* test that you get the same messages from a poll as from signal watches. */
347 GST_START_TEST (test_watch_with_poll)
351 test_bus = gst_bus_new ();
354 gst_bus_add_signal_watch (test_bus);
355 g_signal_connect (test_bus, "message", (GCallback) message_func, NULL);
357 send_10_app_messages ();
359 for (i = 0; i < 10; i++)
360 gst_message_unref (gst_bus_poll (test_bus, GST_MESSAGE_APPLICATION,
361 GST_CLOCK_TIME_NONE));
363 fail_if (gst_bus_have_pending (test_bus), "unexpected messages on bus");
364 fail_unless (messages_seen == 10, "signal handler didn't get 10 messages");
366 gst_bus_remove_signal_watch (test_bus);
368 gst_object_unref (test_bus);
373 /* test that you get the messages with pop. */
374 GST_START_TEST (test_timed_pop)
378 test_bus = gst_bus_new ();
380 send_10_app_messages ();
382 for (i = 0; i < 10; i++)
383 gst_message_unref (gst_bus_timed_pop (test_bus, GST_CLOCK_TIME_NONE));
385 fail_if (gst_bus_have_pending (test_bus), "unexpected messages on bus");
387 gst_object_unref (test_bus);
392 /* test that you get the messages with pop_filtered */
393 GST_START_TEST (test_timed_pop_filtered)
398 test_bus = gst_bus_new ();
400 send_10_app_messages ();
401 for (i = 0; i < 10; i++) {
402 msg = gst_bus_timed_pop_filtered (test_bus, GST_CLOCK_TIME_NONE,
404 fail_unless (msg != NULL);
405 gst_message_unref (msg);
408 /* should flush all messages on the bus with types not matching */
409 send_10_app_messages ();
410 msg = gst_bus_timed_pop_filtered (test_bus, 0,
411 GST_MESSAGE_ANY ^ GST_MESSAGE_APPLICATION);
412 fail_unless (msg == NULL);
413 msg = gst_bus_timed_pop_filtered (test_bus, GST_SECOND / 2,
414 GST_MESSAGE_ANY ^ GST_MESSAGE_APPLICATION);
415 fail_unless (msg == NULL);
416 /* there should be nothing on the bus now */
417 fail_if (gst_bus_have_pending (test_bus), "unexpected messages on bus");
418 msg = gst_bus_timed_pop_filtered (test_bus, 0, GST_MESSAGE_ANY);
419 fail_unless (msg == NULL);
421 send_5app_1el_1err_2app_messages (0);
422 msg = gst_bus_timed_pop_filtered (test_bus, 0,
423 GST_MESSAGE_ANY ^ GST_MESSAGE_APPLICATION);
424 fail_unless (msg != NULL);
425 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
426 gst_message_unref (msg);
427 fail_unless (gst_bus_have_pending (test_bus), "expected messages on bus");
428 msg = gst_bus_timed_pop_filtered (test_bus, 0, GST_MESSAGE_APPLICATION);
429 fail_unless (msg != NULL);
430 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_APPLICATION);
431 gst_message_unref (msg);
432 msg = gst_bus_timed_pop_filtered (test_bus, 0, GST_MESSAGE_ERROR);
433 fail_unless (msg == NULL);
435 gst_object_unref (test_bus);
441 post_delayed_thread (gpointer data)
444 send_5app_1el_1err_2app_messages (1 * G_USEC_PER_SEC);
448 /* test that you get the messages with pop_filtered if there's a timeout*/
449 GST_START_TEST (test_timed_pop_filtered_with_timeout)
455 test_bus = gst_bus_new ();
457 MAIN_START_THREAD_FUNCTIONS (1, post_delayed_thread, NULL);
461 msg = gst_bus_timed_pop_filtered (test_bus, 2 * GST_SECOND,
463 fail_unless (msg == NULL, "Got unexpected %s message",
464 (msg) ? GST_MESSAGE_TYPE_NAME (msg) : "");
465 msg = gst_bus_timed_pop_filtered (test_bus, (3 + 1 + 1 + 1) * GST_SECOND,
466 GST_MESSAGE_ERROR | GST_MESSAGE_ELEMENT);
467 fail_unless (msg != NULL, "expected element message, but got nothing");
468 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_ELEMENT);
469 gst_message_unref (msg);
470 msg = gst_bus_timed_pop_filtered (test_bus, GST_CLOCK_TIME_NONE,
471 GST_MESSAGE_APPLICATION);
472 fail_unless (msg != NULL, "expected application message, but got nothing");
473 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_APPLICATION);
474 gst_message_unref (msg);
475 msg = gst_bus_timed_pop_filtered (test_bus, GST_CLOCK_TIME_NONE,
476 GST_MESSAGE_APPLICATION);
477 fail_unless (msg != NULL, "expected application message, but got nothing");
478 fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_APPLICATION);
479 gst_message_unref (msg);
480 msg = gst_bus_timed_pop_filtered (test_bus, GST_SECOND / 4,
481 GST_MESSAGE_TAG | GST_MESSAGE_ERROR);
482 fail_unless (msg == NULL, "Got unexpected %s message",
483 (msg) ? GST_MESSAGE_TYPE_NAME (msg) : "");
485 MAIN_STOP_THREADS ();
487 gst_object_unref (test_bus);
492 /* test that you get the messages with pop from another thread. */
494 pop_thread (gpointer data)
496 GstBus *bus = GST_BUS_CAST (data);
499 for (i = 0; i < 10; i++)
500 gst_message_unref (gst_bus_timed_pop (bus, GST_CLOCK_TIME_NONE));
505 GST_START_TEST (test_timed_pop_thread)
508 GError *error = NULL;
510 test_bus = gst_bus_new ();
512 thread = g_thread_create (pop_thread, test_bus, TRUE, &error);
513 fail_if (error != NULL);
515 send_10_app_messages ();
517 g_thread_join (thread);
519 fail_if (gst_bus_have_pending (test_bus), "unexpected messages on bus");
521 /* try to pop a message without timeout. */
522 fail_if (gst_bus_timed_pop (test_bus, 0) != NULL);
524 /* with a small timeout */
525 fail_if (gst_bus_timed_pop (test_bus, 1000) != NULL);
527 gst_object_unref (test_bus);
533 cb_bus_call (GstBus * bus, GstMessage * msg, gpointer data)
535 GMainLoop *loop = data;
537 switch (GST_MESSAGE_TYPE (msg)) {
538 case GST_MESSAGE_EOS:
540 GST_INFO ("End-of-stream");
541 g_main_loop_quit (loop);
544 case GST_MESSAGE_ERROR:
548 gst_message_parse_error (msg, &err, NULL);
549 g_error ("Error: %s", err->message);
552 g_main_loop_quit (loop);
557 GST_LOG ("BUS MESSAGE: type=%s", GST_MESSAGE_TYPE_NAME (msg));
565 GST_START_TEST (test_custom_main_context)
569 GstElement *pipeline;
575 ctx = g_main_context_new ();
576 loop = g_main_loop_new (ctx, FALSE);
578 pipeline = gst_pipeline_new (NULL);
579 src = gst_element_factory_make ("fakesrc", NULL);
580 g_object_set (src, "num-buffers", 2000, NULL);
582 sink = gst_element_factory_make ("fakesink", NULL);
584 fail_unless (gst_bin_add (GST_BIN (pipeline), src));
585 fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
586 fail_unless (gst_element_link (src, sink));
588 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
589 source = gst_bus_create_watch (bus);
590 g_source_attach (source, ctx);
591 g_source_set_callback (source, (GSourceFunc) cb_bus_call, loop, NULL);
592 g_source_unref (source);
593 gst_object_unref (bus);
595 GST_INFO ("starting pipeline");
597 gst_element_set_state (pipeline, GST_STATE_PLAYING);
598 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
600 GST_INFO ("running event loop, ctx=%p", ctx);
601 g_main_loop_run (loop);
603 gst_element_set_state (pipeline, GST_STATE_NULL);
607 g_main_context_unref (ctx);
608 g_main_loop_unref (loop);
609 gst_object_unref (pipeline);
617 Suite *s = suite_create ("GstBus");
618 TCase *tc_chain = tcase_create ("stresstest");
620 tcase_set_timeout (tc_chain, 60);
622 suite_add_tcase (s, tc_chain);
623 tcase_add_test (tc_chain, test_hammer_bus);
624 tcase_add_test (tc_chain, test_watch);
625 tcase_add_test (tc_chain, test_watch_with_poll);
626 tcase_add_test (tc_chain, test_watch_with_custom_context);
627 tcase_add_test (tc_chain, test_add_watch_with_custom_context);
628 tcase_add_test (tc_chain, test_timed_pop);
629 tcase_add_test (tc_chain, test_timed_pop_thread);
630 tcase_add_test (tc_chain, test_timed_pop_filtered);
631 tcase_add_test (tc_chain, test_timed_pop_filtered_with_timeout);
632 tcase_add_test (tc_chain, test_custom_main_context);
636 GST_CHECK_MAIN (gst_bus);