test-delayed-accept.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 int connection_cb_called = 0;
  26. static int do_accept_called = 0;
  27. static int close_cb_called = 0;
  28. static int connect_cb_called = 0;
  29. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
  30. uv_buf_t buf;
  31. buf.base = (char*)malloc(size);
  32. buf.len = size;
  33. return buf;
  34. }
  35. static void close_cb(uv_handle_t* handle) {
  36. ASSERT(handle != NULL);
  37. free(handle);
  38. close_cb_called++;
  39. }
  40. static void do_accept(uv_timer_t* timer_handle, int status) {
  41. uv_tcp_t* server;
  42. uv_tcp_t* accepted_handle = (uv_tcp_t*)malloc(sizeof *accepted_handle);
  43. int r;
  44. ASSERT(timer_handle != NULL);
  45. ASSERT(status == 0);
  46. ASSERT(accepted_handle != NULL);
  47. r = uv_tcp_init(uv_default_loop(), accepted_handle);
  48. ASSERT(r == 0);
  49. server = (uv_tcp_t*)timer_handle->data;
  50. r = uv_accept((uv_stream_t*)server, (uv_stream_t*)accepted_handle);
  51. ASSERT(r == 0);
  52. do_accept_called++;
  53. /* Immediately close the accepted handle. */
  54. uv_close((uv_handle_t*)accepted_handle, close_cb);
  55. /* After accepting the two clients close the server handle */
  56. if (do_accept_called == 2) {
  57. uv_close((uv_handle_t*)server, close_cb);
  58. }
  59. /* Dispose the timer. */
  60. uv_close((uv_handle_t*)timer_handle, close_cb);
  61. }
  62. static void connection_cb(uv_stream_t* tcp, int status) {
  63. int r;
  64. uv_timer_t* timer_handle;
  65. ASSERT(status == 0);
  66. timer_handle = (uv_timer_t*)malloc(sizeof *timer_handle);
  67. ASSERT(timer_handle != NULL);
  68. /* Accept the client after 1 second */
  69. r = uv_timer_init(uv_default_loop(), timer_handle);
  70. ASSERT(r == 0);
  71. timer_handle->data = tcp;
  72. r = uv_timer_start(timer_handle, do_accept, 1000, 0);
  73. ASSERT(r == 0);
  74. connection_cb_called++;
  75. }
  76. static void start_server(void) {
  77. struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
  78. uv_tcp_t* server = (uv_tcp_t*)malloc(sizeof *server);
  79. int r;
  80. ASSERT(server != NULL);
  81. r = uv_tcp_init(uv_default_loop(), server);
  82. ASSERT(r == 0);
  83. r = uv_tcp_bind(server, addr);
  84. ASSERT(r == 0);
  85. r = uv_listen((uv_stream_t*)server, 128, connection_cb);
  86. ASSERT(r == 0);
  87. }
  88. static void read_cb(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  89. /* The server will not send anything, it should close gracefully. */
  90. if (buf.base) {
  91. free(buf.base);
  92. }
  93. if (nread != -1) {
  94. ASSERT(nread == 0);
  95. ASSERT(uv_last_error(uv_default_loop()).code == UV_EAGAIN);
  96. } else {
  97. ASSERT(tcp != NULL);
  98. ASSERT(nread == -1);
  99. ASSERT(uv_last_error(uv_default_loop()).code == UV_EOF);
  100. uv_close((uv_handle_t*)tcp, close_cb);
  101. }
  102. }
  103. static void connect_cb(uv_connect_t* req, int status) {
  104. int r;
  105. ASSERT(req != NULL);
  106. ASSERT(status == 0);
  107. /* Not that the server will send anything, but otherwise we'll never know */
  108. /* when the server closes the connection. */
  109. r = uv_read_start((uv_stream_t*)(req->handle), alloc_cb, read_cb);
  110. ASSERT(r == 0);
  111. connect_cb_called++;
  112. free(req);
  113. }
  114. static void client_connect(void) {
  115. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  116. uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof *client);
  117. uv_connect_t* connect_req = malloc(sizeof *connect_req);
  118. int r;
  119. ASSERT(client != NULL);
  120. ASSERT(connect_req != NULL);
  121. r = uv_tcp_init(uv_default_loop(), client);
  122. ASSERT(r == 0);
  123. r = uv_tcp_connect(connect_req, client, addr, connect_cb);
  124. ASSERT(r == 0);
  125. }
  126. TEST_IMPL(delayed_accept) {
  127. start_server();
  128. client_connect();
  129. client_connect();
  130. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  131. ASSERT(connection_cb_called == 2);
  132. ASSERT(do_accept_called == 2);
  133. ASSERT(connect_cb_called == 2);
  134. ASSERT(close_cb_called == 7);
  135. MAKE_VALGRIND_HAPPY();
  136. return 0;
  137. }