1
0

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 void alloc_cb(uv_handle_t* handle,
  44. size_t suggested_size,
  45. uv_buf_t* buf) {
  46. static char slab[64];
  47. buf->base = slab;
  48. buf->len = sizeof(slab);
  49. }
  50. static void timer_cb(uv_timer_t* handle, int status) {
  51. uv_buf_t buf;
  52. int r;
  53. uv_close((uv_handle_t*)handle, close_cb);
  54. buf = uv_buf_init("TEST", 4);
  55. r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
  56. ASSERT(r == 0);
  57. r = uv_shutdown(&shutdown_req, (uv_stream_t*)&conn, shutdown_cb);
  58. ASSERT(r == 0);
  59. }
  60. static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
  61. }
  62. static void connect_cb(uv_connect_t* req, int status) {
  63. int r;
  64. ASSERT(status == 0);
  65. connect_cb_called++;
  66. r = uv_read_start((uv_stream_t*)&conn, alloc_cb, read_cb);
  67. ASSERT(r == 0);
  68. }
  69. static void write_cb(uv_write_t* req, int status) {
  70. ASSERT(status == 0);
  71. write_cb_called++;
  72. }
  73. static void shutdown_cb(uv_shutdown_t* req, int status) {
  74. ASSERT(status == 0);
  75. shutdown_cb_called++;
  76. uv_close((uv_handle_t*)&conn, close_cb);
  77. }
  78. TEST_IMPL(tcp_shutdown_after_write) {
  79. struct sockaddr_in addr;
  80. uv_loop_t* loop;
  81. int r;
  82. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  83. loop = uv_default_loop();
  84. r = uv_timer_init(loop, &timer);
  85. ASSERT(r == 0);
  86. r = uv_timer_start(&timer, timer_cb, 125, 0);
  87. ASSERT(r == 0);
  88. r = uv_tcp_init(loop, &conn);
  89. ASSERT(r == 0);
  90. r = uv_tcp_connect(&connect_req,
  91. &conn,
  92. (const struct sockaddr*) &addr,
  93. connect_cb);
  94. ASSERT(r == 0);
  95. r = uv_run(loop, UV_RUN_DEFAULT);
  96. ASSERT(r == 0);
  97. ASSERT(connect_cb_called == 1);
  98. ASSERT(write_cb_called == 1);
  99. ASSERT(shutdown_cb_called == 1);
  100. ASSERT(conn_close_cb_called == 1);
  101. ASSERT(timer_close_cb_called == 1);
  102. MAKE_VALGRIND_HAPPY();
  103. return 0;
  104. }