test_poll.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * \file
  20. * \brief Poll Tests
  21. *
  22. * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
  23. *
  24. * Verify that the various poll implementations work as desired (ast_poll, ast_poll2)
  25. * \ingroup tests
  26. */
  27. /*** MODULEINFO
  28. <depend>TEST_FRAMEWORK</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. #include <signal.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/utils.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/test.h"
  42. #include "asterisk/poll-compat.h"
  43. static void *failsafe_cancel(void *vparent)
  44. {
  45. pthread_t parent = (pthread_t) (long) vparent;
  46. sleep(1);
  47. pthread_testcancel();
  48. pthread_kill(parent, SIGURG);
  49. sleep(1);
  50. pthread_testcancel();
  51. pthread_kill(parent, SIGURG);
  52. sleep(1);
  53. pthread_testcancel();
  54. pthread_kill(parent, SIGURG);
  55. pthread_exit(NULL);
  56. }
  57. #define RESET for (i = 0; i < 4; i++) { pfd[i].revents = 0; }
  58. AST_TEST_DEFINE(poll_test)
  59. {
  60. #define FDNO 3
  61. int fd[2], res = AST_TEST_PASS, i, res2;
  62. int rdblocker[2];
  63. #if FDNO > 3
  64. int wrblocker[2], consec_interrupt = 0;
  65. #endif
  66. struct pollfd pfd[4] = { { .events = POLLOUT, }, { .events = POLLIN, }, { .events = POLLIN }, { .events = POLLOUT } };
  67. pthread_t failsafe_tid;
  68. struct timeval tv = { 0, 0 };
  69. #if FDNO > 3
  70. char garbage[256] =
  71. "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
  72. "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
  73. "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
  74. "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/";
  75. #endif
  76. switch (cmd) {
  77. case TEST_INIT:
  78. info->name = "poll_test";
  79. info->category = "/main/poll/";
  80. info->summary = "unit test for the ast_poll() API";
  81. info->description =
  82. "Verifies behavior for the ast_poll() API call";
  83. return AST_TEST_NOT_RUN;
  84. case TEST_EXECUTE:
  85. break;
  86. }
  87. ast_test_status_update(test, "Creating handle that should NEVER block on write\n");
  88. if ((fd[0] = open("/dev/null", O_WRONLY)) < 0) {
  89. ast_test_status_update(test, "Unable to open a writable handle to /dev/null: %s\n", strerror(errno));
  90. return AST_TEST_FAIL;
  91. }
  92. ast_test_status_update(test, "Creating handle that should NEVER block on read\n");
  93. if ((fd[1] = open("/dev/zero", O_RDONLY)) < 0) {
  94. ast_test_status_update(test, "Unable to open a readable handle to /dev/zero: %s\n", strerror(errno));
  95. close(fd[0]);
  96. return AST_TEST_FAIL;
  97. }
  98. ast_test_status_update(test, "Creating handle that should block on read\n");
  99. if (pipe(rdblocker) < 0) {
  100. ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
  101. close(fd[0]);
  102. close(fd[1]);
  103. return AST_TEST_FAIL;
  104. }
  105. #if FDNO > 3
  106. ast_test_status_update(test, "Creating handle that should block on write\n");
  107. if (pipe(wrblocker) < 0) {
  108. ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
  109. close(fd[0]);
  110. close(fd[1]);
  111. close(rdblocker[0]);
  112. close(rdblocker[1]);
  113. return AST_TEST_FAIL;
  114. }
  115. ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
  116. if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
  117. ast_test_status_update(test, "Unable to start failsafe thread\n");
  118. close(fd[0]);
  119. close(fd[1]);
  120. close(fd[2]);
  121. close(rdblocker[0]);
  122. close(rdblocker[1]);
  123. close(wrblocker[0]);
  124. close(wrblocker[1]);
  125. return AST_TEST_FAIL;
  126. }
  127. /* Fill the pipe full of data */
  128. ast_test_status_update(test, "Making pipe block on write\n");
  129. for (i = 0; i < 4096; i++) { /* 1MB of data should be more than enough for any pipe */
  130. errno = 0;
  131. if (write(wrblocker[1], garbage, sizeof(garbage)) < sizeof(garbage)) {
  132. ast_test_status_update(test, "Got %d\n", errno);
  133. if (errno == EINTR && ++consec_interrupt > 1) {
  134. break;
  135. }
  136. } else {
  137. consec_interrupt = 0;
  138. }
  139. }
  140. ast_test_status_update(test, "Cancelling failsafe thread.\n");
  141. pthread_cancel(failsafe_tid);
  142. pthread_kill(failsafe_tid, SIGURG);
  143. pthread_join(failsafe_tid, NULL);
  144. #endif
  145. pfd[0].fd = fd[0];
  146. pfd[1].fd = fd[1];
  147. pfd[2].fd = rdblocker[0];
  148. #if FDNO > 3
  149. pfd[3].fd = wrblocker[1];
  150. #endif
  151. /* Need to ensure the infinite timeout doesn't stall the process */
  152. ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
  153. if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
  154. ast_test_status_update(test, "Unable to start failsafe thread\n");
  155. close(fd[0]);
  156. close(fd[1]);
  157. close(rdblocker[0]);
  158. close(rdblocker[1]);
  159. #if FDNO > 3
  160. close(wrblocker[0]);
  161. close(wrblocker[1]);
  162. #endif
  163. return AST_TEST_FAIL;
  164. }
  165. RESET;
  166. if ((res2 = ast_poll(pfd, FDNO, -1)) != 2) {
  167. 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) : "");
  168. res = AST_TEST_FAIL;
  169. }
  170. RESET;
  171. if ((res2 = ast_poll2(pfd, FDNO, NULL)) != 2) {
  172. 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) : "");
  173. res = AST_TEST_FAIL;
  174. }
  175. ast_test_status_update(test, "Cancelling failsafe thread.\n");
  176. pthread_cancel(failsafe_tid);
  177. pthread_kill(failsafe_tid, SIGURG);
  178. pthread_join(failsafe_tid, NULL);
  179. RESET;
  180. if (ast_poll(pfd, FDNO, 0) != 2) {
  181. 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) : "");
  182. res = AST_TEST_FAIL;
  183. }
  184. RESET;
  185. if (ast_poll2(pfd, FDNO, &tv) != 2) {
  186. 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) : "");
  187. res = AST_TEST_FAIL;
  188. }
  189. RESET;
  190. if (ast_poll(pfd, FDNO, 1) != 2) {
  191. 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) : "");
  192. res = AST_TEST_FAIL;
  193. }
  194. tv.tv_usec = 1000;
  195. if (ast_poll2(pfd, FDNO, &tv) != 2) {
  196. 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) : "");
  197. res = AST_TEST_FAIL;
  198. }
  199. close(fd[0]);
  200. close(fd[1]);
  201. close(rdblocker[0]);
  202. close(rdblocker[1]);
  203. #if FDNO > 3
  204. close(wrblocker[0]);
  205. close(wrblocker[1]);
  206. #endif
  207. return res;
  208. }
  209. static int unload_module(void)
  210. {
  211. AST_TEST_UNREGISTER(poll_test);
  212. return 0;
  213. }
  214. static int load_module(void)
  215. {
  216. AST_TEST_REGISTER(poll_test);
  217. return AST_MODULE_LOAD_SUCCESS;
  218. }
  219. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Poll test");