test-osx-select.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
  28. static char buf[1024];
  29. return uv_buf_init(buf, ARRAY_SIZE(buf));
  30. }
  31. static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
  32. fprintf(stdout, "got data %d\n", ++read_count);
  33. if (read_count == 3)
  34. uv_close((uv_handle_t*) stream, NULL);
  35. }
  36. TEST_IMPL(osx_select) {
  37. int r;
  38. int fd;
  39. size_t i;
  40. size_t len;
  41. const char* str;
  42. uv_tty_t tty;
  43. fd = open("/dev/tty", O_RDONLY);
  44. ASSERT(fd >= 0);
  45. r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
  46. ASSERT(r == 0);
  47. uv_read_start((uv_stream_t*) &tty, alloc_cb, read_cb);
  48. // Emulate user-input
  49. str = "got some input\n"
  50. "with a couple of lines\n"
  51. "feel pretty happy\n";
  52. for (i = 0, len = strlen(str); i < len; i++) {
  53. r = ioctl(fd, TIOCSTI, str + i);
  54. ASSERT(r == 0);
  55. }
  56. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  57. ASSERT(read_count == 3);
  58. MAKE_VALGRIND_HAPPY();
  59. return 0;
  60. }
  61. #endif /* __APPLE__ */