test-shutdown-eof.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <stdio.h>
  24. #include <stdlib.h>
  25. static uv_timer_t timer;
  26. static uv_tcp_t tcp;
  27. static uv_connect_t connect_req;
  28. static uv_write_t write_req;
  29. static uv_shutdown_t shutdown_req;
  30. static uv_buf_t qbuf;
  31. static int got_q;
  32. static int got_eof;
  33. static int called_connect_cb;
  34. static int called_shutdown_cb;
  35. static int called_tcp_close_cb;
  36. static int called_timer_close_cb;
  37. static int called_timer_cb;
  38. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
  39. uv_buf_t buf;
  40. buf.base = (char*)malloc(size);
  41. buf.len = size;
  42. return buf;
  43. }
  44. static void read_cb(uv_stream_t* t, ssize_t nread, uv_buf_t buf) {
  45. uv_err_t err = uv_last_error(uv_default_loop());
  46. ASSERT((uv_tcp_t*)t == &tcp);
  47. if (nread == 0) {
  48. ASSERT(err.code == UV_EAGAIN);
  49. free(buf.base);
  50. return;
  51. }
  52. if (!got_q) {
  53. ASSERT(nread == 1);
  54. ASSERT(!got_eof);
  55. ASSERT(buf.base[0] == 'Q');
  56. free(buf.base);
  57. got_q = 1;
  58. puts("got Q");
  59. } else {
  60. ASSERT(err.code == UV_EOF);
  61. if (buf.base) {
  62. free(buf.base);
  63. }
  64. got_eof = 1;
  65. puts("got EOF");
  66. }
  67. }
  68. static void shutdown_cb(uv_shutdown_t *req, int status) {
  69. ASSERT(req == &shutdown_req);
  70. ASSERT(called_connect_cb == 1);
  71. ASSERT(!got_eof);
  72. ASSERT(called_tcp_close_cb == 0);
  73. ASSERT(called_timer_close_cb == 0);
  74. ASSERT(called_timer_cb == 0);
  75. called_shutdown_cb++;
  76. }
  77. static void connect_cb(uv_connect_t *req, int status) {
  78. ASSERT(status == 0);
  79. ASSERT(req == &connect_req);
  80. /* Start reading from our connection so we can receive the EOF. */
  81. uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb);
  82. /*
  83. * Write the letter 'Q' to gracefully kill the echo-server. This will not
  84. * effect our connection.
  85. */
  86. uv_write(&write_req, (uv_stream_t*) &tcp, &qbuf, 1, NULL);
  87. /* Shutdown our end of the connection. */
  88. uv_shutdown(&shutdown_req, (uv_stream_t*) &tcp, shutdown_cb);
  89. called_connect_cb++;
  90. ASSERT(called_shutdown_cb == 0);
  91. }
  92. static void tcp_close_cb(uv_handle_t* handle) {
  93. ASSERT(handle == (uv_handle_t*) &tcp);
  94. ASSERT(called_connect_cb == 1);
  95. ASSERT(got_q);
  96. ASSERT(got_eof);
  97. ASSERT(called_timer_cb == 1);
  98. called_tcp_close_cb++;
  99. }
  100. static void timer_close_cb(uv_handle_t* handle) {
  101. ASSERT(handle == (uv_handle_t*) &timer);
  102. called_timer_close_cb++;
  103. }
  104. static void timer_cb(uv_timer_t* handle, int status) {
  105. ASSERT(handle == &timer);
  106. uv_close((uv_handle_t*) handle, timer_close_cb);
  107. /*
  108. * The most important assert of the test: we have not received
  109. * tcp_close_cb yet.
  110. */
  111. ASSERT(called_tcp_close_cb == 0);
  112. uv_close((uv_handle_t*) &tcp, tcp_close_cb);
  113. called_timer_cb++;
  114. }
  115. /*
  116. * This test has a client which connects to the echo_server and immediately
  117. * issues a shutdown. The echo-server, in response, will also shutdown their
  118. * connection. We check, with a timer, that libuv is not automatically
  119. * calling uv_close when the client receives the EOF from echo-server.
  120. */
  121. TEST_IMPL(shutdown_eof) {
  122. struct sockaddr_in server_addr;
  123. int r;
  124. qbuf.base = "Q";
  125. qbuf.len = 1;
  126. r = uv_timer_init(uv_default_loop(), &timer);
  127. ASSERT(r == 0);
  128. uv_timer_start(&timer, timer_cb, 100, 0);
  129. server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  130. r = uv_tcp_init(uv_default_loop(), &tcp);
  131. ASSERT(!r);
  132. r = uv_tcp_connect(&connect_req, &tcp, server_addr, connect_cb);
  133. ASSERT(!r);
  134. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  135. ASSERT(called_connect_cb == 1);
  136. ASSERT(called_shutdown_cb == 1);
  137. ASSERT(got_eof);
  138. ASSERT(got_q);
  139. ASSERT(called_tcp_close_cb == 1);
  140. ASSERT(called_timer_close_cb == 1);
  141. ASSERT(called_timer_cb == 1);
  142. MAKE_VALGRIND_HAPPY();
  143. return 0;
  144. }