test-pipe-connect-multiple.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (c) 2015 Saúl Ibarra Corretgé <saghul@gmail.com>.
  2. * All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include "uv.h"
  23. #include "task.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. static int connection_cb_called = 0;
  27. static int connect_cb_called = 0;
  28. #define NUM_CLIENTS 4
  29. typedef struct {
  30. uv_pipe_t pipe_handle;
  31. uv_connect_t conn_req;
  32. } client_t;
  33. static uv_pipe_t server_handle;
  34. static client_t clients[NUM_CLIENTS];
  35. static uv_pipe_t connections[NUM_CLIENTS];
  36. static void connection_cb(uv_stream_t* server, int status) {
  37. int r;
  38. uv_pipe_t* conn;
  39. ASSERT(status == 0);
  40. conn = &connections[connection_cb_called];
  41. r = uv_pipe_init(server->loop, conn, 0);
  42. ASSERT(r == 0);
  43. r = uv_accept(server, (uv_stream_t*)conn);
  44. ASSERT(r == 0);
  45. if (++connection_cb_called == NUM_CLIENTS &&
  46. connect_cb_called == NUM_CLIENTS) {
  47. uv_stop(server->loop);
  48. }
  49. }
  50. static void connect_cb(uv_connect_t* connect_req, int status) {
  51. ASSERT(status == 0);
  52. if (++connect_cb_called == NUM_CLIENTS &&
  53. connection_cb_called == NUM_CLIENTS) {
  54. uv_stop(connect_req->handle->loop);
  55. }
  56. }
  57. TEST_IMPL(pipe_connect_multiple) {
  58. #if defined(NO_SELF_CONNECT)
  59. RETURN_SKIP(NO_SELF_CONNECT);
  60. #endif
  61. int i;
  62. int r;
  63. uv_loop_t* loop;
  64. loop = uv_default_loop();
  65. r = uv_pipe_init(loop, &server_handle, 0);
  66. ASSERT(r == 0);
  67. r = uv_pipe_bind(&server_handle, TEST_PIPENAME);
  68. ASSERT(r == 0);
  69. r = uv_listen((uv_stream_t*)&server_handle, 128, connection_cb);
  70. ASSERT(r == 0);
  71. for (i = 0; i < NUM_CLIENTS; i++) {
  72. r = uv_pipe_init(loop, &clients[i].pipe_handle, 0);
  73. ASSERT(r == 0);
  74. uv_pipe_connect(&clients[i].conn_req,
  75. &clients[i].pipe_handle,
  76. TEST_PIPENAME,
  77. connect_cb);
  78. }
  79. uv_run(loop, UV_RUN_DEFAULT);
  80. ASSERT(connection_cb_called == NUM_CLIENTS);
  81. ASSERT(connect_cb_called == NUM_CLIENTS);
  82. MAKE_VALGRIND_HAPPY();
  83. return 0;
  84. }