2fd756ba73dc9a0c12a48078618c00b09992e1f2
[platform/upstream/gstreamer.git] / 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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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 #else
30 #include <sys/socket.h>
31 #endif
32
33 GST_START_TEST (test_poll_wait)
34 {
35   GstPoll *set;
36   GstPollFD rfd = GST_POLL_FD_INIT;
37   GstPollFD wfd = GST_POLL_FD_INIT;
38   gint socks[2];
39   guchar c = 'A';
40
41   set = gst_poll_new (FALSE);
42   fail_if (set == NULL, "Failed to create a GstPoll");
43
44 #ifdef G_OS_WIN32
45   fail_if (_pipe (socks, 4096, _O_BINARY) < 0, "Could not create a pipe");
46 #else
47   fail_if (socketpair (PF_UNIX, SOCK_STREAM, 0, socks) < 0,
48       "Could not create a pipe");
49 #endif
50   rfd.fd = socks[0];
51   wfd.fd = socks[1];
52
53   fail_unless (gst_poll_add_fd (set, &rfd), "Could not add read descriptor");
54   fail_unless (gst_poll_fd_ctl_read (set, &rfd, TRUE),
55       "Could not mark the descriptor as readable");
56
57   fail_unless (write (wfd.fd, &c, 1) == 1, "write() failed");
58
59   fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 1,
60       "One descriptor should be available");
61   fail_unless (gst_poll_fd_can_read (set, &rfd),
62       "Read descriptor should be readable");
63   fail_if (gst_poll_fd_can_write (set, &rfd),
64       "Read descriptor should not be writeable");
65
66   fail_unless (gst_poll_add_fd (set, &wfd), "Could not add write descriptor");
67   fail_unless (gst_poll_fd_ctl_write (set, &wfd, TRUE),
68       "Could not mark the descriptor as writeable");
69
70   fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 2,
71       "Two descriptors should be available");
72   fail_unless (gst_poll_fd_can_read (set, &rfd),
73       "Read descriptor should be readable");
74   fail_if (gst_poll_fd_can_write (set, &rfd),
75       "Read descriptor should not be writeable");
76   fail_if (gst_poll_fd_can_read (set, &wfd),
77       "Write descriptor should not be readable");
78   fail_unless (gst_poll_fd_can_write (set, &wfd),
79       "Write descriptor should be writeable");
80
81   fail_unless (read (rfd.fd, &c, 1) == 1, "read() failed");
82
83   fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 1,
84       "One descriptor should be available");
85   fail_if (gst_poll_fd_can_read (set, &rfd),
86       "Read descriptor should not be readable");
87   fail_if (gst_poll_fd_can_write (set, &rfd),
88       "Read descriptor should not be writeable");
89   fail_if (gst_poll_fd_can_read (set, &wfd),
90       "Write descriptor should not be readable");
91   fail_unless (gst_poll_fd_can_write (set, &wfd),
92       "Write descriptor should be writeable");
93
94   gst_poll_free (set);
95   close (socks[0]);
96   close (socks[1]);
97 }
98
99 GST_END_TEST;
100
101 GST_START_TEST (test_poll_basic)
102 {
103   GstPoll *set;
104   GstPollFD fd = GST_POLL_FD_INIT;
105
106   fd.fd = 1;
107
108   set = gst_poll_new (FALSE);
109   fail_if (set == NULL, "Failed to create a GstPoll");
110
111   fail_unless (gst_poll_add_fd (set, &fd), "Could not add descriptor");
112   fail_unless (gst_poll_fd_ctl_write (set, &fd, TRUE),
113       "Could not mark the descriptor as writeable");
114   fail_unless (gst_poll_fd_ctl_read (set, &fd, TRUE),
115       "Could not mark the descriptor as readable");
116   fail_if (gst_poll_fd_has_closed (set, &fd),
117       "Descriptor should not be closed");
118   fail_if (gst_poll_fd_has_error (set, &fd),
119       "Descriptor should not have an error");
120   fail_if (gst_poll_fd_can_write (set, &fd),
121       "Descriptor should not be writeable");
122   fail_if (gst_poll_fd_can_read (set, &fd),
123       "Descriptor should not be readable");
124   fail_unless (gst_poll_remove_fd (set, &fd), "Could not remove descriptor");
125
126   fail_if (gst_poll_remove_fd (set, &fd),
127       "Could remove already removed descriptor");
128
129   fail_unless (gst_poll_wait (set, 50 * GST_MSECOND) == 0,
130       "Waiting did not timeout");
131
132   gst_poll_free (set);
133
134   set = gst_poll_new (TRUE);
135   fail_if (set == NULL, "Failed to create a GstPoll");
136   gst_poll_set_flushing (set, TRUE);
137   gst_poll_free (set);
138 }
139
140 GST_END_TEST;
141
142 static gpointer
143 delayed_stop (gpointer data)
144 {
145   GstPoll *set = data;
146
147   THREAD_START ();
148
149   g_usleep (500000);
150
151   gst_poll_set_flushing (set, TRUE);
152
153   return NULL;
154 }
155
156 GST_START_TEST (test_poll_wait_stop)
157 {
158   GstPoll *set;
159
160   set = gst_poll_new (TRUE);
161   fail_if (set == NULL, "Failed to create a GstPoll");
162
163   MAIN_START_THREADS (1, delayed_stop, set);
164
165   fail_unless (gst_poll_wait (set, GST_SECOND) != 0, "Waiting timed out");
166
167   MAIN_STOP_THREADS ();
168
169   gst_poll_free (set);
170 }
171
172 GST_END_TEST;
173
174 static gpointer
175 delayed_restart (gpointer data)
176 {
177   GstPoll *set = data;
178   GstPollFD fd = GST_POLL_FD_INIT;
179
180   fd.fd = 1;
181
182   THREAD_START ();
183
184   g_usleep (500000);
185
186   gst_poll_add_fd (set, &fd);
187   gst_poll_fd_ctl_write (set, &fd, TRUE);
188   gst_poll_restart (set);
189
190   return NULL;
191 }
192
193 GST_START_TEST (test_poll_wait_restart)
194 {
195   GstPoll *set;
196   GstPollFD fd = GST_POLL_FD_INIT;
197
198   fd.fd = 1;
199
200   set = gst_poll_new (TRUE);
201   fail_if (set == NULL, "Failed to create a GstPoll");
202
203   MAIN_START_THREADS (1, delayed_restart, set);
204
205   fail_unless (gst_poll_wait (set, GST_SECOND) > 0, "Waiting was interrupted");
206   fail_unless (gst_poll_fd_can_write (set, &fd),
207       "Write descriptor should be writeable");
208
209   MAIN_STOP_THREADS ();
210
211   gst_poll_free (set);
212 }
213
214 GST_END_TEST;
215
216 static gpointer
217 delayed_flush (gpointer data)
218 {
219   GstPoll *set = data;
220
221   THREAD_START ();
222
223   g_usleep (500000);
224   gst_poll_set_flushing (set, TRUE);
225
226   return NULL;
227 }
228
229 GST_START_TEST (test_poll_wait_flush)
230 {
231   GstPoll *set;
232
233   set = gst_poll_new (TRUE);
234   fail_if (set == NULL, "Failed to create a GstPoll");
235
236   gst_poll_set_flushing (set, TRUE);
237   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
238       "Waiting was not flushed");
239   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
240       "Waiting was not flushed");
241
242   gst_poll_set_flushing (set, FALSE);
243   fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
244
245   MAIN_START_THREADS (1, delayed_flush, set);
246
247   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
248       "Waiting was not flushed");
249   fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
250       "Waiting was not flushed");
251
252   gst_poll_set_flushing (set, FALSE);
253   fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
254
255   MAIN_STOP_THREADS ();
256
257   gst_poll_free (set);
258 }
259
260 GST_END_TEST;
261
262 static gpointer
263 delayed_control (gpointer data)
264 {
265   GstPoll *set = data;
266   GstPollFD fd = GST_POLL_FD_INIT;
267
268   fd.fd = 1;
269
270   THREAD_START ();
271
272   g_usleep (500000);
273
274   gst_poll_add_fd (set, &fd);
275   gst_poll_fd_ctl_write (set, &fd, TRUE);
276   gst_poll_restart (set);
277
278   g_mutex_lock (&mutex);
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
332 #ifndef G_OS_WIN32
333   tcase_add_test (tc_chain, test_poll_basic);
334   tcase_add_test (tc_chain, test_poll_wait);
335   tcase_add_test (tc_chain, test_poll_wait_stop);
336   tcase_add_test (tc_chain, test_poll_wait_restart);
337   tcase_add_test (tc_chain, test_poll_wait_flush);
338   tcase_add_test (tc_chain, test_poll_controllable);
339 #else
340   tcase_skip_broken_test (tc_chain, test_poll_basic);
341   tcase_skip_broken_test (tc_chain, test_poll_wait);
342   tcase_skip_broken_test (tc_chain, test_poll_wait_stop);
343   tcase_skip_broken_test (tc_chain, test_poll_wait_restart);
344   tcase_skip_broken_test (tc_chain, test_poll_wait_flush);
345   tcase_skip_broken_test (tc_chain, test_poll_controllable);
346 #endif
347
348   return s;
349 }
350
351 GST_CHECK_MAIN (gst_poll);