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., 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 GRecMutex task_mutex;
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_new (task_func2, &t);
52 g_rec_mutex_init (&task_mutex);
53 gst_task_set_lock (t, &task_mutex);
55 g_cond_init (&task_cond);
56 g_mutex_init (&task_lock);
58 g_mutex_lock (&task_lock);
59 GST_DEBUG ("starting");
60 ret = gst_task_start (t);
61 fail_unless (ret == TRUE);
62 /* wait for it to spin up */
63 GST_DEBUG ("waiting");
64 g_cond_wait (&task_cond, &task_lock);
65 GST_DEBUG ("done waiting");
66 g_mutex_unlock (&task_lock);
68 GST_DEBUG ("joining");
69 ret = gst_task_join (t);
70 fail_unless (ret == TRUE);
72 gst_task_cleanup_all ();
80 task_func (void *data)
82 g_mutex_lock (&task_lock);
84 g_cond_signal (&task_cond);
85 g_mutex_unlock (&task_lock);
88 GST_START_TEST (test_lock_start)
93 t = gst_task_new (task_func, NULL);
96 g_rec_mutex_init (&task_mutex);
97 gst_task_set_lock (t, &task_mutex);
99 g_cond_init (&task_cond);
100 g_mutex_init (&task_lock);
102 g_mutex_lock (&task_lock);
103 GST_DEBUG ("starting");
104 ret = gst_task_start (t);
105 fail_unless (ret == TRUE);
106 /* wait for it to spin up */
107 GST_DEBUG ("waiting");
108 g_cond_wait (&task_cond, &task_lock);
109 GST_DEBUG ("done waiting");
110 g_mutex_unlock (&task_lock);
112 /* cannot set mutex now */
113 ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
115 GST_DEBUG ("joining");
116 ret = gst_task_join (t);
117 fail_unless (ret == TRUE);
119 gst_task_cleanup_all ();
121 gst_object_unref (t);
126 GST_START_TEST (test_lock)
131 t = gst_task_new (task_func, NULL);
134 g_rec_mutex_init (&task_mutex);
135 gst_task_set_lock (t, &task_mutex);
138 ret = gst_task_pause (t);
139 fail_unless (ret == TRUE);
141 g_usleep (1 * G_USEC_PER_SEC / 2);
143 GST_DEBUG ("joining");
144 ret = gst_task_join (t);
145 fail_unless (ret == TRUE);
147 g_usleep (1 * G_USEC_PER_SEC / 2);
149 gst_object_unref (t);
154 GST_START_TEST (test_no_lock)
159 t = gst_task_new (task_func, NULL);
162 /* stop should be possible without lock */
165 /* pause should give a warning */
166 ASSERT_WARNING (ret = gst_task_pause (t));
167 fail_unless (ret == FALSE);
169 /* start should give a warning */
170 ASSERT_WARNING (ret = gst_task_start (t));
171 fail_unless (ret == FALSE);
173 /* stop should be possible without lock */
176 gst_object_unref (t);
181 GST_START_TEST (test_create)
185 t = gst_task_new (task_func, NULL);
188 gst_object_unref (t);
195 gst_task_suite (void)
197 Suite *s = suite_create ("GstTask");
198 TCase *tc_chain = tcase_create ("task tests");
200 suite_add_tcase (s, tc_chain);
201 tcase_add_test (tc_chain, test_create);
202 tcase_add_test (tc_chain, test_no_lock);
203 tcase_add_test (tc_chain, test_lock);
204 tcase_add_test (tc_chain, test_lock_start);
205 tcase_add_test (tc_chain, test_join);
210 GST_CHECK_MAIN (gst_task);