1
0

test-shutdown-close.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /*
  22. * These tests verify that the uv_shutdown callback is always made, even when
  23. * it is immediately followed by an uv_close call.
  24. */
  25. #include "uv.h"
  26. #include "task.h"
  27. static uv_shutdown_t shutdown_req;
  28. static uv_connect_t connect_req;
  29. static int connect_cb_called = 0;
  30. static int shutdown_cb_called = 0;
  31. static int close_cb_called = 0;
  32. static void shutdown_cb(uv_shutdown_t* req, int status) {
  33. ASSERT(req == &shutdown_req);
  34. ASSERT(status == 0 || status == UV_ECANCELED);
  35. shutdown_cb_called++;
  36. }
  37. static void close_cb(uv_handle_t* handle) {
  38. close_cb_called++;
  39. }
  40. static void connect_cb(uv_connect_t* req, int status) {
  41. int r;
  42. ASSERT(req == &connect_req);
  43. ASSERT(status == 0);
  44. r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
  45. ASSERT(r == 0);
  46. ASSERT(0 == uv_is_closing((uv_handle_t*) req->handle));
  47. uv_close((uv_handle_t*) req->handle, close_cb);
  48. ASSERT(1 == uv_is_closing((uv_handle_t*) req->handle));
  49. connect_cb_called++;
  50. }
  51. TEST_IMPL(shutdown_close_tcp) {
  52. struct sockaddr_in addr;
  53. uv_tcp_t h;
  54. int r;
  55. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  56. r = uv_tcp_init(uv_default_loop(), &h);
  57. ASSERT(r == 0);
  58. r = uv_tcp_connect(&connect_req,
  59. &h,
  60. (const struct sockaddr*) &addr,
  61. connect_cb);
  62. ASSERT(r == 0);
  63. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  64. ASSERT(r == 0);
  65. ASSERT(connect_cb_called == 1);
  66. ASSERT(shutdown_cb_called == 1);
  67. ASSERT(close_cb_called == 1);
  68. MAKE_VALGRIND_HAPPY();
  69. return 0;
  70. }
  71. TEST_IMPL(shutdown_close_pipe) {
  72. uv_pipe_t h;
  73. int r;
  74. r = uv_pipe_init(uv_default_loop(), &h, 0);
  75. ASSERT(r == 0);
  76. uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_cb);
  77. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  78. ASSERT(r == 0);
  79. ASSERT(connect_cb_called == 1);
  80. ASSERT(shutdown_cb_called == 1);
  81. ASSERT(close_cb_called == 1);
  82. MAKE_VALGRIND_HAPPY();
  83. return 0;
  84. }