Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / tests / check / gst / gsttask.c
1 /* GStreamer
2  * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * gsttask.c: Unit test for GstTask
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 static GMutex task_lock;
25 static GCond task_cond;
26
27 static GRecMutex task_mutex;
28
29 static void
30 task_func2 (void *data)
31 {
32   gboolean ret;
33   GstTask *t = *((GstTask **) data);
34
35   g_mutex_lock (&task_lock);
36   GST_DEBUG ("signal");
37   g_cond_signal (&task_cond);
38   g_mutex_unlock (&task_lock);
39
40   ASSERT_WARNING (ret = gst_task_join (t));
41   fail_unless (ret == FALSE);
42 }
43
44 GST_START_TEST (test_join)
45 {
46   GstTask *t;
47   gboolean ret;
48
49   t = gst_task_new (task_func2, &t);
50   fail_if (t == NULL);
51
52   g_rec_mutex_init (&task_mutex);
53   gst_task_set_lock (t, &task_mutex);
54
55   g_cond_init (&task_cond);
56   g_mutex_init (&task_lock);
57
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);
67
68   GST_DEBUG ("joining");
69   ret = gst_task_join (t);
70   fail_unless (ret == TRUE);
71
72   gst_task_cleanup_all ();
73
74   gst_object_unref (t);
75 }
76
77 GST_END_TEST;
78
79 static void
80 task_func (void *data)
81 {
82   g_mutex_lock (&task_lock);
83   GST_DEBUG ("signal");
84   g_cond_signal (&task_cond);
85   g_mutex_unlock (&task_lock);
86 }
87
88 GST_START_TEST (test_lock_start)
89 {
90   GstTask *t;
91   gboolean ret;
92
93   t = gst_task_new (task_func, NULL);
94   fail_if (t == NULL);
95
96   g_rec_mutex_init (&task_mutex);
97   gst_task_set_lock (t, &task_mutex);
98
99   g_cond_init (&task_cond);
100   g_mutex_init (&task_lock);
101
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);
111
112   /* cannot set mutex now */
113   ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
114
115   GST_DEBUG ("joining");
116   ret = gst_task_join (t);
117   fail_unless (ret == TRUE);
118
119   gst_task_cleanup_all ();
120
121   gst_object_unref (t);
122 }
123
124 GST_END_TEST;
125
126 GST_START_TEST (test_lock)
127 {
128   GstTask *t;
129   gboolean ret;
130
131   t = gst_task_new (task_func, NULL);
132   fail_if (t == NULL);
133
134   g_rec_mutex_init (&task_mutex);
135   gst_task_set_lock (t, &task_mutex);
136
137   GST_DEBUG ("pause");
138   ret = gst_task_pause (t);
139   fail_unless (ret == TRUE);
140
141   g_usleep (1 * G_USEC_PER_SEC / 2);
142
143   GST_DEBUG ("joining");
144   ret = gst_task_join (t);
145   fail_unless (ret == TRUE);
146
147   g_usleep (1 * G_USEC_PER_SEC / 2);
148
149   gst_object_unref (t);
150 }
151
152 GST_END_TEST;
153
154 GST_START_TEST (test_no_lock)
155 {
156   GstTask *t;
157   gboolean ret;
158
159   t = gst_task_new (task_func, NULL);
160   fail_if (t == NULL);
161
162   /* stop should be possible without lock */
163   gst_task_stop (t);
164
165   /* pause should give a warning */
166   ASSERT_WARNING (ret = gst_task_pause (t));
167   fail_unless (ret == FALSE);
168
169   /* start should give a warning */
170   ASSERT_WARNING (ret = gst_task_start (t));
171   fail_unless (ret == FALSE);
172
173   /* stop should be possible without lock */
174   gst_task_stop (t);
175
176   gst_object_unref (t);
177 }
178
179 GST_END_TEST;
180
181 GST_START_TEST (test_create)
182 {
183   GstTask *t;
184
185   t = gst_task_new (task_func, NULL);
186   fail_if (t == NULL);
187
188   gst_object_unref (t);
189 }
190
191 GST_END_TEST;
192
193
194 static Suite *
195 gst_task_suite (void)
196 {
197   Suite *s = suite_create ("GstTask");
198   TCase *tc_chain = tcase_create ("task tests");
199
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);
206
207   return s;
208 }
209
210 GST_CHECK_MAIN (gst_task);