2 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
4 * gsttask.c: Unit test for GstTask
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include <gst/check/gstcheck.h>
24 static GMutex task_lock;
25 static GCond task_cond;
27 static GRecMutex task_mutex;
29 #define TEST_RACE_ITERATIONS 1000
32 task_signal_pause_func (void *data)
36 g_mutex_lock (&task_lock);
38 g_cond_signal (&task_cond);
41 g_mutex_unlock (&task_lock);
44 GST_START_TEST (test_pause_stop_race)
46 guint it = TEST_RACE_ITERATIONS;
50 t = gst_task_new (task_signal_pause_func, &t, NULL);
53 g_rec_mutex_init (&task_mutex);
54 gst_task_set_lock (t, &task_mutex);
56 g_cond_init (&task_cond);
57 g_mutex_init (&task_lock);
60 g_mutex_lock (&task_lock);
61 GST_DEBUG ("starting");
62 ret = gst_task_start (t);
63 fail_unless (ret == TRUE);
64 /* wait for it to spin up */
65 GST_DEBUG ("waiting");
66 g_cond_wait (&task_cond, &task_lock);
67 GST_DEBUG ("done waiting");
68 g_mutex_unlock (&task_lock);
70 GST_DEBUG ("starting");
71 ret = gst_task_stop (t);
72 fail_unless (ret == TRUE);
74 GST_DEBUG ("joining");
75 ret = gst_task_join (t);
76 fail_unless (ret == TRUE);
79 g_cond_clear (&task_cond);
80 g_mutex_clear (&task_lock);
88 task_func2 (void *data)
91 GstTask *t = *((GstTask **) data);
93 g_mutex_lock (&task_lock);
95 g_cond_signal (&task_cond);
96 g_mutex_unlock (&task_lock);
98 ASSERT_WARNING (ret = gst_task_join (t));
99 fail_unless (ret == FALSE);
102 GST_START_TEST (test_join)
107 t = gst_task_new (task_func2, &t, NULL);
110 g_rec_mutex_init (&task_mutex);
111 gst_task_set_lock (t, &task_mutex);
113 g_cond_init (&task_cond);
114 g_mutex_init (&task_lock);
116 g_mutex_lock (&task_lock);
117 GST_DEBUG ("starting");
118 ret = gst_task_start (t);
119 fail_unless (ret == TRUE);
120 /* wait for it to spin up */
121 GST_DEBUG ("waiting");
122 g_cond_wait (&task_cond, &task_lock);
123 GST_DEBUG ("done waiting");
124 g_mutex_unlock (&task_lock);
126 GST_DEBUG ("joining");
127 ret = gst_task_join (t);
128 fail_unless (ret == TRUE);
130 gst_task_cleanup_all ();
132 gst_object_unref (t);
138 task_func (void *data)
140 g_mutex_lock (&task_lock);
141 GST_DEBUG ("signal");
142 g_cond_signal (&task_cond);
143 g_mutex_unlock (&task_lock);
146 GST_START_TEST (test_lock_start)
151 t = gst_task_new (task_func, NULL, NULL);
154 g_rec_mutex_init (&task_mutex);
155 gst_task_set_lock (t, &task_mutex);
157 g_cond_init (&task_cond);
158 g_mutex_init (&task_lock);
160 g_mutex_lock (&task_lock);
161 GST_DEBUG ("starting");
162 ret = gst_task_start (t);
163 fail_unless (ret == TRUE);
164 /* wait for it to spin up */
165 GST_DEBUG ("waiting");
166 g_cond_wait (&task_cond, &task_lock);
167 GST_DEBUG ("done waiting");
168 g_mutex_unlock (&task_lock);
170 /* cannot set mutex now */
171 ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
173 GST_DEBUG ("joining");
174 ret = gst_task_join (t);
175 fail_unless (ret == TRUE);
177 gst_task_cleanup_all ();
179 gst_object_unref (t);
184 GST_START_TEST (test_lock)
189 t = gst_task_new (task_func, NULL, NULL);
192 g_rec_mutex_init (&task_mutex);
193 gst_task_set_lock (t, &task_mutex);
196 ret = gst_task_pause (t);
197 fail_unless (ret == TRUE);
199 g_usleep (1 * G_USEC_PER_SEC / 2);
201 GST_DEBUG ("joining");
202 ret = gst_task_join (t);
203 fail_unless (ret == TRUE);
205 g_usleep (1 * G_USEC_PER_SEC / 2);
207 gst_object_unref (t);
212 GST_START_TEST (test_no_lock)
217 t = gst_task_new (task_func, NULL, NULL);
220 /* stop should be possible without lock */
223 /* pause should give a warning */
224 ASSERT_WARNING (ret = gst_task_pause (t));
225 fail_unless (ret == FALSE);
227 /* start should give a warning */
228 ASSERT_WARNING (ret = gst_task_start (t));
229 fail_unless (ret == FALSE);
231 /* stop should be possible without lock */
234 gst_object_unref (t);
239 GST_START_TEST (test_create)
243 t = gst_task_new (task_func, NULL, NULL);
246 gst_object_unref (t);
253 gst_task_suite (void)
255 Suite *s = suite_create ("GstTask");
256 TCase *tc_chain = tcase_create ("task tests");
258 suite_add_tcase (s, tc_chain);
259 tcase_add_test (tc_chain, test_create);
260 tcase_add_test (tc_chain, test_no_lock);
261 tcase_add_test (tc_chain, test_lock);
262 tcase_add_test (tc_chain, test_lock_start);
263 tcase_add_test (tc_chain, test_join);
264 tcase_add_test (tc_chain, test_pause_stop_race);
269 GST_CHECK_MAIN (gst_task);