2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
4 * gstbus.c: Unit test for the message bus
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <gst/check/gstcheck.h>
24 static GstBus *test_bus = NULL;
25 static GMainLoop *main_loop;
27 #define NUM_MESSAGES 1000
28 #define NUM_THREADS 10
31 pound_bus_with_messages (gpointer data)
33 gint thread_id = GPOINTER_TO_INT (data);
36 for (i = 0; i < NUM_MESSAGES; i++) {
40 s = gst_structure_new ("test_message",
41 "thread_id", G_TYPE_INT, thread_id, "msg_id", G_TYPE_INT, i, NULL);
42 m = gst_message_new_application (NULL, s);
43 gst_bus_post (test_bus, m);
52 const GstStructure *s;
53 guint message_ids[NUM_THREADS];
56 for (i = 0; i < NUM_THREADS; i++)
62 m = gst_bus_pop (test_bus);
65 g_return_if_fail (GST_MESSAGE_TYPE (m) == GST_MESSAGE_APPLICATION);
67 s = gst_message_get_structure (m);
68 if (!gst_structure_get_int (s, "thread_id", &_t))
69 g_critical ("Invalid message");
70 if (!gst_structure_get_int (s, "msg_id", &_i))
71 g_critical ("Invalid message");
73 g_return_if_fail (_t < NUM_THREADS);
74 g_return_if_fail (_i == message_ids[_t]++);
76 gst_message_unref (m);
79 for (i = 0; i < NUM_THREADS; i++)
80 g_return_if_fail (message_ids[i] == NUM_MESSAGES);
83 GST_START_TEST (test_hammer_bus)
85 GThread *threads[NUM_THREADS];
88 test_bus = gst_bus_new ();
90 for (i = 0; i < NUM_THREADS; i++)
91 threads[i] = g_thread_create (pound_bus_with_messages, GINT_TO_POINTER (i),
94 for (i = 0; i < NUM_THREADS; i++)
95 g_thread_join (threads[i]);
99 gst_object_unref ((GstObject *) test_bus);
105 message_func_eos (GstBus * bus, GstMessage * message, gpointer data)
107 const GstStructure *s;
110 g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS, FALSE);
112 GST_DEBUG ("got EOS message");
114 s = gst_message_get_structure (message);
115 if (!gst_structure_get_int (s, "msg_id", &i))
116 g_critical ("Invalid message");
122 message_func_app (GstBus * bus, GstMessage * message, gpointer data)
124 const GstStructure *s;
127 g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_APPLICATION,
130 GST_DEBUG ("got APP message");
132 s = gst_message_get_structure (message);
133 if (!gst_structure_get_int (s, "msg_id", &i))
134 g_critical ("Invalid message");
140 send_messages (gpointer data)
146 for (i = 0; i < 10; i++) {
147 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
148 m = gst_message_new_application (NULL, s);
149 gst_bus_post (test_bus, m);
150 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
151 m = gst_message_new_custom (GST_MESSAGE_EOS, NULL, s);
152 gst_bus_post (test_bus, m);
158 /* test if adding a signal watch for different message types calls the
159 * respective callbacks. */
160 GST_START_TEST (test_watch)
164 test_bus = gst_bus_new ();
166 main_loop = g_main_loop_new (NULL, FALSE);
168 id = gst_bus_add_watch (test_bus, gst_bus_async_signal_func, NULL);
169 g_signal_connect (test_bus, "message::eos", (GCallback) message_func_eos,
171 g_signal_connect (test_bus, "message::application",
172 (GCallback) message_func_app, NULL);
174 g_idle_add ((GSourceFunc) send_messages, NULL);
175 while (g_main_context_pending (NULL))
176 g_main_context_iteration (NULL, FALSE);
178 g_source_remove (id);
179 g_main_loop_unref (main_loop);
181 gst_object_unref ((GstObject *) test_bus);
186 static gint messages_seen = 0;
189 message_func (GstBus * bus, GstMessage * message, gpointer data)
191 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_APPLICATION);
197 send_10_app_messages (void)
203 for (i = 0; i < 10; i++) {
204 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
205 m = gst_message_new_application (NULL, s);
206 gst_bus_post (test_bus, m);
210 /* test that you get the same messages from a poll as from signal watches. */
211 GST_START_TEST (test_watch_with_poll)
215 test_bus = gst_bus_new ();
217 gst_bus_add_signal_watch (test_bus);
218 g_signal_connect (test_bus, "message", (GCallback) message_func, NULL);
220 send_10_app_messages ();
222 for (i = 0; i < 10; i++)
223 gst_message_unref (gst_bus_poll (test_bus, GST_MESSAGE_APPLICATION,
224 GST_CLOCK_TIME_NONE));
226 fail_if (gst_bus_have_pending (test_bus), "unexpected messages on bus");
227 fail_unless (messages_seen == 10, "signal handler didn't get 10 messages");
229 gst_bus_remove_signal_watch (test_bus);
231 gst_object_unref (test_bus);
239 Suite *s = suite_create ("GstBus");
240 TCase *tc_chain = tcase_create ("stresstest");
242 tcase_set_timeout (tc_chain, 20);
244 suite_add_tcase (s, tc_chain);
245 tcase_add_test (tc_chain, test_hammer_bus);
246 tcase_add_test (tc_chain, test_watch);
247 tcase_add_test (tc_chain, test_watch_with_poll);
251 GST_CHECK_MAIN (gst_bus);