Asterisk - The Open Source Telephony Project GIT-master-2de1a68
test_poll.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2010, Digium, Inc.
5 *
6 * Tilghman Lesher <tlesher AT digium DOT com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \file
21 * \brief Poll Tests
22 *
23 * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
24 *
25 * Verify that the various poll implementations work as desired (ast_poll, ast_poll2)
26 * \ingroup tests
27 */
28
29/*** MODULEINFO
30 <depend>TEST_FRAMEWORK</depend>
31 <support_level>core</support_level>
32 ***/
33
34#include "asterisk.h"
35#include <signal.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <fcntl.h>
39#include <errno.h>
40#include <unistd.h>
41
42#include "asterisk/utils.h"
43#include "asterisk/module.h"
44#include "asterisk/test.h"
46
47static void *failsafe_cancel(void *vparent)
48{
49 pthread_t parent = (pthread_t) (long) vparent;
50
51 sleep(1);
52 pthread_testcancel();
53 pthread_kill(parent, SIGURG);
54 sleep(1);
55 pthread_testcancel();
56 pthread_kill(parent, SIGURG);
57 sleep(1);
58 pthread_testcancel();
59 pthread_kill(parent, SIGURG);
60 pthread_exit(NULL);
61}
62
63#define RESET for (i = 0; i < 4; i++) { pfd[i].revents = 0; }
65{
66#define FDNO 3
67 int fd[2], res = AST_TEST_PASS, i, res2;
68 int rdblocker[2];
69#if FDNO > 3
70 int wrblocker[2], consec_interrupt = 0;
71#endif
72 struct pollfd pfd[4] = { { .events = POLLOUT, }, { .events = POLLIN, }, { .events = POLLIN }, { .events = POLLOUT } };
73 pthread_t failsafe_tid;
74 struct timeval tv = { 0, 0 };
75#if FDNO > 3
76 char garbage[256] =
77 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
78 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
79 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
80 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/";
81#endif
82
83 switch (cmd) {
84 case TEST_INIT:
85 info->name = "poll_test";
86 info->category = "/main/poll/";
87 info->summary = "unit test for the ast_poll() API";
88 info->description =
89 "Verifies behavior for the ast_poll() API call";
90 return AST_TEST_NOT_RUN;
91 case TEST_EXECUTE:
92 break;
93 }
94
95 ast_test_status_update(test, "Creating handle that should NEVER block on write\n");
96 if ((fd[0] = open("/dev/null", O_WRONLY)) < 0) {
97 ast_test_status_update(test, "Unable to open a writable handle to /dev/null: %s\n", strerror(errno));
98 return AST_TEST_FAIL;
99 }
100
101 ast_test_status_update(test, "Creating handle that should NEVER block on read\n");
102 if ((fd[1] = open("/dev/zero", O_RDONLY)) < 0) {
103 ast_test_status_update(test, "Unable to open a readable handle to /dev/zero: %s\n", strerror(errno));
104 close(fd[0]);
105 return AST_TEST_FAIL;
106 }
107
108 ast_test_status_update(test, "Creating handle that should block on read\n");
109 if (pipe(rdblocker) < 0) {
110 ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
111 close(fd[0]);
112 close(fd[1]);
113 return AST_TEST_FAIL;
114 }
115
116#if FDNO > 3
117 ast_test_status_update(test, "Creating handle that should block on write\n");
118 if (pipe(wrblocker) < 0) {
119 ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
120 close(fd[0]);
121 close(fd[1]);
122 close(rdblocker[0]);
123 close(rdblocker[1]);
124 return AST_TEST_FAIL;
125 }
126
127 ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
128 if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
129 ast_test_status_update(test, "Unable to start failsafe thread\n");
130 close(fd[0]);
131 close(fd[1]);
132 close(fd[2]);
133 close(rdblocker[0]);
134 close(rdblocker[1]);
135 close(wrblocker[0]);
136 close(wrblocker[1]);
137 return AST_TEST_FAIL;
138 }
139
140 /* Fill the pipe full of data */
141 ast_test_status_update(test, "Making pipe block on write\n");
142 for (i = 0; i < 4096; i++) { /* 1MB of data should be more than enough for any pipe */
143 errno = 0;
144 if (write(wrblocker[1], garbage, sizeof(garbage)) < sizeof(garbage)) {
145 ast_test_status_update(test, "Got %d\n", errno);
146 if (errno == EINTR && ++consec_interrupt > 1) {
147 break;
148 }
149 } else {
150 consec_interrupt = 0;
151 }
152 }
153
154 ast_test_status_update(test, "Cancelling failsafe thread.\n");
155 pthread_cancel(failsafe_tid);
156 pthread_kill(failsafe_tid, SIGURG);
157 pthread_join(failsafe_tid, NULL);
158#endif
159
160 pfd[0].fd = fd[0];
161 pfd[1].fd = fd[1];
162 pfd[2].fd = rdblocker[0];
163#if FDNO > 3
164 pfd[3].fd = wrblocker[1];
165#endif
166
167 /* Need to ensure the infinite timeout doesn't stall the process */
168 ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
169 if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
170 ast_test_status_update(test, "Unable to start failsafe thread\n");
171 close(fd[0]);
172 close(fd[1]);
173 close(rdblocker[0]);
174 close(rdblocker[1]);
175#if FDNO > 3
176 close(wrblocker[0]);
177 close(wrblocker[1]);
178#endif
179 return AST_TEST_FAIL;
180 }
181
182 RESET;
183 if ((res2 = ast_poll(pfd, FDNO, -1)) != 2) {
184 ast_test_status_update(test, "ast_poll does not return that only two handles are available (inf timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
185 res = AST_TEST_FAIL;
186 }
187
188 RESET;
189 if ((res2 = ast_poll2(pfd, FDNO, NULL)) != 2) {
190 ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (inf timeout): %d %s\n", res2, res2 == -1 ? strerror(errno) : "");
191 res = AST_TEST_FAIL;
192 }
193
194 ast_test_status_update(test, "Cancelling failsafe thread.\n");
195 pthread_cancel(failsafe_tid);
196 pthread_kill(failsafe_tid, SIGURG);
197 pthread_join(failsafe_tid, NULL);
198
199 RESET;
200 if (ast_poll(pfd, FDNO, 0) != 2) {
201 ast_test_status_update(test, "ast_poll does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
202 res = AST_TEST_FAIL;
203 }
204
205 RESET;
206 if (ast_poll2(pfd, FDNO, &tv) != 2) {
207 ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
208 res = AST_TEST_FAIL;
209 }
210
211 RESET;
212 if (ast_poll(pfd, FDNO, 1) != 2) {
213 ast_test_status_update(test, "ast_poll does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
214 res = AST_TEST_FAIL;
215 }
216
217 tv.tv_usec = 1000;
218 if (ast_poll2(pfd, FDNO, &tv) != 2) {
219 ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
220 res = AST_TEST_FAIL;
221 }
222
223 close(fd[0]);
224 close(fd[1]);
225 close(rdblocker[0]);
226 close(rdblocker[1]);
227#if FDNO > 3
228 close(wrblocker[0]);
229 close(wrblocker[1]);
230#endif
231 return res;
232}
233
234static int unload_module(void)
235{
236 AST_TEST_UNREGISTER(poll_test);
237 return 0;
238}
239
240static int load_module(void)
241{
242 AST_TEST_REGISTER(poll_test);
244}
245
Asterisk main include file. File version handling, generic pbx functions.
int errno
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
def info(msg)
int ast_poll2(struct pollfd *pArray, unsigned long n_fds, struct timeval *tv)
Same as poll(2), except the time is specified in microseconds and the tv argument is modified to indi...
Definition: poll.c:268
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
#define NULL
Definition: resample.c:96
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
static void * failsafe_cancel(void *vparent)
Definition: test_poll.c:47
AST_TEST_DEFINE(poll_test)
Definition: test_poll.c:64
#define FDNO
#define RESET
Definition: test_poll.c:63
static int load_module(void)
Definition: test_poll.c:240
static int unload_module(void)
Definition: test_poll.c:234
Utility functions.
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592