test-signal-pending-on-close.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright libuv project 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. #ifndef _WIN32
  22. #include "uv.h"
  23. #include "task.h"
  24. #include <string.h>
  25. #include <unistd.h>
  26. static uv_loop_t loop;
  27. static uv_signal_t signal_hdl;
  28. static uv_pipe_t pipe_hdl;
  29. static uv_write_t write_req;
  30. static char* buf;
  31. static int close_cb_called;
  32. static void stop_loop_cb(uv_signal_t* signal, int signum) {
  33. ASSERT(signum == SIGPIPE);
  34. uv_stop(signal->loop);
  35. }
  36. static void signal_cb(uv_signal_t* signal, int signum) {
  37. ASSERT(0);
  38. }
  39. static void close_cb(uv_handle_t *handle) {
  40. close_cb_called++;
  41. }
  42. static void write_cb(uv_write_t* req, int status) {
  43. ASSERT(req != NULL);
  44. ASSERT(status == UV_EPIPE);
  45. free(buf);
  46. uv_close((uv_handle_t *) &pipe_hdl, close_cb);
  47. uv_close((uv_handle_t *) &signal_hdl, close_cb);
  48. }
  49. TEST_IMPL(signal_pending_on_close) {
  50. int pipefds[2];
  51. uv_buf_t buffer;
  52. int r;
  53. ASSERT(0 == uv_loop_init(&loop));
  54. ASSERT(0 == uv_signal_init(&loop, &signal_hdl));
  55. ASSERT(0 == uv_signal_start(&signal_hdl, signal_cb, SIGPIPE));
  56. ASSERT(0 == pipe(pipefds));
  57. ASSERT(0 == uv_pipe_init(&loop, &pipe_hdl, 0));
  58. ASSERT(0 == uv_pipe_open(&pipe_hdl, pipefds[1]));
  59. /* Write data large enough so it needs loop iteration */
  60. buf = malloc(1<<24);
  61. ASSERT(buf != NULL);
  62. memset(buf, '.', 1<<24);
  63. buffer = uv_buf_init(buf, 1<<24);
  64. r = uv_write(&write_req, (uv_stream_t *) &pipe_hdl, &buffer, 1, write_cb);
  65. ASSERT(0 == r);
  66. /* cause a SIGPIPE on write in next iteration */
  67. close(pipefds[0]);
  68. ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
  69. ASSERT(0 == uv_loop_close(&loop));
  70. ASSERT(2 == close_cb_called);
  71. MAKE_VALGRIND_HAPPY();
  72. return 0;
  73. }
  74. TEST_IMPL(signal_close_loop_alive) {
  75. ASSERT(0 == uv_loop_init(&loop));
  76. ASSERT(0 == uv_signal_init(&loop, &signal_hdl));
  77. ASSERT(0 == uv_signal_start(&signal_hdl, stop_loop_cb, SIGPIPE));
  78. uv_unref((uv_handle_t*) &signal_hdl);
  79. ASSERT(0 == uv_kill(uv_os_getpid(), SIGPIPE));
  80. ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
  81. uv_close((uv_handle_t*) &signal_hdl, close_cb);
  82. ASSERT(1 == uv_loop_alive(&loop));
  83. ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
  84. ASSERT(0 == uv_loop_close(&loop));
  85. ASSERT(1 == close_cb_called);
  86. MAKE_VALGRIND_HAPPY();
  87. return 0;
  88. }
  89. #endif