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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 #include <gst/check/gstcheck.h>
29 #define EINPROGRESS WSAEINPROGRESS
31 #include <sys/socket.h>
34 GST_START_TEST (test_poll_wait)
37 GstPollFD rfd = GST_POLL_FD_INIT;
38 GstPollFD wfd = GST_POLL_FD_INIT;
42 set = gst_poll_new (FALSE);
43 fail_if (set == NULL, "Failed to create a GstPoll");
46 fail_if (_pipe (socks, 4096, _O_BINARY) < 0, "Could not create a pipe");
48 fail_if (socketpair (PF_UNIX, SOCK_STREAM, 0, socks) < 0,
49 "Could not create a pipe");
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");
58 fail_unless (write (wfd.fd, &c, 1) == 1, "write() failed");
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");
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");
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");
82 fail_unless (read (rfd.fd, &c, 1) == 1, "read() failed");
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");
102 GST_START_TEST (test_poll_basic)
105 GstPollFD fd = GST_POLL_FD_INIT;
109 set = gst_poll_new (FALSE);
110 fail_if (set == NULL, "Failed to create a GstPoll");
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");
127 fail_if (gst_poll_remove_fd (set, &fd),
128 "Could remove already removed descriptor");
130 fail_unless (gst_poll_wait (set, 50 * GST_MSECOND) == 0,
131 "Waiting did not timeout");
135 set = gst_poll_new (TRUE);
136 fail_if (set == NULL, "Failed to create a GstPoll");
137 gst_poll_set_flushing (set, TRUE);
144 delayed_stop (gpointer data)
152 gst_poll_set_flushing (set, TRUE);
157 GST_START_TEST (test_poll_wait_stop)
161 set = gst_poll_new (TRUE);
162 fail_if (set == NULL, "Failed to create a GstPoll");
164 MAIN_START_THREADS (1, delayed_stop, set);
166 fail_unless (gst_poll_wait (set, GST_SECOND) != 0, "Waiting timed out");
168 MAIN_STOP_THREADS ();
176 delayed_restart (gpointer data)
179 GstPollFD fd = GST_POLL_FD_INIT;
187 gst_poll_add_fd (set, &fd);
188 gst_poll_fd_ctl_write (set, &fd, TRUE);
189 gst_poll_restart (set);
194 GST_START_TEST (test_poll_wait_restart)
197 GstPollFD fd = GST_POLL_FD_INIT;
201 set = gst_poll_new (TRUE);
202 fail_if (set == NULL, "Failed to create a GstPoll");
204 MAIN_START_THREADS (1, delayed_restart, set);
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");
210 MAIN_STOP_THREADS ();
218 delayed_flush (gpointer data)
225 gst_poll_set_flushing (set, TRUE);
230 GST_START_TEST (test_poll_wait_flush)
234 set = gst_poll_new (TRUE);
235 fail_if (set == NULL, "Failed to create a GstPoll");
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");
243 gst_poll_set_flushing (set, FALSE);
244 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
246 MAIN_START_THREADS (1, delayed_flush, set);
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");
253 gst_poll_set_flushing (set, FALSE);
254 fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
256 MAIN_STOP_THREADS ();
264 delayed_control (gpointer data)
267 GstPollFD fd = GST_POLL_FD_INIT;
275 gst_poll_add_fd (set, &fd);
276 gst_poll_fd_ctl_write (set, &fd, TRUE);
277 gst_poll_restart (set);
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);
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);
341 GST_CHECK_MAIN (gst_poll);