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);
101 GST_END_TEST static gboolean
102 message_func_eos (GstBus * bus, GstMessage * message, gpointer data)
104 const GstStructure *s;
107 g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS, FALSE);
109 GST_DEBUG ("got EOS message");
111 s = gst_message_get_structure (message);
112 if (!gst_structure_get_int (s, "msg_id", &i))
113 g_critical ("Invalid message");
119 message_func_app (GstBus * bus, GstMessage * message, gpointer data)
121 const GstStructure *s;
124 g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_APPLICATION,
127 GST_DEBUG ("got APP message");
129 s = gst_message_get_structure (message);
130 if (!gst_structure_get_int (s, "msg_id", &i))
131 g_critical ("Invalid message");
137 send_messages (gpointer data)
143 for (i = 0; i < 10; i++) {
144 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
145 m = gst_message_new_application (NULL, s);
146 gst_bus_post (test_bus, m);
147 s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
148 m = gst_message_new_custom (GST_MESSAGE_EOS, NULL, s);
149 gst_bus_post (test_bus, m);
155 /* test if adding a signal watch for different message types calls the
156 * respective callbacks. */
157 GST_START_TEST (test_watch)
161 test_bus = gst_bus_new ();
163 main_loop = g_main_loop_new (NULL, FALSE);
165 id = gst_bus_add_watch (test_bus, gst_bus_async_signal_func, NULL);
166 g_signal_connect (test_bus, "message::eos", (GCallback) message_func_eos,
168 g_signal_connect (test_bus, "message::application",
169 (GCallback) message_func_app, NULL);
171 g_idle_add ((GSourceFunc) send_messages, NULL);
172 while (g_main_context_pending (NULL))
173 g_main_context_iteration (NULL, FALSE);
175 g_source_remove (id);
176 g_main_loop_unref (main_loop);
178 gst_object_unref ((GstObject *) test_bus);
180 GST_END_TEST Suite * gstbus_suite (void)
182 Suite *s = suite_create ("GstBus");
183 TCase *tc_chain = tcase_create ("stresstest");
185 tcase_set_timeout (tc_chain, 20);
187 suite_add_tcase (s, tc_chain);
188 tcase_add_test (tc_chain, test_hammer_bus);
189 tcase_add_test (tc_chain, test_watch);
194 main (int argc, char **argv)
198 Suite *s = gstbus_suite ();
199 SRunner *sr = srunner_create (s);
201 gst_check_init (&argc, &argv);
203 srunner_run_all (sr, CK_NORMAL);
204 nf = srunner_ntests_failed (sr);