test-close-fd.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #if !defined(_WIN32)
  22. #include "uv.h"
  23. #include "task.h"
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. static unsigned int read_cb_called;
  27. static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf) {
  28. static char slab[1];
  29. buf->base = slab;
  30. buf->len = sizeof(slab);
  31. }
  32. static void read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
  33. switch (++read_cb_called) {
  34. case 1:
  35. ASSERT(nread == 1);
  36. uv_read_stop(handle);
  37. break;
  38. case 2:
  39. ASSERT(nread == UV_EOF);
  40. uv_close((uv_handle_t *) handle, NULL);
  41. break;
  42. default:
  43. ASSERT(!"read_cb_called > 2");
  44. }
  45. }
  46. TEST_IMPL(close_fd) {
  47. uv_pipe_t pipe_handle;
  48. int fd[2];
  49. ASSERT(0 == pipe(fd));
  50. ASSERT(0 == fcntl(fd[0], F_SETFL, O_NONBLOCK));
  51. ASSERT(0 == uv_pipe_init(uv_default_loop(), &pipe_handle, 0));
  52. ASSERT(0 == uv_pipe_open(&pipe_handle, fd[0]));
  53. fd[0] = -1; /* uv_pipe_open() takes ownership of the file descriptor. */
  54. ASSERT(1 == write(fd[1], "", 1));
  55. ASSERT(0 == close(fd[1]));
  56. fd[1] = -1;
  57. ASSERT(0 == uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb));
  58. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  59. ASSERT(1 == read_cb_called);
  60. ASSERT(0 == uv_is_active((const uv_handle_t *) &pipe_handle));
  61. ASSERT(0 == uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb));
  62. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  63. ASSERT(2 == read_cb_called);
  64. ASSERT(0 != uv_is_closing((const uv_handle_t *) &pipe_handle));
  65. MAKE_VALGRIND_HAPPY();
  66. return 0;
  67. }
  68. #endif /* !defined(_WIN32) */