Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / wearable / tests / check / gst / gstpoll.c
1 /* GStreamer
2  *
3  * unit test for GstPoll
4  *
5  * Copyright (C) <2007> Peter Kjellerstedt <pkj@axis.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24 #include <gst/check/gstcheck.h>
25
26 #ifdef G_OS_WIN32
27 #include <winsock2.h>
28 #include <fcntl.h>
29 #define EINPROGRESS WSAEINPROGRESS
30 #else
31 #include <sys/socket.h>
32 #endif
33
34 GST_START_TEST (test_poll_wait)
35 {
36   GstPoll *set;
37   GstPollFD rfd = GST_POLL_FD_INIT;
38   GstPollFD wfd = GST_POLL_FD_INIT;
39   gint socks[2];
40   guchar c = 'A';
41
42   set = gst_poll_new (FALSE);
43   fail_if (set == NULL, "Failed to create a GstPoll");
44
45 #ifdef G_OS_WIN32
46   fail_if (_pipe (socks, 4096, _O_BINARY) < 0, "Could not create a pipe");
47 #else
48   fail_if (socketpair (PF_UNIX, SOCK_STREAM, 0, socks) < 0,
49       "Could not create a pipe");
50 #endif
51   rfd.fd = socks[0];
52   wfd.fd = socks[1];
53
54   fail_unless (gst_poll_add_fd (set, &rfd), "Could not add read descriptor");
55   fail_unless (gst_poll_fd_ctl_read (set, &rfd, TRUE),
56       "Could not mark the descriptor as readable");
57
58   fail_unless (write (wfd.fd, &c, 1) == 1, "write() failed");
59
60   fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 1,
61       "One descriptor should be available");
62   fail_unless (gst_poll_fd_can_read (set, &rfd),
63       "Read descriptor should be readable");
64   fail_if (gst_poll_fd_can_write (set, &rfd),
65       "Read descriptor should not be writeable");
66
67   fail_unless (gst_poll_add_fd (set, &wfd), "Could not add write descriptor");
68   fail_unless (gst_poll_fd_ctl_write (set, &wfd, TRUE),
69       "Could not mark the descriptor as writeable");
70
71   fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 2,
72       "Two descriptors should be available");
73   fail_unless (gst_poll_fd_can_read (set, &rfd),
74       "Read descriptor should be readable");
75   fail_if (gst_poll_fd_can_write (set, &rfd),
76       "Read descriptor should not be writeable");
77   fail_if (gst_poll_fd_can_read (set, &wfd),
78       "Write descriptor should not be readable");
79   fail_unless (gst_poll_fd_can_write (set, &wfd),
80       "Write descriptor should be writeable");
81
82   fail_unless (read (rfd.fd, &c, 1) == 1, "read() failed");
83
84   fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 1,
85       "One descriptor should be available");
86   fail_if (gst_poll_fd_can_read (set, &rfd),
87       "Read descriptor should not be readable");
88   fail_if (gst_poll_fd_can_write (set, &rfd),
89       "Read descriptor should not be writeable");
90   fail_if (gst_poll_fd_can_read (set, &wfd),
91       "Write descriptor should not be readable");
92   fail_unless (gst_poll_fd_can_write (set, &wfd),
93       "Write descriptor should be writeable");
94
95   gst_poll_free (set);
96   close (socks[0]);
97   close (socks[1]);
98 }
99
100 GST_END_TEST;
101
102 GST_START_TEST (test_poll_basic)
103 {
104   GstPoll *set;
105   GstPollFD fd = GST_POLL_FD_INIT;
106
107   fd.fd = 1;
108
109   set = gst_poll_new (FALSE);
110   fail_if (set == NULL, "Failed to create a GstPoll");
111
112   fail_unless (gst_poll_add_fd (set, &fd), "Could not add descriptor");
113   fail_unless (gst_poll_fd_ctl_write (set, &fd, TRUE),
114       "Could not mark the descriptor as writeable");
115   fail_unless (gst_poll_fd_ctl_read (set, &fd, TRUE),
116       "Could not mark the descriptor as readable");
117   fail_if (gst_poll_fd_has_closed (set, &fd),
118       "Descriptor should not be closed");
119   fail_if (gst_poll_fd_has_error (set, &fd),
120       "Descriptor should not have an error");
121   fail_if (gst_poll_fd_can_write (set, &fd),
122       "Descriptor should not be writeable");
123   fail_if (gst_poll_fd_can_read (set, &fd),
124       "Descriptor should not be readable");
125   fail_unless (gst_poll_remove_fd (set, &fd), "Could not remove descriptor");
126
127   fail_if (gst_poll_remove_fd (set, &fd),
128       "Could remove already removed descriptor");
129
130   fail_unless (gst_poll_wait (set, 50 * GST_MSECOND) == 0,
131       "Waiting did not timeout");
132
133   gst_poll_free (set);
134
135   set = gst_poll_new (TRUE);
136   fail_if (set == NULL, "Failed to create a GstPoll");
137   gst_poll_set_flushing (set, TRUE);
138   gst_poll_free (set);
139 }
140
141 GST_END_TEST;
142
143 static gpointer
144 delayed_stop (gpointer data)
145 {
146   GstPoll *set = data;
147
148   THREAD_START ();
149
150   g_usleep (500000);
151
152   gst_poll_set_flushing (set, TRUE);
153
154   return NULL;
155 }
156
157 GST_START_TEST (test_poll_wait_stop)
158 {
159   GstPoll *set;
160
161   set = gst_poll_new (TRUE);
162   fail_if (set == NULL, "Failed to create a GstPoll");
163
164   MAIN_START_THREADS (1, delayed_stop, set);
165
166   fail_unless (gst_poll_wait (set, GST_SECOND) != 0, "Waiting timed out");
167
168   MAIN_STOP_THREADS ();
169
170   gst_poll_free (set);
171 }
172
173 GST_END_TEST;
174
175 static gpointer
176 delayed_restart (gpointer data)
177 {
178   GstPoll *set = data;
179   GstPollFD fd = GST_POLL_FD_INIT;
180
181   fd.fd = 1;
182
183   THREAD_START ();
184
185   g_usleep (500000);
186
187   gst_poll_add_fd (set, &fd);
188   gst_poll_fd_ctl_write (set, &fd, TRUE);
189   gst_poll_restart (set);
190
191   return NULL;
192 }
193
194 GST_START_TEST (test_poll_wait_restart)
195 {
196   GstPoll *set;
197   GstPollFD fd = GST_POLL_FD_INIT;
198
199   fd.fd = 1;
200
201   set = gst_poll_new (TRUE);
202   fail_if (set == NULL, "Failed to create a GstPoll");
203
204   MAIN_START_THREADS (1, delayed_restart, set);
205
206   fail_unless (gst_poll_wait (set, GST_SECOND) > 0, "Waiting was interrupted");
207   fail_unless (gst_poll_fd_can_write (set, &fd),
208       "Write descriptor should be writeable");
209
210   MAIN_STOP_THREADS ();
211
212   gst_poll_free (set);
213 }
214
215 GST_END_TEST;
216
217 static gpointer
218 delayed_flush (gpointer data)
219 {
220   GstPoll *set = data;
221
222   THREAD_START ();
223
224   g_usleep (500000);
225   gst_poll_set_flushing (set, TRUE);
226
227   return NULL;
228 }
229
230 GST_START_TEST (test_poll_wait_flush)
231 {
232   GstPoll *set;
233
234   set = gst_poll_new (TRUE);
235   fail_if (set == NULL, "Failed to create a GstPoll");
236
237   gst_poll_set_flushing (set, TRUE);
238   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
239       "Waiting was not flushed");
240   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
241       "Waiting was not flushed");
242
243   gst_poll_set_flushing (set, FALSE);
244   fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
245
246   MAIN_START_THREADS (1, delayed_flush, set);
247
248   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
249       "Waiting was not flushed");
250   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
251       "Waiting was not flushed");
252
253   gst_poll_set_flushing (set, FALSE);
254   fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
255
256   MAIN_STOP_THREADS ();
257
258   gst_poll_free (set);
259 }
260
261 GST_END_TEST;
262
263 static gpointer
264 delayed_control (gpointer data)
265 {
266   GstPoll *set = data;
267   GstPollFD fd = GST_POLL_FD_INIT;
268
269   fd.fd = 1;
270
271   THREAD_START ();
272
273   g_usleep (500000);
274
275   gst_poll_add_fd (set, &fd);
276   gst_poll_fd_ctl_write (set, &fd, TRUE);
277   gst_poll_restart (set);
278
279   THREAD_SYNCHRONIZE ();
280
281   g_usleep (500000);
282
283   gst_poll_add_fd (set, &fd);
284   gst_poll_fd_ctl_write (set, &fd, TRUE);
285   gst_poll_restart (set);
286
287   return NULL;
288 }
289
290 GST_START_TEST (test_poll_controllable)
291 {
292   GstPoll *set;
293   GstPollFD fd = GST_POLL_FD_INIT;
294
295   fd.fd = 1;
296
297   set = gst_poll_new (FALSE);
298   fail_if (set == NULL, "Failed to create a GstPoll");
299
300   MAIN_START_THREADS (1, delayed_control, set);
301
302   fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
303
304   fail_unless (gst_poll_remove_fd (set, &fd), "Could not remove descriptor");
305   fail_unless (gst_poll_set_controllable (set, TRUE),
306       "Could not make the set controllable");
307
308   MAIN_SYNCHRONIZE ();
309
310   fail_unless (gst_poll_wait (set, GST_SECOND) > 0, "Waiting was interrupted");
311   fail_unless (gst_poll_fd_can_write (set, &fd),
312       "Write descriptor should be writeable");
313
314   MAIN_STOP_THREADS ();
315
316   gst_poll_free (set);
317 }
318
319 GST_END_TEST;
320
321 static Suite *
322 gst_poll_suite (void)
323 {
324   Suite *s = suite_create ("GstPoll");
325   TCase *tc_chain = tcase_create ("general");
326
327   /* turn off timeout */
328   tcase_set_timeout (tc_chain, 60);
329
330   suite_add_tcase (s, tc_chain);
331   tcase_add_test (tc_chain, test_poll_basic);
332   tcase_add_test (tc_chain, test_poll_wait);
333   tcase_add_test (tc_chain, test_poll_wait_stop);
334   tcase_add_test (tc_chain, test_poll_wait_restart);
335   tcase_add_test (tc_chain, test_poll_wait_flush);
336   tcase_add_test (tc_chain, test_poll_controllable);
337
338   return s;
339 }
340
341 GST_CHECK_MAIN (gst_poll);