1
0

test-shutdown-eof.c 4.7 KB

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