2 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
4 * gstelement.c: Unit test for GstElement
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 GMutex *task_lock;
25 static GCond *task_cond;
27 static GStaticRecMutex task_mutex = G_STATIC_REC_MUTEX_INIT;
30 task_func2 (void *data)
33 GstTask *t = *((GstTask **) data);
35 g_mutex_lock (task_lock);
37 g_cond_signal (task_cond);
38 g_mutex_unlock (task_lock);
40 ASSERT_WARNING (ret = gst_task_join (t));
41 fail_unless (ret == FALSE);
44 GST_START_TEST (test_join)
49 t = gst_task_create (task_func2, &t);
52 gst_task_set_lock (t, &task_mutex);
54 task_cond = g_cond_new ();
55 task_lock = g_mutex_new ();
57 g_mutex_lock (task_lock);
58 GST_DEBUG ("starting");
59 ret = gst_task_start (t);
60 fail_unless (ret == TRUE);
61 /* wait for it to spin up */
62 GST_DEBUG ("waiting");
63 g_cond_wait (task_cond, task_lock);
64 GST_DEBUG ("done waiting");
65 g_mutex_unlock (task_lock);
67 GST_DEBUG ("joining");
68 ret = gst_task_join (t);
69 fail_unless (ret == TRUE);
77 task_func (void *data)
79 g_mutex_lock (task_lock);
81 g_cond_signal (task_cond);
82 g_mutex_unlock (task_lock);
85 GST_START_TEST (test_lock_start)
90 t = gst_task_create (task_func, NULL);
93 gst_task_set_lock (t, &task_mutex);
95 task_cond = g_cond_new ();
96 task_lock = g_mutex_new ();
98 g_mutex_lock (task_lock);
99 GST_DEBUG ("starting");
100 ret = gst_task_start (t);
101 fail_unless (ret == TRUE);
102 /* wait for it to spin up */
103 GST_DEBUG ("waiting");
104 g_cond_wait (task_cond, task_lock);
105 GST_DEBUG ("done waiting");
106 g_mutex_unlock (task_lock);
108 /* cannot set mutex now */
109 ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
111 GST_DEBUG ("joining");
112 ret = gst_task_join (t);
113 fail_unless (ret == TRUE);
115 gst_object_unref (t);
120 GST_START_TEST (test_lock)
125 t = gst_task_create (task_func, NULL);
128 gst_task_set_lock (t, &task_mutex);
131 ret = gst_task_pause (t);
132 fail_unless (ret == TRUE);
134 g_usleep (1 * G_USEC_PER_SEC / 2);
136 GST_DEBUG ("joining");
137 ret = gst_task_join (t);
138 fail_unless (ret == TRUE);
140 g_usleep (1 * G_USEC_PER_SEC / 2);
142 gst_object_unref (t);
147 GST_START_TEST (test_no_lock)
152 t = gst_task_create (task_func, NULL);
155 /* stop should be possible without lock */
158 /* pause should give a warning */
159 ASSERT_WARNING (ret = gst_task_pause (t));
160 fail_unless (ret == FALSE);
162 /* start should give a warning */
163 ASSERT_WARNING (ret = gst_task_start (t));
164 fail_unless (ret == FALSE);
166 /* stop should be possible without lock */
169 gst_object_unref (t);
174 GST_START_TEST (test_create)
178 t = gst_task_create (task_func, NULL);
181 gst_object_unref (t);
188 gst_task_suite (void)
190 Suite *s = suite_create ("GstTask");
191 TCase *tc_chain = tcase_create ("task tests");
193 suite_add_tcase (s, tc_chain);
194 tcase_add_test (tc_chain, test_create);
195 tcase_add_test (tc_chain, test_no_lock);
196 tcase_add_test (tc_chain, test_lock);
197 tcase_add_test (tc_chain, test_lock_start);
198 tcase_add_test (tc_chain, test_join);
203 GST_CHECK_MAIN (gst_task);