3 * unit test for GstPoll
5 * Copyright (C) <2007> Peter Kjellerstedt <pkj@axis.com>
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.
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.
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.
26 #include <gst/check/gstcheck.h>
33 #include <sys/socket.h>
36 GST_START_TEST (test_poll_wait)
39 GstPollFD rfd = GST_POLL_FD_INIT;
40 GstPollFD wfd = GST_POLL_FD_INIT;
44 set = gst_poll_new (FALSE);
45 fail_if (set == NULL, "Failed to create a GstPoll");
48 fail_if (_pipe (socks, 4096, _O_BINARY) < 0, "Could not create a pipe");
50 fail_if (socketpair (PF_UNIX, SOCK_STREAM, 0, socks) < 0,
51 "Could not create a pipe");
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");
60 fail_unless (write (wfd.fd, &c, 1) == 1, "write() failed");
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");
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");
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");
84 fail_unless (read (rfd.fd, &c, 1) == 1, "read() failed");
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");
104 GST_START_TEST (test_poll_basic)
107 GstPollFD fd = GST_POLL_FD_INIT;
111 set = gst_poll_new (FALSE);
112 fail_if (set == NULL, "Failed to create a GstPoll");
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");
129 fail_if (gst_poll_remove_fd (set, &fd),
130 "Could remove already removed descriptor");
132 fail_unless (gst_poll_wait (set, 50 * GST_MSECOND) == 0,
133 "Waiting did not timeout");
137 set = gst_poll_new (TRUE);
138 fail_if (set == NULL, "Failed to create a GstPoll");
139 gst_poll_set_flushing (set, TRUE);
146 delayed_stop (gpointer data)
154 gst_poll_set_flushing (set, TRUE);
159 GST_START_TEST (test_poll_wait_stop)
163 set = gst_poll_new (TRUE);
164 fail_if (set == NULL, "Failed to create a GstPoll");
166 MAIN_START_THREADS (1, delayed_stop, set);
168 fail_unless (gst_poll_wait (set, GST_SECOND) != 0, "Waiting timed out");
170 MAIN_STOP_THREADS ();
178 delayed_restart (gpointer data)
181 GstPollFD fd = GST_POLL_FD_INIT;
189 gst_poll_add_fd (set, &fd);
190 gst_poll_fd_ctl_write (set, &fd, TRUE);
191 gst_poll_restart (set);
196 GST_START_TEST (test_poll_wait_restart)
199 GstPollFD fd = GST_POLL_FD_INIT;
203 set = gst_poll_new (TRUE);
204 fail_if (set == NULL, "Failed to create a GstPoll");
206 MAIN_START_THREADS (1, delayed_restart, set);
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");
212 MAIN_STOP_THREADS ();
220 delayed_flush (gpointer data)
227 gst_poll_set_flushing (set, TRUE);
232 GST_START_TEST (test_poll_wait_flush)
236 set = gst_poll_new (TRUE);
237 fail_if (set == NULL, "Failed to create a GstPoll");
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");
245 gst_poll_set_flushing (set, FALSE);
246 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
248 MAIN_START_THREADS (1, delayed_flush, set);
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");
255 gst_poll_set_flushing (set, FALSE);
256 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
258 MAIN_STOP_THREADS ();
266 delayed_control (gpointer data)
269 GstPollFD fd = GST_POLL_FD_INIT;
277 gst_poll_add_fd (set, &fd);
278 gst_poll_fd_ctl_write (set, &fd, TRUE);
279 gst_poll_restart (set);
281 g_mutex_lock (&mutex);
282 THREAD_SYNCHRONIZE ();
286 gst_poll_add_fd (set, &fd);
287 gst_poll_fd_ctl_write (set, &fd, TRUE);
288 gst_poll_restart (set);
293 GST_START_TEST (test_poll_controllable)
296 GstPollFD fd = GST_POLL_FD_INIT;
300 set = gst_poll_new (FALSE);
301 fail_if (set == NULL, "Failed to create a GstPoll");
303 MAIN_START_THREADS (1, delayed_control, set);
305 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
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");
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");
317 MAIN_STOP_THREADS ();
325 gst_poll_suite (void)
327 Suite *s = suite_create ("GstPoll");
328 TCase *tc_chain = tcase_create ("general");
330 /* turn off timeout */
331 tcase_set_timeout (tc_chain, 60);
333 suite_add_tcase (s, tc_chain);
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);
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);
354 GST_CHECK_MAIN (gst_poll);