test-tcp-shutdown-after-write.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. static void write_cb(uv_write_t* req, int status);
  24. static void shutdown_cb(uv_shutdown_t* req, int status);
  25. static uv_tcp_t conn;
  26. static uv_timer_t timer;
  27. static uv_connect_t connect_req;
  28. static uv_write_t write_req;
  29. static uv_shutdown_t shutdown_req;
  30. static int connect_cb_called;
  31. static int write_cb_called;
  32. static int shutdown_cb_called;
  33. static int conn_close_cb_called;
  34. static int timer_close_cb_called;
  35. static void close_cb(uv_handle_t* handle) {
  36. if (handle == (uv_handle_t*)&conn)
  37. conn_close_cb_called++;
  38. else if (handle == (uv_handle_t*)&timer)
  39. timer_close_cb_called++;
  40. else
  41. ASSERT(0 && "bad handle in close_cb");
  42. }
  43. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
  44. static char slab[64];
  45. return uv_buf_init(slab, sizeof(slab));
  46. }
  47. static void timer_cb(uv_timer_t* handle, int status) {
  48. uv_buf_t buf;
  49. int r;
  50. uv_close((uv_handle_t*)handle, close_cb);
  51. buf = uv_buf_init("TEST", 4);
  52. r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
  53. ASSERT(r == 0);
  54. r = uv_shutdown(&shutdown_req, (uv_stream_t*)&conn, shutdown_cb);
  55. ASSERT(r == 0);
  56. }
  57. static void read_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
  58. }
  59. static void connect_cb(uv_connect_t* req, int status) {
  60. int r;
  61. ASSERT(status == 0);
  62. connect_cb_called++;
  63. r = uv_read_start((uv_stream_t*)&conn, alloc_cb, read_cb);
  64. ASSERT(r == 0);
  65. }
  66. static void write_cb(uv_write_t* req, int status) {
  67. ASSERT(status == 0);
  68. write_cb_called++;
  69. }
  70. static void shutdown_cb(uv_shutdown_t* req, int status) {
  71. ASSERT(status == 0);
  72. shutdown_cb_called++;
  73. uv_close((uv_handle_t*)&conn, close_cb);
  74. }
  75. TEST_IMPL(tcp_shutdown_after_write) {
  76. struct sockaddr_in addr;
  77. uv_loop_t* loop;
  78. int r;
  79. addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  80. loop = uv_default_loop();
  81. r = uv_timer_init(loop, &timer);
  82. ASSERT(r == 0);
  83. r = uv_timer_start(&timer, timer_cb, 125, 0);
  84. ASSERT(r == 0);
  85. r = uv_tcp_init(loop, &conn);
  86. ASSERT(r == 0);
  87. r = uv_tcp_connect(&connect_req, &conn, addr, connect_cb);
  88. ASSERT(r == 0);
  89. r = uv_run(loop, UV_RUN_DEFAULT);
  90. ASSERT(r == 0);
  91. ASSERT(connect_cb_called == 1);
  92. ASSERT(write_cb_called == 1);
  93. ASSERT(shutdown_cb_called == 1);
  94. ASSERT(conn_close_cb_called == 1);
  95. ASSERT(timer_close_cb_called == 1);
  96. MAKE_VALGRIND_HAPPY();
  97. return 0;
  98. }