test-multiple-listen.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 close_cb_called = 0;
  27. static int connect_cb_called = 0;
  28. static uv_tcp_t server;
  29. static uv_tcp_t client;
  30. static void close_cb(uv_handle_t* handle) {
  31. ASSERT(handle != NULL);
  32. close_cb_called++;
  33. }
  34. static void connection_cb(uv_stream_t* tcp, int status) {
  35. ASSERT(status == 0);
  36. uv_close((uv_handle_t*)&server, close_cb);
  37. connection_cb_called++;
  38. }
  39. static void start_server(void) {
  40. struct sockaddr_in addr;
  41. int r;
  42. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  43. r = uv_tcp_init(uv_default_loop(), &server);
  44. ASSERT(r == 0);
  45. r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
  46. ASSERT(r == 0);
  47. r = uv_listen((uv_stream_t*)&server, 128, connection_cb);
  48. ASSERT(r == 0);
  49. r = uv_listen((uv_stream_t*)&server, 128, connection_cb);
  50. ASSERT(r == 0);
  51. }
  52. static void connect_cb(uv_connect_t* req, int status) {
  53. ASSERT(req != NULL);
  54. ASSERT(status == 0);
  55. free(req);
  56. uv_close((uv_handle_t*)&client, close_cb);
  57. connect_cb_called++;
  58. }
  59. static void client_connect(void) {
  60. struct sockaddr_in addr;
  61. uv_connect_t* connect_req = malloc(sizeof *connect_req);
  62. int r;
  63. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  64. ASSERT(connect_req != NULL);
  65. r = uv_tcp_init(uv_default_loop(), &client);
  66. ASSERT(r == 0);
  67. r = uv_tcp_connect(connect_req,
  68. &client,
  69. (const struct sockaddr*) &addr,
  70. connect_cb);
  71. ASSERT(r == 0);
  72. }
  73. TEST_IMPL(multiple_listen) {
  74. start_server();
  75. client_connect();
  76. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  77. ASSERT(connection_cb_called == 1);
  78. ASSERT(connect_cb_called == 1);
  79. ASSERT(close_cb_called == 2);
  80. MAKE_VALGRIND_HAPPY();
  81. return 0;
  82. }