1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/* GStreamer
*
* unit test for GstPoll
*
* Copyright (C) <2007> Peter Kjellerstedt <pkj@axis.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <unistd.h>
#include <gst/check/gstcheck.h>
#ifdef G_OS_WIN32
#include <winsock2.h>
#include <fcntl.h>
#define EINPROGRESS WSAEINPROGRESS
#else
#include <sys/socket.h>
#endif
GST_START_TEST (test_poll_wait)
{
GstPoll *set;
GstPollFD rfd = GST_POLL_FD_INIT;
GstPollFD wfd = GST_POLL_FD_INIT;
gint socks[2];
guchar c = 'A';
set = gst_poll_new (FALSE);
fail_if (set == NULL, "Failed to create a GstPoll");
#ifdef G_OS_WIN32
fail_if (_pipe (socks, 4096, _O_BINARY) < 0, "Could not create a pipe");
#else
fail_if (socketpair (PF_UNIX, SOCK_STREAM, 0, socks) < 0,
"Could not create a pipe");
#endif
rfd.fd = socks[0];
wfd.fd = socks[1];
fail_unless (gst_poll_add_fd (set, &rfd), "Could not add read descriptor");
fail_unless (gst_poll_fd_ctl_read (set, &rfd, TRUE),
"Could not mark the descriptor as readable");
fail_unless (write (wfd.fd, &c, 1) == 1, "write() failed");
fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 1,
"One descriptor should be available");
fail_unless (gst_poll_fd_can_read (set, &rfd),
"Read descriptor should be readable");
fail_if (gst_poll_fd_can_write (set, &rfd),
"Read descriptor should not be writeable");
fail_unless (gst_poll_add_fd (set, &wfd), "Could not add write descriptor");
fail_unless (gst_poll_fd_ctl_write (set, &wfd, TRUE),
"Could not mark the descriptor as writeable");
fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 2,
"Two descriptors should be available");
fail_unless (gst_poll_fd_can_read (set, &rfd),
"Read descriptor should be readable");
fail_if (gst_poll_fd_can_write (set, &rfd),
"Read descriptor should not be writeable");
fail_if (gst_poll_fd_can_read (set, &wfd),
"Write descriptor should not be readable");
fail_unless (gst_poll_fd_can_write (set, &wfd),
"Write descriptor should be writeable");
fail_unless (read (rfd.fd, &c, 1) == 1, "read() failed");
fail_unless (gst_poll_wait (set, GST_CLOCK_TIME_NONE) == 1,
"One descriptor should be available");
fail_if (gst_poll_fd_can_read (set, &rfd),
"Read descriptor should not be readable");
fail_if (gst_poll_fd_can_write (set, &rfd),
"Read descriptor should not be writeable");
fail_if (gst_poll_fd_can_read (set, &wfd),
"Write descriptor should not be readable");
fail_unless (gst_poll_fd_can_write (set, &wfd),
"Write descriptor should be writeable");
gst_poll_free (set);
close (socks[0]);
close (socks[1]);
}
GST_END_TEST;
GST_START_TEST (test_poll_basic)
{
GstPoll *set;
GstPollFD fd = GST_POLL_FD_INIT;
fd.fd = 1;
set = gst_poll_new (FALSE);
fail_if (set == NULL, "Failed to create a GstPoll");
fail_unless (gst_poll_add_fd (set, &fd), "Could not add descriptor");
fail_unless (gst_poll_fd_ctl_write (set, &fd, TRUE),
"Could not mark the descriptor as writeable");
fail_unless (gst_poll_fd_ctl_read (set, &fd, TRUE),
"Could not mark the descriptor as readable");
fail_if (gst_poll_fd_has_closed (set, &fd),
"Descriptor should not be closed");
fail_if (gst_poll_fd_has_error (set, &fd),
"Descriptor should not have an error");
fail_if (gst_poll_fd_can_write (set, &fd),
"Descriptor should not be writeable");
fail_if (gst_poll_fd_can_read (set, &fd),
"Descriptor should not be readable");
fail_unless (gst_poll_remove_fd (set, &fd), "Could not remove descriptor");
fail_if (gst_poll_remove_fd (set, &fd),
"Could remove already removed descriptor");
fail_unless (gst_poll_wait (set, 50 * GST_MSECOND) == 0,
"Waiting did not timeout");
gst_poll_free (set);
set = gst_poll_new (TRUE);
fail_if (set == NULL, "Failed to create a GstPoll");
gst_poll_set_flushing (set, TRUE);
gst_poll_free (set);
}
GST_END_TEST;
static gpointer
delayed_stop (gpointer data)
{
GstPoll *set = data;
THREAD_START ();
g_usleep (500000);
gst_poll_set_flushing (set, TRUE);
return NULL;
}
GST_START_TEST (test_poll_wait_stop)
{
GstPoll *set;
set = gst_poll_new (TRUE);
fail_if (set == NULL, "Failed to create a GstPoll");
MAIN_START_THREADS (1, delayed_stop, set);
fail_unless (gst_poll_wait (set, GST_SECOND) != 0, "Waiting timed out");
MAIN_STOP_THREADS ();
gst_poll_free (set);
}
GST_END_TEST;
static gpointer
delayed_restart (gpointer data)
{
GstPoll *set = data;
GstPollFD fd = GST_POLL_FD_INIT;
fd.fd = 1;
THREAD_START ();
g_usleep (500000);
gst_poll_add_fd (set, &fd);
gst_poll_fd_ctl_write (set, &fd, TRUE);
gst_poll_restart (set);
return NULL;
}
GST_START_TEST (test_poll_wait_restart)
{
GstPoll *set;
GstPollFD fd = GST_POLL_FD_INIT;
fd.fd = 1;
set = gst_poll_new (TRUE);
fail_if (set == NULL, "Failed to create a GstPoll");
MAIN_START_THREADS (1, delayed_restart, set);
fail_unless (gst_poll_wait (set, GST_SECOND) > 0, "Waiting was interrupted");
fail_unless (gst_poll_fd_can_write (set, &fd),
"Write descriptor should be writeable");
MAIN_STOP_THREADS ();
gst_poll_free (set);
}
GST_END_TEST;
static gpointer
delayed_flush (gpointer data)
{
GstPoll *set = data;
THREAD_START ();
g_usleep (500000);
gst_poll_set_flushing (set, TRUE);
return NULL;
}
GST_START_TEST (test_poll_wait_flush)
{
GstPoll *set;
set = gst_poll_new (TRUE);
fail_if (set == NULL, "Failed to create a GstPoll");
gst_poll_set_flushing (set, TRUE);
fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
"Waiting was not flushed");
fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
"Waiting was not flushed");
gst_poll_set_flushing (set, FALSE);
fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
MAIN_START_THREADS (1, delayed_flush, set);
fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
"Waiting was not flushed");
fail_unless (gst_poll_wait (set, GST_SECOND) == -1 && errno == EBUSY,
"Waiting was not flushed");
gst_poll_set_flushing (set, FALSE);
fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
MAIN_STOP_THREADS ();
gst_poll_free (set);
}
GST_END_TEST;
static gpointer
delayed_control (gpointer data)
{
GstPoll *set = data;
GstPollFD fd = GST_POLL_FD_INIT;
fd.fd = 1;
THREAD_START ();
g_usleep (500000);
gst_poll_add_fd (set, &fd);
gst_poll_fd_ctl_write (set, &fd, TRUE);
gst_poll_restart (set);
THREAD_SYNCHRONIZE ();
g_usleep (500000);
gst_poll_add_fd (set, &fd);
gst_poll_fd_ctl_write (set, &fd, TRUE);
gst_poll_restart (set);
return NULL;
}
GST_START_TEST (test_poll_controllable)
{
GstPoll *set;
GstPollFD fd = GST_POLL_FD_INIT;
fd.fd = 1;
set = gst_poll_new (FALSE);
fail_if (set == NULL, "Failed to create a GstPoll");
MAIN_START_THREADS (1, delayed_control, set);
fail_unless (gst_poll_wait (set, GST_SECOND) == 0, "Waiting did not timeout");
fail_unless (gst_poll_remove_fd (set, &fd), "Could not remove descriptor");
fail_unless (gst_poll_set_controllable (set, TRUE),
"Could not make the set controllable");
MAIN_SYNCHRONIZE ();
fail_unless (gst_poll_wait (set, GST_SECOND) > 0, "Waiting was interrupted");
fail_unless (gst_poll_fd_can_write (set, &fd),
"Write descriptor should be writeable");
MAIN_STOP_THREADS ();
gst_poll_free (set);
}
GST_END_TEST;
static Suite *
gst_poll_suite (void)
{
Suite *s = suite_create ("GstPoll");
TCase *tc_chain = tcase_create ("general");
/* turn off timeout */
tcase_set_timeout (tc_chain, 60);
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, test_poll_basic);
tcase_add_test (tc_chain, test_poll_wait);
tcase_add_test (tc_chain, test_poll_wait_stop);
tcase_add_test (tc_chain, test_poll_wait_restart);
tcase_add_test (tc_chain, test_poll_wait_flush);
tcase_add_test (tc_chain, test_poll_controllable);
return s;
}
GST_CHECK_MAIN (gst_poll);
|