1
0

test-tcp-close.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <errno.h>
  24. #include <string.h> /* memset */
  25. #define NUM_WRITE_REQS 32
  26. static uv_tcp_t tcp_handle;
  27. static uv_connect_t connect_req;
  28. static int write_cb_called;
  29. static int close_cb_called;
  30. static void connect_cb(uv_connect_t* req, int status);
  31. static void write_cb(uv_write_t* req, int status);
  32. static void close_cb(uv_handle_t* handle);
  33. static void connect_cb(uv_connect_t* conn_req, int status) {
  34. uv_write_t* req;
  35. uv_buf_t buf;
  36. int i, r;
  37. buf = uv_buf_init("PING", 4);
  38. for (i = 0; i < NUM_WRITE_REQS; i++) {
  39. req = malloc(sizeof *req);
  40. ASSERT(req != NULL);
  41. r = uv_write(req, (uv_stream_t*)&tcp_handle, &buf, 1, write_cb);
  42. ASSERT(r == 0);
  43. }
  44. uv_close((uv_handle_t*)&tcp_handle, close_cb);
  45. }
  46. static void write_cb(uv_write_t* req, int status) {
  47. /* write callbacks should run before the close callback */
  48. ASSERT(close_cb_called == 0);
  49. ASSERT(req->handle == (uv_stream_t*)&tcp_handle);
  50. write_cb_called++;
  51. free(req);
  52. }
  53. static void close_cb(uv_handle_t* handle) {
  54. ASSERT(handle == (uv_handle_t*)&tcp_handle);
  55. close_cb_called++;
  56. }
  57. static void connection_cb(uv_stream_t* server, int status) {
  58. ASSERT(status == 0);
  59. }
  60. static void start_server(uv_loop_t* loop, uv_tcp_t* handle) {
  61. struct sockaddr_in addr;
  62. int r;
  63. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  64. r = uv_tcp_init(loop, handle);
  65. ASSERT(r == 0);
  66. r = uv_tcp_bind(handle, (const struct sockaddr*) &addr, 0);
  67. ASSERT(r == 0);
  68. r = uv_listen((uv_stream_t*)handle, 128, connection_cb);
  69. ASSERT(r == 0);
  70. uv_unref((uv_handle_t*)handle);
  71. }
  72. /* Check that pending write requests have their callbacks
  73. * invoked when the handle is closed.
  74. */
  75. TEST_IMPL(tcp_close) {
  76. struct sockaddr_in addr;
  77. uv_tcp_t tcp_server;
  78. uv_loop_t* loop;
  79. int r;
  80. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  81. loop = uv_default_loop();
  82. /* We can't use the echo server, it doesn't handle ECONNRESET. */
  83. start_server(loop, &tcp_server);
  84. r = uv_tcp_init(loop, &tcp_handle);
  85. ASSERT(r == 0);
  86. r = uv_tcp_connect(&connect_req,
  87. &tcp_handle,
  88. (const struct sockaddr*) &addr,
  89. connect_cb);
  90. ASSERT(r == 0);
  91. ASSERT(write_cb_called == 0);
  92. ASSERT(close_cb_called == 0);
  93. r = uv_run(loop, UV_RUN_DEFAULT);
  94. ASSERT(r == 0);
  95. printf("%d of %d write reqs seen\n", write_cb_called, NUM_WRITE_REQS);
  96. ASSERT(write_cb_called == NUM_WRITE_REQS);
  97. ASSERT(close_cb_called == 1);
  98. MAKE_VALGRIND_HAPPY();
  99. return 0;
  100. }