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.
25 #include <gst/check/gstcheck.h>
27 static GMutex task_lock;
28 static GCond task_cond;
30 static GRecMutex task_mutex;
32 #define TEST_RACE_ITERATIONS 1000
35 task_signal_pause_func (void *data)
39 g_mutex_lock (&task_lock);
41 g_cond_signal (&task_cond);
44 g_mutex_unlock (&task_lock);
47 GST_START_TEST (test_pause_stop_race)
49 guint it = TEST_RACE_ITERATIONS;
53 t = gst_task_new (task_signal_pause_func, &t, NULL);
56 g_rec_mutex_init (&task_mutex);
57 gst_task_set_lock (t, &task_mutex);
59 g_cond_init (&task_cond);
60 g_mutex_init (&task_lock);
63 g_mutex_lock (&task_lock);
64 GST_DEBUG ("starting");
65 ret = gst_task_start (t);
66 fail_unless (ret == TRUE);
67 /* wait for it to spin up */
68 GST_DEBUG ("waiting");
69 g_cond_wait (&task_cond, &task_lock);
70 GST_DEBUG ("done waiting");
71 g_mutex_unlock (&task_lock);
73 GST_DEBUG ("starting");
74 ret = gst_task_stop (t);
75 fail_unless (ret == TRUE);
77 GST_DEBUG ("joining");
78 ret = gst_task_join (t);
79 fail_unless (ret == TRUE);
82 g_cond_clear (&task_cond);
83 g_mutex_clear (&task_lock);
91 task_func2 (void *data)
94 GstTask *t = *((GstTask **) data);
96 g_mutex_lock (&task_lock);
98 g_cond_signal (&task_cond);
99 g_mutex_unlock (&task_lock);
101 ASSERT_WARNING (ret = gst_task_join (t));
102 fail_unless (ret == FALSE);
105 GST_START_TEST (test_join)
110 t = gst_task_new (task_func2, &t, NULL);
113 g_rec_mutex_init (&task_mutex);
114 gst_task_set_lock (t, &task_mutex);
116 g_cond_init (&task_cond);
117 g_mutex_init (&task_lock);
119 g_mutex_lock (&task_lock);
120 GST_DEBUG ("starting");
121 ret = gst_task_start (t);
122 fail_unless (ret == TRUE);
123 /* wait for it to spin up */
124 GST_DEBUG ("waiting");
125 g_cond_wait (&task_cond, &task_lock);
126 GST_DEBUG ("done waiting");
127 g_mutex_unlock (&task_lock);
129 GST_DEBUG ("joining");
130 ret = gst_task_join (t);
131 fail_unless (ret == TRUE);
133 gst_task_cleanup_all ();
135 gst_object_unref (t);
141 task_func (void *data)
143 g_mutex_lock (&task_lock);
144 GST_DEBUG ("signal");
145 g_cond_signal (&task_cond);
146 g_mutex_unlock (&task_lock);
149 GST_START_TEST (test_lock_start)
154 t = gst_task_new (task_func, NULL, NULL);
157 g_rec_mutex_init (&task_mutex);
158 gst_task_set_lock (t, &task_mutex);
160 g_cond_init (&task_cond);
161 g_mutex_init (&task_lock);
163 g_mutex_lock (&task_lock);
164 GST_DEBUG ("starting");
165 ret = gst_task_start (t);
166 fail_unless (ret == TRUE);
167 /* wait for it to spin up */
168 GST_DEBUG ("waiting");
169 g_cond_wait (&task_cond, &task_lock);
170 GST_DEBUG ("done waiting");
171 g_mutex_unlock (&task_lock);
173 /* cannot set mutex now */
174 ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
176 GST_DEBUG ("joining");
177 ret = gst_task_join (t);
178 fail_unless (ret == TRUE);
180 gst_task_cleanup_all ();
182 gst_object_unref (t);
187 GST_START_TEST (test_lock)
192 t = gst_task_new (task_func, NULL, NULL);
195 g_rec_mutex_init (&task_mutex);
196 gst_task_set_lock (t, &task_mutex);
199 ret = gst_task_pause (t);
200 fail_unless (ret == TRUE);
202 g_usleep (1 * G_USEC_PER_SEC / 2);
204 GST_DEBUG ("joining");
205 ret = gst_task_join (t);
206 fail_unless (ret == TRUE);
208 g_usleep (1 * G_USEC_PER_SEC / 2);
210 gst_object_unref (t);
215 GST_START_TEST (test_no_lock)
220 t = gst_task_new (task_func, NULL, NULL);
223 /* stop should be possible without lock */
226 /* pause should give a warning */
227 ASSERT_WARNING (ret = gst_task_pause (t));
228 fail_unless (ret == FALSE);
230 /* start should give a warning */
231 ASSERT_WARNING (ret = gst_task_start (t));
232 fail_unless (ret == FALSE);
234 /* stop should be possible without lock */
237 gst_object_unref (t);
242 GST_START_TEST (test_create)
246 t = gst_task_new (task_func, NULL, NULL);
249 gst_object_unref (t);
256 gst_task_suite (void)
258 Suite *s = suite_create ("GstTask");
259 TCase *tc_chain = tcase_create ("task tests");
261 suite_add_tcase (s, tc_chain);
262 tcase_add_test (tc_chain, test_create);
263 tcase_add_test (tc_chain, test_no_lock);
264 tcase_add_test (tc_chain, test_lock);
265 tcase_add_test (tc_chain, test_lock_start);
266 tcase_add_test (tc_chain, test_join);
267 tcase_add_test (tc_chain, test_pause_stop_race);
272 GST_CHECK_MAIN (gst_task);