test-tcp-close.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. int r;
  62. r = uv_tcp_init(loop, handle);
  63. ASSERT(r == 0);
  64. r = uv_tcp_bind(handle, uv_ip4_addr("127.0.0.1", TEST_PORT));
  65. ASSERT(r == 0);
  66. r = uv_listen((uv_stream_t*)handle, 128, connection_cb);
  67. ASSERT(r == 0);
  68. uv_unref((uv_handle_t*)handle);
  69. }
  70. /* Check that pending write requests have their callbacks
  71. * invoked when the handle is closed.
  72. */
  73. TEST_IMPL(tcp_close) {
  74. uv_loop_t* loop;
  75. uv_tcp_t tcp_server;
  76. int r;
  77. loop = uv_default_loop();
  78. /* We can't use the echo server, it doesn't handle ECONNRESET. */
  79. start_server(loop, &tcp_server);
  80. r = uv_tcp_init(loop, &tcp_handle);
  81. ASSERT(r == 0);
  82. r = uv_tcp_connect(&connect_req,
  83. &tcp_handle,
  84. uv_ip4_addr("127.0.0.1", TEST_PORT),
  85. connect_cb);
  86. ASSERT(r == 0);
  87. ASSERT(write_cb_called == 0);
  88. ASSERT(close_cb_called == 0);
  89. r = uv_run(loop, UV_RUN_DEFAULT);
  90. ASSERT(r == 0);
  91. printf("%d of %d write reqs seen\n", write_cb_called, NUM_WRITE_REQS);
  92. ASSERT(write_cb_called == NUM_WRITE_REQS);
  93. ASSERT(close_cb_called == 1);
  94. MAKE_VALGRIND_HAPPY();
  95. return 0;
  96. }