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.
24 #include <gst/check/gstcheck.h>
30 #include <sys/socket.h>
33 GST_START_TEST (test_poll_wait)
36 GstPollFD rfd = GST_POLL_FD_INIT;
37 GstPollFD wfd = GST_POLL_FD_INIT;
41 set = gst_poll_new (FALSE);
42 fail_if (set == NULL, "Failed to create a GstPoll");
45 fail_if (_pipe (socks, 4096, _O_BINARY) < 0, "Could not create a pipe");
47 fail_if (socketpair (PF_UNIX, SOCK_STREAM, 0, socks) < 0,
48 "Could not create a pipe");
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");
57 fail_unless (write (wfd.fd, &c, 1) == 1, "write() failed");
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");
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");
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");
81 fail_unless (read (rfd.fd, &c, 1) == 1, "read() failed");
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");
101 GST_START_TEST (test_poll_basic)
104 GstPollFD fd = GST_POLL_FD_INIT;
108 set = gst_poll_new (FALSE);
109 fail_if (set == NULL, "Failed to create a GstPoll");
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");
126 fail_if (gst_poll_remove_fd (set, &fd),
127 "Could remove already removed descriptor");
129 fail_unless (gst_poll_wait (set, 50 * GST_MSECOND) == 0,
130 "Waiting did not timeout");
134 set = gst_poll_new (TRUE);
135 fail_if (set == NULL, "Failed to create a GstPoll");
136 gst_poll_set_flushing (set, TRUE);
143 delayed_stop (gpointer data)
151 gst_poll_set_flushing (set, TRUE);
156 GST_START_TEST (test_poll_wait_stop)
160 set = gst_poll_new (TRUE);
161 fail_if (set == NULL, "Failed to create a GstPoll");
163 MAIN_START_THREADS (1, delayed_stop, set);
165 fail_unless (gst_poll_wait (set, GST_SECOND) != 0, "Waiting timed out");
167 MAIN_STOP_THREADS ();
175 delayed_restart (gpointer data)
178 GstPollFD fd = GST_POLL_FD_INIT;
186 gst_poll_add_fd (set, &fd);
187 gst_poll_fd_ctl_write (set, &fd, TRUE);
188 gst_poll_restart (set);
193 GST_START_TEST (test_poll_wait_restart)
196 GstPollFD fd = GST_POLL_FD_INIT;
200 set = gst_poll_new (TRUE);
201 fail_if (set == NULL, "Failed to create a GstPoll");
203 MAIN_START_THREADS (1, delayed_restart, set);
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");
209 MAIN_STOP_THREADS ();
217 delayed_flush (gpointer data)
224 gst_poll_set_flushing (set, TRUE);
229 GST_START_TEST (test_poll_wait_flush)
233 set = gst_poll_new (TRUE);
234 fail_if (set == NULL, "Failed to create a GstPoll");
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");
242 gst_poll_set_flushing (set, FALSE);
243 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
245 MAIN_START_THREADS (1, delayed_flush, set);
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");
252 gst_poll_set_flushing (set, FALSE);
253 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
255 MAIN_STOP_THREADS ();
263 delayed_control (gpointer data)
266 GstPollFD fd = GST_POLL_FD_INIT;
274 gst_poll_add_fd (set, &fd);
275 gst_poll_fd_ctl_write (set, &fd, TRUE);
276 gst_poll_restart (set);
278 g_mutex_lock (&mutex);
279 THREAD_SYNCHRONIZE ();
283 gst_poll_add_fd (set, &fd);
284 gst_poll_fd_ctl_write (set, &fd, TRUE);
285 gst_poll_restart (set);
290 GST_START_TEST (test_poll_controllable)
293 GstPollFD fd = GST_POLL_FD_INIT;
297 set = gst_poll_new (FALSE);
298 fail_if (set == NULL, "Failed to create a GstPoll");
300 MAIN_START_THREADS (1, delayed_control, set);
302 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
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");
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");
314 MAIN_STOP_THREADS ();
322 gst_poll_suite (void)
324 Suite *s = suite_create ("GstPoll");
325 TCase *tc_chain = tcase_create ("general");
327 /* turn off timeout */
328 tcase_set_timeout (tc_chain, 60);
330 suite_add_tcase (s, tc_chain);
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);
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);
351 GST_CHECK_MAIN (gst_poll);