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 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);
71 gst_task_cleanup_all ();
79 task_func (void *data)
81 g_mutex_lock (task_lock);
83 g_cond_signal (task_cond);
84 g_mutex_unlock (task_lock);
87 GST_START_TEST (test_lock_start)
92 t = gst_task_create (task_func, NULL);
95 gst_task_set_lock (t, &task_mutex);
97 task_cond = g_cond_new ();
98 task_lock = g_mutex_new ();
100 g_mutex_lock (task_lock);
101 GST_DEBUG ("starting");
102 ret = gst_task_start (t);
103 fail_unless (ret == TRUE);
104 /* wait for it to spin up */
105 GST_DEBUG ("waiting");
106 g_cond_wait (task_cond, task_lock);
107 GST_DEBUG ("done waiting");
108 g_mutex_unlock (task_lock);
110 /* cannot set mutex now */
111 ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
113 GST_DEBUG ("joining");
114 ret = gst_task_join (t);
115 fail_unless (ret == TRUE);
117 gst_task_cleanup_all ();
119 gst_object_unref (t);
124 GST_START_TEST (test_lock)
129 t = gst_task_create (task_func, NULL);
132 gst_task_set_lock (t, &task_mutex);
135 ret = gst_task_pause (t);
136 fail_unless (ret == TRUE);
138 g_usleep (1 * G_USEC_PER_SEC / 2);
140 GST_DEBUG ("joining");
141 ret = gst_task_join (t);
142 fail_unless (ret == TRUE);
144 g_usleep (1 * G_USEC_PER_SEC / 2);
146 gst_object_unref (t);
151 GST_START_TEST (test_no_lock)
156 t = gst_task_create (task_func, NULL);
159 /* stop should be possible without lock */
162 /* pause should give a warning */
163 ASSERT_WARNING (ret = gst_task_pause (t));
164 fail_unless (ret == FALSE);
166 /* start should give a warning */
167 ASSERT_WARNING (ret = gst_task_start (t));
168 fail_unless (ret == FALSE);
170 /* stop should be possible without lock */
173 gst_object_unref (t);
178 GST_START_TEST (test_create)
182 t = gst_task_create (task_func, NULL);
185 gst_object_unref (t);
192 gst_task_suite (void)
194 Suite *s = suite_create ("GstTask");
195 TCase *tc_chain = tcase_create ("task tests");
197 suite_add_tcase (s, tc_chain);
198 tcase_add_test (tc_chain, test_create);
199 tcase_add_test (tc_chain, test_no_lock);
200 tcase_add_test (tc_chain, test_lock);
201 tcase_add_test (tc_chain, test_lock_start);
202 tcase_add_test (tc_chain, test_join);
207 GST_CHECK_MAIN (gst_task);