1
0

test-shutdown-close.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. int err = uv_last_error(uv_default_loop()).code;
  34. ASSERT(req == &shutdown_req);
  35. ASSERT(status == 0 || (status == -1 && err == UV_ECANCELED));
  36. shutdown_cb_called++;
  37. }
  38. static void close_cb(uv_handle_t* handle) {
  39. close_cb_called++;
  40. }
  41. static void connect_cb(uv_connect_t* req, int status) {
  42. int r;
  43. ASSERT(req == &connect_req);
  44. ASSERT(status == 0);
  45. r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
  46. ASSERT(r == 0);
  47. ASSERT(!uv_is_closing((uv_handle_t*) req->handle));
  48. uv_close((uv_handle_t*) req->handle, close_cb);
  49. ASSERT(uv_is_closing((uv_handle_t*) req->handle));
  50. connect_cb_called++;
  51. }
  52. TEST_IMPL(shutdown_close_tcp) {
  53. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  54. uv_tcp_t h;
  55. int r;
  56. r = uv_tcp_init(uv_default_loop(), &h);
  57. ASSERT(r == 0);
  58. r = uv_tcp_connect(&connect_req, &h, addr, connect_cb);
  59. ASSERT(r == 0);
  60. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  61. ASSERT(r == 0);
  62. ASSERT(connect_cb_called == 1);
  63. ASSERT(shutdown_cb_called == 1);
  64. ASSERT(close_cb_called == 1);
  65. MAKE_VALGRIND_HAPPY();
  66. return 0;
  67. }
  68. TEST_IMPL(shutdown_close_pipe) {
  69. uv_pipe_t h;
  70. int r;
  71. r = uv_pipe_init(uv_default_loop(), &h, 0);
  72. ASSERT(r == 0);
  73. uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_cb);
  74. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  75. ASSERT(r == 0);
  76. ASSERT(connect_cb_called == 1);
  77. ASSERT(shutdown_cb_called == 1);
  78. ASSERT(close_cb_called == 1);
  79. MAKE_VALGRIND_HAPPY();
  80. return 0;
  81. }