36 #include <sys/types.h>
45 #include "asterisk/poll-compat.h"
47 static void *failsafe_cancel(
void *vparent)
49 pthread_t parent = (pthread_t) (
long) vparent;
53 pthread_kill(parent, SIGURG);
56 pthread_kill(parent, SIGURG);
59 pthread_kill(parent, SIGURG);
63 #define RESET for (i = 0; i < 4; i++) { pfd[i].revents = 0; }
67 int fd[2], res = AST_TEST_PASS, i, res2;
70 int wrblocker[2], consec_interrupt = 0;
72 struct pollfd pfd[4] = { { .events = POLLOUT, }, { .events = POLLIN, }, { .events = POLLIN }, { .events = POLLOUT } };
73 pthread_t failsafe_tid;
74 struct timeval tv = { 0, 0 };
77 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
78 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
79 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
80 "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/";
85 info->name =
"poll_test";
86 info->category =
"/main/poll/";
87 info->summary =
"unit test for the ast_poll() API";
89 "Verifies behavior for the ast_poll() API call";
90 return AST_TEST_NOT_RUN;
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));
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));
105 return AST_TEST_FAIL;
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));
113 return AST_TEST_FAIL;
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));
124 return AST_TEST_FAIL;
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");
137 return AST_TEST_FAIL;
141 ast_test_status_update(
test,
"Making pipe block on write\n");
142 for (i = 0; i < 4096; i++) {
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) {
150 consec_interrupt = 0;
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);
162 pfd[2].fd = rdblocker[0];
164 pfd[3].fd = wrblocker[1];
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");
179 return AST_TEST_FAIL;
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) :
"");
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) :
"");
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);
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) :
"");
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) :
"");
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) :
"");
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) :
"");
234 static int unload_module(
void)
236 AST_TEST_UNREGISTER(poll_test);
240 static int load_module(
void)
242 AST_TEST_REGISTER(poll_test);
Asterisk main include file. File version handling, generic pbx functions.
#define AST_TEST_DEFINE(hdr)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.