gstpad: Probes that return HANDLED can reset the data info field
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/check/gstcheck.h>
26
27 static GMutex task_lock;
28 static GCond task_cond;
29
30 static GRecMutex task_mutex;
31
32 #define TEST_RACE_ITERATIONS 1000
33
34 static void
35 task_signal_pause_func (void *data)
36 {
37   GstTask **t = data;
38
39   g_mutex_lock (&task_lock);
40   GST_DEBUG ("signal");
41   g_cond_signal (&task_cond);
42
43   gst_task_pause (*t);
44   g_mutex_unlock (&task_lock);
45 }
46
47 GST_START_TEST (test_pause_stop_race)
48 {
49   guint it = TEST_RACE_ITERATIONS;
50   GstTask *t;
51   gboolean ret;
52
53   t = gst_task_new (task_signal_pause_func, &t, NULL);
54   fail_if (t == NULL);
55
56   g_rec_mutex_init (&task_mutex);
57   gst_task_set_lock (t, &task_mutex);
58
59   g_cond_init (&task_cond);
60   g_mutex_init (&task_lock);
61
62   while (it-- > 0) {
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);
72
73     GST_DEBUG ("starting");
74     ret = gst_task_stop (t);
75     fail_unless (ret == TRUE);
76
77     GST_DEBUG ("joining");
78     ret = gst_task_join (t);
79     fail_unless (ret == TRUE);
80   }
81
82   g_cond_clear (&task_cond);
83   g_mutex_clear (&task_lock);
84
85   gst_object_unref (t);
86 }
87
88 GST_END_TEST;
89
90 static void
91 task_func2 (void *data)
92 {
93   gboolean ret;
94   GstTask *t = *((GstTask **) data);
95
96   g_mutex_lock (&task_lock);
97   GST_DEBUG ("signal");
98   g_cond_signal (&task_cond);
99   g_mutex_unlock (&task_lock);
100
101   ASSERT_WARNING (ret = gst_task_join (t));
102   fail_unless (ret == FALSE);
103 }
104
105 GST_START_TEST (test_join)
106 {
107   GstTask *t;
108   gboolean ret;
109
110   t = gst_task_new (task_func2, &t, NULL);
111   fail_if (t == NULL);
112
113   g_rec_mutex_init (&task_mutex);
114   gst_task_set_lock (t, &task_mutex);
115
116   g_cond_init (&task_cond);
117   g_mutex_init (&task_lock);
118
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);
128
129   GST_DEBUG ("joining");
130   ret = gst_task_join (t);
131   fail_unless (ret == TRUE);
132
133   gst_task_cleanup_all ();
134
135   gst_object_unref (t);
136 }
137
138 GST_END_TEST;
139
140 static void
141 task_func (void *data)
142 {
143   g_mutex_lock (&task_lock);
144   GST_DEBUG ("signal");
145   g_cond_signal (&task_cond);
146   g_mutex_unlock (&task_lock);
147 }
148
149 GST_START_TEST (test_lock_start)
150 {
151   GstTask *t;
152   gboolean ret;
153
154   t = gst_task_new (task_func, NULL, NULL);
155   fail_if (t == NULL);
156
157   g_rec_mutex_init (&task_mutex);
158   gst_task_set_lock (t, &task_mutex);
159
160   g_cond_init (&task_cond);
161   g_mutex_init (&task_lock);
162
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);
172
173   /* cannot set mutex now */
174   ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
175
176   GST_DEBUG ("joining");
177   ret = gst_task_join (t);
178   fail_unless (ret == TRUE);
179
180   gst_task_cleanup_all ();
181
182   gst_object_unref (t);
183 }
184
185 GST_END_TEST;
186
187 GST_START_TEST (test_lock)
188 {
189   GstTask *t;
190   gboolean ret;
191
192   t = gst_task_new (task_func, NULL, NULL);
193   fail_if (t == NULL);
194
195   g_rec_mutex_init (&task_mutex);
196   gst_task_set_lock (t, &task_mutex);
197
198   GST_DEBUG ("pause");
199   ret = gst_task_pause (t);
200   fail_unless (ret == TRUE);
201
202   g_usleep (1 * G_USEC_PER_SEC / 2);
203
204   GST_DEBUG ("joining");
205   ret = gst_task_join (t);
206   fail_unless (ret == TRUE);
207
208   g_usleep (1 * G_USEC_PER_SEC / 2);
209
210   gst_object_unref (t);
211 }
212
213 GST_END_TEST;
214
215 GST_START_TEST (test_no_lock)
216 {
217   GstTask *t;
218   gboolean ret;
219
220   t = gst_task_new (task_func, NULL, NULL);
221   fail_if (t == NULL);
222
223   /* stop should be possible without lock */
224   gst_task_stop (t);
225
226   /* pause should give a warning */
227   ASSERT_WARNING (ret = gst_task_pause (t));
228   fail_unless (ret == FALSE);
229
230   /* start should give a warning */
231   ASSERT_WARNING (ret = gst_task_start (t));
232   fail_unless (ret == FALSE);
233
234   /* stop should be possible without lock */
235   gst_task_stop (t);
236
237   gst_object_unref (t);
238 }
239
240 GST_END_TEST;
241
242 GST_START_TEST (test_create)
243 {
244   GstTask *t;
245
246   t = gst_task_new (task_func, NULL, NULL);
247   fail_if (t == NULL);
248
249   gst_object_unref (t);
250 }
251
252 GST_END_TEST;
253
254
255 static Suite *
256 gst_task_suite (void)
257 {
258   Suite *s = suite_create ("GstTask");
259   TCase *tc_chain = tcase_create ("task tests");
260
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);
268
269   return s;
270 }
271
272 GST_CHECK_MAIN (gst_task);