gstreamer/tests/check/gst/gstbus.c
Wim Taymans 1c1af875d4 GstBusHandler -> GstBusFunc, return value has the same meaning as any other GSource (FALSE == remove source).
Original commit message from CVS:
* check/gst/gstbin.c: (pop_messages), (GST_START_TEST):
* check/gst/gstbus.c: (message_func_eos), (message_func_app),
(send_messages), (GST_START_TEST), (gstbus_suite):
* check/gst/gstpipeline.c: (GST_START_TEST):
* check/pipelines/cleanup.c: (run_pipeline):
* check/pipelines/simple_launch_lines.c: (run_pipeline),
(GST_START_TEST):
* gst/gstbus.c: (gst_bus_have_pending), (gst_bus_source_prepare),
(gst_bus_source_check), (gst_bus_source_dispatch),
(gst_bus_create_watch), (gst_bus_add_watch_full),
(gst_bus_add_watch), (poll_func), (poll_timeout), (gst_bus_poll):
* gst/gstbus.h:
* tools/gst-launch.c: (event_loop):
* tools/gst-md5sum.c: (event_loop):
GstBusHandler -> GstBusFunc, return value has the same meaning as
any other GSource (FALSE == remove source).
_add_watch() and _add_watch_full() now take a MessageType mask to
only handle specific types of messages.
_poll() returns the GstMessage instead of the message type to avoid
race conditions.
_have_pending() takes a MessageType mask now too.
Added testsuite for multiple bus watches.
Fix testsuites and applications for new bus API.
2005-09-19 11:18:03 +00:00

208 lines
4.9 KiB
C

/* GStreamer
* Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
*
* gstbus.c: Unit test for the message bus
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <gst/check/gstcheck.h>
static GstBus *test_bus = NULL;
static GMainLoop *main_loop;
#define NUM_MESSAGES 1000
#define NUM_THREADS 10
static gpointer
pound_bus_with_messages (gpointer data)
{
gint thread_id = GPOINTER_TO_INT (data);
gint i;
for (i = 0; i < NUM_MESSAGES; i++) {
GstMessage *m;
GstStructure *s;
s = gst_structure_new ("test_message",
"thread_id", G_TYPE_INT, thread_id, "msg_id", G_TYPE_INT, i, NULL);
m = gst_message_new_application (NULL, s);
gst_bus_post (test_bus, m);
}
return NULL;
}
static void
pull_messages ()
{
GstMessage *m;
const GstStructure *s;
guint message_ids[NUM_THREADS];
gint i;
for (i = 0; i < NUM_THREADS; i++)
message_ids[i] = 0;
while (1) {
gint _t, _i;
m = gst_bus_pop (test_bus);
if (!m)
break;
g_return_if_fail (GST_MESSAGE_TYPE (m) == GST_MESSAGE_APPLICATION);
s = gst_message_get_structure (m);
if (!gst_structure_get_int (s, "thread_id", &_t))
g_critical ("Invalid message");
if (!gst_structure_get_int (s, "msg_id", &_i))
g_critical ("Invalid message");
g_return_if_fail (_t < NUM_THREADS);
g_return_if_fail (_i == message_ids[_t]++);
gst_message_unref (m);
}
for (i = 0; i < NUM_THREADS; i++)
g_return_if_fail (message_ids[i] == NUM_MESSAGES);
}
GST_START_TEST (test_hammer_bus)
{
GThread *threads[NUM_THREADS];
gint i;
test_bus = gst_bus_new ();
for (i = 0; i < NUM_THREADS; i++)
threads[i] = g_thread_create (pound_bus_with_messages, GINT_TO_POINTER (i),
TRUE, NULL);
for (i = 0; i < NUM_THREADS; i++)
g_thread_join (threads[i]);
pull_messages ();
gst_object_unref ((GstObject *) test_bus);
}
GST_END_TEST static gboolean
message_func_eos (GstBus * bus, GstMessage * message, gpointer data)
{
const GstStructure *s;
gint i;
g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS, FALSE);
GST_DEBUG ("got EOS message");
s = gst_message_get_structure (message);
if (!gst_structure_get_int (s, "msg_id", &i))
g_critical ("Invalid message");
return i != 9;
}
static gboolean
message_func_app (GstBus * bus, GstMessage * message, gpointer data)
{
const GstStructure *s;
gint i;
g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_APPLICATION,
FALSE);
GST_DEBUG ("got APP message");
s = gst_message_get_structure (message);
if (!gst_structure_get_int (s, "msg_id", &i))
g_critical ("Invalid message");
return i != 9;
}
static gboolean
send_messages (gpointer data)
{
GstMessage *m;
GstStructure *s;
gint i;
for (i = 0; i < 10; i++) {
s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
m = gst_message_new_application (NULL, s);
gst_bus_post (test_bus, m);
s = gst_structure_new ("test_message", "msg_id", G_TYPE_INT, i, NULL);
m = gst_message_new_custom (GST_MESSAGE_EOS, NULL, s);
gst_bus_post (test_bus, m);
}
return FALSE;
}
/* test id adding two watches for different message types calls the
* respective callbacks. */
GST_START_TEST (test_watch)
{
guint id1, id2;
test_bus = gst_bus_new ();
main_loop = g_main_loop_new (NULL, FALSE);
id2 = gst_bus_add_watch (test_bus, GST_MESSAGE_EOS, message_func_eos, NULL);
id1 =
gst_bus_add_watch (test_bus, GST_MESSAGE_APPLICATION, message_func_app,
NULL);
g_idle_add ((GSourceFunc) send_messages, NULL);
while (g_main_context_pending (NULL))
g_main_context_iteration (NULL, FALSE);
g_source_remove (id1);
g_source_remove (id2);
g_main_loop_unref (main_loop);
gst_object_unref ((GstObject *) test_bus);
}
GST_END_TEST Suite * gstbus_suite (void)
{
Suite *s = suite_create ("GstBus");
TCase *tc_chain = tcase_create ("stresstest");
tcase_set_timeout (tc_chain, 20);
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, test_hammer_bus);
tcase_add_test (tc_chain, test_watch);
return s;
}
int
main (int argc, char **argv)
{
int nf;
Suite *s = gstbus_suite ();
SRunner *sr = srunner_create (s);
gst_check_init (&argc, &argv);
srunner_run_all (sr, CK_NORMAL);
nf = srunner_ntests_failed (sr);
srunner_free (sr);
return nf;
}