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