1
0

test-delayed-accept.c 4.8 KB

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