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 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. if (read_count == 3)
  35. uv_close((uv_handle_t*) stream, NULL);
  36. }
  37. TEST_IMPL(osx_select) {
  38. int r;
  39. int fd;
  40. size_t i;
  41. size_t len;
  42. const char* str;
  43. uv_tty_t tty;
  44. fd = open("/dev/tty", O_RDONLY);
  45. ASSERT(fd >= 0);
  46. r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
  47. ASSERT(r == 0);
  48. uv_read_start((uv_stream_t*) &tty, alloc_cb, read_cb);
  49. /* Emulate user-input */
  50. str = "got some input\n"
  51. "with a couple of lines\n"
  52. "feel pretty happy\n";
  53. for (i = 0, len = strlen(str); i < len; i++) {
  54. r = ioctl(fd, TIOCSTI, str + i);
  55. ASSERT(r == 0);
  56. }
  57. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  58. ASSERT(read_count == 3);
  59. MAKE_VALGRIND_HAPPY();
  60. return 0;
  61. }
  62. #endif /* __APPLE__ */