test-osx-select.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #ifdef __APPLE__
  24. #include <sys/ioctl.h>
  25. #include <string.h>
  26. static int read_count;
  27. static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  28. static char slab[1024];
  29. buf->base = slab;
  30. buf->len = sizeof(slab);
  31. }
  32. static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
  33. fprintf(stdout, "got data %d\n", ++read_count);
  34. fflush(stdout);
  35. if (read_count == 3)
  36. uv_close((uv_handle_t*) stream, NULL);
  37. }
  38. TEST_IMPL(osx_select) {
  39. int r;
  40. int fd;
  41. size_t i;
  42. size_t len;
  43. const char* str;
  44. uv_tty_t tty;
  45. fd = open("/dev/tty", O_RDONLY);
  46. if (fd < 0) {
  47. fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));
  48. fflush(stderr);
  49. return TEST_SKIP;
  50. }
  51. r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
  52. ASSERT(r == 0);
  53. uv_read_start((uv_stream_t*) &tty, alloc_cb, read_cb);
  54. /* Emulate user-input */
  55. str = "got some input\n"
  56. "with a couple of lines\n"
  57. "feel pretty happy\n";
  58. for (i = 0, len = strlen(str); i < len; i++) {
  59. r = ioctl(fd, TIOCSTI, str + i);
  60. ASSERT(r == 0);
  61. }
  62. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  63. ASSERT(read_count == 3);
  64. MAKE_VALGRIND_HAPPY();
  65. return 0;
  66. }
  67. TEST_IMPL(osx_select_many_fds) {
  68. int r;
  69. int fd;
  70. size_t i;
  71. size_t len;
  72. const char* str;
  73. struct sockaddr_in addr;
  74. uv_tty_t tty;
  75. uv_tcp_t tcps[1500];
  76. TEST_FILE_LIMIT(ARRAY_SIZE(tcps) + 100);
  77. r = uv_ip4_addr("127.0.0.1", 0, &addr);
  78. ASSERT(r == 0);
  79. for (i = 0; i < ARRAY_SIZE(tcps); i++) {
  80. r = uv_tcp_init(uv_default_loop(), &tcps[i]);
  81. ASSERT(r == 0);
  82. r = uv_tcp_bind(&tcps[i], (const struct sockaddr *) &addr, 0);
  83. ASSERT(r == 0);
  84. uv_unref((uv_handle_t*) &tcps[i]);
  85. }
  86. fd = open("/dev/tty", O_RDONLY);
  87. if (fd < 0) {
  88. fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));
  89. fflush(stderr);
  90. return TEST_SKIP;
  91. }
  92. r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
  93. ASSERT(r == 0);
  94. r = uv_read_start((uv_stream_t*) &tty, alloc_cb, read_cb);
  95. ASSERT(r == 0);
  96. /* Emulate user-input */
  97. str = "got some input\n"
  98. "with a couple of lines\n"
  99. "feel pretty happy\n";
  100. for (i = 0, len = strlen(str); i < len; i++) {
  101. r = ioctl(fd, TIOCSTI, str + i);
  102. ASSERT(r == 0);
  103. }
  104. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  105. ASSERT(read_count == 3);
  106. MAKE_VALGRIND_HAPPY();
  107. return 0;
  108. }
  109. #endif /* __APPLE__ */