tests/check/: use the new macro
[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  * gstelement.c: Unit test for GstElement
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 GStaticRecMutex task_mutex = G_STATIC_REC_MUTEX_INIT;
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_create (task_func2, &t);
50   fail_if (t == NULL);
51
52   gst_task_set_lock (t, &task_mutex);
53
54   task_cond = g_cond_new ();
55   task_lock = g_mutex_new ();
56
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);
66
67   GST_DEBUG ("joining");
68   ret = gst_task_join (t);
69   fail_unless (ret == TRUE);
70
71   gst_object_unref (t);
72 }
73
74 GST_END_TEST;
75
76 static void
77 task_func (void *data)
78 {
79   g_mutex_lock (task_lock);
80   GST_DEBUG ("signal");
81   g_cond_signal (task_cond);
82   g_mutex_unlock (task_lock);
83 }
84
85 GST_START_TEST (test_lock_start)
86 {
87   GstTask *t;
88   gboolean ret;
89
90   t = gst_task_create (task_func, NULL);
91   fail_if (t == NULL);
92
93   gst_task_set_lock (t, &task_mutex);
94
95   task_cond = g_cond_new ();
96   task_lock = g_mutex_new ();
97
98   g_mutex_lock (task_lock);
99   GST_DEBUG ("starting");
100   ret = gst_task_start (t);
101   fail_unless (ret == TRUE);
102   /* wait for it to spin up */
103   GST_DEBUG ("waiting");
104   g_cond_wait (task_cond, task_lock);
105   GST_DEBUG ("done waiting");
106   g_mutex_unlock (task_lock);
107
108   /* cannot set mutex now */
109   ASSERT_WARNING (gst_task_set_lock (t, &task_mutex));
110
111   GST_DEBUG ("joining");
112   ret = gst_task_join (t);
113   fail_unless (ret == TRUE);
114
115   gst_object_unref (t);
116 }
117
118 GST_END_TEST;
119
120 GST_START_TEST (test_lock)
121 {
122   GstTask *t;
123   gboolean ret;
124
125   t = gst_task_create (task_func, NULL);
126   fail_if (t == NULL);
127
128   gst_task_set_lock (t, &task_mutex);
129
130   GST_DEBUG ("pause");
131   ret = gst_task_pause (t);
132   fail_unless (ret == TRUE);
133
134   g_usleep (1 * G_USEC_PER_SEC / 2);
135
136   GST_DEBUG ("joining");
137   ret = gst_task_join (t);
138   fail_unless (ret == TRUE);
139
140   g_usleep (1 * G_USEC_PER_SEC / 2);
141
142   gst_object_unref (t);
143 }
144
145 GST_END_TEST;
146
147 GST_START_TEST (test_no_lock)
148 {
149   GstTask *t;
150   gboolean ret;
151
152   t = gst_task_create (task_func, NULL);
153   fail_if (t == NULL);
154
155   /* stop should be possible without lock */
156   gst_task_stop (t);
157
158   /* pause should give a warning */
159   ASSERT_WARNING (ret = gst_task_pause (t));
160   fail_unless (ret == FALSE);
161
162   /* start should give a warning */
163   ASSERT_WARNING (ret = gst_task_start (t));
164   fail_unless (ret == FALSE);
165
166   /* stop should be possible without lock */
167   gst_task_stop (t);
168
169   gst_object_unref (t);
170 }
171
172 GST_END_TEST;
173
174 GST_START_TEST (test_create)
175 {
176   GstTask *t;
177
178   t = gst_task_create (task_func, NULL);
179   fail_if (t == NULL);
180
181   gst_object_unref (t);
182 }
183
184 GST_END_TEST;
185
186
187 Suite *
188 gst_task_suite (void)
189 {
190   Suite *s = suite_create ("GstTask");
191   TCase *tc_chain = tcase_create ("task tests");
192
193   suite_add_tcase (s, tc_chain);
194   tcase_add_test (tc_chain, test_create);
195   tcase_add_test (tc_chain, test_no_lock);
196   tcase_add_test (tc_chain, test_lock);
197   tcase_add_test (tc_chain, test_lock_start);
198   tcase_add_test (tc_chain, test_join);
199
200   return s;
201 }
202
203 GST_CHECK_MAIN (gst_task);